升级NewNormal与NewSync

This commit is contained in:
白茶清欢 2023-12-14 16:08:59 +08:00
parent 7439281703
commit 68e33b965b
3 changed files with 52 additions and 594 deletions

49
common.go Normal file
View File

@ -0,0 +1,49 @@
// Package easymap ...
//
// Description : easymap ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-12-14 15:51
package easymap
import "git.zhangdeman.cn/zhangdeman/util"
// GetShardAndKeyFunc 获取数据所处分片以及key
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:55 2023/12/14
type GetShardAndKeyFunc func(interface{}) (int, interface{})
// DefaultGetShardAndKeyFunc 默认实现
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:57 2023/12/14
func DefaultGetShardAndKeyFunc(key interface{}) GetShardAndKeyFunc {
return func(key interface{}) (int, interface{}) {
return -1, key
}
}
// GetShardAndKeyFuncWithShardCount 获取数据分片和key
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:59 2023/12/14
func GetShardAndKeyFuncWithShardCount(shardCnt int, key interface{}) GetShardAndKeyFunc {
return func(key interface{}) (int, interface{}) {
return util.Hash.GetHashIDMod(key, shardCnt), key
}
}
// common 公共基础结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:52 2023/12/14
type common struct {
data map[interface{}]interface{} // 输入数据
GetShardAndKeyFunc GetShardAndKeyFunc // 获取分片以及key的函数
}

324
normal.go
View File

