1
+ // AUTO-GENERATED by mapSpec.sh
2
+ // DO NOT EDIT
3
+ // generated from https://github.com/OneOfOne/cmap/tree/master/internal/cmap
4
+
1
5
package cmap
2
6
3
7
import (
@@ -6,21 +10,16 @@ import (
6
10
)
7
11
8
12
type KV struct {
9
- Key KT
10
- Value VT
13
+ Key interface {}
14
+ Value interface {}
11
15
}
12
16
13
- // DefaultShardCount is the default number of shards to use when New() or NewFromJSON() are called.
14
- // The default is 256.
15
- const DefaultShardCount = 1 << 8
16
-
17
17
// CMap is a concurrent safe sharded map to scale on multiple cores.
18
18
type CMap struct {
19
- shards []* lmap
19
+ shards []* LockedMap
20
20
// HashFn allows using a custom hash function that's used to determain the key's shard.
21
- // Defaults to DefaultKeyHasher
22
- HashFn func (KT ) uint32
23
-
21
+ // Defaults to DefaultKeyHasher.
22
+ HashFn func (interface {}) uint32
24
23
keysPool sync.Pool
25
24
mod uint32
26
25
}
@@ -38,49 +37,43 @@ func NewSize(shardCount int) *CMap {
38
37
} else if shardCount & (shardCount - 1 ) != 0 {
39
38
panic ("shardCount must be a power of 2" )
40
39
}
41
-
42
40
cm := & CMap {
43
- shards : make ([]* lmap , shardCount ),
41
+ shards : make ([]* LockedMap , shardCount ),
44
42
mod : uint32 (shardCount ) - 1 ,
45
43
HashFn : DefaultKeyHasher ,
46
44
}
47
-
48
45
for i := range cm .shards {
49
- cm .shards [i ] = newLmap (shardCount )
46
+ cm .shards [i ] = NewLockedMapSize (shardCount )
50
47
}
51
-
52
48
cm .keysPool .New = func () interface {} {
53
- out := make ([]KT , 0 , shardCount ) // good starting round
54
-
55
- return & out // return a ptr to avoid extra allocation on Get/Put
49
+ out := make ([]interface {}, 0 , shardCount ) // good starting round
50
+ return & out // return a ptr to avoid extra allocation on Get/Put
56
51
}
57
-
58
52
return cm
59
53
}
60
-
61
- func (cm * CMap ) shard (key KT ) * lmap {
54
+ func (cm * CMap ) shard (key interface {}) * LockedMap {
62
55
h := cm .HashFn (key )
63
56
return cm .shards [h & cm .mod ]
64
57
}
65
58
66
59
// Get is the equivalent of `val := map[key]`.
67
- func (cm * CMap ) Get (key KT ) (val VT ) {
60
+ func (cm * CMap ) Get (key interface {} ) (val interface {} ) {
68
61
return cm .shard (key ).Get (key )
69
62
}
70
63
71
64
// GetOK is the equivalent of `val, ok := map[key]`.
72
- func (cm * CMap ) GetOK (key KT ) (val VT , ok bool ) {
65
+ func (cm * CMap ) GetOK (key interface {} ) (val interface {} , ok bool ) {
73
66
return cm .shard (key ).GetOK (key )
74
67
}
75
68
76
69
// Set is the equivalent of `map[key] = val`.
77
- func (cm * CMap ) Set (key KT , val VT ) {
70
+ func (cm * CMap ) Set (key interface {} , val interface {} ) {
78
71
cm .shard (key ).Set (key , val )
79
72
}
80
73
81
74
// SetIfNotExists will only assign val to key if it wasn't already set.
82
75
// Use `CMap.Update` if you need more logic.
83
- func (cm * CMap ) SetIfNotExists (key KT , val VT ) (set bool ) {
76
+ func (cm * CMap ) SetIfNotExists (key interface {} , val interface {} ) (set bool ) {
84
77
sh := cm .shard (key )
85
78
sh .l .Lock ()
86
79
if _ , ok := sh .m [key ]; ! ok {
@@ -91,28 +84,28 @@ func (cm *CMap) SetIfNotExists(key KT, val VT) (set bool) {
91
84
}
92
85
93
86
// Has is the equivalent of `_, ok := map[key]`.
94
- func (cm * CMap ) Has (key KT ) bool { return cm .shard (key ).Has (key ) }
87
+ func (cm * CMap ) Has (key interface {} ) bool { return cm .shard (key ).Has (key ) }
95
88
96
89
// Delete is the equivalent of `delete(map, key)`.
97
- func (cm * CMap ) Delete (key KT ) { cm .shard (key ).Delete (key ) }
90
+ func (cm * CMap ) Delete (key interface {} ) { cm .shard (key ).Delete (key ) }
98
91
99
92
// DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.
100
- func (cm * CMap ) DeleteAndGet (key KT ) VT { return cm .shard (key ).DeleteAndGet (key ) }
93
+ func (cm * CMap ) DeleteAndGet (key interface {}) interface {} { return cm .shard (key ).DeleteAndGet (key ) }
101
94
102
95
// Update calls `fn` with the key's old value (or nil if it didn't exist) and assign the returned value to the key.
103
96
// The shard containing the key will be locked, it is NOT safe to call other cmap funcs inside `fn`.
104
- func (cm * CMap ) Update (key KT , fn func (oldval VT ) (newval VT )) {
97
+ func (cm * CMap ) Update (key interface {} , fn func (oldval interface {} ) (newval interface {} )) {
105
98
cm .shard (key ).Update (key , fn )
106
99
}
107
100
108
101
// Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.
109
- func (cm * CMap ) Swap (key KT , val VT ) VT {
102
+ func (cm * CMap ) Swap (key interface {} , val interface {}) interface {} {
110
103
return cm .shard (key ).Swap (key , val )
111
104
}
112
105
113
106
// Keys returns a slice of all the keys of the map.
114
- func (cm * CMap ) Keys () []KT {
115
- out := make ([]KT , 0 , cm .Len ())
107
+ func (cm * CMap ) Keys () []interface {} {
108
+ out := make ([]interface {} , 0 , cm .Len ())
116
109
for i := range cm .shards {
117
110
sh := cm .shards [i ]
118
111
sh .l .RLock ()
@@ -125,11 +118,10 @@ func (cm *CMap) Keys() []KT {
125
118
}
126
119
127
120
// ForEach loops over all the key/values in all the shards in order.
128
- // You can break early by returning an error.
121
+ // You can break early by returning an error or Break .
129
122
// It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.
130
- func (cm * CMap ) ForEach (fn func (key KT , val VT ) error ) error {
131
- keysP := cm .keysPool .Get ().(* []KT )
132
-
123
+ func (cm * CMap ) ForEach (fn func (key interface {}, val interface {}) error ) error {
124
+ keysP := cm .keysPool .Get ().(* []interface {})
133
125
defer cm .keysPool .Put (keysP )
134
126
for i := range cm .shards {
135
127
keys := (* keysP )[:0 ]
@@ -144,9 +136,9 @@ func (cm *CMap) ForEach(fn func(key KT, val VT) error) error {
144
136
}
145
137
146
138
// ForEachLocked loops over all the key/values in the map.
147
- // You can break early by returning an error.
139
+ // You can break early by returning an error or Break .
148
140
// It is **NOT* safe to modify the map while using this iterator.
149
- func (cm * CMap ) ForEachLocked (fn func (key KT , val VT ) error ) error {
141
+ func (cm * CMap ) ForEachLocked (fn func (key interface {} , val interface {} ) error ) error {
150
142
for i := range cm .shards {
151
143
if err := cm .shards [i ].ForEachLocked (fn ); err != nil {
152
144
if err == Break {
@@ -181,9 +173,8 @@ func (cm *CMap) IterLocked(ctx context.Context, buffer int) <-chan *KV {
181
173
}()
182
174
return ch
183
175
}
184
-
185
176
func (cm * CMap ) iterContext (ctx context.Context , ch chan <- * KV , locked bool ) {
186
- fn := func (k KT , v VT ) error {
177
+ fn := func (k interface {} , v interface {} ) error {
187
178
select {
188
179
case <- ctx .Done ():
189
180
return Break
@@ -209,3 +200,100 @@ func (cm *CMap) Len() int {
209
200
210
201
// NumShards returns the number of shards in the map.
211
202
func (cm * CMap ) NumShards () int { return len (cm .shards ) }
203
+
204
+ type LockedMap struct {
205
+ m map [interface {}]interface {}
206
+ l * sync.RWMutex
207
+ }
208
+
209
+ func NewLockedMap () * LockedMap {
210
+ return NewLockedMapSize (0 )
211
+ }
212
+ func NewLockedMapSize (cap int ) * LockedMap {
213
+ return & LockedMap {
214
+ m : make (map [interface {}]interface {}, cap ),
215
+ l : new (sync.RWMutex ),
216
+ }
217
+ }
218
+ func (lm LockedMap ) Set (key interface {}, v interface {}) {
219
+ lm .l .Lock ()
220
+ lm .m [key ] = v
221
+ lm .l .Unlock ()
222
+ }
223
+ func (lm LockedMap ) Update (key interface {}, fn func (oldVal interface {}) (newVal interface {})) {
224
+ lm .l .Lock ()
225
+ lm .m [key ] = fn (lm .m [key ])
226
+ lm .l .Unlock ()
227
+ }
228
+ func (lm LockedMap ) Swap (key interface {}, newV interface {}) (oldV interface {}) {
229
+ lm .l .Lock ()
230
+ oldV = lm .m [key ]
231
+ lm .m [key ] = newV
232
+ lm .l .Unlock ()
233
+ return
234
+ }
235
+ func (lm LockedMap ) Get (key interface {}) (v interface {}) {
236
+ lm .l .RLock ()
237
+ v = lm .m [key ]
238
+ lm .l .RUnlock ()
239
+ return
240
+ }
241
+ func (lm LockedMap ) GetOK (key interface {}) (v interface {}, ok bool ) {
242
+ lm .l .RLock ()
243
+ v , ok = lm .m [key ]
244
+ lm .l .RUnlock ()
245
+ return
246
+ }
247
+ func (lm LockedMap ) Has (key interface {}) (ok bool ) {
248
+ lm .l .RLock ()
249
+ _ , ok = lm .m [key ]
250
+ lm .l .RUnlock ()
251
+ return
252
+ }
253
+ func (lm LockedMap ) Delete (key interface {}) {
254
+ lm .l .Lock ()
255
+ delete (lm .m , key )
256
+ lm .l .Unlock ()
257
+ }
258
+ func (lm LockedMap ) DeleteAndGet (key interface {}) (v interface {}) {
259
+ lm .l .Lock ()
260
+ v = lm .m [key ]
261
+ delete (lm .m , key )
262
+ lm .l .Unlock ()
263
+ return v
264
+ }
265
+ func (lm LockedMap ) Len () (ln int ) {
266
+ lm .l .RLock ()
267
+ ln = len (lm .m )
268
+ lm .l .RUnlock ()
269
+ return
270
+ }
271
+ func (lm LockedMap ) ForEach (keys []interface {}, fn func (key interface {}, val interface {}) error ) (err error ) {
272
+ lm .l .RLock ()
273
+ for key := range lm .m {
274
+ keys = append (keys , key )
275
+ }
276
+ lm .l .RUnlock ()
277
+ for _ , key := range keys {
278
+ lm .l .RLock ()
279
+ val , ok := lm .m [key ]
280
+ lm .l .RUnlock ()
281
+ if ! ok {
282
+ continue
283
+ }
284
+ if err = fn (key , val ); err != nil {
285
+ return
286
+ }
287
+ }
288
+ return
289
+ }
290
+ func (lm LockedMap ) ForEachLocked (fn func (key interface {}, val interface {}) error ) (err error ) {
291
+ lm .l .RLock ()
292
+ defer lm .l .RUnlock ()
293
+ for key , val := range lm .m {
294
+ if err = fn (key , val ); err != nil {
295
+ return
296
+ }
297
+ }
298
+ return
299
+ }
0 commit comments