搬迁easymap
This commit is contained in:
commit
7713928d92
22
.gitignore
vendored
Normal file
22
.gitignore
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Go template
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
*.xlsx
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
.idea
|
||||
.vscode
|
||||
mail_test.go
|
||||
|
37
abstruct.go
Normal file
37
abstruct.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Package easymap ...
|
||||
//
|
||||
// Description : easymap...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-23 9:55 下午
|
||||
package easymap
|
||||
|
||||
// EasyMap 约束各种数据接口的实现
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 9:56 下午 2021/2/23
|
||||
type EasyMap interface {
|
||||
Get(key interface{}) (interface{}, error)
|
||||
GetWithReceiver(key interface{}, dest interface{}) error
|
||||
GetUint(key interface{}) (uint, error)
|
||||
GetUint8(key interface{}) (uint8, error)
|
||||
GetUint16(key interface{}) (uint16, error)
|
||||
GetUint32(key interface{}) (uint32, error)
|
||||
GetUint64(key interface{}) (uint64, error)
|
||||
GetInt(key interface{}) (int, error)
|
||||
GetInt8(key interface{}) (int8, error)
|
||||
GetInt16(key interface{}) (int16, error)
|
||||
GetInt32(key interface{}) (int32, error)
|
||||
GetInt64(key interface{}) (int64, error)
|
||||
GetFloat32(key interface{}) (float32, error)
|
||||
GetFloat64(key interface{}) (float64, error)
|
||||
GetBool(key interface{}) (bool, error)
|
||||
GetString(key interface{}) (string, error)
|
||||
Set(key interface{}, value interface{})
|
||||
Del(key interface{})
|
||||
Exist(key interface{}) bool
|
||||
GetAll() map[interface{}]interface{}
|
||||
GetAllForMapKeyString() map[string]interface{}
|
||||
}
|
37
error.go
Normal file
37
error.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Package easymap ...
|
||||
//
|
||||
// Description : easymap...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-23 10:15 下午
|
||||
package easymap
|
||||
|
||||
import "github.com/pkg/errors"
|
||||
|
||||
// keyNotFound key 不存在
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:17 下午 2021/2/23
|
||||
func keyNotFound(key interface{}) error {
|
||||
return errors.Errorf("%v 未找到", key)
|
||||
}
|
||||
|
||||
// convertFail 数据类型妆换失败
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:28 下午 2021/2/23
|
||||
func convertFail(err error) error {
|
||||
return errors.Wrapf(err, "数据类型转换失败")
|
||||
}
|
||||
|
||||
// segmentError ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 1:44 下午 2021/2/24
|
||||
func segmentError() error {
|
||||
return errors.New("segment需要大于0")
|
||||
}
|
13
go.mod
Normal file
13
go.mod
Normal file
@ -0,0 +1,13 @@
|
||||
module git.zhangdeman.cn/zhangdeman/easymap
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514075609-25936d457a72
|
||||
github.com/pkg/errors v0.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
10
go.sum
Normal file
10
go.sum
Normal file
@ -0,0 +1,10 @@
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514075609-25936d457a72 h1:kPx8DFXmsyBeEiLjolzG8ZlIGYUhBbTWcn65AuMGLy8=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514075609-25936d457a72/go.mod h1:YI/XeTmrr9+8dxa4ThPkmNcEE8WHG5pZkKujpSWwIxM=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
288
normal.go
Normal file
288
normal.go
Normal file
@ -0,0 +1,288 @@
|
||||
// Package easymap ...
|
||||
//
|
||||
// Description : 普通的的map,增加锁支持
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-23 10:03 下午
|
||||
package easymap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
)
|
||||
|
||||
// NewNormal 获取map实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:07 下午 2021/2/23
|
||||
func NewNormal(withLock bool) EasyMap {
|
||||
em := &normal{
|
||||
data: make(map[interface{}]interface{}),
|
||||
withLock: withLock,
|
||||
}
|
||||
if withLock {
|
||||
em.lock = &sync.RWMutex{}
|
||||
}
|
||||
return em
|
||||
}
|
||||
|
||||
// normal 普通map,内部可以加锁
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:03 下午 2021/2/23
|
||||
type normal struct {
|
||||
data map[interface{}]interface{}
|
||||
withLock bool
|
||||
lock *sync.RWMutex
|
||||
}
|
||||
|
||||
func (n *normal) Get(key interface{}) (interface{}, error) {
|
||||
if !n.Exist(key) {
|
||||
return nil, keyNotFound(key)
|
||||
}
|
||||
n.RLock()
|
||||
defer n.RUnlock()
|
||||
return n.data[key], nil
|
||||
}
|
||||
|
||||
func (n *normal) GetWithReceiver(key interface{}, dest interface{}) error {
|
||||
var (
|
||||
val interface{}
|
||||
err error
|
||||
)
|
||||
if val, err = n.Get(key); nil != err {
|
||||
return err
|
||||
}
|
||||
return convertFail(util.ConvertAssign(dest, val))
|
||||
}
|
||||
|
||||
func (n *normal) GetUint(key interface{}) (uint, error) {
|
||||
var (
|
||||
result uint
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetUint8(key interface{}) (uint8, error) {
|
||||
var (
|
||||
result uint8
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetUint16(key interface{}) (uint16, error) {
|
||||
var (
|
||||
result uint16
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetUint32(key interface{}) (uint32, error) {
|
||||
var (
|
||||
result uint32
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetUint64(key interface{}) (uint64, error) {
|
||||
var (
|
||||
result uint64
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetInt(key interface{}) (int, error) {
|
||||
var (
|
||||
result int
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetInt8(key interface{}) (int8, error) {
|
||||
var (
|
||||
result int8
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetInt16(key interface{}) (int16, error) {
|
||||
var (
|
||||
result int16
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetInt32(key interface{}) (int32, error) {
|
||||
var (
|
||||
result int32
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetInt64(key interface{}) (int64, error) {
|
||||
var (
|
||||
result int64
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetFloat32(key interface{}) (float32, error) {
|
||||
var (
|
||||
result float32
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetFloat64(key interface{}) (float64, error) {
|
||||
var (
|
||||
result float64
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetBool(key interface{}) (bool, error) {
|
||||
var (
|
||||
result bool
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return false, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) GetString(key interface{}) (string, error) {
|
||||
var (
|
||||
result string
|
||||
err error
|
||||
)
|
||||
if err = n.GetWithReceiver(key, &result); nil != err {
|
||||
return "", err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *normal) Set(key interface{}, value interface{}) {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
n.data[key] = value
|
||||
}
|
||||
|
||||
func (n *normal) Del(key interface{}) {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
delete(n.data, key)
|
||||
}
|
||||
|
||||
func (n *normal) Exist(key interface{}) bool {
|
||||
n.RLock()
|
||||
defer n.RUnlock()
|
||||
_, exist := n.data[key]
|
||||
return exist
|
||||
}
|
||||
|
||||
// GetAll 读取全部数据使用的是原始数据深拷贝,避免获取到全部数据之后,直接读取导致并发读写
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:36 下午 2021/2/23
|
||||
func (n *normal) GetAll() map[interface{}]interface{} {
|
||||
result := make(map[interface{}]interface{})
|
||||
n.lock.RLock()
|
||||
defer n.lock.RUnlock()
|
||||
for key, val := range n.data {
|
||||
result[key] = val
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (n *normal) RLock() {
|
||||
if n.withLock {
|
||||
n.lock.RLock()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *normal) RUnlock() {
|
||||
if n.withLock {
|
||||
n.lock.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *normal) Lock() {
|
||||
if n.withLock {
|
||||
n.lock.Lock()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *normal) Unlock() {
|
||||
if n.withLock {
|
||||
n.lock.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// GetAllForMapKeyString ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 9:48 下午 2021/9/15
|
||||
func (n *normal) GetAllForMapKeyString() map[string]interface{} {
|
||||
fullData := n.GetAll()
|
||||
finalData := make(map[string]interface{})
|
||||
for k, v := range fullData {
|
||||
finalData[fmt.Sprintf("%v", k)] = v
|
||||
}
|
||||
return finalData
|
||||
}
|
157
segment.go
Normal file
157
segment.go
Normal file
@ -0,0 +1,157 @@
|
||||
// Package easymap ...
|
||||
//
|
||||
// Description : 分段存储的map,并发行更好,分段数量为 1, 将退化成普通的
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-23 10:47 下午
|
||||
package easymap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
)
|
||||
|
||||
// NewSegment 获取分段map实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:05 下午 2021/2/24
|
||||
func NewSegment(segmentCnt int, withLock bool) (EasyMap, error) {
|
||||
if segmentCnt <= 0 {
|
||||
return nil, segmentError()
|
||||
}
|
||||
em := &segment{
|
||||
segment: segmentCnt,
|
||||
}
|
||||
em.dataTable = make([]EasyMap, segmentCnt)
|
||||
for i := 0; i < segmentCnt; i++ {
|
||||
em.dataTable[i] = NewNormal(withLock)
|
||||
}
|
||||
return em, nil
|
||||
}
|
||||
|
||||
type segment struct {
|
||||
dataTable []EasyMap
|
||||
segment int
|
||||
}
|
||||
|
||||
func (s *segment) Get(key interface{}) (interface{}, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].Get(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetWithReceiver(key interface{}, dest interface{}) error {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetWithReceiver(key, dest)
|
||||
}
|
||||
|
||||
func (s *segment) GetUint(key interface{}) (uint, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetUint(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetUint8(key interface{}) (uint8, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetUint8(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetUint16(key interface{}) (uint16, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetUint16(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetUint32(key interface{}) (uint32, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetUint32(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetUint64(key interface{}) (uint64, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetUint64(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetInt(key interface{}) (int, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetInt(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetInt8(key interface{}) (int8, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetInt8(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetInt16(key interface{}) (int16, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetInt16(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetInt32(key interface{}) (int32, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetInt32(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetInt64(key interface{}) (int64, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetInt64(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetFloat32(key interface{}) (float32, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetFloat32(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetFloat64(key interface{}) (float64, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetFloat64(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetBool(key interface{}) (bool, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetBool(key)
|
||||
}
|
||||
|
||||
func (s *segment) GetString(key interface{}) (string, error) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].GetString(key)
|
||||
}
|
||||
|
||||
func (s *segment) Set(key interface{}, value interface{}) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
s.dataTable[segmentIndex].Set(key, value)
|
||||
}
|
||||
|
||||
func (s *segment) Del(key interface{}) {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
s.dataTable[segmentIndex].Del(key)
|
||||
}
|
||||
|
||||
func (s *segment) Exist(key interface{}) bool {
|
||||
segmentIndex := util.Hash.GetHashIDMod(key, s.segment)
|
||||
return s.dataTable[segmentIndex].Exist(key)
|
||||
}
|
||||
|
||||
func (s *segment) 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 *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
|
||||
}
|
235
sync_normal.go
Normal file
235
sync_normal.go
Normal file
@ -0,0 +1,235 @@
|
||||
// Package easymap...
|
||||
//
|
||||
// Description : 内置sync.Map + segment
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-24 2:08 下午
|
||||
package easymap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
)
|
||||
|
||||
func NewSync() EasyMap {
|
||||
return &syncMap{}
|
||||
}
|
||||
|
||||
type syncMap struct {
|
||||
data sync.Map
|
||||
}
|
||||
|
||||
func (s *syncMap) Get(key interface{}) (interface{}, error) {
|
||||
val, exist := s.data.Load(key)
|
||||
if !exist {
|
||||
return nil, keyNotFound(key)
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetWithReceiver(key interface{}, dest interface{}) error {
|
||||
var (
|
||||
val interface{}
|
||||
err error
|
||||
)
|
||||
if val, err = s.Get(key); nil != err {
|
||||
return err
|
||||
}
|
||||
if err = util.ConvertAssign(dest, val); nil != err {
|
||||
return convertFail(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetUint(key interface{}) (uint, error) {
|
||||
var (
|
||||
result uint
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetUint8(key interface{}) (uint8, error) {
|
||||
var (
|
||||
result uint8
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetUint16(key interface{}) (uint16, error) {
|
||||
var (
|
||||
result uint16
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetUint32(key interface{}) (uint32, error) {
|
||||
var (
|
||||
result uint32
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetUint64(key interface{}) (uint64, error) {
|
||||
var (
|
||||
result uint64
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetInt(key interface{}) (int, error) {
|
||||
var (
|
||||
result int
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetInt8(key interface{}) (int8, error) {
|
||||
var (
|
||||
result int8
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetInt16(key interface{}) (int16, error) {
|
||||
var (
|
||||
result int16
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetInt32(key interface{}) (int32, error) {
|
||||
var (
|
||||
result int32
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetInt64(key interface{}) (int64, error) {
|
||||
var (
|
||||
result int64
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetFloat32(key interface{}) (float32, error) {
|
||||
var (
|
||||
result float32
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetFloat64(key interface{}) (float64, error) {
|
||||
var (
|
||||
result float64
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetBool(key interface{}) (bool, error) {
|
||||
var (
|
||||
result bool
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return false, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) GetString(key interface{}) (string, error) {
|
||||
var (
|
||||
result string
|
||||
err error
|
||||
)
|
||||
if err = s.GetWithReceiver(key, &result); nil != err {
|
||||
return "", err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *syncMap) Set(key interface{}, value interface{}) {
|
||||
s.data.Store(key, value)
|
||||
}
|
||||
|
||||
func (s *syncMap) Del(key interface{}) {
|
||||
s.data.Delete(key)
|
||||
}
|
||||
|
||||
func (s *syncMap) Exist(key interface{}) bool {
|
||||
_, exist := s.data.Load(key)
|
||||
return exist
|
||||
}
|
||||
|
||||
func (s *syncMap) GetAll() map[interface{}]interface{} {
|
||||
result := make(map[interface{}]interface{})
|
||||
s.data.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 (s *syncMap) 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
|
||||
}
|
21
sync_normal_test.go
Normal file
21
sync_normal_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
// Package easymap...
|
||||
//
|
||||
// Description : easymap...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-24 5:57 下午
|
||||
package easymap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSyncNormal(t *testing.T) {
|
||||
syncMap := NewSync()
|
||||
syncMap.Set("name", "zhangdeman")
|
||||
syncMap.Set("age", 25)
|
||||
syncMap.Set("height", 180)
|
||||
fmt.Println(syncMap.GetAll())
|
||||
}
|
138
sync_segment.go
Normal file
138
sync_segment.go
Normal file
@ -0,0 +1,138 @@
|
||||
// Package easymap...
|
||||
//
|
||||
// Description : 内置sync.Map + segment
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-24 2:08 下午
|
||||
package easymap
|
||||
|
||||
import (
|
||||
"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
|
||||
}
|
Loading…
Reference in New Issue
Block a user