@ -7,334 +7,12 @@
// Date : 2021-02-23 10:03 下午 // Date : 2021-02-23 10:03 下午
package easymap package easymap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sync"
"git.zhangdeman.cn/zhangdeman/util"
)
// NewNormal 获取map实例 // NewNormal 获取map实例
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:07 下午 2021/2/23 // Date : 10:07 下午 2021/2/23
func NewNormal(withLock bool) EasyMap { func NewNormal(withLock bool) EasyMap {
em := &normal{ em, _ := NewSegment(1, withLock)
data: make(map[interface{}]interface{}),
withLock: withLock,
}
if withLock {
em.lock = &sync.RWMutex{}
}
return em return em
} }
// normal 普通map,内部可以加锁
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:03 下午 2021/2/23
type normal struct {
data map[interface{}]interface{}
withLock bool
lock *sync.RWMutex
}
func (n *normal) Get(key interface{}) (interface{}, error) {
if !n.Exist(key) {
return nil, keyNotFound(key)
}
n.RLock()
defer n.RUnlock()
return n.data[key], nil
}
func (n *normal) GetWithReceiver(key interface{}, dest interface{}) error {
var (
val interface{}
err error
)
if val, err = n.Get(key); nil != err {
return err
}
return convertFail(util.ConvertAssign(dest, val))
}
func (n *normal) GetUint(key interface{}) (uint, error) {
var (
result uint
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetUint8(key interface{}) (uint8, error) {
var (
result uint8
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetUint16(key interface{}) (uint16, error) {
var (
result uint16
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetUint32(key interface{}) (uint32, error) {
var (
result uint32
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetUint64(key interface{}) (uint64, error) {
var (
result uint64
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetInt(key interface{}) (int, error) {
var (
result int
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetInt8(key interface{}) (int8, error) {
var (
result int8
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetInt16(key interface{}) (int16, error) {
var (
result int16
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetInt32(key interface{}) (int32, error) {
var (
result int32
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetInt64(key interface{}) (int64, error) {
var (
result int64
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetFloat32(key interface{}) (float32, error) {
var (
result float32
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetFloat64(key interface{}) (float64, error) {
var (
result float64
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (n *normal) GetBool(key interface{}) (bool, error) {
var (
result bool
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return false, err
}
return result, nil
}
func (n *normal) GetString(key interface{}) (string, error) {
var (
result string
err error
)
if err = n.GetWithReceiver(key, &result); nil != err {
return "", err
}
return result, nil
}
func (n *normal) Set(key interface{}, value interface{}) {
n.Lock()
defer n.Unlock()
n.data[key] = value
}
func (n *normal) Del(key interface{}) {
n.Lock()
defer n.Unlock()
delete(n.data, key)
}
func (n *normal) Exist(key interface{}) bool {
n.RLock()
defer n.RUnlock()
_, exist := n.data[key]
return exist
}
// GetAll 读取全部数据使用的是原始数据深拷贝
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:36 下午 2021/2/23
func (n *normal) GetAll() map[interface{}]interface{} {
result := make(map[interface{}]interface{})
n.RLock()
defer n.RUnlock()
for key, val := range n.data {
result[key] = val
}
return result
}
func (n *normal) RLock() {
if n.withLock {
n.lock.RLock()
}
}
func (n *normal) RUnlock() {
if n.withLock {
n.lock.RUnlock()
}
}
func (n *normal) Lock() {
if n.withLock {
n.lock.Lock()
}
}
func (n *normal) Unlock() {
if n.withLock {
n.lock.Unlock()
}
}
// GetAllForMapKeyString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:48 下午 2021/9/15
func (n *normal) GetAllForMapKeyString() map[string]interface{} {
fullData := n.GetAll()
finalData := make(map[string]interface{})
for k, v := range fullData {
finalData[fmt.Sprintf("%v", k)] = v
}
return finalData
}
// Iterator ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:32 2023/3/7
func (n *normal) Iterator(handleFunc IteratorFunc) {
if nil == handleFunc {
return
}
n.RLock()
defer n.RUnlock()
for key, val := range n.data {
if !handleFunc(key, val) {
// 终止迭代
break
}
}
}
// ToStruct map转为结构体
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:33 2023/8/10
func (n *normal) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
mapData := n.GetAll()
byteData, err := json.Marshal(mapData)
if nil != err {
return err
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:44 2023/8/15
func (n *normal) ToString() string {
mapData := n.GetAllForMapKeyString()
byteData, _ := json.Marshal(mapData)
return string(byteData)
}

View File

@ -7,276 +7,7 @@
// Date : 2021-02-24 2:08 下午 // Date : 2021-02-24 2:08 下午
package easymap package easymap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sync"
"git.zhangdeman.cn/zhangdeman/util"
)
func NewSync() EasyMap { func NewSync() EasyMap {
return &syncMap{} em, _ := NewSegmentSync(1)
} return em
type syncMap struct {
data sync.Map
}
func (s *syncMap) Get(key interface{}) (interface{}, error) {
val, exist := s.data.Load(key)
if !exist {
return nil, keyNotFound(key)
}
return val, nil
}
func (s *syncMap) GetWithReceiver(key interface{}, dest interface{}) error {
var (
val interface{}
err error
)
if val, err = s.Get(key); nil != err {
return err
}
if err = util.ConvertAssign(dest, val); nil != err {
return convertFail(err)
}
return nil
}
func (s *syncMap) GetUint(key interface{}) (uint, error) {
var (
result uint
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetUint8(key interface{}) (uint8, error) {
var (
result uint8
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetUint16(key interface{}) (uint16, error) {
var (
result uint16
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetUint32(key interface{}) (uint32, error) {
var (
result uint32
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetUint64(key interface{}) (uint64, error) {
var (
result uint64
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetInt(key interface{}) (int, error) {
var (
result int
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetInt8(key interface{}) (int8, error) {
var (
result int8
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetInt16(key interface{}) (int16, error) {
var (
result int16
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetInt32(key interface{}) (int32, error) {
var (
result int32
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetInt64(key interface{}) (int64, error) {
var (
result int64
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetFloat32(key interface{}) (float32, error) {
var (
result float32
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetFloat64(key interface{}) (float64, error) {
var (
result float64
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return 0, err
}
return result, nil
}
func (s *syncMap) GetBool(key interface{}) (bool, error) {
var (
result bool
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return false, err
}
return result, nil
}
func (s *syncMap) GetString(key interface{}) (string, error) {
var (
result string
err error
)
if err = s.GetWithReceiver(key, &result); nil != err {
return "", err
}
return result, nil
}
func (s *syncMap) Set(key interface{}, value interface{}) {
s.data.Store(key, value)
}
func (s *syncMap) Del(key interface{}) {
s.data.Delete(key)
}
func (s *syncMap) Exist(key interface{}) bool {
_, exist := s.data.Load(key)
return exist
}
func (s *syncMap) GetAll() map[interface{}]interface{} {
result := make(map[interface{}]interface{})
s.data.Range(func(key, value interface{}) bool {
result[key] = value
return true
})
return result
}
// GetAllForMapKeyString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:48 下午 2021/9/15
func (s *syncMap) GetAllForMapKeyString() map[string]interface{} {
fullData := s.GetAll()
finalData := make(map[string]interface{})
for k, v := range fullData {
finalData[fmt.Sprintf("%v", k)] = v
}
return finalData
}
// Iterator 迭代数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:27 2023/3/7
func (s *syncMap) Iterator(handleFunc IteratorFunc) {
if nil == handleFunc {
return
}
s.data.Range(func(key, value interface{}) bool {
return handleFunc(key, value)
})
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:47 2023/8/10
func (s *syncMap) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
mapData := s.GetAll()
byteData, err := json.Marshal(mapData)
if nil != err {
return err
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:44 2023/8/15
func (s *syncMap) ToString() string {
mapData := s.GetAllForMapKeyString()
byteData, _ := json.Marshal(mapData)
return string(byteData)
} }