增加带segment且并发安全的map

This commit is contained in:
白茶清欢 2021-02-24 14:03:43 +08:00
parent 99dc9b8b69
commit fe75bce121
4 changed files with 152 additions and 2 deletions

View File

@ -26,3 +26,12 @@ func keyNotFound(key interface{}) error {
func convertFail(err error) error { func convertFail(err error) error {
return errors.Wrapf(err, "数据类型转换失败") return errors.Wrapf(err, "数据类型转换失败")
} }
// segmentError ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 1:44 下午 2021/2/24
func segmentError() error {
return errors.New("segment需要大于0")
}

View File

@ -7,5 +7,141 @@
// Date : 2021-02-23 10:47 下午 // Date : 2021-02-23 10:47 下午
package easymap package easymap
type segment struct { import (
"fmt"
"github.com/spaolacci/murmur3"
)
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[0] = NewNormal(withLock)
}
return em, nil
}
type segment struct {
dataTable []EasyMap
segment int
}
func (s *segment) Get(key interface{}) (interface{}, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].Get(key)
}
func (s *segment) GetWithReceiver(key interface{}, dest interface{}) error {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetWithReceiver(key, dest)
}
func (s *segment) GetUint(key interface{}) (uint, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetUint(key)
}
func (s *segment) GetUint8(key interface{}) (uint8, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetUint8(key)
}
func (s *segment) GetUint16(key interface{}) (uint16, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetUint16(key)
}
func (s *segment) GetUint32(key interface{}) (uint32, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetUint32(key)
}
func (s *segment) GetUint64(key interface{}) (uint64, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetUint64(key)
}
func (s *segment) GetInt(key interface{}) (int, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetInt(key)
}
func (s *segment) GetInt8(key interface{}) (int8, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetInt8(key)
}
func (s *segment) GetInt16(key interface{}) (int16, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetInt16(key)
}
func (s *segment) GetInt32(key interface{}) (int32, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetInt32(key)
}
func (s *segment) GetInt64(key interface{}) (int64, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetInt64(key)
}
func (s *segment) GetFloat32(key interface{}) (float32, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetFloat32(key)
}
func (s *segment) GetFloat64(key interface{}) (float64, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetFloat64(key)
}
func (s *segment) GetBool(key interface{}) (bool, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetBool(key)
}
func (s *segment) GetString(key interface{}) (string, error) {
segmentIndex := s.getSegment(key)
return s.dataTable[segmentIndex].GetString(key)
}
func (s *segment) Set(key interface{}, value interface{}) {
segmentIndex := s.getSegment(key)
s.dataTable[segmentIndex].Set(key, value)
}
func (s *segment) Del(key interface{}) {
segmentIndex := s.getSegment(key)
s.dataTable[segmentIndex].Del(key)
}
func (s *segment) Exist(key interface{}) bool {
segmentIndex := s.getSegment(key)
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
}
// getSegment 根据key获取segment
//
// Author : go_developer@163.com<张德满>
//
// Date : 1:51 下午 2021/2/24
func (s *segment) getSegment(key interface{}) int {
return int(murmur3.Sum64([]byte(fmt.Sprintf("%v", key))) % uint64(s.segment))
} }

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/go-developer/gopkg
go 1.15 go 1.15
require github.com/pkg/errors v0.9.1 require (
github.com/pkg/errors v0.9.1
github.com/spaolacci/murmur3 v1.1.0
)

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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=