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) {
return globalMap.Get(key)
}
func (m *StarMapKV) Get(key string) (interface{}, error) {
var err error
kvmu.RLock()
defer kvmu.RUnlock()
data, ok := kvMap[key]
m.kvmu.RLock()
defer m.kvmu.RUnlock()
data, ok := m.kvMap[key]
if !ok {
err = errors.New("key not exists")
}
return data, err
}
func MustGet(key string) interface{} {
result, _ := Get(key)
func (m *StarMapKV) MustGet(key string) interface{} {
result, _ := m.Get(key)
return result
}
func MustGet(key string) interface{} {
return globalMap.MustGet(key)
}
func Store(key string, value interface{}) error {
kvmu.Lock()
defer kvmu.Unlock()
kvMap[key] = value
return globalMap.Store(key, value)
}
func (m *StarMapKV) Store(key string, value interface{}) error {
m.kvmu.Lock()
defer m.kvmu.Unlock()
m.kvMap[key] = value
return nil
}
func Delete(key string) error {
kvmu.Lock()
defer kvmu.Unlock()
delete(kvMap, key)
return globalMap.Delete(key)
}
func (m *StarMapKV) Delete(key string) error {
m.kvmu.Lock()
defer m.kvmu.Unlock()
delete(m.kvMap, key)
return nil
}
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) {
break
}

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

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

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

@ -5,7 +5,9 @@ import (
)
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 {
data.Key = "error"
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 {
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
recvData, err := decodeMap([]byte(data.Value))
if err != nil {
data.Key = "error"
data.Reply(err.Error())
return ""
}
err = StoreMap(recvData)
err = kv.kvmap.StoreMap(recvData)
if err != nil {
data.Key = "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 {
delete(kvMapPro, data.Value)
kv.kvmu.Lock()
defer kv.kvmu.Unlock()
delete(kv.kvmap.kvMapPro, data.Value)
return "ok"
}

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

Loading…
Cancel
Save