-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_mapof.go
356 lines (315 loc) · 9 KB
/
concurrent_mapof.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//go:build go1.18
// +build go1.18
package cmap
import (
"encoding/json"
"sync"
"github.com/fufuok/cmap/internal/xxhash"
)
// Hashable allowed map key types constraint
type Hashable interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 | ~string | ~complex64 | ~complex128
}
// MapOf a "thread" safe map of type comparable:Anything.
// To avoid lock bottlenecks this map is dived to several (ShardCount) map shards.
type MapOf[K comparable, V any] struct {
shards []*SharedOf[K, V]
sharding func(key K) uint64
shardCount int
}
// SharedOf a "thread" safe string to anything map.
type SharedOf[K comparable, V any] struct {
items map[K]V
sync.RWMutex
}
func create[K comparable, V any](sharding func(key K) uint64, numShards ...int) *MapOf[K, V] {
sc := ShardCount
if len(numShards) > 0 && numShards[0] > ShardCount {
sc = numShards[0]
}
m := &MapOf[K, V]{
shards: make([]*SharedOf[K, V], sc),
sharding: sharding,
shardCount: sc,
}
for i := 0; i < sc; i++ {
m.shards[i] = &SharedOf[K, V]{items: make(map[K]V)}
}
return m
}
// NewOf creates a new concurrent map, optionally specify the number of shards.
func NewOf[K Hashable, V any](numShards ...int) *MapOf[K, V] {
return NewTypedMapOf[K, V](xxhash.GenHasher64[K](), numShards...)
}
// NewTypedMapOf creates a new concurrent map, optionally specify the number of shards.
func NewTypedMapOf[K comparable, V any](sharding func(key K) uint64, numShards ...int) *MapOf[K, V] {
return create[K, V](sharding, numShards...)
}
// GetShard returns shard under given key
func (m *MapOf[K, V]) GetShard(key K) *SharedOf[K, V] {
return m.shards[uint(m.sharding(key))%uint(m.shardCount)]
}
func (m *MapOf[K, V]) MSet(data map[K]V) {
for key, value := range data {
shard := m.GetShard(key)
shard.Lock()
shard.items[key] = value
shard.Unlock()
}
}
// Set sets the given value under the specified key.
func (m *MapOf[K, V]) Set(key K, value V) {
// Get map shard.
shard := m.GetShard(key)
shard.Lock()
shard.items[key] = value
shard.Unlock()
}
// UpsertCbOf callback to return new element to be inserted into the map
// It is called while lock is held, therefore it MUST NOT
// try to access other keys in same map, as it can lead to deadlock since
// Go sync.RWLock is not reentrant
type UpsertCbOf[V any] func(exist bool, valueInMap V, newValue V) V
// Upsert insert or update - updates existing element or inserts a new one using UpsertCbOf
func (m *MapOf[K, V]) Upsert(key K, value V, cb UpsertCbOf[V]) (res V) {
shard := m.GetShard(key)
shard.Lock()
v, ok := shard.items[key]
res = cb(ok, v, value)
shard.items[key] = res
shard.Unlock()
return res
}
// SetIfAbsent sets the given value under the specified key if no value was associated with it.
func (m *MapOf[K, V]) SetIfAbsent(key K, value V) bool {
// Get map shard.
shard := m.GetShard(key)
shard.Lock()
_, ok := shard.items[key]
if !ok {
shard.items[key] = value
}
shard.Unlock()
return !ok
}
// Get retrieves an element from map under given key.
func (m *MapOf[K, V]) Get(key K) (V, bool) {
// Get shard
shard := m.GetShard(key)
shard.RLock()
// Get item from shard.
val, ok := shard.items[key]
shard.RUnlock()
return val, ok
}
// GetValue get retrieves an element from map under given key.
func (m *MapOf[K, V]) GetValue(key K) (val V) {
val, _ = m.Get(key)
return
}
// Count returns the number of elements within the map.
func (m *MapOf[K, V]) Count() int {
count := 0
for i := 0; i < m.shardCount; i++ {
shard := m.shards[i]
shard.RLock()
count += len(shard.items)
shard.RUnlock()
}
return count
}
// Has looks up an item under specified key
func (m *MapOf[K, V]) Has(key K) bool {
// Get shard
shard := m.GetShard(key)
shard.RLock()
// See if element is within shard.
_, ok := shard.items[key]
shard.RUnlock()
return ok
}
// Remove removes an element from the map.
func (m *MapOf[K, V]) Remove(key K) {
// Try to get shard.
shard := m.GetShard(key)
shard.Lock()
delete(shard.items, key)
shard.Unlock()
}
// RemoveCbOf is a callback executed in a map.RemoveCbOf() call, while Lock is held
// If returns true, the element will be removed from the map
type RemoveCbOf[K any, V any] func(key K, v V, 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 (m *MapOf[K, V]) RemoveCb(key K, cb RemoveCbOf[K, V]) bool {
// Try to get shard.
shard := m.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 (m *MapOf[K, V]) Pop(key K) (v V, exists bool) {
// Try to get shard.
shard := m.GetShard(key)
shard.Lock()
v, exists = shard.items[key]
delete(shard.items, key)
shard.Unlock()
return v, exists
}
// IsEmpty checks if map is empty.
func (m *MapOf[K, V]) IsEmpty() bool {
return m.Count() == 0
}
// TupleOf used by the Iter & IterBuffered functions to wrap two variables together over a channel,
type TupleOf[K comparable, V any] struct {
Key K
Val V
}
// IterBuffered returns a buffered iterator which could be used in a for range loop.
func (m *MapOf[K, V]) IterBuffered() <-chan TupleOf[K, V] {
chans := snapshotOf(m)
total := 0
for _, c := range chans {
total += cap(c)
}
ch := make(chan TupleOf[K, V], total)
go fanInOf(chans, ch)
return ch
}
// Clear removes all items from map.
func (m *MapOf[K, V]) Clear() {
for item := range m.IterBuffered() {
m.Remove(item.Key)
}
}
// Returns a array of channels that contains elements in each shard,
// which likely takes a snapshotOf of `m`.
// It returns once the size of each buffered channel is determined,
// before all the channels are populated using goroutines.
func snapshotOf[K comparable, V any](m *MapOf[K, V]) (chans []chan TupleOf[K, V]) {
// When you access map items before initializing.
if len(m.shards) == 0 {
panic(`cmap.MapOf is not initialized. Should run New() before usage.`)
}
chans = make([]chan TupleOf[K, V], m.shardCount)
wg := sync.WaitGroup{}
wg.Add(m.shardCount)
// Foreach shard.
for index, shard := range m.shards {
go func(index int, shard *SharedOf[K, V]) {
// Foreach key, value pair.
shard.RLock()
chans[index] = make(chan TupleOf[K, V], len(shard.items))
wg.Done()
for key, val := range shard.items {
chans[index] <- TupleOf[K, V]{key, val}
}
shard.RUnlock()
close(chans[index])
}(index, shard)
}
wg.Wait()
return chans
}
// fanInOf reads elements from channels `chans` into channel `out`
func fanInOf[K comparable, V any](chans []chan TupleOf[K, V], out chan TupleOf[K, V]) {
wg := sync.WaitGroup{}
wg.Add(len(chans))
for _, ch := range chans {
go func(ch chan TupleOf[K, V]) {
for t := range ch {
out <- t
}
wg.Done()
}(ch)
}
wg.Wait()
close(out)
}
// Items returns all items as map[string]V
func (m *MapOf[K, V]) Items() map[K]V {
tmp := make(map[K]V)
// Insert items to temporary map.
for item := range m.IterBuffered() {
tmp[item.Key] = item.Val
}
return tmp
}
// IterCbOf iterator callbacalled 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 IterCbOf[K comparable, V any] func(key K, v V)
// IterCb Callback based iterator, cheapest way to read
// all elements in a map.
func (m *MapOf[K, V]) IterCb(fn IterCbOf[K, V]) {
for idx := range m.shards {
shard := (m.shards)[idx]
shard.RLock()
for key, value := range shard.items {
fn(key, value)
}
shard.RUnlock()
}
}
// Keys returns all keys as []string
func (m *MapOf[K, V]) Keys() []K {
count := m.Count()
ch := make(chan K, count)
go func() {
// Foreach shard.
wg := sync.WaitGroup{}
wg.Add(m.shardCount)
for _, shard := range m.shards {
go func(shard *SharedOf[K, V]) {
// Foreach key, value pair.
shard.RLock()
for key := range shard.items {
ch <- key
}
shard.RUnlock()
wg.Done()
}(shard)
}
wg.Wait()
close(ch)
}()
// Generate keys
keys := make([]K, 0, count)
for k := range ch {
keys = append(keys, k)
}
return keys
}
// MarshalJSON reviles MapOf "private" variables to json marshal.
func (m *MapOf[K, V]) MarshalJSON() ([]byte, error) {
// Create a temporary map, which will hold all item spread across shards.
tmp := make(map[K]V)
// Insert items to temporary map.
for item := range m.IterBuffered() {
tmp[item.Key] = item.Val
}
return json.Marshal(tmp)
}
// UnmarshalJSON reverse process of Marshal.
func (m *MapOf[K, V]) UnmarshalJSON(b []byte) (err error) {
tmp := make(map[K]V)
// Unmarshal into a single map.
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
// foreach key,value pair in temporary map insert into our concurrent map.
for key, val := range tmp {
m.Set(key, val)
}
return nil
}