easymap/segment.go

288 lines
5.5 KiB
Go
Raw Normal View History

2022-05-14 16:07:21 +08:00
// Package easymap ...
//
// Description : 分段存储的map并发行更好,分段数量为 1 将退化成普通的
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-02-23 10:47 下午
package easymap
import (
2023-08-10 15:48:46 +08:00
"encoding/json"
"errors"
2022-05-14 16:07:21 +08:00
"fmt"
"git.zhangdeman.cn/zhangdeman/util"
2023-12-24 22:10:47 +08:00
"git.zhangdeman.cn/zhangdeman/serialize"
2022-05-14 16:07:21 +08:00
)
// NewSegment 获取分段map实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:05 下午 2021/2/24
2023-12-24 22:10:47 +08:00
func NewSegment(segmentCnt int) (EasyMap, error) {
2022-05-14 16:07:21 +08:00
if segmentCnt <= 0 {
return nil, segmentError()
}
em := &segment{
2023-12-24 22:10:47 +08:00
common: &common{},
2022-05-14 16:07:21 +08:00
segment: segmentCnt,
}
2023-12-24 22:10:47 +08:00
em.dataTable = make([]map[interface{}]interface{}, segmentCnt)
2022-05-14 16:07:21 +08:00
for i := 0; i < segmentCnt; i++ {
2023-12-24 22:10:47 +08:00
em.dataTable[i] = make(map[interface{}]interface{})
2022-05-14 16:07:21 +08:00
}
2023-12-24 22:10:47 +08:00
em.common.initLock()
2022-05-14 16:07:21 +08:00
return em, nil
}
type segment struct {
2023-12-24 22:10:47 +08:00
common *common
dataTable []map[interface{}]interface{}
2022-05-14 16:07:21 +08:00
segment int
}
2023-12-24 22:10:47 +08:00
func (s *segment) Get(key interface{}) (interface{}, bool) {
2022-05-14 16:07:21 +08:00
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
2023-12-24 22:10:47 +08:00
s.common.RLock()
defer s.common.RUnlock()
data, exist := s.dataTable[segmentIndex][key]
return data, exist
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetWithReceiver(key interface{}, dest interface{}) error {
2023-12-24 22:10:47 +08:00
if nil == dest {
return errors.New("dest is nil")
}
val, exist := s.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)
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetUint(key interface{}) (uint, error) {
2023-12-24 22:10:47 +08:00
var (
val uint
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetUint8(key interface{}) (uint8, error) {
2023-12-24 22:10:47 +08:00
var (
val uint8
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetUint16(key interface{}) (uint16, error) {
2023-12-24 22:10:47 +08:00
var (
val uint16
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetUint32(key interface{}) (uint32, error) {
2023-12-24 22:10:47 +08:00
var (
val uint32
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetUint64(key interface{}) (uint64, error) {
2023-12-24 22:10:47 +08:00
var (
val uint64
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetInt(key interface{}) (int, error) {
2023-12-24 22:10:47 +08:00
var (
val int
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetInt8(key interface{}) (int8, error) {
2023-12-24 22:10:47 +08:00
var (
val int8
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetInt16(key interface{}) (int16, error) {
2023-12-24 22:10:47 +08:00
var (
val int16
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetInt32(key interface{}) (int32, error) {
2023-12-24 22:10:47 +08:00
var (
val int32
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetInt64(key interface{}) (int64, error) {
2023-12-24 22:10:47 +08:00
var (
val int64
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetFloat32(key interface{}) (float32, error) {
2023-12-24 22:10:47 +08:00
var (
val float32
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetFloat64(key interface{}) (float64, error) {
2023-12-24 22:10:47 +08:00
var (
val float64
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetBool(key interface{}) (bool, error) {
2023-12-24 22:10:47 +08:00
var (
val bool
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetString(key interface{}) (string, error) {
2023-12-24 22:10:47 +08:00
var (
val string
err error
)
err = s.GetWithReceiver(key, &val)
return val, err
2022-05-14 16:07:21 +08:00
}
func (s *segment) Set(key interface{}, value interface{}) {
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
2023-12-24 22:10:47 +08:00
s.dataTable[segmentIndex][key] = value
2022-05-14 16:07:21 +08:00
}
func (s *segment) Del(key interface{}) {
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
2023-12-24 22:10:47 +08:00
s.common.Lock()
defer s.common.Unlock()
delete(s.dataTable[segmentIndex], key)
2022-05-14 16:07:21 +08:00
}
func (s *segment) Exist(key interface{}) bool {
2023-12-24 22:10:47 +08:00
_, exist := s.Get(key)
return exist
2022-05-14 16:07:21 +08:00
}
func (s *segment) GetAll() map[interface{}]interface{} {
result := make(map[interface{}]interface{})
2023-12-24 22:10:47 +08:00
s.common.RLock()
defer s.common.RUnlock()
2022-05-14 16:07:21 +08:00
for i := 0; i < s.segment; i++ {
2023-12-24 22:10:47 +08:00
for k, v := range s.dataTable[i] {
2022-05-14 16:07:21 +08:00
result[k] = v
}
}
return result
}
// GetAllForMapKeyString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:48 下午 2021/9/15
func (s *segment) 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
}
2023-03-07 17:34:02 +08:00
// Iterator ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:32 2023/3/7
func (s *segment) Iterator(handleFunc IteratorFunc) {
if nil == handleFunc {
return
}
2023-12-24 22:10:47 +08:00
s.common.Lock()
s.common.Unlock()
2023-03-07 17:48:41 +08:00
isBreak := false
2023-03-07 17:34:02 +08:00
for i := 0; i < s.segment; i++ {
2023-12-24 22:10:47 +08:00
for k, v := range s.dataTable[i] {
2023-03-07 17:48:41 +08:00
if !handleFunc(k, v) {
isBreak = true
break
}
}
if isBreak {
break
2023-03-07 17:34:02 +08:00
}
}
}
2023-08-10 15:48:46 +08:00
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:42 2023/8/10
func (s *segment) 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
}
2023-12-24 22:10:47 +08:00
return serialize.JSON.UnmarshalWithNumber(byteData, receiver)
2023-08-10 15:48:46 +08:00
}
2023-08-15 20:46:43 +08:00
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:44 2023/8/15
func (s *segment) ToString() string {
mapData := s.GetAllForMapKeyString()
2023-12-24 22:10:47 +08:00
return serialize.JSON.MarshalForString(mapData)
2023-08-15 20:46:43 +08:00
}