forked from mmomtchev/SharedMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.umd.js
760 lines (713 loc) · 27.4 KB
/
index.umd.js
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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.default = factory());
}(this, (function () { 'use strict';
const UINT32_MAX = 0xFFFFFFFF;
const UINT32_UNDEFINED = 0xFFFFFFFF;
/**
* This is MurmurHash2
* @private
* @param {string}
* @return {number}
*/
function _hash(str) {
var
l = str.length,
h = 17 ^ l,
i = 0,
k;
while (l >= 4) {
k =
((str.charCodeAt(i) & 0xff)) |
((str.charCodeAt(++i) & 0xff) << 8) |
((str.charCodeAt(++i) & 0xff) << 16) |
((str.charCodeAt(++i) & 0xff) << 14);
k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
k ^= k >>> 14;
k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16)) ^ k;
l -= 4;
++i;
}
/* eslint-disable no-fallthrough */
switch (l) {
case 3: h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
case 2: h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
case 1: h ^= (str.charCodeAt(i) & 0xff);
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
}
/* eslint-enable no-fallthrough */
h ^= h >>> 13;
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
h ^= h >>> 15;
h = h >>> 0;
return (h != UINT32_UNDEFINED) ? h : 1;
}
function align32(v) {
return (v & 0xFFFFFFFFFFFFC) + ((v & 0x3) ? 0x4 : 0);
}
const META = {
maxSize: 0,
keySize: 1,
objSize: 2,
length: 3
};
const LOCK = {
SHAREDREAD: 0,
READLOCK: 1,
READERS: 2,
SHAREDWRITE: 3,
WRITELOCK: 4,
WRITERS: 5
};
class Deadlock extends Error {
constructor(...params) {
super(...params);
}
}
/**
* SharedMap
*
* zero-dependency
* high-performance
* unordered
* Vanilla JS implementation of SharedMap,
* a synchronous multi-threading capable,
* fine-grain-locked with deadlock recovery,
* static memory allocated,
* coalesced-chaining HashMap,
* backed by SharedArrayBuffer
* that supports deleting
* and is capable of auto-defragmenting itself on delete unless almost full
* compatible with both Node.js and SharedArrayBuffer-enabled browsers
* @author Momtchil Momtchev <[email protected]>
* @see http://github.com/mmomtchev/SharedMap
*/
class SharedMap {
/**
* Creates a new SharedMap
* @param {number} maxSize - Maximum number of entries
* @param {number} keySize - Maximum length of keys in UTF-16 codepoints
* @param {number} objSize - Maximum length of values in UTF-16 codepoints
* @return {SharedMap}
*/
constructor(maxSize, keySize, objSize) {
maxSize = align32(maxSize);
keySize = align32(keySize);
objSize = align32(objSize);
if (!(maxSize > 0 && keySize > 0 && objSize > 0))
throw new RangeError('maxSize, keySize and objSize must be positive numbers');
this.storage = new SharedArrayBuffer(
Object.keys(META).length * Uint32Array.BYTES_PER_ELEMENT
+ (keySize + objSize) * maxSize * Uint16Array.BYTES_PER_ELEMENT
+ maxSize * Uint32Array.BYTES_PER_ELEMENT
+ Math.ceil(maxSize / 32) * Int32Array.BYTES_PER_ELEMENT
+ Object.keys(LOCK).length * Int32Array.BYTES_PER_ELEMENT);
let offset = 0;
this.meta = new Uint32Array(this.storage, offset, Object.keys(META).length);
offset += this.meta.byteLength;
this.meta[META.maxSize] = maxSize;
this.meta[META.keySize] = keySize;
this.meta[META.objSize] = objSize;
this.meta[META.length] = 0;
this.keysData = new Uint16Array(this.storage, offset, this.meta[META.keySize] * this.meta[META.maxSize]);
offset += this.keysData.byteLength;
this.valuesData = new Uint16Array(this.storage, offset, this.meta[META.objSize] * this.meta[META.maxSize]);
offset += this.valuesData.byteLength;
this.chaining = new Uint32Array(this.storage, offset, this.meta[META.maxSize]);
offset += this.chaining.byteLength;
this.linelocks = new Int32Array(this.storage, offset, Math.ceil(maxSize / 32));
offset += this.linelocks.byteLength;
this.maplock = new Int32Array(this.storage, offset, Object.keys(LOCK).length);
this.stats = { set: 0, delete: 0, collisions: 0, rechains: 0, get: 0, deadlock: 0 };
}
/**
* Number of elements present
* @return {number}
*/
get length() {
/* We do not hold a lock here */
return Atomics.load(this.meta, META.length);
}
/**
* Maximum number of elements allowed
* @return {number}
*/
get size() {
return this.meta[META.maxSize];
}
/* eslint-disable no-constant-condition */
/**
* @private
*/
_lock(l) {
while (true) {
let state;
state = Atomics.exchange(this.maplock, l, 1);
if (state == 0)
return;
Atomics.wait(this.maplock, l, state);
}
}
/**
* @private
*/
_unlock(l) {
const state = Atomics.exchange(this.maplock, l, 0);
if (state == 0)
throw new Error('maplock desync ' + l);
Atomics.notify(this.maplock, l);
}
/**
* @private
*/
_lockLine(pos) {
const bitmask = 1 << (pos % 32);
const index = Math.floor(pos / 32);
while (true) {
const state = Atomics.or(this.linelocks, index, bitmask);
if ((state & bitmask) == 0)
return pos;
Atomics.wait(this.linelocks, index, state);
}
}
/* eslint-enable no-constant-condition */
/**
* @private
*/
_unlockLine(pos) {
const bitmask = 1 << (pos % 32);
const notbitmask = (~bitmask) & UINT32_MAX;
const index = Math.floor(pos / 32);
const state = Atomics.and(this.linelocks, index, notbitmask);
if ((state & bitmask) == 0)
throw new Error('linelock desync ' + pos);
Atomics.notify(this.linelocks, index);
}
/**
* @private
*/
_lockLineSliding(oldLock, newLock) {
if (newLock <= oldLock)
throw new Deadlock();
this._lockLine(newLock);
this._unlockLine(oldLock);
return newLock;
}
/**
* Acquire an exclusive lock,
* All operations that need it, automatically acquire it,
* Use only if you need to block all other threads from accessing the map;
* The thread holding the lock can then call map.set(k, v, {lockHeld: true})
* @return {void}
*/
lockExclusive() {
this._lock(LOCK.READLOCK);
}
/**
* Release the exclusive lock
* @return {void}
*/
unlockExclusive() {
this._unlock(LOCK.READLOCK);
}
/**
* @private
*/
_lockSharedRead() {
this._lock(LOCK.SHAREDREAD);
if (++this.maplock[LOCK.READERS] == 1)
this._lock(LOCK.READLOCK);
this._unlock(LOCK.SHAREDREAD);
}
/**
* @private
*/
_unlockSharedRead() {
this._lock(LOCK.SHAREDREAD);
if (--this.maplock[LOCK.READERS] == 0)
this._unlock(LOCK.READLOCK);
this._unlock(LOCK.SHAREDREAD);
}
/**
* @private
*/
_lockSharedWrite() {
this._lockSharedRead();
this._lock(LOCK.SHAREDWRITE);
if (++this.maplock[LOCK.WRITERS] == 1)
this._lock(LOCK.WRITELOCK);
this._unlock(LOCK.SHAREDWRITE);
}
/**
* @private
*/
_unlockSharedWrite() {
this._lock(LOCK.SHAREDWRITE);
if (--this.maplock[LOCK.WRITERS] == 0)
this._unlock(LOCK.WRITELOCK);
this._unlock(LOCK.SHAREDWRITE);
this._unlockSharedRead();
}
/**
* Acquire a write lock,
* All operations that need it, automatically acquire it,
* Use only if you need to block all other threads from writing to the map,
* The thread holding the lock can then call map.set(k, v, {lockHeld: true})
* @example
* myMap.lockWrite();
* for (let k of myMap.keys({lockWrite: true}))
* myMap.set(k,
* myMap.get(k, {lockWrite: true}).toUpperCase(),
* {lockWrite: true});
* myMap.unlockWrite();
* @return {void}
*/
lockWrite() {
this._lockSharedRead();
this._lock(LOCK.WRITELOCK);
}
/**
* Release the write lock
* @return {void}
*/
unlockWrite() {
this._unlock(LOCK.WRITELOCK);
this._unlockSharedRead();
}
/**
* @private
*/
_match(key, pos) {
let i;
for (i = 0; i < key.length; i++)
if (this.keysData[pos * this.meta[META.keySize] + i] !== key.charCodeAt(i))
break;
return i === key.length && this.keysData[pos * this.meta[META.keySize] + i] === 0;
}
/**
* @private
*/
_decodeValue(pos) {
const eos = this.valuesData.subarray(pos * this.meta[META.objSize], (pos + 1) * this.meta[META.objSize]).findIndex(x => x === 0);
const end = eos < 0 ? (pos + 1) * this.meta[META.objSize] : pos * this.meta[META.objSize] + eos;
return String.fromCharCode.apply(null, this.valuesData.subarray(pos * this.meta[META.objSize], end));
}
/**
* @private
*/
_decodeKey(pos) {
const eos = this.keysData.subarray(pos * this.meta[META.keySize], (pos + 1) * this.meta[META.keySize]).findIndex(x => x === 0);
const end = eos < 0 ? (pos + 1) * this.meta[META.keySize] : pos * this.meta[META.keySize] + eos;
return String.fromCharCode.apply(null, this.keysData.subarray(pos * this.meta[META.keySize], end));
}
/**
* These two are debugging aids
* @private
*/
/* c8 ignore next 8 */
_decodeBucket(pos, n) {
return `pos: ${pos}`
+ ` hash: ${this._hash(this._decodeKey(pos))}`
+ ` key: ${this._decodeKey(pos)}`
+ ` value: ${this._decodeValue(pos)}`
+ ` chain: ${this.chaining[pos]}`
+ ((n > 0 && this.chaining[pos] !== UINT32_UNDEFINED) ? '\n' + (this._decodeBucket(this.chaining[pos], n - 1)) : '');
}
/**
* @private
*/
/* c8 ignore next 5 */
__printMap() {
for (let i = 0; i < this.meta[META.maxSize]; i++)
console.log(this._decodeBucket(i, 0));
if (typeof process !== 'undefined') process.exit(1);
}
/**
* @private
*/
_write(pos, key, value) {
let i;
for (i = 0; i < key.length; i++)
this.keysData[pos * this.meta[META.keySize] + i] = key.charCodeAt(i);
this.keysData[pos * this.meta[META.keySize] + i] = 0;
for (i = 0; i < value.length; i++)
this.valuesData[pos * this.meta[META.objSize] + i] = value.charCodeAt(i);
this.valuesData[pos * this.meta[META.objSize] + i] = 0;
}
/**
* @private
*/
_set(key, value, exclusive) {
/* Hash */
let pos = this._hash(key);
/* Check for full table condition */
if (Atomics.load(this.meta, META.length) === this.meta[META.maxSize])
if (!this._find(key, exclusive))
throw new RangeError('SharedMap is full');
/* Find the first free bucket, remembering the last occupied one to chain it */
let toChain;
let slidingLock;
exclusive || (slidingLock = this._lockLine(pos, exclusive));
try {
while (this.keysData[pos * this.meta[META.keySize]] !== 0) {
this.stats.collisions++;
/* Replacing existing key */
if (this._match(key, pos)) {
let i;
for (i = 0; i < value.length; i++)
this.valuesData[pos * this.meta[META.objSize] + i] = value.charCodeAt(i);
this.valuesData[pos * this.meta[META.objSize] + i] = 0;
exclusive || this._unlockLine(slidingLock);
return;
}
if (this.chaining[pos] === UINT32_UNDEFINED || toChain !== undefined) {
/* This is the last collision element, we will chain ourselves to it */
if (toChain == undefined) {
toChain = pos;
pos = (pos + 1) % this.meta[META.maxSize];
exclusive || (slidingLock = this._lockLine(pos));
} else {
/* Now lets find the first free position (or a match of a preexising key) */
pos = (pos + 1) % this.meta[META.maxSize];
exclusive || (slidingLock = this._lockLineSliding(slidingLock, pos));
}
} else {
/* We are following the collision chain here */
pos = this.chaining[pos];
exclusive || (slidingLock = this._lockLineSliding(slidingLock, pos));
}
}
/* Copy the element into place, chaining when needed */
this._write(pos, key, value);
this.chaining[pos] = UINT32_UNDEFINED;
/* Use Atomics to increase the length, we do not hold an exclusive lock here */
Atomics.add(this.meta, META.length, 1);
if (toChain !== undefined) {
this.chaining[toChain] = pos;
exclusive || this._unlockLine(toChain);
toChain = undefined;
}
exclusive || this._unlockLine(slidingLock);
} catch (e) {
if (!exclusive) {
this._unlockLine(slidingLock);
if (toChain !== undefined)
this._unlockLine(toChain);
}
throw e;
}
}
/**
* @typedef SharedMapOptions
* @type {object}
* @property {boolean} lockWrite Already holding write lock, useful when manually locking with lockWrite
* @property {boolean} lockExclusive Already holding exclusive lock, useful when manually locking with lockExclusive
*/
/**
* Add/replace an element, fully thread-safe, multiple get/set can execute in parallel
* @param {string} key
* @param {string|number} value
* @param {SharedMapOptions} [opt] options, { lockWrite: true } if manually calling lockWrite
* @throws {RangeError} when the map is full
* @throws {RangeError} when the input values do not fit
* @throws {TypeError} when the input values are of a wrong type
* @return {void}
*/
set(key, value, opt) {
if (typeof key !== 'string' || key.length === 0)
throw new TypeError(`SharedMap keys must be non-emptry strings, invalid key ${key}`);
if (typeof value === 'number')
value = value.toString();
if (typeof value !== 'string')
throw new TypeError('SharedMap can contain only strings and numbers which will be converted to strings');
if (key.length > this.meta[META.keySize])
throw new RangeError(`SharedMap key ${key} does not fit in ${this.meta[META.keySize] * Uint16Array.BYTES_PER_ELEMENT} bytes, ${this.meta[META.keySize]} UTF-16 code points`);
if (value.length > this.meta[META.objSize])
throw new RangeError(`SharedMap value ${value} does not fit in ${this.meta[META.objSize] * Uint16Array.BYTES_PER_ELEMENT} bytes, ${this.meta[META.objSize]} UTF-16 code points`);
const lockHeld = opt && (opt.lockWrite || opt.lockExclusive);
this.stats.set++;
lockHeld || this._lockSharedWrite();
try {
this._set(key, value, lockHeld);
lockHeld || this._unlockSharedWrite();
} catch (e) {
lockHeld || this._unlockSharedWrite();
if (e instanceof Deadlock && !lockHeld) {
this.lockExclusive();
this.stats.deadlock++;
try {
this._set(key, value, true);
this.unlockExclusive();
} catch (e) {
this.unlockExclusive();
throw e;
}
} else
throw e;
}
}
/**
* @private
*/
_find(key, exclusive) {
let slidingLock;
try {
/* Hash */
let pos = this._hash(key);
let previous = UINT32_UNDEFINED;
this.stats.get++;
exclusive || (slidingLock = this._lockLine(pos));
/* Loop through the bucket chaining */
while (pos !== UINT32_UNDEFINED && this.keysData[pos * this.meta[META.keySize]] !== 0) {
if (this._match(key, pos)) {
return { pos, previous };
}
previous = pos;
pos = this.chaining[pos];
if (pos !== UINT32_UNDEFINED && !exclusive)
slidingLock = this._lockLineSliding(slidingLock, pos);
}
exclusive || this._unlockLine(slidingLock);
return undefined;
} catch (e) {
exclusive || this._unlockLine(slidingLock);
throw e;
}
}
/**
* Get an element, fully thread-safe, multiple get/set can execute in parallel
* @param {string} key
* @param {SharedMapOptions} [opt] options, { lockWrite: true } if manually calling lockWrite
* @return {string|undefined}
*/
get(key, opt) {
let pos, val;
const lockHeld = opt && (opt.lockWrite || opt.lockExclusive);
lockHeld || this._lockSharedRead();
try {
pos = this._find(key, lockHeld);
if (pos !== undefined) {
val = this._decodeValue(pos.pos);
lockHeld || this._unlockLine(pos.pos);
}
lockHeld || this._unlockSharedRead();
} catch (e) {
lockHeld || this._unlockSharedRead();
if (e instanceof Deadlock && !lockHeld) {
this.lockExclusive();
this.stats.deadlock++;
try {
pos = this._find(key, true);
if (pos !== undefined) {
val = this._decodeValue(pos.pos);
}
this.unlockExclusive();
} catch (e) {
this.unlockExclusive();
throw e;
}
} else
throw e;
}
return val;
}
/**
* Find an element, fully thread-safe, identical to get(key) !== undefined
* @param {string} key
* @param {SharedMapOptions} [opt] options, { lockWrite: true } if manually calling lockWrite
* @return {boolean}
*/
has(key, opt) {
return this.get(key, opt) !== undefined;
}
/**
* @private
*/
_hash(s) {
if (typeof s.hash === 'function')
return s.hash(s) % this.meta[META.maxSize];
if (typeof s.hash === 'number')
return s.hash % this.meta[META.maxSize];
else
return _hash(s) % this.meta[META.maxSize];
}
/**
* Delete an element, fully thread-safe, acquires an exlusive lock and it is very expensive
* @param {string} key
* @param {SharedMapOptions} [opt] options, { lockExclusive: true } if manually calling lockExlusive
* @throws {RangeError} when the key does not exit
* @throws {Error} when calling map.delete(key, value, { lockWrite: true, lockExclusive: false })
* @return {void}
*/
delete(key, opt) {
/* delete is slow */
const lockHeld = opt && opt.lockExclusive;
if (opt && opt.lockWrite && !lockHeld) {
throw new Error('delete requires an exclusive lock');
}
let find;
try {
lockHeld || this.lockExclusive();
find = this._find(key, true);
} catch (e) {
lockHeld || this.unlockExclusive();
throw e;
}
if (find === undefined) {
lockHeld || this.unlockExclusive();
throw new RangeError(`SharedMap does not contain key ${key}`);
}
this.stats.delete++;
const { pos, previous } = find;
const next = this.chaining[pos];
this.keysData[pos * this.meta[META.keySize]] = 0;
if (previous !== UINT32_UNDEFINED)
this.chaining[previous] = UINT32_UNDEFINED;
Atomics.sub(this.meta, META.length, 1);
if (next === UINT32_UNDEFINED) {
/* There was no further chaining, just delete this element */
/* and unchain it from the previous */
lockHeld || this.unlockExclusive();
return;
}
/* Full rechaining */
/* Some slight optimization avoiding copying some elements around
* is possible, but the O(n) complexity is not
*/
this.stats.rechains++;
let el = next;
let chain = [];
while (el !== UINT32_UNDEFINED) {
chain.push({ key: this._decodeKey(el), value: this._decodeValue(el) });
this.keysData[el * this.meta[META.keySize]] = 0;
Atomics.sub(this.meta, META.length, 1);
el = this.chaining[el];
}
for (el of chain) {
this._set(el.key, el.value, true);
}
lockHeld || this.unlockExclusive();
}
/**
* @private
*/
*_keys(exclusive) {
for (let pos = 0; pos < this.meta[META.maxSize]; pos++) {
exclusive || this._lockSharedRead();
exclusive || this._lockLine(pos);
if (this.keysData[pos * this.meta[META.keySize]] !== 0) {
yield pos;
} else {
exclusive || this._unlockLine(pos);
exclusive || this._unlockSharedRead();
}
}
}
/**
* A generator that can be used to iterate over the keys, thread-safe but allows
* additions and deletions during the iteration
* @param {SharedMapOptions} [opt] options, { lockWrite: true } if manually calling lockWrite
* @return {Iterable}
*/
*keys(opt) {
const lockHeld = opt && (opt.lockWrite || opt.lockExclusive);
for (let pos of this._keys(lockHeld)) {
const k = this._decodeKey(pos);
lockHeld || this._unlockLine(pos);
lockHeld || this._unlockSharedRead();
yield k;
}
}
/**
* @callback mapCallback callback(currentValue[, key] )}
* map.get(key)=currentValue is guaranteed while the callback runs,
* You shall not manipulate the map in the callback, use an explicitly-locked
* keys() in this case (look at the example for lockWrite)
*
* @param {string} currentValue
* @param {string} [key]
*/
/**
* A thread-safe map(). Doesn't block additions or deletions
* between two calls of the callback,
* all map operations are guaranteed atomic,
* map.get(index)=currentValue is guaranteed while the callback runs,
* You shall not manipulate the map in the callback, use an explicitly-locked
* keys() in this case (look at the example for lockWrite)
*
* @param {mapCallback} cb callback
* @param {*} [thisArg] callback will have its this set to thisArg
* @return {Array}
*/
map(cb, thisArg) {
const a = [];
for (let pos of this._keys()) {
const k = this._decodeKey(pos);
const v = this._decodeValue(pos);
try {
a.push(cb.call(thisArg, v, k));
this._unlockLine(pos);
this._unlockSharedRead();
} catch (e) {
this._unlockLine(pos);
this._unlockSharedRead();
throw e;
}
}
return a;
}
/**
* @callback reduceCallback callback(accumulator, currentValue[, key] )}
* all map operations are guaranteed atomic,
* map.get(key)=currentValue is guaranteed while the callback runs,
* You shall not manipulate the map in the callback, use an explicitly-locked
* keys() in this case (look at the example for lockWrite)
*
* @param accumulator
* @param {string} currentValue
* @param {string} [key]
*/
/**
* A thread-safe reduce(). Doesn't block additions or deletions
* between two calls of the callback,
* map.get(key)=currentValue is guaranteed while the callback runs,
* You shall not manipulate the map in the callback, use an explicitly-locked
* keys() in this case (look at the example for lockWrite)
*
* @param {reduceCallback} cb callback
* @param {*} initialValue initial value of the accumulator
* @return {*}
*/
reduce(cb, initialValue) {
let a = initialValue;
for (let pos of this._keys(false)) {
const k = this._decodeKey(pos);
const v = this._decodeValue(pos);
try {
a = cb(a, v, k);
this._unlockLine(pos);
this._unlockSharedRead();
} catch (e) {
this._unlockLine(pos);
this._unlockSharedRead();
throw e;
}
}
return a;
}
/**
* Clear the SharedMap
* @return {void}
*/
clear() {
this.lockExclusive();
this.keysData.fill(0);
this.valuesData.fill(0);
Atomics.store(this.meta, META.length, 0);
this.unlockExclusive();
}
}
return SharedMap;
})));