serializable-bptree 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,741 @@
1
+ // src/utils/BinarySearch.ts
2
+ var BinarySearch = class {
3
+ comparator;
4
+ constructor(comparator) {
5
+ this.comparator = comparator;
6
+ }
7
+ _withRange(array, value, left = 0, right = array.length - 1) {
8
+ while (left <= right) {
9
+ const mid = Math.floor((left + right) / 2);
10
+ const guess = array[mid];
11
+ if (this.comparator.isSame(guess, value)) {
12
+ return mid;
13
+ } else if (this.comparator.isLower(guess, value)) {
14
+ left = mid + 1;
15
+ continue;
16
+ } else {
17
+ right = mid - 1;
18
+ continue;
19
+ }
20
+ }
21
+ return -1;
22
+ }
23
+ leftest(array, value) {
24
+ let i = this._withRange(array, value);
25
+ if (i === -1) {
26
+ return -1;
27
+ }
28
+ while (i > 0) {
29
+ if (!this.comparator.isSame(array[i - 1], value)) {
30
+ break;
31
+ }
32
+ i--;
33
+ }
34
+ return i;
35
+ }
36
+ rightest(array, value) {
37
+ let i = this._withRange(array, value);
38
+ if (i === -1) {
39
+ return -1;
40
+ }
41
+ const max = array.length - 1;
42
+ while (i < max) {
43
+ if (!this.comparator.isSame(array[i + 1], value)) {
44
+ break;
45
+ }
46
+ i++;
47
+ }
48
+ return i;
49
+ }
50
+ range(array, value) {
51
+ const left = this.leftest(array, value);
52
+ const right = this.rightest(array, value) + 1;
53
+ return [left, right];
54
+ }
55
+ };
56
+
57
+ // src/BPTree.ts
58
+ var BPTree = class {
59
+ strategy;
60
+ comparator;
61
+ search;
62
+ order;
63
+ nodes;
64
+ root;
65
+ _creates;
66
+ _updates;
67
+ _updatedHead;
68
+ _createNodeId() {
69
+ const id = this.strategy.id();
70
+ if (id === 0) {
71
+ throw new Error(`The node's id should never be 0.`);
72
+ }
73
+ return id;
74
+ }
75
+ _createNode(keys, values, leaf = false, parent = 0, next = 0) {
76
+ const id = this._createNodeId();
77
+ const node = {
78
+ id,
79
+ keys,
80
+ values,
81
+ leaf,
82
+ parent,
83
+ next
84
+ };
85
+ this.nodes.set(id, node);
86
+ return node;
87
+ }
88
+ /**
89
+ * @param strategy An instance of a strategy that manages the read/write state of a node.
90
+ * @param comparator An instance of a comparator that compares the size of values.
91
+ */
92
+ constructor(strategy, comparator) {
93
+ const head = strategy.readHead();
94
+ this._creates = /* @__PURE__ */ new Map();
95
+ this._updates = /* @__PURE__ */ new Map();
96
+ this._updatedHead = null;
97
+ this.nodes = /* @__PURE__ */ new Map();
98
+ this.search = new BinarySearch(comparator);
99
+ this.strategy = strategy;
100
+ this.comparator = comparator;
101
+ if (head === null) {
102
+ this.order = strategy.order;
103
+ this.root = this._createNode([], [], true);
104
+ this._setHeadUpdate(this._headState);
105
+ this._setCreates(this.root);
106
+ this._emitHeadUpdates();
107
+ this._emitCreates();
108
+ } else {
109
+ const { root, order } = head;
110
+ this.order = order;
111
+ this.root = this.getNode(root);
112
+ }
113
+ if (this.order < 3) {
114
+ throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
115
+ }
116
+ }
117
+ get _headState() {
118
+ const root = this.root.id;
119
+ const order = this.order;
120
+ return {
121
+ root,
122
+ order
123
+ };
124
+ }
125
+ _setHeadUpdate(head) {
126
+ this._updatedHead = head;
127
+ }
128
+ _setCreates(node) {
129
+ this._creates.set(node.id, node);
130
+ }
131
+ _setUpdates(node) {
132
+ this._updates.set(node.id, node);
133
+ }
134
+ _emitHeadUpdates() {
135
+ if (this._updatedHead !== null) {
136
+ this.strategy.writeHead(this._updatedHead);
137
+ }
138
+ this._updatedHead = null;
139
+ }
140
+ _emitCreates() {
141
+ for (const node of this._creates.values()) {
142
+ this.strategy.write(node.id, node);
143
+ }
144
+ this._creates.clear();
145
+ }
146
+ _emitUpdates() {
147
+ for (const node of this._updates.values()) {
148
+ this.strategy.write(node.id, node);
149
+ }
150
+ this._updates.clear();
151
+ }
152
+ getNode(id) {
153
+ if (!this.nodes.has(id)) {
154
+ this.nodes.set(id, this.strategy.read(id));
155
+ }
156
+ return this.nodes.get(id);
157
+ }
158
+ leftestNode() {
159
+ let node = this.root;
160
+ while (!node.leaf) {
161
+ node = node.keys[0];
162
+ }
163
+ return node;
164
+ }
165
+ _insertableNode(value) {
166
+ let node = this.root;
167
+ while (!node.leaf) {
168
+ for (let i = 0, len = node.values.length; i < len; i++) {
169
+ const nValue = node.values[i];
170
+ if (this.comparator.isSame(value, nValue)) {
171
+ node = node.keys[i + 1];
172
+ break;
173
+ } else if (this.comparator.isLower(value, nValue)) {
174
+ node = node.keys[i];
175
+ break;
176
+ } else if (i + 1 === node.values.length) {
177
+ node = node.keys[i + 1];
178
+ break;
179
+ }
180
+ }
181
+ }
182
+ return node;
183
+ }
184
+ /**
185
+ * It returns whether there is a value in the tree.
186
+ * @param key The key value to search for.
187
+ * @param value The value to search for.
188
+ */
189
+ exists(key, value) {
190
+ const node = this._insertableNode(value);
191
+ for (let i = 0, len = node.values.length; i < len; i++) {
192
+ const nValue = node.values[i];
193
+ if (this.comparator.isSame(value, nValue)) {
194
+ const keys = node.keys[i];
195
+ return keys.includes(key);
196
+ }
197
+ }
198
+ return false;
199
+ }
200
+ _insertAtLeaf(node, key, value) {
201
+ if (node.values.length) {
202
+ for (let i = 0, len = node.values.length; i < len; i++) {
203
+ const nValue = node.values[i];
204
+ if (this.comparator.isSame(value, nValue)) {
205
+ const keys = node.keys[i];
206
+ keys.push(key);
207
+ this._setUpdates(node);
208
+ break;
209
+ } else if (this.comparator.isLower(value, nValue)) {
210
+ node.values.splice(i, 0, value);
211
+ node.keys.splice(i, 0, [key]);
212
+ this._setUpdates(node);
213
+ break;
214
+ } else if (i + 1 === node.values.length) {
215
+ node.values.push(value);
216
+ node.keys.push([key]);
217
+ this._setUpdates(node);
218
+ break;
219
+ }
220
+ }
221
+ } else {
222
+ node.values = [value];
223
+ node.keys = [[key]];
224
+ this._setUpdates(node);
225
+ }
226
+ }
227
+ _insertInParent(node, value, pointer) {
228
+ if (this.root === node) {
229
+ const root = this._createNode([node, pointer], [value]);
230
+ this.root = root;
231
+ node.parent = root.id;
232
+ pointer.parent = root.id;
233
+ this._setHeadUpdate(this._headState);
234
+ this._setCreates(root);
235
+ this._setUpdates(node);
236
+ this._setUpdates(pointer);
237
+ return;
238
+ }
239
+ const parentNode = this.getNode(node.parent);
240
+ for (let i = 0, len = parentNode.keys.length; i < len; i++) {
241
+ const nKeys = parentNode.keys[i];
242
+ if (nKeys === node) {
243
+ parentNode.values.splice(i, 0, value);
244
+ parentNode.keys.splice(i + 1, 0, pointer);
245
+ this._setUpdates(parentNode);
246
+ if (parentNode.keys.length > this.order) {
247
+ const parentPointer = this._createNode([], []);
248
+ parentPointer.parent = parentNode.parent;
249
+ const mid = Math.ceil(this.order / 2) - 1;
250
+ parentPointer.values = parentNode.values.slice(mid + 1);
251
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
252
+ const midValue = parentNode.values[mid];
253
+ if (mid === 0) {
254
+ parentNode.values = parentNode.values.slice(0, mid + 1);
255
+ } else {
256
+ parentNode.values = parentNode.values.slice(0, mid);
257
+ }
258
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
259
+ for (const k of parentNode.keys) {
260
+ const key = k;
261
+ key.parent = parentNode.id;
262
+ }
263
+ for (const k of parentPointer.keys) {
264
+ const key = k;
265
+ key.parent = parentPointer.id;
266
+ }
267
+ this._insertInParent(parentNode, midValue, parentPointer);
268
+ this._setCreates(parentPointer);
269
+ this._setUpdates(parentNode);
270
+ }
271
+ }
272
+ }
273
+ }
274
+ _equalCondition(condition) {
275
+ return Object.prototype.hasOwnProperty.call(condition, "equal");
276
+ }
277
+ _notEqualCondition(condition) {
278
+ return Object.prototype.hasOwnProperty.call(condition, "notEqual");
279
+ }
280
+ _onlyGtCondition(condition) {
281
+ return Object.prototype.hasOwnProperty.call(condition, "gt") && !Object.prototype.hasOwnProperty.call(condition, "lt");
282
+ }
283
+ _onlyLtCondition(condition) {
284
+ return Object.prototype.hasOwnProperty.call(condition, "lt") && !Object.prototype.hasOwnProperty.call(condition, "gt");
285
+ }
286
+ _rangeCondition(condition) {
287
+ return Object.prototype.hasOwnProperty.call(condition, "gt") && Object.prototype.hasOwnProperty.call(condition, "lt");
288
+ }
289
+ _getPairsFromValue(value) {
290
+ const node = this._insertableNode(value);
291
+ const [start, end] = this.search.range(node.values, value);
292
+ if (start === -1) {
293
+ return [];
294
+ }
295
+ const pairs = [];
296
+ for (let i = start; i < end; i++) {
297
+ const keys = node.keys[i];
298
+ for (const key of keys) {
299
+ pairs.push({ key, value });
300
+ }
301
+ }
302
+ return pairs;
303
+ }
304
+ _getPairsFromNEValue(value) {
305
+ const pairs = [];
306
+ let node = this.leftestNode();
307
+ let done = false;
308
+ while (!done) {
309
+ for (let i = 0, len = node.values.length; i < len; i++) {
310
+ const nValue = node.values[i];
311
+ const keys = node.keys[i];
312
+ if (this.comparator.isSame(nValue, value) === false) {
313
+ for (const key of keys) {
314
+ pairs.push({ key, value: nValue });
315
+ }
316
+ }
317
+ }
318
+ if (!node.next) {
319
+ done = true;
320
+ break;
321
+ }
322
+ node = this.getNode(node.next);
323
+ }
324
+ return pairs;
325
+ }
326
+ _getPairsFromRange(gt, lt) {
327
+ const pairs = [];
328
+ let node = this._insertableNode(gt);
329
+ let done = false;
330
+ let found = false;
331
+ while (!done) {
332
+ for (let i = 0, len = node.values.length; i < len; i++) {
333
+ const nValue = node.values[i];
334
+ const keys = node.keys[i];
335
+ if (this.comparator.isHigher(nValue, gt) && this.comparator.isLower(nValue, lt)) {
336
+ found = true;
337
+ for (const key of keys) {
338
+ pairs.push({ key, value: nValue });
339
+ }
340
+ } else if (found) {
341
+ done = true;
342
+ break;
343
+ }
344
+ }
345
+ if (!node.next) {
346
+ done = true;
347
+ break;
348
+ }
349
+ node = this.getNode(node.next);
350
+ }
351
+ return pairs;
352
+ }
353
+ _getPairsFromGt(gt) {
354
+ const pairs = [];
355
+ let node = this._insertableNode(gt);
356
+ let done = false;
357
+ let found = false;
358
+ while (!done) {
359
+ for (let i = 0, len = node.values.length; i < len; i++) {
360
+ const nValue = node.values[i];
361
+ const keys = node.keys[i];
362
+ if (this.comparator.isHigher(nValue, gt)) {
363
+ found = true;
364
+ for (const key of keys) {
365
+ pairs.push({ key, value: nValue });
366
+ }
367
+ } else if (found) {
368
+ done = true;
369
+ break;
370
+ }
371
+ }
372
+ if (!node.next) {
373
+ done = true;
374
+ break;
375
+ }
376
+ node = this.getNode(node.next);
377
+ }
378
+ return pairs;
379
+ }
380
+ _getPairsFromLt(lt) {
381
+ const pairs = [];
382
+ let node = this.leftestNode();
383
+ let done = false;
384
+ let found = false;
385
+ while (!done) {
386
+ for (let i = 0, len = node.values.length; i < len; i++) {
387
+ const nValue = node.values[i];
388
+ const keys = node.keys[i];
389
+ if (this.comparator.isLower(nValue, lt)) {
390
+ found = true;
391
+ for (const key of keys) {
392
+ pairs.push({ key, value: nValue });
393
+ }
394
+ } else if (found) {
395
+ done = true;
396
+ break;
397
+ }
398
+ }
399
+ if (!node.next) {
400
+ done = true;
401
+ break;
402
+ }
403
+ node = this.getNode(node.next);
404
+ }
405
+ return pairs;
406
+ }
407
+ /**
408
+ * It searches for a value within the tree. The result is returned as an array sorted in ascending order based on the value.
409
+ * The result includes the key and value attributes, and you can use the `gt`, `lt`, `equal`, `notEqual` condition statements.
410
+ * @param condition You can use the `gt`, `lt`, `equal`, `notEqual` condition statements.
411
+ */
412
+ where(condition) {
413
+ if (this._equalCondition(condition)) {
414
+ return this._getPairsFromValue(condition.equal);
415
+ } else if (this._notEqualCondition(condition)) {
416
+ return this._getPairsFromNEValue(condition.notEqual);
417
+ } else if (this._rangeCondition(condition)) {
418
+ const { gt, lt } = condition;
419
+ return this._getPairsFromRange(gt, lt);
420
+ } else if (this._onlyGtCondition(condition)) {
421
+ return this._getPairsFromGt(condition.gt);
422
+ } else if (this._onlyLtCondition(condition)) {
423
+ return this._getPairsFromLt(condition.lt);
424
+ } else {
425
+ throw new Error(`The 'condition' parameter is invalid.`);
426
+ }
427
+ }
428
+ /**
429
+ * You enter the key and value as a pair. You can later search for the pair by value.
430
+ * This data is stored in the tree, sorted in ascending order of value.
431
+ * @param key The key of the pair.
432
+ * @param value The value of the pair.
433
+ */
434
+ insert(key, value) {
435
+ const before = this._insertableNode(value);
436
+ this._insertAtLeaf(before, key, value);
437
+ if (before.values.length === this.order) {
438
+ const after = this._createNode(
439
+ [],
440
+ [],
441
+ true,
442
+ before.parent,
443
+ before.next
444
+ );
445
+ const mid = Math.ceil(this.order / 2) - 1;
446
+ after.values = before.values.slice(mid + 1);
447
+ after.keys = before.keys.slice(mid + 1);
448
+ before.values = before.values.slice(0, mid + 1);
449
+ before.keys = before.keys.slice(0, mid + 1);
450
+ before.next = after.id;
451
+ this._insertInParent(before, after.values[0], after);
452
+ this._setCreates(after);
453
+ this._setUpdates(before);
454
+ }
455
+ this._emitHeadUpdates();
456
+ this._emitCreates();
457
+ this._emitUpdates();
458
+ }
459
+ /**
460
+ * Deletes the pair that matches the key and value.
461
+ * @param key The key of the pair.
462
+ * @param value The value of the pair.
463
+ */
464
+ delete(key, value) {
465
+ const node = this._insertableNode(value);
466
+ for (let i = 0, len = node.values.length; i < len; i++) {
467
+ const nValue = node.values[i];
468
+ if (this.comparator.isSame(value, nValue)) {
469
+ const keys = node.keys[i];
470
+ if (keys.includes(key)) {
471
+ if (keys.length > 1) {
472
+ keys.splice(keys.indexOf(key), 1);
473
+ this._setUpdates(node);
474
+ } else if (node === this.root) {
475
+ node.values.splice(i, 1);
476
+ node.keys.splice(i, 1);
477
+ this._setUpdates(node);
478
+ } else {
479
+ keys.splice(keys.indexOf(key), 1);
480
+ node.keys.splice(i, 1);
481
+ node.values.splice(node.values.indexOf(value), 1);
482
+ this._deleteEntry(node, key, value);
483
+ this._setUpdates(node);
484
+ }
485
+ }
486
+ }
487
+ }
488
+ this._emitHeadUpdates();
489
+ this._emitCreates();
490
+ this._emitUpdates();
491
+ }
492
+ _deleteEntry(node, key, value) {
493
+ if (!node.leaf) {
494
+ for (let i = 0, len = node.keys.length; i < len; i++) {
495
+ const nKey = node.keys[i];
496
+ if (nKey === key) {
497
+ node.keys.splice(i, 1);
498
+ this._setUpdates(node);
499
+ break;
500
+ }
501
+ }
502
+ for (let i = 0, len = node.values.length; i < len; i++) {
503
+ const nValue = node.values[i];
504
+ if (this.comparator.isSame(value, nValue)) {
505
+ node.values.splice(i, 1);
506
+ this._setUpdates(node);
507
+ break;
508
+ }
509
+ }
510
+ }
511
+ if (this.root === node && node.keys.length === 1) {
512
+ const keys = node.keys;
513
+ this.root = keys[0];
514
+ this.root.parent = 0;
515
+ this._setHeadUpdate(this._headState);
516
+ this._setUpdates(this.root);
517
+ return;
518
+ } else if (this.root === node) {
519
+ return;
520
+ } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
521
+ let isPredecessor = false;
522
+ let parentNode = this.getNode(node.parent);
523
+ let prevNode = null;
524
+ let nextNode = null;
525
+ let prevK = null;
526
+ let postK = null;
527
+ for (let i = 0, len = parentNode.keys.length; i < len; i++) {
528
+ const nKey = parentNode.keys[i];
529
+ if (nKey === node) {
530
+ if (i > 0) {
531
+ prevNode = parentNode.keys[i - 1];
532
+ prevK = parentNode.values[i - 1];
533
+ }
534
+ if (i < parentNode.keys.length - 1) {
535
+ nextNode = parentNode.keys[i + 1];
536
+ postK = parentNode.values[i];
537
+ }
538
+ }
539
+ }
540
+ let pointer;
541
+ let guess;
542
+ if (prevNode === null) {
543
+ pointer = nextNode;
544
+ guess = postK;
545
+ } else if (nextNode === null) {
546
+ isPredecessor = true;
547
+ pointer = prevNode;
548
+ guess = prevK;
549
+ } else {
550
+ if (node.values.length + nextNode.values.length < this.order) {
551
+ pointer = nextNode;
552
+ guess = postK;
553
+ } else {
554
+ isPredecessor = true;
555
+ pointer = prevNode;
556
+ guess = prevK;
557
+ }
558
+ }
559
+ if (node.values.length + pointer.values.length < this.order) {
560
+ if (!isPredecessor) {
561
+ const pTemp = pointer;
562
+ pointer = node;
563
+ node = pTemp;
564
+ }
565
+ pointer.keys.push(...node.keys);
566
+ if (!node.leaf) {
567
+ pointer.values.push(guess);
568
+ } else {
569
+ pointer.next = node.next;
570
+ }
571
+ pointer.values.push(...node.values);
572
+ if (!pointer.leaf) {
573
+ const keys = pointer.keys;
574
+ for (const key2 of keys) {
575
+ key2.parent = pointer.id;
576
+ }
577
+ }
578
+ this._deleteEntry(this.getNode(node.parent), node, guess);
579
+ this._setUpdates(pointer);
580
+ } else {
581
+ if (isPredecessor) {
582
+ let pointerPm;
583
+ let pointerKm;
584
+ if (!node.leaf) {
585
+ pointerPm = pointer.keys.splice(-1)[0];
586
+ pointerKm = pointer.values.splice(-1)[0];
587
+ node.keys = [pointerPm, ...node.keys];
588
+ node.values = [guess, ...node.values];
589
+ parentNode = this.getNode(node.parent);
590
+ for (let i = 0, len = parentNode.values.length; i < len; i++) {
591
+ const nValue = parentNode.values[i];
592
+ if (this.comparator.isSame(guess, nValue)) {
593
+ parentNode.values[i] = pointerKm;
594
+ this._setUpdates(parentNode);
595
+ break;
596
+ }
597
+ }
598
+ } else {
599
+ pointerPm = pointer.keys.splice(-1)[0];
600
+ pointerKm = pointer.values.splice(-1)[0];
601
+ node.keys = [pointerPm, ...node.keys];
602
+ node.values = [pointerKm, ...node.values];
603
+ parentNode = this.getNode(node.parent);
604
+ for (let i = 0, len = parentNode.values.length; i < len; i++) {
605
+ const nValue = parentNode.values[i];
606
+ if (this.comparator.isSame(guess, nValue)) {
607
+ parentNode.values[i] = pointerKm;
608
+ this._setUpdates(parentNode);
609
+ break;
610
+ }
611
+ }
612
+ }
613
+ this._setUpdates(node);
614
+ this._setUpdates(pointer);
615
+ } else {
616
+ let pointerP0;
617
+ let pointerK0;
618
+ if (!node.leaf) {
619
+ pointerP0 = pointer.keys.splice(0, 1)[0];
620
+ pointerK0 = pointer.values.splice(0, 1)[0];
621
+ node.keys = [...node.keys, pointerP0];
622
+ node.values = [...node.values, guess];
623
+ parentNode = this.getNode(node.parent);
624
+ for (let i = 0, len = parentNode.values.length; i < len; i++) {
625
+ const nValue = parentNode.values[i];
626
+ if (this.comparator.isSame(guess, nValue)) {
627
+ parentNode.values[i] = pointerK0;
628
+ this._setUpdates(parentNode);
629
+ break;
630
+ }
631
+ }
632
+ } else {
633
+ pointerP0 = pointer.keys.splice(0, 1)[0];
634
+ pointerK0 = pointer.values.splice(0, 1)[0];
635
+ node.keys = [...node.keys, pointerP0];
636
+ node.values = [...node.values, pointerK0];
637
+ parentNode = this.getNode(node.parent);
638
+ for (let i = 0, len = parentNode.values.length; i < len; i++) {
639
+ const nValue = parentNode.values[i];
640
+ if (this.comparator.isSame(guess, nValue)) {
641
+ parentNode.values[i] = pointer.values[0];
642
+ this._setUpdates(parentNode);
643
+ break;
644
+ }
645
+ }
646
+ }
647
+ this._setUpdates(node);
648
+ this._setUpdates(pointer);
649
+ }
650
+ if (!pointer.leaf) {
651
+ const keys = pointer.keys;
652
+ for (const key2 of keys) {
653
+ key2.parent = pointer.id;
654
+ this._setUpdates(pointer);
655
+ }
656
+ }
657
+ if (!node.leaf) {
658
+ const keys = node.keys;
659
+ for (const key2 of keys) {
660
+ key2.parent = node.id;
661
+ this._setUpdates(node);
662
+ }
663
+ }
664
+ if (!parentNode.leaf) {
665
+ const keys = parentNode.keys;
666
+ for (const key2 of keys) {
667
+ key2.parent = parentNode.id;
668
+ this._setUpdates(parentNode);
669
+ }
670
+ }
671
+ }
672
+ }
673
+ }
674
+ };
675
+
676
+ // src/SerializeStrategy.ts
677
+ var SerializeStrategy = class {
678
+ order;
679
+ constructor(order) {
680
+ this.order = order;
681
+ }
682
+ };
683
+ var InMemoryStoreStrategy = class extends SerializeStrategy {
684
+ data;
685
+ constructor(order) {
686
+ super(order);
687
+ this.data = {
688
+ head: null,
689
+ node: {}
690
+ };
691
+ }
692
+ id() {
693
+ return Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER - 1);
694
+ }
695
+ read(id) {
696
+ if (Object.prototype.hasOwnProperty.call(this.data, id)) {
697
+ throw new Error(`The tree attempted to reference node '${id}', but couldn't find the corresponding node.`);
698
+ }
699
+ return this.data.node[id];
700
+ }
701
+ write(id, node) {
702
+ this.data.node[id] = node;
703
+ }
704
+ readHead() {
705
+ return this.data.head;
706
+ }
707
+ writeHead(head) {
708
+ this.data.head = head;
709
+ }
710
+ };
711
+
712
+ // src/ValueComparator.ts
713
+ var ValueComparator = class {
714
+ isLower(value, than) {
715
+ return this.asc(value, than) < 0;
716
+ }
717
+ isSame(value, than) {
718
+ return this.asc(value, than) === 0;
719
+ }
720
+ isHigher(value, than) {
721
+ return this.asc(value, than) > 0;
722
+ }
723
+ };
724
+ var NumericComparator = class extends ValueComparator {
725
+ asc(a, b) {
726
+ return a - b;
727
+ }
728
+ };
729
+ var StringComparator = class extends ValueComparator {
730
+ asc(a, b) {
731
+ return a.localeCompare(b);
732
+ }
733
+ };
734
+ export {
735
+ BPTree,
736
+ InMemoryStoreStrategy,
737
+ NumericComparator,
738
+ SerializeStrategy,
739
+ StringComparator,
740
+ ValueComparator
741
+ };