From 31e90d97b50f950ba1e529ac8bdd2a489069b3d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Wed, 24 Feb 2021 23:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E5=8F=96=E8=8E=B7=E5=8F=96hashID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easymap/segment.go | 56 ++++++++++++++++++----------------------- easymap/sync_segment.go | 46 +++++++++++++++------------------ util/hash.go | 32 +++++++++++++++++++++++ 3 files changed, 77 insertions(+), 57 deletions(-) create mode 100644 util/hash.go diff --git a/easymap/segment.go b/easymap/segment.go index cb80d2d..bdacc02 100644 --- a/easymap/segment.go +++ b/easymap/segment.go @@ -8,11 +8,14 @@ package easymap import ( - "fmt" - - "github.com/spaolacci/murmur3" + "github.com/go-developer/gopkg/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() @@ -33,97 +36,97 @@ type segment struct { } func (s *segment) Get(key interface{}) (interface{}, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].Get(key) } func (s *segment) GetWithReceiver(key interface{}, dest interface{}) error { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetWithReceiver(key, dest) } func (s *segment) GetUint(key interface{}) (uint, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetUint(key) } func (s *segment) GetUint8(key interface{}) (uint8, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetUint8(key) } func (s *segment) GetUint16(key interface{}) (uint16, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetUint16(key) } func (s *segment) GetUint32(key interface{}) (uint32, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetUint32(key) } func (s *segment) GetUint64(key interface{}) (uint64, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetUint64(key) } func (s *segment) GetInt(key interface{}) (int, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetInt(key) } func (s *segment) GetInt8(key interface{}) (int8, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetInt8(key) } func (s *segment) GetInt16(key interface{}) (int16, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetInt16(key) } func (s *segment) GetInt32(key interface{}) (int32, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetInt32(key) } func (s *segment) GetInt64(key interface{}) (int64, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetInt64(key) } func (s *segment) GetFloat32(key interface{}) (float32, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetFloat32(key) } func (s *segment) GetFloat64(key interface{}) (float64, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetFloat64(key) } func (s *segment) GetBool(key interface{}) (bool, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetBool(key) } func (s *segment) GetString(key interface{}) (string, error) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].GetString(key) } func (s *segment) Set(key interface{}, value interface{}) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) s.dataTable[segmentIndex].Set(key, value) } func (s *segment) Del(key interface{}) { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) s.dataTable[segmentIndex].Del(key) } func (s *segment) Exist(key interface{}) bool { - segmentIndex := s.getSegment(key) + segmentIndex := util.GetHashIDMod(key, s.segment) return s.dataTable[segmentIndex].Exist(key) } @@ -136,12 +139,3 @@ func (s *segment) GetAll() map[interface{}]interface{} { } 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)) -} diff --git a/easymap/sync_segment.go b/easymap/sync_segment.go index cafb647..2b38693 100644 --- a/easymap/sync_segment.go +++ b/easymap/sync_segment.go @@ -8,9 +8,7 @@ package easymap import ( - "fmt" - - "github.com/spaolacci/murmur3" + "github.com/go-developer/gopkg/util" ) // NewSegmentSync 获取SegmentSync实例 @@ -38,79 +36,79 @@ type segmentSync struct { } func (s *segmentSync) Get(key interface{}) (interface{}, error) { - return s.dataTable[s.getSegment(key)].Get(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].Get(key) } func (s *segmentSync) GetWithReceiver(key interface{}, dest interface{}) error { - return s.dataTable[s.getSegment(key)].GetWithReceiver(key, dest) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetWithReceiver(key, dest) } func (s *segmentSync) GetUint(key interface{}) (uint, error) { - return s.dataTable[s.getSegment(key)].GetUint(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetUint(key) } func (s *segmentSync) GetUint8(key interface{}) (uint8, error) { - return s.dataTable[s.getSegment(key)].GetUint8(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetUint8(key) } func (s *segmentSync) GetUint16(key interface{}) (uint16, error) { - return s.dataTable[s.getSegment(key)].GetUint16(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetUint16(key) } func (s *segmentSync) GetUint32(key interface{}) (uint32, error) { - return s.dataTable[s.getSegment(key)].GetUint32(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetUint32(key) } func (s *segmentSync) GetUint64(key interface{}) (uint64, error) { - return s.dataTable[s.getSegment(key)].GetUint64(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetUint64(key) } func (s *segmentSync) GetInt(key interface{}) (int, error) { - return s.dataTable[s.getSegment(key)].GetInt(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetInt(key) } func (s *segmentSync) GetInt8(key interface{}) (int8, error) { - return s.dataTable[s.getSegment(key)].GetInt8(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetInt8(key) } func (s *segmentSync) GetInt16(key interface{}) (int16, error) { - return s.dataTable[s.getSegment(key)].GetInt16(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetInt16(key) } func (s *segmentSync) GetInt32(key interface{}) (int32, error) { - return s.dataTable[s.getSegment(key)].GetInt32(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetInt32(key) } func (s *segmentSync) GetInt64(key interface{}) (int64, error) { - return s.dataTable[s.getSegment(key)].GetInt64(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetInt64(key) } func (s *segmentSync) GetFloat32(key interface{}) (float32, error) { - return s.dataTable[s.getSegment(key)].GetFloat32(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetFloat32(key) } func (s *segmentSync) GetFloat64(key interface{}) (float64, error) { - return s.dataTable[s.getSegment(key)].GetFloat64(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetFloat64(key) } func (s *segmentSync) GetBool(key interface{}) (bool, error) { - return s.dataTable[s.getSegment(key)].GetBool(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetBool(key) } func (s *segmentSync) GetString(key interface{}) (string, error) { - return s.dataTable[s.getSegment(key)].GetString(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].GetString(key) } func (s *segmentSync) Set(key interface{}, value interface{}) { - s.dataTable[s.getSegment(key)].Set(key, value) + s.dataTable[util.GetHashIDMod(key, s.segment)].Set(key, value) } func (s *segmentSync) Del(key interface{}) { - s.dataTable[s.getSegment(key)].Del(key) + s.dataTable[util.GetHashIDMod(key, s.segment)].Del(key) } func (s *segmentSync) Exist(key interface{}) bool { - return s.dataTable[s.getSegment(key)].Exist(key) + return s.dataTable[util.GetHashIDMod(key, s.segment)].Exist(key) } func (s *segmentSync) GetAll() map[interface{}]interface{} { @@ -122,7 +120,3 @@ func (s *segmentSync) GetAll() map[interface{}]interface{} { } return result } - -func (s *segmentSync) getSegment(key interface{}) int { - return int(murmur3.Sum64([]byte(fmt.Sprintf("%v", key))) % uint64(s.segment)) -} diff --git a/util/hash.go b/util/hash.go new file mode 100644 index 0000000..c47245b --- /dev/null +++ b/util/hash.go @@ -0,0 +1,32 @@ +// Package util... +// +// Description : util... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-02-24 11:04 下午 +package util + +import ( + "fmt" + + "github.com/spaolacci/murmur3" +) + +// GetHashID ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:04 下午 2021/2/24 +func GetHashID(key interface{}) uint64 { + return murmur3.Sum64([]byte(fmt.Sprintf("%v", key))) +} + +// GetHashIDMod 获取hashID并取模 +// +// Author : go_developer@163.com<张德满> +// +// Date : 11:07 下午 2021/2/24 +func GetHashIDMod(key interface{}, shard int) int { + return int(GetHashID(key) % uint64(shard)) +}