@@ -16,8 +16,9 @@ type Stringer interface {
16
16
// A "thread" safe map of type string:Anything.
17
17
// To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.
18
18
type ConcurrentMap [K comparable , V any ] struct {
19
- shards []* ConcurrentMapShared [K , V ]
20
- sharding func (key K ) uint32
19
+ shards []* ConcurrentMapShared [K , V ]
20
+ sharding func (key K ) uint32
21
+ shardCount uint32
21
22
}
22
23
23
24
// A "thread" safe string to anything map.
@@ -26,35 +27,41 @@ type ConcurrentMapShared[K comparable, V any] struct {
26
27
sync.RWMutex // Read Write mutex, guards access to internal map.
27
28
}
28
29
29
- func create [K comparable , V any ](sharding func (key K ) uint32 ) ConcurrentMap [K , V ] {
30
+ func create [K comparable , V any ](sharding func (key K ) uint32 , shardCount uint32 ) ConcurrentMap [K , V ] {
30
31
m := ConcurrentMap [K , V ]{
31
- sharding : sharding ,
32
- shards : make ([]* ConcurrentMapShared [K , V ], SHARD_COUNT ),
32
+ sharding : sharding ,
33
+ shards : make ([]* ConcurrentMapShared [K , V ], shardCount ),
34
+ shardCount : shardCount ,
33
35
}
34
- for i := 0 ; i < SHARD_COUNT ; i ++ {
36
+ for i := 0 ; i < int ( m . shardCount ) ; i ++ {
35
37
m .shards [i ] = & ConcurrentMapShared [K , V ]{items : make (map [K ]V )}
36
38
}
37
39
return m
38
40
}
39
41
40
42
// Creates a new concurrent map.
41
43
func New [V any ]() ConcurrentMap [string , V ] {
42
- return create [string , V ](fnv32 )
44
+ return create [string , V ](fnv32 , uint32 ( SHARD_COUNT ) )
43
45
}
44
46
45
47
// Creates a new concurrent map.
46
48
func NewStringer [K Stringer , V any ]() ConcurrentMap [K , V ] {
47
- return create [K , V ](strfnv32 [K ])
49
+ return create [K , V ](strfnv32 [K ], uint32 ( SHARD_COUNT ) )
48
50
}
49
51
50
52
// Creates a new concurrent map.
51
53
func NewWithCustomShardingFunction [K comparable , V any ](sharding func (key K ) uint32 ) ConcurrentMap [K , V ] {
52
- return create [K , V ](sharding )
54
+ return create [K , V ](sharding , uint32 (SHARD_COUNT ))
55
+ }
56
+
57
+ // NewWithCustomShardingCountFunction Create a new concurrent map using the given shardCount
58
+ func NewWithCustomShardingCountFunction [K comparable , V any ](sharding func (key K ) uint32 , shardCount uint32 ) ConcurrentMap [K , V ] {
59
+ return create [K , V ](sharding , shardCount )
53
60
}
54
61
55
62
// GetShard returns shard under given key
56
63
func (m ConcurrentMap [K , V ]) GetShard (key K ) * ConcurrentMapShared [K , V ] {
57
- return m .shards [uint (m .sharding (key ))% uint (SHARD_COUNT )]
64
+ return m .shards [uint (m .sharding (key ))% uint (m . shardCount )]
58
65
}
59
66
60
67
func (m ConcurrentMap [K , V ]) MSet (data map [K ]V ) {
@@ -119,7 +126,7 @@ func (m ConcurrentMap[K, V]) Get(key K) (V, bool) {
119
126
// Count returns the number of elements within the map.
120
127
func (m ConcurrentMap [K , V ]) Count () int {
121
128
count := 0
122
- for i := 0 ; i < SHARD_COUNT ; i ++ {
129
+ for i := 0 ; i < int ( m . shardCount ) ; i ++ {
123
130
shard := m .shards [i ]
124
131
shard .RLock ()
125
132
count += len (shard .items )
@@ -228,9 +235,9 @@ func snapshot[K comparable, V any](m ConcurrentMap[K, V]) (chans []chan Tuple[K,
228
235
if len (m .shards ) == 0 {
229
236
panic (`cmap.ConcurrentMap is not initialized. Should run New() before usage.` )
230
237
}
231
- chans = make ([]chan Tuple [K , V ], SHARD_COUNT )
238
+ chans = make ([]chan Tuple [K , V ], m . shardCount )
232
239
wg := sync.WaitGroup {}
233
- wg .Add (SHARD_COUNT )
240
+ wg .Add (int ( m . shardCount ) )
234
241
// Foreach shard.
235
242
for index , shard := range m .shards {
236
243
go func (index int , shard * ConcurrentMapShared [K , V ]) {
@@ -303,7 +310,7 @@ func (m ConcurrentMap[K, V]) Keys() []K {
303
310
go func () {
304
311
// Foreach shard.
305
312
wg := sync.WaitGroup {}
306
- wg .Add (SHARD_COUNT )
313
+ wg .Add (int ( m . shardCount ) )
307
314
for _ , shard := range m .shards {
308
315
go func (shard * ConcurrentMapShared [K , V ]) {
309
316
// Foreach key, value pair.
0 commit comments