forked from aceld/zinx
/
shard_lock_map.go
297 lines (260 loc) · 7.12 KB
/
shard_lock_map.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package zutils
import (
"encoding/json"
"sync"
)
var ShardCount = 32
// ShardLockMaps A "thread" safe map of type string:Anything.
// To avoid lock bottlenecks this map is dived to several (ShardCount) map shards.
type ShardLockMaps struct {
shards []*SingleShardMap
hash IHash
}
// SingleShardMap A "thread" safe string to anything map.
type SingleShardMap struct {
items map[string]interface{}
sync.RWMutex
}
// createShardLockMaps Creates a new concurrent map.
func createShardLockMaps(hash IHash) ShardLockMaps {
slm := ShardLockMaps{
shards: make([]*SingleShardMap, ShardCount),
hash: hash,
}
for i := 0; i < ShardCount; i++ {
slm.shards[i] = &SingleShardMap{items: make(map[string]interface{})}
}
return slm
}
// NewShardLockMaps Creates a new ShardLockMaps.
func NewShardLockMaps() ShardLockMaps {
return createShardLockMaps(DefaultHash())
}
func NewWithCustomHash(hash IHash) ShardLockMaps {
return createShardLockMaps(hash)
}
// GetShard returns shard under given key
func (slm ShardLockMaps) GetShard(key string) *SingleShardMap {
return slm.shards[slm.hash.Sum(key)%uint32(ShardCount)]
}
// Count returns the number of elements within the map.
func (slm ShardLockMaps) Count() int {
count := 0
for i := 0; i < ShardCount; i++ {
shard := slm.shards[i]
shard.RLock()
count += len(shard.items)
shard.RUnlock()
}
return count
}
// Get retrieves an element from map under given key.
func (slm ShardLockMaps) Get(key string) (interface{}, bool) {
shard := slm.GetShard(key)
shard.RLock()
val, ok := shard.items[key]
shard.RUnlock()
return val, ok
}
// Set Sets the given value under the specified key.
func (slm ShardLockMaps) Set(key string, value interface{}) {
shard := slm.GetShard(key)
shard.Lock()
shard.items[key] = value
shard.Unlock()
}
// SetNX Sets the given value under the specified key if no value was associated with it.
func (slm ShardLockMaps) SetNX(key string, value interface{}) bool {
shard := slm.GetShard(key)
shard.Lock()
_, ok := shard.items[key]
if !ok {
shard.items[key] = value
}
shard.Unlock()
return !ok
}
// MSet Sets the given value under the specified key.
func (slm ShardLockMaps) MSet(data map[string]interface{}) {
for key, value := range data {
shard := slm.GetShard(key)
shard.Lock()
shard.items[key] = value
shard.Unlock()
}
}
// Has Looks up an item under specified key
func (slm ShardLockMaps) Has(key string) bool {
shard := slm.GetShard(key)
shard.RLock()
_, ok := shard.items[key]
shard.RUnlock()
return ok
}
// Remove removes an element from the map.
func (slm ShardLockMaps) Remove(key string) {
shard := slm.GetShard(key)
shard.Lock()
delete(shard.items, key)
shard.Unlock()
}
// RemoveCb is a callback executed in a map.RemoveCb() call, while Lock is held
// If returns true, the element will be removed from the map
type RemoveCb func(key string, v interface{}, exists bool) bool
// RemoveCb locks the shard containing the key, retrieves its current value and calls the callback with those params
// If callback returns true and element exists, it will remove it from the map
// Returns the value returned by the callback (even if element was not present in the map)
func (slm ShardLockMaps) RemoveCb(key string, cb RemoveCb) bool {
shard := slm.GetShard(key)
shard.Lock()
v, ok := shard.items[key]
remove := cb(key, v, ok)
if remove && ok {
delete(shard.items, key)
}
shard.Unlock()
return remove
}
// Pop removes an element from the map and returns it
func (slm ShardLockMaps) Pop(key string) (v interface{}, exists bool) {
shard := slm.GetShard(key)
shard.Lock()
v, exists = shard.items[key]
delete(shard.items, key)
shard.Unlock()
return v, exists
}
// Clear removes all items from map.
func (slm ShardLockMaps) Clear() {
for item := range slm.IterBuffered() {
slm.Remove(item.Key)
}
}
// IsEmpty checks if map is empty.
func (slm ShardLockMaps) IsEmpty() bool {
return slm.Count() == 0
}
// Tuple Used by the IterBuffered functions to wrap two variables together over a channel,
type Tuple struct {
Key string
Val interface{}
}
// Returns a array of channels that contains elements in each shard,
// which likely takes a snapshot of `slm`.
// It returns once the size of each buffered channel is determined,
// before all the channels are populated using goroutines.
func snapshot(slm ShardLockMaps) (chanList []chan Tuple) {
chanList = make([]chan Tuple, ShardCount)
wg := sync.WaitGroup{}
wg.Add(ShardCount)
for index, shard := range slm.shards {
go func(index int, shard *SingleShardMap) {
shard.RLock()
chanList[index] = make(chan Tuple, len(shard.items))
wg.Done()
for key, val := range shard.items {
chanList[index] <- Tuple{key, val}
}
shard.RUnlock()
close(chanList[index])
}(index, shard)
}
wg.Wait()
return chanList
}
// fanIn reads elements from channels `chanList` into channel `out`
func fanIn(chanList []chan Tuple, out chan Tuple) {
wg := sync.WaitGroup{}
wg.Add(len(chanList))
for _, ch := range chanList {
go func(ch chan Tuple) {
for t := range ch {
out <- t
}
wg.Done()
}(ch)
}
wg.Wait()
close(out)
}
// IterBuffered returns a buffered iterator which could be used in a for range loop.
func (slm ShardLockMaps) IterBuffered() <-chan Tuple {
chanList := snapshot(slm)
total := 0
for _, c := range chanList {
total += cap(c)
}
ch := make(chan Tuple, total)
go fanIn(chanList, ch)
return ch
}
// Items returns all items as map[string]interface{}
func (slm ShardLockMaps) Items() map[string]interface{} {
tmp := make(map[string]interface{})
for item := range slm.IterBuffered() {
tmp[item.Key] = item.Val
}
return tmp
}
// Keys returns all keys as []string
func (slm ShardLockMaps) Keys() []string {
count := slm.Count()
ch := make(chan string, count)
go func() {
wg := sync.WaitGroup{}
wg.Add(ShardCount)
for _, shard := range slm.shards {
go func(shard *SingleShardMap) {
shard.RLock()
for key := range shard.items {
ch <- key
}
shard.RUnlock()
wg.Done()
}(shard)
}
wg.Wait()
close(ch)
}()
keys := make([]string, 0, count)
for k := range ch {
keys = append(keys, k)
}
return keys
}
// IterCb Iterator callback,called for every key,value found in maps.
// RLock is held for all calls for a given shard
// therefore callback sess consistent view of a shard,
// but not across the shards
type IterCb func(key string, v interface{})
// IterCb Callback based iterator, cheapest way to read
// all elements in a map.
func (slm ShardLockMaps) IterCb(fn IterCb) {
for idx := range slm.shards {
shard := (slm.shards)[idx]
shard.RLock()
for key, value := range shard.items {
fn(key, value)
}
shard.RUnlock()
}
}
// MarshalJSON Reviles ConcurrentMap "private" variables to json marshal.
func (slm ShardLockMaps) MarshalJSON() ([]byte, error) {
tmp := make(map[string]interface{})
for item := range slm.IterBuffered() {
tmp[item.Key] = item.Val
}
return json.Marshal(tmp)
}
// UnmarshalJSON Reverse process of Marshal.
func (slm ShardLockMaps) UnmarshalJSON(b []byte) (err error) {
tmp := make(map[string]interface{})
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
for key, val := range tmp {
slm.Set(key, val)
}
return nil
}