2023-12-14 16:08:59 +08:00
|
|
|
// Package easymap ...
|
|
|
|
//
|
|
|
|
// Description : easymap ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-12-14 15:51
|
|
|
|
package easymap
|
|
|
|
|
2023-12-24 22:44:07 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/util"
|
|
|
|
"sync"
|
|
|
|
)
|
2023-12-14 16:08:59 +08:00
|
|
|
|
2023-12-24 22:10:47 +08:00
|
|
|
// common 公共基础结构
|
2023-12-14 16:08:59 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
2023-12-24 22:10:47 +08:00
|
|
|
// Date : 15:52 2023/12/14
|
|
|
|
type common struct {
|
2023-12-24 22:44:07 +08:00
|
|
|
normalDataTable []map[interface{}]interface{} // 普通数组
|
|
|
|
syncMapDataTable []sync.Map // sync_map类型
|
|
|
|
dataTableType string // 数据表类型
|
|
|
|
segment int // 分片熟练
|
|
|
|
lock *sync.RWMutex // 数据锁
|
2023-12-24 22:10:47 +08:00
|
|
|
}
|
2023-12-14 16:08:59 +08:00
|
|
|
|
2023-12-24 22:10:47 +08:00
|
|
|
// initLock ...
|
2023-12-14 16:08:59 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
2023-12-24 22:10:47 +08:00
|
|
|
// Date : 21:25 2023/12/24
|
|
|
|
func (c *common) initLock() {
|
2023-12-24 22:51:41 +08:00
|
|
|
if c.dataTableType == normalDataTableType {
|
|
|
|
c.lock = &sync.RWMutex{}
|
|
|
|
}
|
2023-12-14 16:08:59 +08:00
|
|
|
}
|
|
|
|
|
2023-12-24 22:10:47 +08:00
|
|
|
// Lock ...
|
2023-12-14 16:08:59 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
2023-12-24 22:10:47 +08:00
|
|
|
// Date : 21:27 2023/12/24
|
|
|
|
func (c *common) Lock() {
|
|
|
|
if nil == c.lock {
|
2023-12-24 22:44:07 +08:00
|
|
|
return
|
2023-12-14 16:08:59 +08:00
|
|
|
}
|
2023-12-24 22:10:47 +08:00
|
|
|
c.lock.Lock()
|
2023-12-14 16:08:59 +08:00
|
|
|
}
|
|
|
|
|
2023-12-24 22:10:47 +08:00
|
|
|
// Unlock ...
|
2023-12-14 16:08:59 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
2023-12-24 22:10:47 +08:00
|
|
|
// Date : 21:27 2023/12/24
|
|
|
|
func (c *common) Unlock() {
|
2023-12-24 22:44:07 +08:00
|
|
|
if nil == c.lock {
|
|
|
|
return
|
|
|
|
}
|
2023-12-24 22:10:47 +08:00
|
|
|
c.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RLock ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:43 2023/12/24
|
|
|
|
func (c *common) RLock() {
|
2023-12-24 22:44:07 +08:00
|
|
|
if nil == c.lock {
|
|
|
|
return
|
|
|
|
}
|
2023-12-24 22:10:47 +08:00
|
|
|
c.lock.RLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:43 2023/12/24
|
|
|
|
func (c *common) RUnlock() {
|
2023-12-24 22:44:07 +08:00
|
|
|
if nil == c.lock {
|
|
|
|
return
|
|
|
|
}
|
2023-12-24 22:10:47 +08:00
|
|
|
c.lock.RUnlock()
|
2023-12-14 16:08:59 +08:00
|
|
|
}
|
2023-12-24 22:44:07 +08:00
|
|
|
|
|
|
|
func (c *common) Get(key interface{}) (interface{}, bool) {
|
|
|
|
segmentIndex := util.Hash.GetHashIDMod(key, c.segment)
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
if c.dataTableType == normalDataTableType {
|
|
|
|
data, exist := c.normalDataTable[segmentIndex][key]
|
|
|
|
return data, exist
|
|
|
|
} else {
|
|
|
|
// sync map
|
|
|
|
return c.syncMapDataTable[segmentIndex].Load(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetWithReceiver(key interface{}, dest interface{}) error {
|
|
|
|
if nil == dest {
|
|
|
|
return errors.New("dest is nil")
|
|
|
|
}
|
|
|
|
val, exist := c.Get(key)
|
|
|
|
if !exist {
|
|
|
|
return fmt.Errorf("key %v not exist", key)
|
|
|
|
}
|
|
|
|
if nil == val {
|
|
|
|
return errors.New("value is nil")
|
|
|
|
}
|
|
|
|
return util.ConvertAssign(dest, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetUint(key interface{}) (uint, error) {
|
|
|
|
var (
|
|
|
|
val uint
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetUint8(key interface{}) (uint8, error) {
|
|
|
|
var (
|
|
|
|
val uint8
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetUint16(key interface{}) (uint16, error) {
|
|
|
|
var (
|
|
|
|
val uint16
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetUint32(key interface{}) (uint32, error) {
|
|
|
|
var (
|
|
|
|
val uint32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetUint64(key interface{}) (uint64, error) {
|
|
|
|
var (
|
|
|
|
val uint64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetInt(key interface{}) (int, error) {
|
|
|
|
var (
|
|
|
|
val int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetInt8(key interface{}) (int8, error) {
|
|
|
|
var (
|
|
|
|
val int8
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetInt16(key interface{}) (int16, error) {
|
|
|
|
var (
|
|
|
|
val int16
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetInt32(key interface{}) (int32, error) {
|
|
|
|
var (
|
|
|
|
val int32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetInt64(key interface{}) (int64, error) {
|
|
|
|
var (
|
|
|
|
val int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetFloat32(key interface{}) (float32, error) {
|
|
|
|
var (
|
|
|
|
val float32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetFloat64(key interface{}) (float64, error) {
|
|
|
|
var (
|
|
|
|
val float64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetBool(key interface{}) (bool, error) {
|
|
|
|
var (
|
|
|
|
val bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) GetString(key interface{}) (string, error) {
|
|
|
|
var (
|
|
|
|
val string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
err = c.GetWithReceiver(key, &val)
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) Set(key interface{}, value interface{}) {
|
|
|
|
segmentIndex := util.Hash.GetHashIDMod(key, c.segment)
|
2023-12-29 11:21:22 +08:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2023-12-24 22:44:07 +08:00
|
|
|
if c.dataTableType == normalDataTableType {
|
|
|
|
c.normalDataTable[segmentIndex][key] = value
|
|
|
|
} else {
|
|
|
|
// sync map
|
|
|
|
c.syncMapDataTable[segmentIndex].Store(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) Del(key interface{}) {
|
|
|
|
segmentIndex := util.Hash.GetHashIDMod(key, c.segment)
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
if c.dataTableType == normalDataTableType {
|
|
|
|
delete(c.normalDataTable[segmentIndex], key)
|
|
|
|
} else {
|
|
|
|
c.syncMapDataTable[segmentIndex].Delete(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *common) Exist(key interface{}) bool {
|
|
|
|
_, exist := c.Get(key)
|
|
|
|
return exist
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:22:51 +08:00
|
|
|
// Count 计算元素数量
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:21 2024/1/30
|
|
|
|
func (c *common) Count() int {
|
|
|
|
cnt := 0
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
for i := 0; i < c.segment; i++ {
|
|
|
|
cnt = cnt + len(c.normalDataTable[i])
|
|
|
|
}
|
|
|
|
return cnt
|
|
|
|
}
|
|
|
|
|
2023-12-24 22:44:07 +08:00
|
|
|
func (c *common) GetAll() map[interface{}]interface{} {
|
|
|
|
result := make(map[interface{}]interface{})
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
for i := 0; i < c.segment; i++ {
|
|
|
|
if c.dataTableType == normalDataTableType {
|
|
|
|
for k, v := range c.normalDataTable[i] {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.syncMapDataTable[i].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 (c *common) GetAllForMapKeyString() map[string]interface{} {
|
|
|
|
fullData := c.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 (c *common) Iterator(handleFunc IteratorFunc) {
|
|
|
|
if nil == handleFunc {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
c.Unlock()
|
|
|
|
isBreak := false
|
|
|
|
for i := 0; i < c.segment; i++ {
|
|
|
|
if c.dataTableType == normalDataTableType {
|
|
|
|
for k, v := range c.normalDataTable[i] {
|
|
|
|
if !handleFunc(k, v) {
|
|
|
|
isBreak = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isBreak {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.syncMapDataTable[i].Range(func(key, value any) bool {
|
|
|
|
return handleFunc(key, value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStruct ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:42 2023/8/10
|
|
|
|
func (c *common) ToStruct(receiver interface{}) error {
|
|
|
|
if nil == receiver {
|
|
|
|
return errors.New("receiver is nil")
|
|
|
|
}
|
|
|
|
mapData := c.GetAll()
|
|
|
|
byteData, err := json.Marshal(mapData)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return serialize.JSON.UnmarshalWithNumber(byteData, receiver)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToString 转字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 20:44 2023/8/15
|
|
|
|
func (c *common) ToString() string {
|
|
|
|
mapData := c.GetAllForMapKeyString()
|
|
|
|
return serialize.JSON.MarshalForString(mapData)
|
|
|
|
}
|
2024-03-08 16:30:30 +08:00
|
|
|
|
|
|
|
// Filter 对数据进行过滤
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:25 2024/3/8
|
|
|
|
func (c *common) Filter(ignoreFieldList []string, rewriteFieldTable map[string]string) map[string]interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
ignoreFieldTable := make(map[string]bool)
|
|
|
|
for _, item := range ignoreFieldList {
|
|
|
|
ignoreFieldTable[item] = true
|
|
|
|
}
|
|
|
|
data := c.GetAllForMapKeyString()
|
|
|
|
for field, val := range data {
|
|
|
|
if _, exist := ignoreFieldTable[field]; exist {
|
|
|
|
// 忽略当前字段
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if rewriteField, exist := rewriteFieldTable[field]; exist && len(rewriteField) > 0 {
|
|
|
|
field = rewriteField
|
|
|
|
}
|
|
|
|
result[field] = val
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|