435 lines
8.5 KiB
Go
435 lines
8.5 KiB
Go
// Package easymap ...
|
|
//
|
|
// Description : easymap ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-12-14 15:51
|
|
package easymap
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
"git.zhangdeman.cn/zhangdeman/util"
|
|
"sync"
|
|
)
|
|
|
|
// common 公共基础结构
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 15:52 2023/12/14
|
|
type common struct {
|
|
normalDataTable []map[interface{}]interface{} // 普通数组
|
|
syncMapDataTable []sync.Map // sync_map类型
|
|
dataTableType string // 数据表类型
|
|
segment int // 分片熟练
|
|
lock *sync.RWMutex // 数据锁
|
|
}
|
|
|
|
// initLock ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:25 2023/12/24
|
|
func (c *common) initLock() {
|
|
if c.dataTableType == normalDataTableType {
|
|
c.lock = &sync.RWMutex{}
|
|
}
|
|
}
|
|
|
|
// Lock ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:27 2023/12/24
|
|
func (c *common) Lock() {
|
|
if nil == c.lock {
|
|
return
|
|
}
|
|
c.lock.Lock()
|
|
}
|
|
|
|
// Unlock ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:27 2023/12/24
|
|
func (c *common) Unlock() {
|
|
if nil == c.lock {
|
|
return
|
|
}
|
|
c.lock.Unlock()
|
|
}
|
|
|
|
// RLock ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:43 2023/12/24
|
|
func (c *common) RLock() {
|
|
if nil == c.lock {
|
|
return
|
|
}
|
|
c.lock.RLock()
|
|
}
|
|
|
|
// RUnlock ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:43 2023/12/24
|
|
func (c *common) RUnlock() {
|
|
if nil == c.lock {
|
|
return
|
|
}
|
|
c.lock.RUnlock()
|
|
}
|
|
|
|
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)
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
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.MarshalForStringIgnoreError(mapData)
|
|
}
|
|
|
|
// 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{} {
|
|
if nil == ignoreFieldList {
|
|
ignoreFieldList = make([]string, 0)
|
|
}
|
|
if nil == rewriteFieldTable {
|
|
rewriteFieldTable = make(map[string]string)
|
|
}
|
|
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
|
|
}
|
|
|
|
// Merge 合并数据
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 11:01 2024/3/11
|
|
func (c *common) Merge(mergeData ...EasyMap) {
|
|
for _, itemData := range mergeData {
|
|
allData := itemData.GetAll()
|
|
for k, v := range allData {
|
|
c.Set(k, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MergeWithReceiver 合并并转换
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 11:06 2024/3/11
|
|
func (c *common) MergeWithReceiver(receiver interface{}, mergeData ...EasyMap) error {
|
|
c.Merge(mergeData...)
|
|
if nil == receiver {
|
|
return nil
|
|
}
|
|
return c.ToStruct(receiver)
|
|
}
|