升级分片map

This commit is contained in:
2023-12-24 22:10:47 +08:00
parent 68e33b965b
commit d43d18b1b2
5 changed files with 180 additions and 82 deletions

View File

@ -7,36 +7,7 @@
// 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
}
}
import "sync"
// common 公共基础结构
//
@ -44,6 +15,53 @@ func GetShardAndKeyFuncWithShardCount(shardCnt int, key interface{}) GetShardAnd
//
// Date : 15:52 2023/12/14
type common struct {
data map[interface{}]interface{} // 输入数据
GetShardAndKeyFunc GetShardAndKeyFunc // 获取分片以及key的函数
lock *sync.RWMutex // 数据
}
// initLock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:25 2023/12/24
func (c *common) initLock() {
c.lock = &sync.RWMutex{}
}
// Lock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:27 2023/12/24
func (c *common) Lock() {
if nil == c.lock {
}
c.lock.Lock()
}
// Unlock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:27 2023/12/24
func (c *common) Unlock() {
c.lock.Unlock()
}
// RLock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:43 2023/12/24
func (c *common) RLock() {
c.lock.RLock()
}
// RUnlock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:43 2023/12/24
func (c *common) RUnlock() {
c.lock.RUnlock()
}