easymap/sync_segment.go

190 lines
4.8 KiB
Go
Raw Normal View History

2022-05-14 16:07:21 +08:00
// Package easymap...
//
// Description : 内置sync.Map + segment
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-02-24 2:08 下午
package easymap
import (
2023-08-10 15:48:46 +08:00
"bytes"
"encoding/json"
"errors"
2022-05-14 16:07:21 +08:00
"fmt"
"git.zhangdeman.cn/zhangdeman/util"
)
// NewSegmentSync 获取SegmentSync实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:02 下午 2021/2/24
func NewSegmentSync(segment int) (EasyMap, error) {
if segment <= 0 {
return nil, segmentError()
}
ss := &segmentSync{
segment: segment,
}
ss.dataTable = make([]EasyMap, segment)
for i := 0; i < segment; i++ {
ss.dataTable[i] = NewSync()
}
return ss, nil
}
type segmentSync struct {
dataTable []EasyMap
segment int
}
func (s *segmentSync) Get(key interface{}) (interface{}, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].Get(key)
}
func (s *segmentSync) GetWithReceiver(key interface{}, dest interface{}) error {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetWithReceiver(key, dest)
}
func (s *segmentSync) GetUint(key interface{}) (uint, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetUint(key)
}
func (s *segmentSync) GetUint8(key interface{}) (uint8, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetUint8(key)
}
func (s *segmentSync) GetUint16(key interface{}) (uint16, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetUint16(key)
}
func (s *segmentSync) GetUint32(key interface{}) (uint32, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetUint32(key)
}
func (s *segmentSync) GetUint64(key interface{}) (uint64, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetUint64(key)
}
func (s *segmentSync) GetInt(key interface{}) (int, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetInt(key)
}
func (s *segmentSync) GetInt8(key interface{}) (int8, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetInt8(key)
}
func (s *segmentSync) GetInt16(key interface{}) (int16, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetInt16(key)
}
func (s *segmentSync) GetInt32(key interface{}) (int32, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetInt32(key)
}
func (s *segmentSync) GetInt64(key interface{}) (int64, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetInt64(key)
}
func (s *segmentSync) GetFloat32(key interface{}) (float32, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetFloat32(key)
}
func (s *segmentSync) GetFloat64(key interface{}) (float64, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetFloat64(key)
}
func (s *segmentSync) GetBool(key interface{}) (bool, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetBool(key)
}
func (s *segmentSync) GetString(key interface{}) (string, error) {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].GetString(key)
}
func (s *segmentSync) Set(key interface{}, value interface{}) {
s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].Set(key, value)
}
func (s *segmentSync) Del(key interface{}) {
s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].Del(key)
}
func (s *segmentSync) Exist(key interface{}) bool {
return s.dataTable[util.Hash.GetHashIDMod(key, s.segment)].Exist(key)
}
func (s *segmentSync) GetAll() map[interface{}]interface{} {
result := make(map[interface{}]interface{})
for i := 0; i < s.segment; i++ {
for k, v := range s.dataTable[i].GetAll() {
result[k] = v
}
}
return result
}
// GetAllForMapKeyString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:48 下午 2021/9/15
func (s *segmentSync) 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
func (s *segmentSync) Iterator(handleFunc IteratorFunc) {
if nil == handleFunc {
return
}
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++ {
for k, v := range s.dataTable[i].GetAll() {
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:48 2023/8/10
func (s *segmentSync) 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
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}
2023-08-15 20:46:43 +08:00
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:44 2023/8/15
func (s *segmentSync) ToString() string {
mapData := s.GetAll()
byteData, _ := json.Marshal(mapData)
return string(byteData)
}