-
Notifications
You must be signed in to change notification settings - Fork 0
/
slotmap.go
182 lines (156 loc) · 3.25 KB
/
slotmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package cmap
import (
"fmt"
"sync"
"time"
)
// All slot-map apis is testing and thus should not put into product use.
const (
Sl_Is_Extending = 2
)
// SlotMap consist of many map slots
// It can auto reduce data to those slots by s.hash().
// If sm.autoExtend = true, slots will work in extendable size of slots concurrently safe.
type SlotMap struct {
// not used then
// if autoExtend = true, slots size will adjust autonomously, about 200000 per slot
// autoExtend is not changeable in runtime, decide auto-extendable or not when call newSlotMap()
autoExtend bool
extendingState int
slotOverWeighNum int
// only when autoExtend=true, checkIneval will make sense.
checkInteval time.Duration
l *sync.RWMutex
slots []Map
}
func NewSlotMap(slotNum int, autoExtend bool, checkIneval time.Duration) *SlotMap {
s := &SlotMap{
autoExtend: autoExtend,
checkInteval: checkIneval,
slotOverWeighNum: 200000,
l: &sync.RWMutex{},
slots: make([]Map, slotNum, 2*slotNum),
}
if autoExtend {
go func() {
for {
fmt.Println("[extend]Start daily extend spying")
s.extend()
time.Sleep(s.checkInteval)
}
}()
}
return s
}
func (s *SlotMap) isExtending() bool {
s.rLock()
state := s.extendingState
s.rUnlock()
return state == Sl_Is_Extending
}
func (s *SlotMap) setExtendingState() {
s.lock()
s.extendingState = Sl_Is_Extending
s.unlock()
}
func (s *SlotMap) freeExtendingState() {
s.lock()
s.extendingState = 0
s.unlock()
}
func (s *SlotMap) shouldExend() bool {
s.l.RLock()
slotNum := len(s.slots)
maxOverSlotNum := slotNum / 3
var numOverSlotNum = 0
for i, _ := range s.slots {
if s.slots[i].IsBusy() {
continue
} else {
if s.slots[i].MLen() > s.slotOverWeighNum {
numOverSlotNum++
if numOverSlotNum >= maxOverSlotNum {
return true
} else {
continue
}
}
}
}
s.l.RUnlock()
return false
}
func (s *SlotMap) extend() {
if s.isExtending() {
fmt.Println("[extend]Another extend job is working, no need to extend twice")
return
}
if !s.shouldExend() {
fmt.Println("[extend]Slots have healthy length, no need to extend")
return
}
s.setExtendingState()
defer s.freeExtendingState()
s.lock()
s.unlock()
}
func (s *SlotMap) lock() {
if s.autoExtend {
s.l.Lock()
}
}
func (s *SlotMap) unlock() {
if s.autoExtend {
s.l.Unlock()
}
}
func (s *SlotMap) rLock() {
if s.autoExtend {
s.l.RLock()
}
}
func (s *SlotMap) rUnlock() {
if s.autoExtend {
s.l.RUnlock()
}
}
func (s *SlotMap) SlotNum() int {
s.rLock()
num := len(s.slots)
s.rUnlock()
return num
}
func (s SlotMap) hash(key string) int {
var slotNum int
s.rLock()
slotNum = len(s.slots)
s.rUnlock()
return UsMBCRC16([]byte(key)) % slotNum
}
func (s *SlotMap) Set(key string, value interface{}) {
s.rLock()
slot := s.slots[s.hash(key)]
s.rUnlock()
slot.Set(key, value)
return
}
func (s *SlotMap) SetEx(key string, value interface{}, seconds int) {
s.rLock()
slot := s.slots[s.hash(key)]
s.rUnlock()
slot.SetEx(key, value, seconds)
return
}
func (s *SlotMap) Get(key string) (interface{}, bool) {
s.rLock()
slot := s.slots[s.hash(key)]
s.rUnlock()
return slot.Get(key)
}
func (s *SlotMap) Delete(key string) {
s.rLock()
slot := s.slots[s.hash(key)]
s.rUnlock()
slot.Delete(key)
return
}