-
Notifications
You must be signed in to change notification settings - Fork 11
/
map_sharded.go
258 lines (227 loc) · 7.26 KB
/
map_sharded.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
package async
import (
"fmt"
"hash/fnv"
"sync"
)
// ShardedMap implements the async.Map interface in a thread-safe manner,
// delegating load/store operations to one of the underlying async.SynchronizedMaps
// (shards), using a key hash to calculate the shard number.
// A ShardedMap must not be copied.
type ShardedMap[K comparable, V any] struct {
shards uint64
shardMap []*SynchronizedMap[K, V]
hashFunc func(K) uint64
}
var _ Map[int, any] = (*ShardedMap[int, any])(nil)
// NewShardedMap returns a new ShardedMap, where shards is the number of partitions for this
// map. It uses the 64-bit FNV-1a hash function to calculate the shard number for a key.
// If the shards argument is not positive, NewShardedMap will panic.
func NewShardedMap[K comparable, V any](shards int) *ShardedMap[K, V] {
return NewShardedMapWithHash[K, V](shards, func(key K) uint64 {
h := fnv.New64a()
h.Write([]byte(fmt.Sprint(key)))
return h.Sum64()
})
}
// NewShardedMapWithHash returns a new ShardedMap, where shards is the number of partitions
// for this map, and hashFunc is a hash function to calculate the shard number for a key.
// If shards is not positive or hashFunc is nil, NewShardedMapWithHash will panic.
func NewShardedMapWithHash[K comparable, V any](shards int, hashFunc func(K) uint64) *ShardedMap[K, V] {
if shards < 1 {
panic(fmt.Sprintf("nonpositive shards: %d", shards))
}
if hashFunc == nil {
panic("hashFunc is nil")
}
shardMap := make([]*SynchronizedMap[K, V], shards)
for i := 0; i < shards; i++ {
shardMap[i] = NewSynchronizedMap[K, V]()
}
return &ShardedMap[K, V]{
shards: uint64(shards),
shardMap: shardMap,
hashFunc: hashFunc,
}
}
// Clear removes all of the mappings from this map.
func (sm *ShardedMap[K, V]) Clear() {
for _, shard := range sm.shardMap {
shard.Clear()
}
}
// ComputeIfAbsent attempts to compute a value using the given mapping
// function and enters it into the map, if the specified key is not
// already associated with a value.
func (sm *ShardedMap[K, V]) ComputeIfAbsent(key K, mappingFunction func(K) *V) *V {
return sm.shard(key).ComputeIfAbsent(key, mappingFunction)
}
// ContainsKey returns true if this map contains a mapping for the
// specified key.
func (sm *ShardedMap[K, V]) ContainsKey(key K) bool {
return sm.shard(key).ContainsKey(key)
}
// Get returns the value to which the specified key is mapped, or nil if
// this map contains no mapping for the key.
func (sm *ShardedMap[K, V]) Get(key K) *V {
return sm.shard(key).Get(key)
}
// GetOrDefault returns the value to which the specified key is mapped, or
// defaultValue if this map contains no mapping for the key.
func (sm *ShardedMap[K, V]) GetOrDefault(key K, defaultValue *V) *V {
return sm.shard(key).GetOrDefault(key, defaultValue)
}
// IsEmpty returns true if this map contains no key-value mappings.
func (sm *ShardedMap[K, V]) IsEmpty() bool {
for _, shard := range sm.shardMap {
if !shard.IsEmpty() {
return false
}
}
return true
}
// KeySet returns a slice of the keys contained in this map.
func (sm *ShardedMap[K, V]) KeySet() []K {
var keys []K
for _, shard := range sm.shardMap {
keys = append(keys, shard.KeySet()...)
}
return keys
}
// Put associates the specified value with the specified key in this map.
func (sm *ShardedMap[K, V]) Put(key K, value *V) {
sm.shard(key).Put(key, value)
}
// Remove removes the mapping for a key from this map if it is present,
// returning the previous value or nil if none.
func (sm *ShardedMap[K, V]) Remove(key K) *V {
return sm.shard(key).Remove(key)
}
// Size returns the number of key-value mappings in this map.
func (sm *ShardedMap[K, V]) Size() int {
var size int
for _, shard := range sm.shardMap {
size += shard.Size()
}
return size
}
// Values returns a slice of the values contained in this map.
func (sm *ShardedMap[K, V]) Values() []*V {
var values []*V
for _, shard := range sm.shardMap {
values = append(values, shard.Values()...)
}
return values
}
// shard returns an underlying synchronized map for the key.
func (sm *ShardedMap[K, V]) shard(key K) Map[K, V] {
return sm.shardMap[sm.hashFunc(key)%sm.shards]
}
// SynchronizedMap implements the async.Map interface in a thread-safe manner,
// delegating load/store operations to a Go map and using a sync.RWMutex
// for synchronization.
type SynchronizedMap[K comparable, V any] struct {
sync.RWMutex
store map[K]*V
}
var _ Map[int, any] = (*SynchronizedMap[int, any])(nil)
// NewSynchronizedMap returns a new SynchronizedMap.
func NewSynchronizedMap[K comparable, V any]() *SynchronizedMap[K, V] {
return &SynchronizedMap[K, V]{
store: make(map[K]*V),
}
}
// Clear removes all of the mappings from this map.
func (sync *SynchronizedMap[K, V]) Clear() {
sync.Lock()
defer sync.Unlock()
sync.store = make(map[K]*V)
}
// ComputeIfAbsent attempts to compute a value using the given mapping
// function and enters it into the map, if the specified key is not
// already associated with a value.
func (sync *SynchronizedMap[K, V]) ComputeIfAbsent(key K, mappingFunction func(K) *V) *V {
sync.Lock()
defer sync.Unlock()
value, ok := sync.store[key]
if !ok {
value = mappingFunction(key)
sync.store[key] = value
}
return value
}
// ContainsKey returns true if this map contains a mapping for the
// specified key.
func (sync *SynchronizedMap[K, V]) ContainsKey(key K) bool {
sync.RLock()
defer sync.RUnlock()
_, ok := sync.store[key]
return ok
}
// Get returns the value to which the specified key is mapped, or nil if
// this map contains no mapping for the key.
func (sync *SynchronizedMap[K, V]) Get(key K) *V {
sync.RLock()
defer sync.RUnlock()
return sync.store[key]
}
// GetOrDefault returns the value to which the specified key is mapped, or
// defaultValue if this map contains no mapping for the key.
func (sync *SynchronizedMap[K, V]) GetOrDefault(key K, defaultValue *V) *V {
sync.RLock()
defer sync.RUnlock()
value, ok := sync.store[key]
if ok {
return value
}
return defaultValue
}
// IsEmpty returns true if this map contains no key-value mappings.
func (sync *SynchronizedMap[K, V]) IsEmpty() bool {
sync.RLock()
defer sync.RUnlock()
return len(sync.store) == 0
}
// KeySet returns a slice of the keys contained in this map.
func (sync *SynchronizedMap[K, V]) KeySet() []K {
sync.RLock()
defer sync.RUnlock()
keys := make([]K, 0, len(sync.store))
for key := range sync.store {
keys = append(keys, key)
}
return keys
}
// Put associates the specified value with the specified key in this map.
func (sync *SynchronizedMap[K, V]) Put(key K, value *V) {
sync.Lock()
defer sync.Unlock()
sync.store[key] = value
}
// Remove removes the mapping for a key from this map if it is present,
// returning the previous value or nil if none.
func (sync *SynchronizedMap[K, V]) Remove(key K) *V {
sync.Lock()
defer sync.Unlock()
value, ok := sync.store[key]
if ok {
delete(sync.store, key)
}
return value
}
// Size returns the number of key-value mappings in this map.
func (sync *SynchronizedMap[K, V]) Size() int {
sync.RLock()
defer sync.RUnlock()
return len(sync.store)
}
// Values returns a slice of the values contained in this map.
func (sync *SynchronizedMap[K, V]) Values() []*V {
sync.RLock()
defer sync.RUnlock()
values := make([]*V, 0, len(sync.store))
for _, value := range sync.store {
values = append(values, value)
}
return values
}