gopkg/easymap/segment.go

158 lines
4.3 KiB
Go
Raw Normal View History

2021-02-23 22:49:06 +08:00
// Package easymap...
//
// Description : 分段存储的map并发行更好,分段数量为 1 将退化成普通的
//
2021-07-25 19:05:59 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-02-23 22:49:06 +08:00
//
// Date : 2021-02-23 10:47 下午
package easymap
2021-02-24 14:03:43 +08:00
import (
2021-09-15 21:51:13 +08:00
"fmt"
2021-07-25 18:50:56 +08:00
"git.zhangdeman.cn/zhangdeman/gopkg/util"
2021-02-24 14:03:43 +08:00
)
2021-02-24 23:18:29 +08:00
// NewSegment 获取分段map实例
//
2021-07-25 19:05:59 +08:00
// Author : go_developer@163.com<白茶清欢>
2021-02-24 23:18:29 +08:00
//
// Date : 11:05 下午 2021/2/24
2021-02-24 14:03:43 +08:00
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++ {
2021-04-09 15:52:58 +08:00
em.dataTable[i] = NewNormal(withLock)
2021-02-24 14:03:43 +08:00
}
return em, nil
}
2021-02-23 22:49:06 +08:00
type segment struct {
2021-02-24 14:03:43 +08:00
dataTable []EasyMap
segment int
}
func (s *segment) Get(key interface{}) (interface{}, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].Get(key)
}
func (s *segment) GetWithReceiver(key interface{}, dest interface{}) error {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetWithReceiver(key, dest)
}
func (s *segment) GetUint(key interface{}) (uint, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetUint(key)
}
func (s *segment) GetUint8(key interface{}) (uint8, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetUint8(key)
}
func (s *segment) GetUint16(key interface{}) (uint16, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetUint16(key)
}
func (s *segment) GetUint32(key interface{}) (uint32, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetUint32(key)
}
func (s *segment) GetUint64(key interface{}) (uint64, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetUint64(key)
}
func (s *segment) GetInt(key interface{}) (int, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetInt(key)
}
func (s *segment) GetInt8(key interface{}) (int8, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetInt8(key)
}
func (s *segment) GetInt16(key interface{}) (int16, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetInt16(key)
}
func (s *segment) GetInt32(key interface{}) (int32, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetInt32(key)
}
func (s *segment) GetInt64(key interface{}) (int64, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetInt64(key)
}
func (s *segment) GetFloat32(key interface{}) (float32, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetFloat32(key)
}
func (s *segment) GetFloat64(key interface{}) (float64, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetFloat64(key)
}
func (s *segment) GetBool(key interface{}) (bool, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetBool(key)
}
func (s *segment) GetString(key interface{}) (string, error) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
return s.dataTable[segmentIndex].GetString(key)
}
func (s *segment) Set(key interface{}, value interface{}) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
s.dataTable[segmentIndex].Set(key, value)
}
func (s *segment) Del(key interface{}) {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
s.dataTable[segmentIndex].Del(key)
}
func (s *segment) Exist(key interface{}) bool {
2021-02-24 23:18:29 +08:00
segmentIndex := util.GetHashIDMod(key, s.segment)
2021-02-24 14:03:43 +08:00
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
}
2021-09-15 21:51:13 +08:00
// 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
}