easymap/common.go

435 lines
8.2 KiB
Go
Raw Normal View History

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 {
2024-11-01 16:25:29 +08:00
normalDataTable []map[any]any // 普通数组
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
2024-11-01 16:25:29 +08:00
func (c *common) Get(key any) (any, bool) {
2023-12-24 22:44:07 +08:00
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)
}
}
2024-11-01 16:25:29 +08:00
func (c *common) GetWithReceiver(key any, dest any) error {
2023-12-24 22:44:07 +08:00
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)
}
2024-11-01 16:25:29 +08:00
func (c *common) GetUint(key any) (uint, error) {
2023-12-24 22:44:07 +08:00
var (
val uint
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetUint8(key any) (uint8, error) {
2023-12-24 22:44:07 +08:00
var (
val uint8
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetUint16(key any) (uint16, error) {
2023-12-24 22:44:07 +08:00
var (
val uint16
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetUint32(key any) (uint32, error) {
2023-12-24 22:44:07 +08:00
var (
val uint32
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetUint64(key any) (uint64, error) {
2023-12-24 22:44:07 +08:00
var (
val uint64
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetInt(key any) (int, error) {
2023-12-24 22:44:07 +08:00
var (
val int
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetInt8(key any) (int8, error) {
2023-12-24 22:44:07 +08:00
var (
val int8
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetInt16(key any) (int16, error) {
2023-12-24 22:44:07 +08:00
var (
val int16
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetInt32(key any) (int32, error) {
2023-12-24 22:44:07 +08:00
var (
val int32
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetInt64(key any) (int64, error) {
2023-12-24 22:44:07 +08:00
var (
val int64
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetFloat32(key any) (float32, error) {
2023-12-24 22:44:07 +08:00
var (
val float32
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetFloat64(key any) (float64, error) {
2023-12-24 22:44:07 +08:00
var (
val float64
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetBool(key any) (bool, error) {
2023-12-24 22:44:07 +08:00
var (
val bool
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) GetString(key any) (string, error) {
2023-12-24 22:44:07 +08:00
var (
val string
err error
)
err = c.GetWithReceiver(key, &val)
return val, err
}
2024-11-01 16:25:29 +08:00
func (c *common) Set(key any, value any) {
2023-12-24 22:44:07 +08:00
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)
}
}
2024-11-01 16:25:29 +08:00
func (c *common) Del(key any) {
2023-12-24 22:44:07 +08:00
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)
}
}
2024-11-01 16:25:29 +08:00
func (c *common) Exist(key any) bool {
2023-12-24 22:44:07 +08:00
_, 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
}
2024-11-01 16:25:29 +08:00
func (c *common) GetAll() map[any]any {
result := make(map[any]any)
2023-12-24 22:44:07 +08:00
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 {
2024-11-01 16:25:29 +08:00
c.syncMapDataTable[i].Range(func(key, value any) bool {
2023-12-24 22:44:07 +08:00
result[key] = value
return true
})
}
}
return result
}
// GetAllForMapKeyString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:48 下午 2021/9/15
2024-11-01 16:25:29 +08:00
func (c *common) GetAllForMapKeyString() map[string]any {
2023-12-24 22:44:07 +08:00
fullData := c.GetAll()
2024-11-01 16:25:29 +08:00
finalData := make(map[string]any)
2023-12-24 22:44:07 +08:00
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
2024-11-01 16:25:29 +08:00
func (c *common) ToStruct(receiver any) error {
2023-12-24 22:44:07 +08:00
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()
2024-10-24 21:43:52 +08:00
return serialize.JSON.MarshalForStringIgnoreError(mapData)
2023-12-24 22:44:07 +08:00
}
2024-03-08 16:30:30 +08:00
// Filter 对数据进行过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2024/3/8
2024-11-01 16:25:29 +08:00
func (c *common) Filter(ignoreFieldList []string, rewriteFieldTable map[string]string) map[string]any {
2024-03-08 16:35:03 +08:00
if nil == ignoreFieldList {
ignoreFieldList = make([]string, 0)
}
if nil == rewriteFieldTable {
rewriteFieldTable = make(map[string]string)
}
2024-11-01 16:25:29 +08:00
result := make(map[string]any)
2024-03-08 16:30:30 +08:00
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
}
2024-03-11 11:08:08 +08:00
// 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
2024-11-01 16:25:29 +08:00
func (c *common) MergeWithReceiver(receiver any, mergeData ...EasyMap) error {
2024-03-11 11:08:08 +08:00
c.Merge(mergeData...)
if nil == receiver {
return nil
}
return c.ToStruct(receiver)
}