-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
324 lines (268 loc) · 7.08 KB
/
map.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
package main
import (
"sync"
"github.com/cornelk/hashmap"
"github.com/dustinxie/lockfree"
"github.com/fanliao/go-concurrentMap"
"github.com/orcaman/concurrent-map"
"github.com/tidwall/shardmap"
)
// ConcurrentMap is the interface all of our
// concurrently accessible maps should conform to
type ConcurrentMap interface {
Get(string) (interface{}, bool)
Set(string, interface{})
}
type UnshardedSingleMutexMap struct {
sync.Mutex
m map[string]interface{}
}
func (m *UnshardedSingleMutexMap) Get(key string) (interface{}, bool) {
m.Lock()
val, exists := m.m[key]
m.Unlock()
return val, exists
}
func (m *UnshardedSingleMutexMap) Set(key string, value interface{}) {
m.Lock()
m.m[key] = value
m.Unlock()
}
func NewUnshardedSingleMutexMap() *UnshardedSingleMutexMap {
return &UnshardedSingleMutexMap{
m: make(map[string]interface{}),
}
}
type UnshardedSingleRWMutexMap struct {
sync.RWMutex
m map[string]interface{}
}
func (m *UnshardedSingleRWMutexMap) Get(key string) (interface{}, bool) {
m.RLock()
val, exists := m.m[key]
m.RUnlock()
return val, exists
}
func (m *UnshardedSingleRWMutexMap) Set(key string, value interface{}) {
m.Lock()
m.m[key] = value
m.Unlock()
}
func NewUnshardedSingleRWMutexMap() *UnshardedSingleRWMutexMap {
return &UnshardedSingleRWMutexMap{
m: make(map[string]interface{}),
}
}
type mutexShard struct {
sync.Mutex
m map[string]interface{}
}
type ShardedMultiMutexMap struct {
shards []*mutexShard
}
func (m *ShardedMultiMutexMap) Get(key string) (interface{}, bool) {
shardIdx := getShardIndex(key, len(m.shards))
shard := m.shards[shardIdx]
shard.Lock()
val, exists := shard.m[key]
shard.Unlock()
return val, exists
}
func (m *ShardedMultiMutexMap) Set(key string, value interface{}) {
shardIdx := getShardIndex(key, len(m.shards))
shard := m.shards[shardIdx]
shard.Lock()
shard.m[key] = value
shard.Unlock()
}
func NewShardedMultiMutexMap(shardCount int) *ShardedMultiMutexMap {
shards := make([]*mutexShard, shardCount, shardCount)
for i := 0; i < shardCount; i++ {
shards[i] = &mutexShard{
m: make(map[string]interface{}),
}
}
return &ShardedMultiMutexMap{
shards: shards,
}
}
type rwmutexShard struct {
sync.RWMutex
m map[string]interface{}
}
type ShardedMultiRWMutexMap struct {
shards []*rwmutexShard
}
func (m *ShardedMultiRWMutexMap) Get(key string) (interface{}, bool) {
shardIdx := getShardIndex(key, len(m.shards))
shard := m.shards[shardIdx]
shard.RLock()
val, exists := shard.m[key]
shard.RUnlock()
return val, exists
}
func (m *ShardedMultiRWMutexMap) Set(key string, value interface{}) {
shardIdx := getShardIndex(key, len(m.shards))
shard := m.shards[shardIdx]
shard.Lock()
shard.m[key] = value
shard.Unlock()
}
func NewShardedMultiRWMutexMap(shardCount int) *ShardedMultiRWMutexMap {
shards := make([]*rwmutexShard, shardCount, shardCount)
for i := 0; i < shardCount; i++ {
shards[i] = &rwmutexShard{
m: make(map[string]interface{}),
}
}
return &ShardedMultiRWMutexMap{
shards: shards,
}
}
// ShardedMultiSegregatedRWMutexMap is a sharded
// map that, unlike other types, keeps shard RW Mutexes
// segragated from the actual data shards
type ShardedMultiSegregatedRWMutexMap struct {
shards []map[string]interface{}
mu []sync.RWMutex
}
func (m *ShardedMultiSegregatedRWMutexMap) Get(key string) (interface{}, bool) {
shardIdx := getShardIndex(key, len(m.shards))
shard := m.shards[shardIdx]
m.mu[shardIdx].RLock()
val, exists := shard[key]
m.mu[shardIdx].RUnlock()
return val, exists
}
func (m *ShardedMultiSegregatedRWMutexMap) Set(key string, value interface{}) {
shardIdx := getShardIndex(key, len(m.shards))
shard := m.shards[shardIdx]
m.mu[shardIdx].Lock()
shard[key] = value
m.mu[shardIdx].Unlock()
}
func NewShardedMultiSegragatedRWMutexMap(shardCount int) *ShardedMultiSegregatedRWMutexMap {
shards := make([]map[string]interface{}, shardCount, shardCount)
for i := 0; i < shardCount; i++ {
shards[i] = make(map[string]interface{})
}
return &ShardedMultiSegregatedRWMutexMap{
shards: shards,
mu: make([]sync.RWMutex, shardCount),
}
}
type OrcamanLibrary struct {
internal cmap.ConcurrentMap
}
func (m *OrcamanLibrary) Get(key string) (interface{}, bool) {
return m.internal.Get(key)
}
func (m *OrcamanLibrary) Set(key string, value interface{}) {
m.internal.Set(key, value)
}
func NewOrcamanLibrary() *OrcamanLibrary {
return &OrcamanLibrary{
internal: cmap.New(),
}
}
type FanliaoLibrary struct {
internal *concurrent.ConcurrentMap
}
func (m *FanliaoLibrary) Get(key string) (interface{}, bool) {
val, err := m.internal.Get(key)
if err != nil || val == nil {
return "", false
}
return val, true
}
func (m *FanliaoLibrary) Set(key string, value interface{}) {
m.internal.Put(key, value)
}
func NewFanliaoLibrary() *FanliaoLibrary {
return &FanliaoLibrary{
internal: concurrent.NewConcurrentMap(),
}
}
type TidwallLibrary struct {
internal *shardmap.Map
}
func (m *TidwallLibrary) Get(key string) (interface{}, bool) {
return m.internal.Get(key)
}
func (m *TidwallLibrary) Set(key string, value interface{}) {
m.internal.Set(key, value)
}
func NewTidwallLibrary() *TidwallLibrary {
return &TidwallLibrary{
internal: &shardmap.Map{},
}
}
type CornelkLibrary struct {
internal *hashmap.Map[string, interface{}]
}
func (m *CornelkLibrary) Get(key string) (interface{}, bool) {
return m.internal.Get(key)
}
func (m *CornelkLibrary) Set(key string, value interface{}) {
m.internal.Set(key, value)
}
func NewCornelkLibrary() *CornelkLibrary {
return &CornelkLibrary{
internal: hashmap.NewSized[string, interface{}](2_000_000),
}
}
type DustinxieLibrary struct {
internal lockfree.HashMap
}
func (m *DustinxieLibrary) Get(key string) (interface{}, bool) {
return m.internal.Get(key)
}
func (m *DustinxieLibrary) Set(key string, value interface{}) {
m.internal.Set(key, value)
}
func NewDustinxieLibrary() *DustinxieLibrary {
return &DustinxieLibrary{
internal: lockfree.NewHashMap(),
}
}
type SyncMap struct {
sync.Map
}
func (m *SyncMap) Get(key string) (interface{}, bool) {
return m.Load(key)
}
func (m *SyncMap) Set(key string, value interface{}) {
m.Store(key, value)
}
func NewSyncMap() *SyncMap {
return &SyncMap{}
}
// getShardIndex returns the index of the shard that
// the key belongs in
func getShardIndex(key string, shards int) uint {
return uint(djbHash(key)) % uint(shards)
}
// fnv32 deterministically generates a 32 bit number
// for a given string.
// https://github.com/orcaman/concurrent-map
func fnv32(key string) uint32 {
hash := uint32(2166136261)
const prime32 = uint32(16777619)
keyLength := len(key)
for i := 0; i < keyLength; i++ {
hash *= prime32
hash ^= uint32(key[i])
}
return hash
}
// DjbHash is for sharding the map.
// according to internets, this is fastest hashing algorithm ever made.
// we dont need security, we need distribution which this provides for us.
// https://github.com/zutto/shardedmap/blob/master/ShardedMap.go
func djbHash(key string) uint32 {
var hash uint32 = 5381 //magic constant, apparently this hash fewest collisions possible.
for _, chr := range key {
hash = ((hash << 5) + hash) + uint32(chr)
}
return hash
}