imporve some case

master v1.1.1
兔子 4 years ago
parent c8e513063c
commit 3c9d51a44a

@ -32,37 +32,54 @@ func (stack *StarStack) MustPop() interface{} {
} }
func Get(key string) (interface{}, error) { func Get(key string) (interface{}, error) {
return globalMap.Get(key)
}
func (m *StarMapKV) Get(key string) (interface{}, error) {
var err error var err error
kvmu.RLock() m.kvmu.RLock()
defer kvmu.RUnlock() defer m.kvmu.RUnlock()
data, ok := kvMap[key] data, ok := m.kvMap[key]
if !ok { if !ok {
err = errors.New("key not exists") err = errors.New("key not exists")
} }
return data, err return data, err
} }
func MustGet(key string) interface{} { func (m *StarMapKV) MustGet(key string) interface{} {
result, _ := Get(key) result, _ := m.Get(key)
return result return result
} }
func MustGet(key string) interface{} {
return globalMap.MustGet(key)
}
func Store(key string, value interface{}) error { func Store(key string, value interface{}) error {
kvmu.Lock() return globalMap.Store(key, value)
defer kvmu.Unlock() }
kvMap[key] = value func (m *StarMapKV) Store(key string, value interface{}) error {
m.kvmu.Lock()
defer m.kvmu.Unlock()
m.kvMap[key] = value
return nil return nil
} }
func Delete(key string) error { func Delete(key string) error {
kvmu.Lock() return globalMap.Delete(key)
defer kvmu.Unlock() }
delete(kvMap, key) func (m *StarMapKV) Delete(key string) error {
m.kvmu.Lock()
defer m.kvmu.Unlock()
delete(m.kvMap, key)
return nil return nil
} }
func Range(run func(k string, v interface{}) bool) error { func Range(run func(k string, v interface{}) bool) error {
for k, v := range kvMap { return globalMap.Range(run)
}
func (m *StarMapKV) Range(run func(k string, v interface{}) bool) error {
for k, v := range m.kvMap {
if !run(k, v) { if !run(k, v) {
break break
} }

@ -15,9 +15,12 @@ func (sample StarMapSample) GetName() string {
} }
func GetMap(value StarMap) error { func GetMap(value StarMap) error {
kvmuPro.RLock() return globalMap.GetMap(value)
defer kvmuPro.RUnlock() }
value, ok := kvMapPro[value.GetName()] func (m *StarMapKV) GetMap(value StarMap) error {
m.kvmuPro.RLock()
defer m.kvmuPro.RUnlock()
value, ok := m.kvMapPro[value.GetName()]
if !ok { if !ok {
return errors.New("key not exists") return errors.New("key not exists")
} }
@ -25,13 +28,21 @@ func GetMap(value StarMap) error {
} }
func StoreMap(value StarMap) error { func StoreMap(value StarMap) error {
kvmuPro.Lock() return globalMap.StoreMap(value)
defer kvmuPro.Unlock() }
kvMapPro[value.GetName()] = value func (m *StarMapKV) StoreMap(value StarMap) error {
m.kvmuPro.Lock()
defer m.kvmuPro.Unlock()
m.kvMapPro[value.GetName()] = value
return nil return nil
} }
func DeleteMap(value StarMap) error { func DeleteMap(value StarMap) error {
delete(kvMapPro, value.GetName()) return globalMap.DeleteMap(value)
}
func (m *StarMapKV) DeleteMap(value StarMap) error {
m.kvmuPro.RLock()
defer m.kvmuPro.RUnlock()
delete(m.kvMapPro, value.GetName())
return nil return nil
} }

@ -17,6 +17,7 @@ type RemoteKv struct {
setKeepAlive bool setKeepAlive bool
TimeOut time.Duration TimeOut time.Duration
ErrMsg string ErrMsg string
kvmap StarMapKV
} }
func NewServer(addr string) (*RemoteKv, error) { func NewServer(addr string) (*RemoteKv, error) {
@ -24,6 +25,7 @@ func NewServer(addr string) (*RemoteKv, error) {
var err error var err error
kv.addr = addr kv.addr = addr
kv.TimeOut = time.Second * 10 kv.TimeOut = time.Second * 10
kv.kvmap = NewStarMap()
kv.server, err = notify.NewNotifyS("tcp", addr) kv.server, err = notify.NewNotifyS("tcp", addr)
if err == nil { if err == nil {
kv.bind() kv.bind()

@ -9,7 +9,7 @@ type basicReply struct {
func (kv *RemoteKv) get(data notify.SMsg) string { func (kv *RemoteKv) get(data notify.SMsg) string {
key := data.Value key := data.Value
value, err := Get(key) value, err := kv.kvmap.Get(key)
if err != nil { if err != nil {
sendData, err := encode(basicReply{"error", []byte("key not exists")}) sendData, err := encode(basicReply{"error", []byte("key not exists")})
if err != nil { if err != nil {
@ -47,7 +47,7 @@ func (kv *RemoteKv) store(data notify.SMsg) string {
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
err = Store(inf.Key, customStruct) err = kv.kvmap.Store(inf.Key, customStruct)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
@ -55,7 +55,7 @@ func (kv *RemoteKv) store(data notify.SMsg) string {
} }
func (kv *RemoteKv) delete(data notify.SMsg) string { func (kv *RemoteKv) delete(data notify.SMsg) string {
err := Delete(data.Value) err := kv.kvmap.Delete(data.Value)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }

@ -5,7 +5,9 @@ import (
) )
func (kv *RemoteKv) getMap(data notify.SMsg) string { func (kv *RemoteKv) getMap(data notify.SMsg) string {
val, ok := kvMapPro[data.Value] kv.kvmu.Lock()
defer kv.kvmu.Unlock()
val, ok := kv.kvmap.kvMapPro[data.Value]
if !ok { if !ok {
data.Key = "error" data.Key = "error"
data.Reply("key not exists") data.Reply("key not exists")
@ -21,13 +23,15 @@ func (kv *RemoteKv) getMap(data notify.SMsg) string {
} }
func (kv *RemoteKv) storeMap(data notify.SMsg) string { func (kv *RemoteKv) storeMap(data notify.SMsg) string {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
recvData, err := decodeMap([]byte(data.Value)) recvData, err := decodeMap([]byte(data.Value))
if err != nil { if err != nil {
data.Key = "error" data.Key = "error"
data.Reply(err.Error()) data.Reply(err.Error())
return "" return ""
} }
err = StoreMap(recvData) err = kv.kvmap.StoreMap(recvData)
if err != nil { if err != nil {
data.Key = "error" data.Key = "error"
data.Reply(err.Error()) data.Reply(err.Error())
@ -37,6 +41,8 @@ func (kv *RemoteKv) storeMap(data notify.SMsg) string {
} }
func (kv *RemoteKv) deleteMap(data notify.SMsg) string { func (kv *RemoteKv) deleteMap(data notify.SMsg) string {
delete(kvMapPro, data.Value) kv.kvmu.Lock()
defer kv.kvmu.Unlock()
delete(kv.kvmap.kvMapPro, data.Value)
return "ok" return "ok"
} }

@ -2,10 +2,14 @@ package starmap
import "sync" import "sync"
var kvMap map[string]interface{} var globalMap StarMapKV
var kvmu sync.RWMutex
var kvMapPro map[string]StarMap type StarMapKV struct {
var kvmuPro sync.RWMutex kvMap map[string]interface{}
kvmu sync.RWMutex
kvMapPro map[string]StarMap
kvmuPro sync.RWMutex
}
type StarStack struct { type StarStack struct {
kvPushmu sync.RWMutex kvPushmu sync.RWMutex
@ -13,6 +17,12 @@ type StarStack struct {
} }
func init() { func init() {
kvMap = make(map[string]interface{}) globalMap = NewStarMap()
kvMapPro = make(map[string]StarMap) }
func NewStarMap() StarMapKV {
var mp StarMapKV
mp.kvMap = make(map[string]interface{})
mp.kvMapPro = make(map[string]StarMap)
return mp
} }

Loading…
Cancel
Save