Repository navigation
Expand file tree
/
Copy pathcmap.go
More file actions
333 lines (297 loc) · 6.95 KB
/
Copy pathcmap.go
File metadata and controls
333 lines (297 loc) · 6.95 KB
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
package cmap
import (
"sync"
"golang.org/x/sync/errgroup"
"github.com/libranwmq/cmap/common/jsonx"
"github.com/libranwmq/cmap/common/nocopy"
)
// ConcurrentMapInterface is the interface of ConcurrentMap
type ConcurrentMapInterface[K Stringer, V any] interface {
Count() int
GetSlotNum() int
IsEmpty() bool
GetSlot(key K) *ConcurrentMapSlotted[K, V]
Set(key K, value V)
SetIfAbsent(key K, value V) bool
MSet(data map[K]V)
Upsert(key K, value V, cb UpsertCb[K, V]) V
Get(key K) (V, bool)
Pop(key K) (v V, ok bool)
Has(key K) bool
Remove(key K)
RemoveCb(key K, cb RemoveCb[K, V]) bool
Iter() <-chan Tuple[K, V]
IterCb(key K, v V, cb IterCb[K, V])
Items() map[K]V
Keys() []K
Tuples() []Tuple[K, V]
Clear()
UnmarshalJSON(b []byte) error
MarshalJSON() ([]byte, error)
}
type SlotingFunc[K Stringer] func(key K) uint32
type concurrentMap[K Stringer, V any] struct {
_ nocopy.NoCopy
slotNum int
sloting SlotingFunc[K]
slots []*ConcurrentMapSlotted[K, V]
}
// ConcurrentMapSlotted The key/value pairs stored in the slots of a concurrent map
type ConcurrentMapSlotted[K Stringer, V any] struct {
items map[K]V
sync.RWMutex
}
// New create a new ConcurrentMap
func New[K Stringer, V any](opts ...ConcurrentMapOption[K, V]) ConcurrentMapInterface[K, V] {
cm := &concurrentMap[K, V]{
sloting: defaultSloting[K],
slotNum: defaultSlotCount,
}
for _, opt := range opts {
opt(cm)
}
cm.slots = make([]*ConcurrentMapSlotted[K, V], cm.slotNum)
for i := range cm.slots {
cm.slots[i] = &ConcurrentMapSlotted[K, V]{
items: make(map[K]V),
}
}
return cm
}
// Count return the number of elements in the ConcurrentMap
func (c *concurrentMap[K, V]) Count() int {
var count int
for _, slot := range c.slots {
slot.RLock()
count += len(slot.items)
slot.RUnlock()
}
return count
}
// GetSlotNum get slot number
func (c *concurrentMap[K, V]) GetSlotNum() int {
return c.slotNum
}
// GetSlot get slot by key
func (c *concurrentMap[K, V]) GetSlot(key K) *ConcurrentMapSlotted[K, V] {
return c.slots[c.sloting(key)%uint32(c.slotNum)]
}
// IsEmpty return true if the ConcurrentMap is empty
func (c *concurrentMap[K, V]) IsEmpty() bool {
return c.Count() == 0
}
// Set set a key-value pair
func (c *concurrentMap[K, V]) Set(key K, value V) {
slot := c.GetSlot(key)
slot.Lock()
slot.items[key] = value
slot.Unlock()
}
// SetIfAbsent set a key-value pair if the key is absent
func (c *concurrentMap[K, V]) SetIfAbsent(key K, value V) bool {
slot := c.GetSlot(key)
slot.Lock()
defer slot.Unlock()
_, ok := slot.items[key]
if !ok {
slot.items[key] = value
}
return !ok
}
// MSet set a key-value pair
func (c *concurrentMap[K, V]) MSet(data map[K]V) {
for key, value := range data {
c.Set(key, value)
}
}
// Upsert set a key-value pair if the key is absent
func (c *concurrentMap[K, V]) Upsert(key K, value V, cb UpsertCb[K, V]) V {
slot := c.GetSlot(key)
slot.Lock()
defer slot.Unlock()
val, ok := slot.items[key]
slot.items[key] = cb(ok, val, value)
return slot.items[key]
}
// Get get a key-value pair
func (c *concurrentMap[K, V]) Get(key K) (V, bool) {
slot := c.GetSlot(key)
slot.RLock()
val, ok := slot.items[key]
slot.RUnlock()
return val, ok
}
// Pop get and remove a key-value pair
func (c *concurrentMap[K, V]) Pop(key K) (v V, ok bool) {
slot := c.GetSlot(key)
slot.Lock()
val, ok := slot.items[key]
delete(slot.items, key)
slot.Unlock()
return val, ok
}
// Has check if a key exists
func (c *concurrentMap[K, V]) Has(key K) bool {
slot := c.GetSlot(key)
slot.RLock()
_, ok := slot.items[key]
slot.RUnlock()
return ok
}
// Remove remove a key-value pair
func (c *concurrentMap[K, V]) Remove(key K) {
slot := c.GetSlot(key)
slot.Lock()
delete(slot.items, key)
slot.Unlock()
}
// RemoveCb remove a key-value pair if the callback returns true
func (c *concurrentMap[K, V]) RemoveCb(key K, cb RemoveCb[K, V]) bool {
slot := c.GetSlot(key)
slot.Lock()
v, ok := slot.items[key]
remove := cb(key, v, ok)
if remove && ok {
delete(slot.items, key)
}
slot.Unlock()
return remove
}
// Iter return a channel of key-value pairs
func (c *concurrentMap[K, V]) Iter() <-chan Tuple[K, V] {
chans := c.snapshot()
total := 0
for _, c := range chans {
total += cap(c)
}
ch := make(chan Tuple[K, V], total)
go fanIn(chans, ch)
return ch
}
// IterCb iterate over the ConcurrentMap with a callback
func (c *concurrentMap[K, V]) IterCb(key K, v V, cb IterCb[K, V]) {
for idx := range c.slots {
slot := (c.slots)[idx]
slot.RLock()
for key, value := range slot.items {
cb(key, value)
}
slot.RUnlock()
}
}
// Items return a map of key-value pairs
func (c *concurrentMap[K, V]) Items() map[K]V {
items := make(map[K]V)
for t := range c.Iter() {
items[t.Key] = t.Value
}
return items
}
// Keys return a slice of keys
func (c *concurrentMap[K, V]) Keys() []K {
count := c.Count()
ch := make(chan K, count)
go func() {
var eg errgroup.Group
for _, slot := range c.slots {
slot := slot
eg.Go(func() error {
slot.RLock()
for key := range slot.items {
ch <- key
}
slot.RUnlock()
return nil
})
}
_ = eg.Wait()
close(ch)
}()
keys := make([]K, 0, count)
for k := range ch {
keys = append(keys, k)
}
return keys
}
// Tuples return a slice of key-value pairs
func (c *concurrentMap[K, V]) Tuples() []Tuple[K, V] {
count := c.Count()
ch := make(chan Tuple[K, V], count)
go func() {
var eg errgroup.Group
for _, slot := range c.slots {
slot := slot
eg.Go(func() error {
slot.RLock()
for key, val := range slot.items {
ch <- Tuple[K, V]{
Key: key,
Value: val,
}
}
slot.RUnlock()
return nil
})
}
_ = eg.Wait()
close(ch)
}()
tuples := make([]Tuple[K, V], 0, count)
for t := range ch {
tuples = append(tuples, t)
}
return tuples
}
// Clear remove all key-value pairs
func (c *concurrentMap[K, V]) Clear() {
for item := range c.Iter() {
c.Remove(item.Key)
}
}
// UnmarshalJSON unmarshal JSON to ConcurrentMap
func (c *concurrentMap[K, V]) UnmarshalJSON(b []byte) error {
tmp := make(map[K]V)
if err := jsonx.UnmarshalJSON(b, &tmp); err != nil {
return err
}
c.MSet(tmp)
return nil
}
// MarshalJSON marshal ConcurrentMap to JSON
func (c *concurrentMap[K, V]) MarshalJSON() ([]byte, error) {
return jsonx.MarshalJSON(c.Items())
}
func (c *concurrentMap[K, V]) snapshot() []chan Tuple[K, V] {
chans := make([]chan Tuple[K, V], c.slotNum)
var eg errgroup.Group
for index, slot := range c.slots {
index := index
slot := slot
eg.Go(func() error {
slot.RLock()
chans[index] = make(chan Tuple[K, V], len(slot.items))
for key, val := range slot.items {
chans[index] <- Tuple[K, V]{key, val}
}
slot.RUnlock()
close(chans[index])
return nil
})
}
_ = eg.Wait()
return chans
}
func fanIn[K Stringer, V any](chans []chan Tuple[K, V], out chan Tuple[K, V]) {
var eg errgroup.Group
for _, ch := range chans {
ch := ch
eg.Go(func() error {
for t := range ch {
out <- t
}
return nil
})
}
_ = eg.Wait()
close(out)
}