-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen_growing_map.go
801 lines (720 loc) · 18.6 KB
/
open_growing_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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//go:generate benchmarkCodeGen
package atomicmap
import (
"fmt"
"log"
"math"
"sync/atomic"
"time"
"unsafe"
"github.com/xaionaro-go/atomicmap/hasher"
"github.com/xaionaro-go/spinlock"
)
const (
growAtFullness = 0.85
maximalSize = 1 << 32
lockSleepInterval = 300 * time.Nanosecond
defaultBlockSize = 65536
)
var (
threadSafe = true
forbidGrowing = false
)
type Map = *openAddressGrowingMap
func powerOfTwoGT(v uint64) uint64 {
shiftedV := v
for (v+1)^v < (v + 1) {
shiftedV >>= 1
v |= shiftedV
}
v++
return v
}
func isPowerOfTwo(v uint64) bool {
return (v-1)^v == v
}
func powerOfTwoGE(v uint64) uint64 {
if isPowerOfTwo(v) {
return v
}
return powerOfTwoGT(v)
}
func fixBlockSize(blockSize uint64) uint64 {
// This functions fixes blockSize value to be a power of 2
if blockSize <= 0 {
log.Printf("Invalid block size: %v. Setting to %d\n", blockSize, defaultBlockSize)
blockSize = defaultBlockSize
}
if (blockSize-1)^blockSize < blockSize {
blockSize = powerOfTwoGT(blockSize)
log.Printf("blockSize should be a power of 2 (1, 2, 4, 8, 16, ...). Setting to %v", blockSize)
}
return blockSize
}
func New() Map {
return NewWithArgs(0)
}
// blockSize should be a power of 2 and should be greater than the maximal amount of elements you're planning to store. Keep in mind: the higher blockSize you'll set the longer initialization will be (and more memory will be consumed).
func NewWithArgs(blockSize uint64) Map {
if blockSize <= 0 {
blockSize = defaultBlockSize
}
blockSize = fixBlockSize(blockSize)
result := &openAddressGrowingMap{initialSize: blockSize, threadSafety: threadSafe}
if err := result.growTo(blockSize); err != nil {
panic(err)
}
result.SetForbidGrowing(forbidGrowing)
return result
}
func newWithArgsIface(blockSize uint64) iMap {
return NewWithArgs(blockSize)
}
func (m *openAddressGrowingMap) SetThreadSafety(threadSafety bool) {
m.threadSafety = threadSafety
}
func (m *openAddressGrowingMap) IsForbiddenToGrow() bool {
return atomic.LoadInt32(&m.forbidGrowing) != 0
}
func (m *openAddressGrowingMap) SetForbidGrowing(forbidGrowing bool) {
if forbidGrowing {
atomic.StoreInt32(&m.forbidGrowing, 1)
} else {
if atomic.LoadInt32(&m.forbidGrowing) != 0 {
panic(`Not supported, yet: you cannot reenable growing`)
}
atomic.StoreInt32(&m.forbidGrowing, 0)
}
}
/*func (m *openAddressGrowingMap) increaseConcurrency() {
if !m.threadSafety {
return
}
if m.IsForbiddenToGrow() {
return
}
}
func (m *openAddressGrowingMap) decreaseConcurrency() {
if !m.threadSafety {
return
}
if m.IsForbiddenToGrow() {
return
}
}*/
func (m *openAddressGrowingMap) lock() {
if !m.threadSafety {
return
}
m.locker.Lock()
}
func (m *openAddressGrowingMap) unlock() {
if !m.threadSafety {
return
}
m.locker.Unlock()
}
type openAddressGrowingMap struct {
initialSize uint64
busySlots int64
*storage
writeConcurrency int32
threadSafety bool
forbidGrowing int32
isGrowing int32
locker spinlock.Locker
}
func (m *openAddressGrowingMap) waitUntilNoWrite() {
for atomic.LoadInt32(&m.writeConcurrency) != 0 {
time.Sleep(lockSleepInterval)
}
}
func (m *openAddressGrowingMap) isEnoughFreeSpace() bool {
return float64(m.BusySlots()+uint64(atomic.LoadInt32(&m.writeConcurrency)))/float64(len(m.items)) < growAtFullness
}
func (m *openAddressGrowingMap) concedeToGrowing() {
for atomic.LoadInt32(&m.isGrowing) != 0 {
time.Sleep(lockSleepInterval)
}
}
func (m *openAddressGrowingMap) SetBytesByBytes(key []byte, value []byte) error {
return m.set(func() (uint64, uint8, bool) {
return hasher.PreHashBytes(key)
}, func(slot *mapSlot) bool {
return hasher.IsEqualKey(slot.key, key)
}, func(slot *mapSlot) {
slot.key = key
}, func(slot *mapSlot) {
slot.bytesValue = value
})
}
func (m *openAddressGrowingMap) SetByUintptrUsingFunc(key uintptr, setValueFunc func(v *interface{})) error {
return m.set(func() (uint64, uint8, bool) {
return hasher.PreHashUintptr(key)
}, func(slot *mapSlot) bool {
return hasher.IsEqualKey(slot.key, key)
}, func(slot *mapSlot) {
slot.key = key
}, func(slot *mapSlot) {
setValueFunc(&slot.value)
})
}
func (m *openAddressGrowingMap) Set(key Key, value interface{}) error {
return m.set(func() (uint64, uint8, bool) {
return hasher.PreHash(key)
}, func(slot *mapSlot) bool {
return hasher.IsEqualKey(slot.key, key)
}, func(slot *mapSlot) {
slot.key = key
}, func(slot *mapSlot) {
slot.value = value
})
}
func (m *openAddressGrowingMap) Swap(key Key, value interface{}) (oldValue interface{}, err error) {
err = m.set(func() (uint64, uint8, bool) {
return hasher.PreHash(key)
}, func(slot *mapSlot) bool {
return hasher.IsEqualKey(slot.key, key)
}, func(slot *mapSlot) {
slot.key = key
}, func(slot *mapSlot) {
oldValue = slot.value
slot.value = value
})
return
}
func (m *openAddressGrowingMap) set(getPreHash func() (uint64, uint8, bool), compareKey func(*mapSlot) bool, setKey func(*mapSlot), setValue func(*mapSlot)) error {
/*if m.currentSize == len(m.storage) {
return NoSpaceLeft
}*/
if m.threadSafety {
m.concedeToGrowing()
if !m.isEnoughFreeSpace() {
if err := m.growTo(m.size() << 1); err != nil {
return err
}
}
atomic.AddInt32(&m.writeConcurrency, 1)
//m.increaseConcurrency()
}
preHashValue, typeID, preHashValueIsFull := getPreHash()
hashValue := hasher.CompleteHash(preHashValue, typeID)
idxValue := m.getIdx(hashValue)
if !preHashValueIsFull {
typeID = 0
}
var slot *mapSlot
slid := uint64(0)
for { // Going forward through the storage while a collision (to find a free slots)
slot = &m.items[idxValue].mapSlot
if slot.isSet.CompareAndSwap(isSet_notSet, isSet_setting) {
break
} else {
if slot.isSet.CompareAndSwap(isSet_removed, isSet_setting) {
break
}
}
if m.threadSafety {
if !slot.setIsUpdating() {
if slot.isSet.CompareAndSwap(isSet_removed, isSet_setting) {
break
} else {
continue // try again
}
}
}
if slot.hashValue == hashValue {
var isEqualKey bool
if typeID != 0 || slot.fastKeyType != 0 {
isEqualKey = slot.fastKey == preHashValue && slot.fastKeyType == typeID
} else {
isEqualKey = compareKey(slot)
}
if isEqualKey {
if m.threadSafety {
slot.waitForReadersOut()
}
setValue(slot)
if m.threadSafety {
slot.isSet.Store(isSet_set)
atomic.AddInt32(&m.writeConcurrency, -1)
//m.decreaseConcurrency()
}
return nil
}
}
slot.isSet.Store(isSet_set)
slid++
idxValue++
if idxValue >= m.size() {
idxValue = 0
}
if slid > m.size() {
panic(fmt.Errorf("%v %v %v %v", slid, m.size(), m.BusySlots(), m.isGrowing))
}
}
slot.hashValue = hashValue
if preHashValueIsFull {
slot.fastKey, slot.fastKeyType = preHashValue, typeID
}
setKey(slot)
setValue(slot)
slot.slid = slid
atomic.AddInt64(&m.busySlots, 1)
slot.isSet.Store(isSet_set)
if m.threadSafety {
atomic.AddInt32(&m.writeConcurrency, -1)
//m.decreaseConcurrency()
}
if !m.isEnoughFreeSpace() {
if err := m.growTo(m.size() << 1); err != nil {
return nil
}
}
return nil
}
func copySlot(newSlot, oldSlot *mapSlot) { // is sligtly faster than "*newSlot = *oldSlot"
newSlot.isSet = oldSlot.isSet
newSlot.hashValue = oldSlot.hashValue
newSlot.key = oldSlot.key
newSlot.fastKey, newSlot.fastKeyType = oldSlot.fastKey, oldSlot.fastKeyType
newSlot.value = oldSlot.value
}
func (m *openAddressGrowingMap) growTo(newSize uint64) error {
if m.IsForbiddenToGrow() {
return ForbiddenToGrow
}
if newSize > maximalSize {
return NoSpaceLeft
}
if m.size() >= newSize {
return nil
}
if m.threadSafety {
if !atomic.CompareAndSwapInt32(&m.isGrowing, 0, 1) {
return AlreadyGrowing
}
defer atomic.StoreInt32(&m.isGrowing, 0)
m.lock()
defer m.unlock()
m.waitUntilNoWrite()
}
if m.size() >= newSize {
return nil
}
newStorage := newStorage(newSize)
newStorage.copyOldItemsAfterGrowing(m.storage)
atomic.StorePointer((*unsafe.Pointer)((unsafe.Pointer)(&m.storage)), (unsafe.Pointer)(newStorage))
return nil
}
func (m *openAddressGrowingMap) GetByUintptr(key uintptr) (interface{}, error) {
if m.BusySlots() == 0 {
return nil, NotFound
}
//m.increaseConcurrency()
preHashValue, typeID, preHashValueIsFull := hasher.PreHashUintptr(key)
hashValue := hasher.CompleteHash(preHashValue, typeID)
var fastKey uint64
var fastKeyType uint8
if preHashValueIsFull {
fastKey, fastKeyType = preHashValue, typeID
}
return m.getByHashValue(fastKey, fastKeyType, hashValue, func(slot *mapSlot) bool {
slotKey, ok := slot.key.(uintptr)
if !ok {
return false
}
if slotKey != key {
return false
}
return true
})
}
func (m *openAddressGrowingMap) GetByUint64(key uint64) (interface{}, error) {
if m.BusySlots() == 0 {
return nil, NotFound
}
//m.increaseConcurrency()
preHashValue, typeID, preHashValueIsFull := hasher.PreHashUint64(key)
hashValue := hasher.CompleteHash(preHashValue, typeID)
var fastKey uint64
var fastKeyType uint8
if preHashValueIsFull {
fastKey, fastKeyType = preHashValue, typeID
}
return m.getByHashValue(fastKey, fastKeyType, hashValue, func(slot *mapSlot) bool {
slotKey, ok := slot.key.(uint64)
if !ok {
return false
}
if slotKey != key {
return false
}
return true
})
}
func (m *openAddressGrowingMap) GetByBytes(key []byte) (interface{}, error) {
if m.BusySlots() == 0 {
return nil, NotFound
}
//m.increaseConcurrency()
preHashValue, typeID, preHashValueIsFull := hasher.PreHashBytes(key)
hashValue := hasher.CompleteHash(preHashValue, typeID)
var fastKey uint64
var fastKeyType uint8
if preHashValueIsFull {
fastKey, fastKeyType = preHashValue, typeID
}
return m.getByHashValue(fastKey, fastKeyType, hashValue, func(slot *mapSlot) bool {
slotKey, ok := slot.key.([]byte)
if !ok {
return false
}
if len(slotKey) != len(key) {
return false
}
l := len(key)
for i := 0; i < l; i++ {
if slotKey[i] != key[i] {
return false
}
}
return true
})
}
func (m *openAddressGrowingMap) Get(key Key) (interface{}, error) {
if m.BusySlots() == 0 {
return nil, NotFound
}
//m.increaseConcurrency()
preHashValue, typeID, preHashValueIsFull := hasher.PreHash(key)
hashValue := hasher.CompleteHash(preHashValue, typeID)
var fastKey uint64
var fastKeyType uint8
if preHashValueIsFull {
fastKey, fastKeyType = preHashValue, typeID
}
return m.getByHashValue(fastKey, fastKeyType, hashValue, func(slot *mapSlot) bool {
return hasher.IsEqualKey(slot.key, key)
})
}
func (m *openAddressGrowingMap) getByHashValue(fastKey uint64, fastKeyType uint8, hashValue uint64, isRightSlotFn func(*mapSlot) bool) (interface{}, error) {
idxValue := m.getIdx(hashValue)
for {
slot := &m.items[idxValue].mapSlot
idxValue++
if idxValue >= m.size() {
idxValue = 0
}
var isSetStatus isSet
if m.threadSafety {
isSetStatus = slot.increaseReaders()
} else {
isSetStatus = slot.IsSet()
}
if isSetStatus == isSet_notSet {
break
} else if isSetStatus == isSet_removed {
continue
} else if isSetStatus != isSet_set {
panic("shouldn't happened")
}
if slot.hashValue != hashValue {
if m.threadSafety {
slot.decreaseReaders()
}
continue
}
var isRightSlot bool
if slot.fastKeyType != 0 || fastKeyType != 0 {
isRightSlot = slot.fastKey == fastKey && slot.fastKeyType == fastKeyType
} else {
isRightSlot = isRightSlotFn(slot)
}
if !isRightSlot {
if m.threadSafety {
slot.decreaseReaders()
}
continue
}
var value interface{}
if slot.bytesValue != nil {
value = slot.bytesValue
} else {
value = slot.value
}
if m.threadSafety {
slot.decreaseReaders()
}
//m.decreaseConcurrency()
return value, nil
}
//m.decreaseConcurrency()
return nil, NotFound
}
// loopy slid handler on free'ing a slot
func (m *openAddressGrowingMap) setEmptySlot(idxValue uint64, slot *mapSlot) {
m.lock()
// searching for a replacement to the slot (if somebody slid forward)
slid := uint64(0)
realRemoveIdxValue := idxValue
freeIdxValue := idxValue
freeSlot := slot
for {
slid++
realRemoveIdxValue++
if realRemoveIdxValue >= m.size() {
realRemoveIdxValue = 0
}
realRemoveSlot := &m.items[realRemoveIdxValue].mapSlot
if realRemoveSlot.isSet == isSet_notSet {
break
}
if realRemoveSlot.slid < slid {
continue
}
// searching for the last slot to move
previousRealRemoveIdxValue := realRemoveIdxValue
previousRealRemoveSlot := realRemoveSlot
for {
slid++
realRemoveIdxValue++
if realRemoveIdxValue >= m.size() {
realRemoveIdxValue = 0
}
realRemoveSlot := &m.items[realRemoveIdxValue].mapSlot
if realRemoveSlot.isSet == isSet_notSet {
break
}
if realRemoveSlot.slid < slid {
continue
}
previousRealRemoveIdxValue = realRemoveIdxValue
previousRealRemoveSlot = realRemoveSlot
}
realRemoveIdxValue = previousRealRemoveIdxValue
realRemoveSlot = previousRealRemoveSlot
*freeSlot = *realRemoveSlot
freeSlot.slid -= realRemoveIdxValue - freeIdxValue
freeSlot = realRemoveSlot
freeIdxValue = realRemoveIdxValue
slid = 0
}
freeSlot.value = nil
freeSlot.isSet = isSet_notSet
atomic.AddInt64(&m.busySlots, -1)
m.unlock()
}
type ConditionFunc func(value interface{}) bool
func (m *openAddressGrowingMap) unset(key Key, conditionFunc ConditionFunc) (*mapSlot, uint64) {
preHashValue, typeID, preHashValueIsFull := hasher.PreHash(key)
hashValue := hasher.CompleteHash(preHashValue, typeID)
idxValue := m.getIdx(hashValue)
if !preHashValueIsFull {
typeID = 0
}
for {
slot := &m.items[idxValue].mapSlot
curIdxValue := idxValue
idxValue++
if idxValue >= m.size() {
idxValue = 0
}
switch slot.IsSet() {
case isSet_notSet:
return nil, 0
case isSet_removed:
continue
}
if m.threadSafety {
if !slot.setIsUpdating() {
continue
}
}
if slot.hashValue != hashValue {
slot.isSet.Store(isSet_set)
continue
}
var isEqualKey bool
if slot.fastKeyType != 0 || typeID != 0 {
isEqualKey = slot.fastKey == preHashValue && slot.fastKeyType == typeID
} else {
isEqualKey = hasher.IsEqualKey(slot.key, key)
}
if !isEqualKey {
slot.isSet.Store(isSet_set)
continue
}
if conditionFunc != nil {
var value interface{}
if slot.bytesValue != nil {
value = slot.bytesValue
} else {
value = slot.value
}
if !conditionFunc(value) {
slot.isSet.Store(isSet_set)
return nil, curIdxValue
}
}
slot.isSet.Store(isSet_set)
return slot, curIdxValue
}
return nil, math.MaxUint64
}
func (m *openAddressGrowingMap) Unset(key Key) error {
return m.UnsetIf(key, nil)
}
func (m *openAddressGrowingMap) UnsetIf(key Key, conditionFunc ConditionFunc) error {
if m.BusySlots() == 0 {
return NotFound
}
//m.increaseConcurrency()
atomic.AddInt32(&m.writeConcurrency, 1)
//slot, idx := m.unset(key)
slot, idx := m.unset(key, conditionFunc)
atomic.AddInt32(&m.writeConcurrency, -1)
//m.decreaseConcurrency()
if slot == nil {
if idx == math.MaxUint64 {
return NotFound
} else {
return ConditionFailed
}
}
//if m.IsForbiddenToGrow() {
slot.value = nil
slot.bytesValue = nil
slot.isSet.Store(isSet_removed)
atomic.AddInt64(&m.busySlots, -1)
//} else {
// m.setEmptySlot(idx, slot)
//}
return nil
}
func (m *openAddressGrowingMap) Len() int {
if m == nil {
return 0
}
return int(atomic.LoadInt64(&m.busySlots))
}
func (m *openAddressGrowingMap) BusySlots() uint64 {
if m == nil {
return 0
}
return uint64(atomic.LoadInt64(&m.busySlots))
}
/*func (m *openAddressGrowingMap) Reset() {
m.lock()
*m = openAddressGrowingMap{initialSize: m.initialSize, concurrency: -1, threadSafety: threadSafe}
m.growTo(m.initialSize)
}*/
func (m *openAddressGrowingMap) Hash(key Key) uint64 {
return hasher.Hash(key)
}
func (m *openAddressGrowingMap) CheckConsistency() error {
m.lock()
count := 0
for i := uint64(0); i < m.size(); i++ {
slot := m.items[i].mapSlot
if slot.isSet != isSet_set {
continue
}
count++
}
if count != m.Len() {
return fmt.Errorf("count != m.Len(): %v %v", count, m.Len())
}
m.unlock()
for i := uint64(0); i < m.size(); i++ {
slot := m.items[i].mapSlot
if slot.IsSet() != isSet_set {
continue
}
foundValue, err := m.Get(slot.key)
if foundValue != slot.value || err != nil {
hashValue := hasher.Hash(slot.key)
expectedIdxValue := m.getIdx(hashValue)
return fmt.Errorf("m.Get(slot.key) != slot.value: %v(%v) %v; i:%v key:%v fastkey:%v,%v expectedIdx:%v", foundValue, err, slot.value, i, slot.key, slot.fastKey, slot.fastKeyType, expectedIdxValue)
}
}
return nil
}
func (m *openAddressGrowingMap) HasKey(key Key) bool {
hashValue := hasher.Hash(key)
idxValue := m.getIdx(hashValue)
return m.items[idxValue].mapSlot.IsSet() == isSet_set
}
// Keys() returns a slice that contains all keys.
// If you're using Keys() in a concurrent way then keep in mind:
// Keys() scans internal storage of the map while it could be changed
// (it doesn't lock the access to the map), so you can get a mix of
// different map states from different time moments as the result
func (m *openAddressGrowingMap) Keys() []interface{} {
r := make([]interface{}, 0, m.BusySlots())
for idxValue := uint64(0); idxValue < m.size(); idxValue++ {
slot := &m.items[idxValue].mapSlot
if m.threadSafety {
switch slot.increaseReaders() {
case isSet_notSet, isSet_removed:
continue
}
} else {
if slot.IsSet() != isSet_set {
continue
}
}
r = append(r, slot.key)
if m.threadSafety {
slot.decreaseReaders()
}
}
return r
}
// ToSTDMap converts to a standart map `map[Key]interface{}`.
// If you're using ToSTDMap() in a concurrent way then keep in mind:
// ToSTDMap() scans internal storage of the map while it could be changed
// (it doesn't lock the access to the map), so you can get a mix of
// different map states from different time moments as the result
func (m *openAddressGrowingMap) ToSTDMap() map[Key]interface{} {
r := map[Key]interface{}{}
if m.BusySlots() == 0 {
return r
}
//m.increaseConcurrency()
for idxValue := uint64(0); idxValue < m.size(); idxValue++ {
slot := &m.items[idxValue].mapSlot
if m.threadSafety {
switch slot.increaseReaders() {
case isSet_notSet, isSet_removed:
continue
}
} else {
if slot.IsSet() != isSet_set {
continue
}
}
switch key := slot.key.(type) {
case []byte:
r[string(key)] = slot.value
default:
r[slot.key] = slot.value
}
if m.threadSafety {
slot.decreaseReaders()
}
}
//m.decreaseConcurrency()
return r
}
func (m *openAddressGrowingMap) FromSTDMap(stdMap map[Key]interface{}) {
expectedSize := uint64(float64(len(stdMap))/growAtFullness) + 1
if expectedSize > m.initialSize {
if err := m.growTo(powerOfTwoGE(expectedSize)); err != nil {
panic(err)
}
}
for k, v := range stdMap {
m.Set(k, v)
}
}