You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
starmap/basicrw.go

89 lines
1.7 KiB
Go

4 years ago
package starmap
import "errors"
4 years ago
func (stack *StarStack) Count() int {
stack.kvPushmu.Lock()
defer stack.kvPushmu.Unlock()
return len(stack.kvStack)
}
func (stack *StarStack) Push(val interface{}) error {
stack.kvPushmu.Lock()
defer stack.kvPushmu.Unlock()
stack.kvStack = append(stack.kvStack, val)
return nil
}
func (stack *StarStack) Pop() (interface{}, error) {
stack.kvPushmu.Lock()
defer stack.kvPushmu.Unlock()
if len(stack.kvStack) == 0 {
return nil, errors.New("Empty Stacks")
}
val := stack.kvStack[0]
stack.kvStack = stack.kvStack[1:]
return val, nil
}
func (stack *StarStack) MustPop() interface{} {
val, _ := stack.Pop()
return val
}
4 years ago
func Get(key string) (interface{}, error) {
return globalMap.Get(key)
}
func (m *StarMapKV) Get(key string) (interface{}, error) {
4 years ago
var err error
m.kvmu.RLock()
defer m.kvmu.RUnlock()
data, ok := m.kvMap[key]
4 years ago
if !ok {
err = errors.New("key not exists")
}
return data, err
}
func (m *StarMapKV) MustGet(key string) interface{} {
result, _ := m.Get(key)
4 years ago
return result
}
func MustGet(key string) interface{} {
return globalMap.MustGet(key)
}
4 years ago
4 years ago
func Store(key string, value interface{}) error {
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
4 years ago
return nil
}
func Delete(key string) error {
return globalMap.Delete(key)
}
func (m *StarMapKV) Delete(key string) error {
m.kvmu.Lock()
defer m.kvmu.Unlock()
delete(m.kvMap, key)
4 years ago
return nil
}
func Range(run func(k string, v interface{}) bool) error {
return globalMap.Range(run)
}
func (m *StarMapKV) Range(run func(k string, v interface{}) bool) error {
for k, v := range m.kvMap {
4 years ago
if !run(k, v) {
break
}
}
return nil
}