serializable-bptree 8.1.4 → 8.1.5

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.
@@ -230,6 +230,41 @@ var MVCCTransaction = class {
230
230
  });
231
231
  }
232
232
  }
233
+ /**
234
+ * BINARY SEARCH HELPER: Finds the index of the last element in the array
235
+ * where item[key] <= target. Assumes the array is sorted by 'key' ascending.
236
+ */
237
+ _findLastLE(array, target, property) {
238
+ let left = 0;
239
+ let right = array.length - 1;
240
+ let result = -1;
241
+ while (left <= right) {
242
+ const mid = left + right >> 1;
243
+ if (array[mid][property] <= target) {
244
+ result = mid;
245
+ left = mid + 1;
246
+ } else {
247
+ right = mid - 1;
248
+ }
249
+ }
250
+ return result;
251
+ }
252
+ /**
253
+ * BINARY SEARCH HELPER: Finds the index of the element in the array
254
+ * where item[key] === target. Assumes the array is sorted by 'key' ascending.
255
+ */
256
+ _findExact(array, target, property) {
257
+ let left = 0;
258
+ let right = array.length - 1;
259
+ while (left <= right) {
260
+ const mid = left + right >> 1;
261
+ const val = array[mid][property];
262
+ if (val === target) return mid;
263
+ if (val < target) left = mid + 1;
264
+ else right = mid - 1;
265
+ }
266
+ return -1;
267
+ }
233
268
  _bufferCreate(key, value, version) {
234
269
  if (version === void 0) this.localVersion++;
235
270
  const targetVersion = version ?? this.localVersion;
@@ -415,58 +450,62 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
415
450
  return this._diskExists(key, this.snapshotVersion);
416
451
  }
417
452
  _existsSnapshot(key, snapshotVersion, snapshotLocalVersion) {
418
- if (this.writeBuffer.has(key)) {
419
- const keyModVersion = this.keyVersions.get(key);
420
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
421
- return true;
422
- }
423
- }
424
- if (this.deleteBuffer.has(key)) {
425
- const keyModVersion = this.keyVersions.get(key);
426
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
427
- return false;
428
- }
429
- }
430
- const history = this.bufferHistory.get(key);
431
- if (history && snapshotLocalVersion !== void 0) {
432
- for (let i = history.length - 1; i >= 0; i--) {
433
- if (history[i].version <= snapshotLocalVersion) {
434
- return history[i].exists;
435
- }
453
+ let current = this;
454
+ let slVer = snapshotLocalVersion;
455
+ while (current) {
456
+ if (current.writeBuffer.has(key)) {
457
+ const keyModVersion = current.keyVersions.get(key);
458
+ if (slVer === void 0 || keyModVersion <= slVer) return true;
459
+ }
460
+ if (current.deleteBuffer.has(key)) {
461
+ const keyModVersion = current.keyVersions.get(key);
462
+ if (slVer === void 0 || keyModVersion <= slVer) return false;
463
+ }
464
+ const history = current.bufferHistory.get(key);
465
+ if (history && slVer !== void 0) {
466
+ const idx = current._findLastLE(history, slVer, "version");
467
+ if (idx >= 0) return history[idx].exists;
468
+ }
469
+ if (current.parent) {
470
+ slVer = current.snapshotLocalVersion;
471
+ current = current.parent;
472
+ } else {
473
+ return current._diskExists(key, snapshotVersion);
436
474
  }
437
475
  }
438
- if (this.parent) {
439
- return this.parent._existsSnapshot(key, snapshotVersion, this.snapshotLocalVersion);
440
- } else {
441
- return this._diskExists(key, snapshotVersion);
442
- }
476
+ return false;
443
477
  }
444
478
  _readSnapshot(key, snapshotVersion, snapshotLocalVersion) {
445
- if (this.writeBuffer.has(key)) {
446
- const keyModVersion = this.keyVersions.get(key);
447
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
448
- return this.writeBuffer.get(key);
479
+ let current = this;
480
+ let slVer = snapshotLocalVersion;
481
+ while (current) {
482
+ if (current.writeBuffer.has(key)) {
483
+ const keyModVersion = current.keyVersions.get(key);
484
+ if (slVer === void 0 || keyModVersion <= slVer) {
485
+ return current.writeBuffer.get(key);
486
+ }
449
487
  }
450
- }
451
- if (this.deleteBuffer.has(key)) {
452
- const keyModVersion = this.keyVersions.get(key);
453
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
454
- return null;
488
+ if (current.deleteBuffer.has(key)) {
489
+ const keyModVersion = current.keyVersions.get(key);
490
+ if (slVer === void 0 || keyModVersion <= slVer) {
491
+ return null;
492
+ }
455
493
  }
456
- }
457
- const history = this.bufferHistory.get(key);
458
- if (history && snapshotLocalVersion !== void 0) {
459
- for (let i = history.length - 1; i >= 0; i--) {
460
- if (history[i].version <= snapshotLocalVersion) {
461
- return history[i].exists ? history[i].value : null;
494
+ const history = current.bufferHistory.get(key);
495
+ if (history && slVer !== void 0) {
496
+ const idx = current._findLastLE(history, slVer, "version");
497
+ if (idx >= 0) {
498
+ return history[idx].exists ? history[idx].value : null;
462
499
  }
463
500
  }
501
+ if (current.parent) {
502
+ slVer = current.snapshotLocalVersion;
503
+ current = current.parent;
504
+ } else {
505
+ return current._diskRead(key, snapshotVersion);
506
+ }
464
507
  }
465
- if (this.parent) {
466
- return this.parent._readSnapshot(key, snapshotVersion, this.snapshotLocalVersion);
467
- } else {
468
- return this._diskRead(key, snapshotVersion);
469
- }
508
+ return null;
470
509
  }
471
510
  commit(label) {
472
511
  const { created, updated, deleted } = this.getResultEntries();
@@ -671,19 +710,15 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
671
710
  }
672
711
  let targetVerObj = null;
673
712
  let nextVerObj = null;
674
- for (const v of versions) {
675
- if (v.version <= snapshotVersion) targetVerObj = v;
676
- else {
677
- nextVerObj = v;
678
- break;
679
- }
680
- }
713
+ const idx = this._findLastLE(versions, snapshotVersion, "version");
714
+ if (idx >= 0) targetVerObj = versions[idx];
715
+ if (idx + 1 < versions.length) nextVerObj = versions[idx + 1];
681
716
  if (!targetVerObj) {
682
717
  if (nextVerObj) {
683
718
  const cached2 = this.deletedCache.get(key);
684
719
  if (cached2) {
685
- const match = cached2.find((c) => c.deletedAtVersion === nextVerObj.version);
686
- if (match) return match.value;
720
+ const cIdx = this._findExact(cached2, nextVerObj.version, "deletedAtVersion");
721
+ if (cIdx >= 0) return cached2[cIdx].value;
687
722
  }
688
723
  }
689
724
  return null;
@@ -701,8 +736,8 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
701
736
  }
702
737
  const cached = this.deletedCache.get(key);
703
738
  if (cached) {
704
- const match = cached.find((c) => c.deletedAtVersion === nextVerObj.version);
705
- if (match) return match.value;
739
+ const cIdx = this._findExact(cached, nextVerObj.version, "deletedAtVersion");
740
+ if (cIdx >= 0) return cached[cIdx].value;
706
741
  }
707
742
  return null;
708
743
  }
@@ -719,19 +754,15 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
719
754
  }
720
755
  let targetVerObj = null;
721
756
  let nextVerObj = null;
722
- for (const v of versions) {
723
- if (v.version <= snapshotVersion) targetVerObj = v;
724
- else {
725
- nextVerObj = v;
726
- break;
727
- }
728
- }
757
+ const idx = this._findLastLE(versions, snapshotVersion, "version");
758
+ if (idx >= 0) targetVerObj = versions[idx];
759
+ if (idx + 1 < versions.length) nextVerObj = versions[idx + 1];
729
760
  if (!targetVerObj) {
730
761
  if (nextVerObj) {
731
762
  const cached = this.deletedCache.get(key);
732
763
  if (cached) {
733
- const match = cached.find((c) => c.deletedAtVersion === nextVerObj.version);
734
- if (match) return true;
764
+ const cIdx = this._findExact(cached, nextVerObj.version, "deletedAtVersion");
765
+ if (cIdx >= 0) return true;
735
766
  }
736
767
  }
737
768
  return false;
@@ -1084,58 +1115,62 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1084
1115
  return await this._diskExists(key, this.snapshotVersion);
1085
1116
  }
1086
1117
  async _existsSnapshot(key, snapshotVersion, snapshotLocalVersion) {
1087
- if (this.writeBuffer.has(key)) {
1088
- const keyModVersion = this.keyVersions.get(key);
1089
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
1090
- return true;
1091
- }
1092
- }
1093
- if (this.deleteBuffer.has(key)) {
1094
- const keyModVersion = this.keyVersions.get(key);
1095
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
1096
- return false;
1097
- }
1098
- }
1099
- const history = this.bufferHistory.get(key);
1100
- if (history && snapshotLocalVersion !== void 0) {
1101
- for (let i = history.length - 1; i >= 0; i--) {
1102
- if (history[i].version <= snapshotLocalVersion) {
1103
- return history[i].exists;
1104
- }
1118
+ let current = this;
1119
+ let slVer = snapshotLocalVersion;
1120
+ while (current) {
1121
+ if (current.writeBuffer.has(key)) {
1122
+ const keyModVersion = current.keyVersions.get(key);
1123
+ if (slVer === void 0 || keyModVersion <= slVer) return true;
1124
+ }
1125
+ if (current.deleteBuffer.has(key)) {
1126
+ const keyModVersion = current.keyVersions.get(key);
1127
+ if (slVer === void 0 || keyModVersion <= slVer) return false;
1128
+ }
1129
+ const history = current.bufferHistory.get(key);
1130
+ if (history && slVer !== void 0) {
1131
+ const idx = current._findLastLE(history, slVer, "version");
1132
+ if (idx >= 0) return history[idx].exists;
1133
+ }
1134
+ if (current.parent) {
1135
+ slVer = current.snapshotLocalVersion;
1136
+ current = current.parent;
1137
+ } else {
1138
+ return await current._diskExists(key, snapshotVersion);
1105
1139
  }
1106
1140
  }
1107
- if (this.parent) {
1108
- return this.parent._existsSnapshot(key, snapshotVersion, this.snapshotLocalVersion);
1109
- } else {
1110
- return await this._diskExists(key, snapshotVersion);
1111
- }
1141
+ return false;
1112
1142
  }
1113
1143
  async _readSnapshot(key, snapshotVersion, snapshotLocalVersion) {
1114
- if (this.writeBuffer.has(key)) {
1115
- const keyModVersion = this.keyVersions.get(key);
1116
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
1117
- return this.writeBuffer.get(key);
1144
+ let current = this;
1145
+ let slVer = snapshotLocalVersion;
1146
+ while (current) {
1147
+ if (current.writeBuffer.has(key)) {
1148
+ const keyModVersion = current.keyVersions.get(key);
1149
+ if (slVer === void 0 || keyModVersion <= slVer) {
1150
+ return current.writeBuffer.get(key);
1151
+ }
1118
1152
  }
1119
- }
1120
- if (this.deleteBuffer.has(key)) {
1121
- const keyModVersion = this.keyVersions.get(key);
1122
- if (snapshotLocalVersion === void 0 || keyModVersion <= snapshotLocalVersion) {
1123
- return null;
1153
+ if (current.deleteBuffer.has(key)) {
1154
+ const keyModVersion = current.keyVersions.get(key);
1155
+ if (slVer === void 0 || keyModVersion <= slVer) {
1156
+ return null;
1157
+ }
1124
1158
  }
1125
- }
1126
- const history = this.bufferHistory.get(key);
1127
- if (history && snapshotLocalVersion !== void 0) {
1128
- for (let i = history.length - 1; i >= 0; i--) {
1129
- if (history[i].version <= snapshotLocalVersion) {
1130
- return history[i].exists ? history[i].value : null;
1159
+ const history = current.bufferHistory.get(key);
1160
+ if (history && slVer !== void 0) {
1161
+ const idx = current._findLastLE(history, slVer, "version");
1162
+ if (idx >= 0) {
1163
+ return history[idx].exists ? history[idx].value : null;
1131
1164
  }
1132
1165
  }
1166
+ if (current.parent) {
1167
+ slVer = current.snapshotLocalVersion;
1168
+ current = current.parent;
1169
+ } else {
1170
+ return await current._diskRead(key, snapshotVersion);
1171
+ }
1133
1172
  }
1134
- if (this.parent) {
1135
- return this.parent._readSnapshot(key, snapshotVersion, this.snapshotLocalVersion);
1136
- } else {
1137
- return await this._diskRead(key, snapshotVersion);
1138
- }
1173
+ return null;
1139
1174
  }
1140
1175
  async commit(label) {
1141
1176
  const { created, updated, deleted } = this.getResultEntries();
@@ -1343,19 +1378,15 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1343
1378
  }
1344
1379
  let targetVerObj = null;
1345
1380
  let nextVerObj = null;
1346
- for (const v of versions) {
1347
- if (v.version <= snapshotVersion) targetVerObj = v;
1348
- else {
1349
- nextVerObj = v;
1350
- break;
1351
- }
1352
- }
1381
+ const idx = this._findLastLE(versions, snapshotVersion, "version");
1382
+ if (idx >= 0) targetVerObj = versions[idx];
1383
+ if (idx + 1 < versions.length) nextVerObj = versions[idx + 1];
1353
1384
  if (!targetVerObj) {
1354
1385
  if (nextVerObj) {
1355
1386
  const cached2 = this.deletedCache.get(key);
1356
1387
  if (cached2) {
1357
- const match = cached2.find((c) => c.deletedAtVersion === nextVerObj.version);
1358
- if (match) return match.value;
1388
+ const cIdx = this._findExact(cached2, nextVerObj.version, "deletedAtVersion");
1389
+ if (cIdx >= 0) return cached2[cIdx].value;
1359
1390
  }
1360
1391
  }
1361
1392
  return null;
@@ -1373,8 +1404,8 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1373
1404
  }
1374
1405
  const cached = this.deletedCache.get(key);
1375
1406
  if (cached) {
1376
- const match = cached.find((c) => c.deletedAtVersion === nextVerObj.version);
1377
- if (match) return match.value;
1407
+ const cIdx = this._findExact(cached, nextVerObj.version, "deletedAtVersion");
1408
+ if (cIdx >= 0) return cached[cIdx].value;
1378
1409
  }
1379
1410
  return null;
1380
1411
  }
@@ -1391,19 +1422,15 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1391
1422
  }
1392
1423
  let targetVerObj = null;
1393
1424
  let nextVerObj = null;
1394
- for (const v of versions) {
1395
- if (v.version <= snapshotVersion) targetVerObj = v;
1396
- else {
1397
- nextVerObj = v;
1398
- break;
1399
- }
1400
- }
1425
+ const idx = this._findLastLE(versions, snapshotVersion, "version");
1426
+ if (idx >= 0) targetVerObj = versions[idx];
1427
+ if (idx + 1 < versions.length) nextVerObj = versions[idx + 1];
1401
1428
  if (!targetVerObj) {
1402
1429
  if (nextVerObj) {
1403
1430
  const cached = this.deletedCache.get(key);
1404
1431
  if (cached) {
1405
- const match = cached.find((c) => c.deletedAtVersion === nextVerObj.version);
1406
- if (match) return true;
1432
+ const cIdx = this._findExact(cached, nextVerObj.version, "deletedAtVersion");
1433
+ if (cIdx >= 0) return true;
1407
1434
  }
1408
1435
  }
1409
1436
  return false;
@@ -1799,7 +1826,6 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1799
1826
  }
1800
1827
  _insertAtLeaf(node, key, value) {
1801
1828
  let leaf = node;
1802
- leaf = this._cloneNode(leaf);
1803
1829
  if (leaf.values.length) {
1804
1830
  for (let i = 0, len = leaf.values.length; i < len; i++) {
1805
1831
  const nValue = leaf.values[i];
@@ -1808,15 +1834,18 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1808
1834
  if (keys.includes(key)) {
1809
1835
  break;
1810
1836
  }
1811
- keys.push(key);
1837
+ leaf = this._cloneNode(leaf);
1838
+ leaf.keys[i].push(key);
1812
1839
  this._updateNode(leaf);
1813
1840
  return leaf;
1814
1841
  } else if (this.comparator.isLower(value, nValue)) {
1842
+ leaf = this._cloneNode(leaf);
1815
1843
  leaf.values.splice(i, 0, value);
1816
1844
  leaf.keys.splice(i, 0, [key]);
1817
1845
  this._updateNode(leaf);
1818
1846
  return leaf;
1819
1847
  } else if (i + 1 === leaf.values.length) {
1848
+ leaf = this._cloneNode(leaf);
1820
1849
  leaf.values.push(value);
1821
1850
  leaf.keys.push([key]);
1822
1851
  this._updateNode(leaf);
@@ -1824,6 +1853,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1824
1853
  }
1825
1854
  }
1826
1855
  } else {
1856
+ leaf = this._cloneNode(leaf);
1827
1857
  leaf.values = [value];
1828
1858
  leaf.keys = [[key]];
1829
1859
  this._updateNode(leaf);
@@ -1831,17 +1861,17 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1831
1861
  }
1832
1862
  return leaf;
1833
1863
  }
1834
- _insertInParent(node, value, pointer) {
1835
- node = this._cloneNode(node);
1836
- pointer = this._cloneNode(pointer);
1864
+ _insertInParent(node, value, newSiblingNode) {
1837
1865
  if (this.rootId === node.id) {
1838
- const root = this._createNode(false, [node.id, pointer.id], [value]);
1866
+ node = this._cloneNode(node);
1867
+ newSiblingNode = this._cloneNode(newSiblingNode);
1868
+ const root = this._createNode(false, [node.id, newSiblingNode.id], [value]);
1839
1869
  this.rootId = root.id;
1840
1870
  node.parent = root.id;
1841
- pointer.parent = root.id;
1842
- if (pointer.leaf) {
1843
- node.next = pointer.id;
1844
- pointer.prev = node.id;
1871
+ newSiblingNode.parent = root.id;
1872
+ if (newSiblingNode.leaf) {
1873
+ node.next = newSiblingNode.id;
1874
+ newSiblingNode.prev = node.id;
1845
1875
  }
1846
1876
  this._writeHead({
1847
1877
  root: root.id,
@@ -1849,7 +1879,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1849
1879
  data: this.strategy.head.data
1850
1880
  });
1851
1881
  this._updateNode(node);
1852
- this._updateNode(pointer);
1882
+ this._updateNode(newSiblingNode);
1853
1883
  return;
1854
1884
  }
1855
1885
  const parentNode = this._cloneNode(this.getNode(node.parent));
@@ -1858,29 +1888,30 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1858
1888
  throw new Error(`Node ${node.id} not found in parent ${parentNode.id}`);
1859
1889
  }
1860
1890
  parentNode.values.splice(nodeIndex, 0, value);
1861
- parentNode.keys.splice(nodeIndex + 1, 0, pointer.id);
1862
- pointer.parent = parentNode.id;
1863
- if (pointer.leaf) {
1864
- const leftSibling = node;
1891
+ parentNode.keys.splice(nodeIndex + 1, 0, newSiblingNode.id);
1892
+ newSiblingNode = this._cloneNode(newSiblingNode);
1893
+ newSiblingNode.parent = parentNode.id;
1894
+ if (newSiblingNode.leaf) {
1895
+ const leftSibling = this._cloneNode(node);
1865
1896
  const oldNextId = leftSibling.next;
1866
- pointer.prev = leftSibling.id;
1867
- pointer.next = oldNextId;
1868
- leftSibling.next = pointer.id;
1897
+ newSiblingNode.prev = leftSibling.id;
1898
+ newSiblingNode.next = oldNextId;
1899
+ leftSibling.next = newSiblingNode.id;
1869
1900
  this._updateNode(leftSibling);
1870
1901
  if (oldNextId) {
1871
1902
  const oldNext = this._cloneNode(this.getNode(oldNextId));
1872
- oldNext.prev = pointer.id;
1903
+ oldNext.prev = newSiblingNode.id;
1873
1904
  this._updateNode(oldNext);
1874
1905
  }
1875
1906
  }
1876
1907
  this._updateNode(parentNode);
1877
- this._updateNode(pointer);
1908
+ this._updateNode(newSiblingNode);
1878
1909
  if (parentNode.keys.length > this.order) {
1879
- const parentPointer = this._createNode(false, [], []);
1880
- parentPointer.parent = parentNode.parent;
1910
+ const newSiblingNodeRecursive = this._createNode(false, [], []);
1911
+ newSiblingNodeRecursive.parent = parentNode.parent;
1881
1912
  const mid = Math.ceil(this.order / 2) - 1;
1882
- parentPointer.values = parentNode.values.slice(mid + 1);
1883
- parentPointer.keys = parentNode.keys.slice(mid + 1);
1913
+ newSiblingNodeRecursive.values = parentNode.values.slice(mid + 1);
1914
+ newSiblingNodeRecursive.keys = parentNode.keys.slice(mid + 1);
1884
1915
  const midValue = parentNode.values[mid];
1885
1916
  parentNode.values = parentNode.values.slice(0, mid);
1886
1917
  parentNode.keys = parentNode.keys.slice(0, mid + 1);
@@ -1889,13 +1920,13 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1889
1920
  n.parent = parentNode.id;
1890
1921
  this._updateNode(n);
1891
1922
  }
1892
- for (const k of parentPointer.keys) {
1923
+ for (const k of newSiblingNodeRecursive.keys) {
1893
1924
  const n = this._cloneNode(this.getNode(k));
1894
- n.parent = parentPointer.id;
1925
+ n.parent = newSiblingNodeRecursive.id;
1895
1926
  this._updateNode(n);
1896
1927
  }
1897
1928
  this._updateNode(parentNode);
1898
- this._insertInParent(parentNode, midValue, parentPointer);
1929
+ this._insertInParent(parentNode, midValue, newSiblingNodeRecursive);
1899
1930
  }
1900
1931
  }
1901
1932
  insertableNode(value) {
@@ -2186,7 +2217,6 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2186
2217
  }
2187
2218
  }
2188
2219
  _deleteEntry(node, key) {
2189
- node = this._cloneNode(node);
2190
2220
  if (!node.leaf) {
2191
2221
  let keyIndex = -1;
2192
2222
  for (let i = 0, len = node.keys.length; i < len; i++) {
@@ -2196,6 +2226,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2196
2226
  }
2197
2227
  }
2198
2228
  if (keyIndex !== -1) {
2229
+ node = this._cloneNode(node);
2199
2230
  node.keys.splice(keyIndex, 1);
2200
2231
  const valueIndex = keyIndex > 0 ? keyIndex - 1 : 0;
2201
2232
  node.values.splice(valueIndex, 1);
@@ -2235,73 +2266,75 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2235
2266
  const nKey = parentNode.keys[i];
2236
2267
  if (nKey === node.id) {
2237
2268
  if (i > 0) {
2238
- prevNode = this._cloneNode(this.getNode(parentNode.keys[i - 1]));
2269
+ prevNode = this.getNode(parentNode.keys[i - 1]);
2239
2270
  prevValue = parentNode.values[i - 1];
2240
2271
  }
2241
2272
  if (i < parentNode.keys.length - 1) {
2242
- nextNode = this._cloneNode(this.getNode(parentNode.keys[i + 1]));
2273
+ nextNode = this.getNode(parentNode.keys[i + 1]);
2243
2274
  postValue = parentNode.values[i];
2244
2275
  }
2245
2276
  }
2246
2277
  }
2247
- let pointer;
2278
+ let siblingNode;
2248
2279
  let guess;
2249
2280
  if (prevNode === null) {
2250
- pointer = nextNode;
2281
+ siblingNode = nextNode;
2251
2282
  guess = postValue;
2252
2283
  } else if (nextNode === null) {
2253
2284
  isPredecessor = true;
2254
- pointer = prevNode;
2285
+ siblingNode = prevNode;
2255
2286
  guess = prevValue;
2256
2287
  } else {
2257
2288
  if (node.values.length + nextNode.values.length < this.order) {
2258
- pointer = nextNode;
2289
+ siblingNode = nextNode;
2259
2290
  guess = postValue;
2260
2291
  } else {
2261
2292
  isPredecessor = true;
2262
- pointer = prevNode;
2293
+ siblingNode = prevNode;
2263
2294
  guess = prevValue;
2264
2295
  }
2265
2296
  }
2266
- if (!pointer) {
2297
+ if (!siblingNode) {
2267
2298
  return node;
2268
2299
  }
2269
- if (node.values.length + pointer.values.length < this.order) {
2300
+ node = this._cloneNode(node);
2301
+ siblingNode = this._cloneNode(siblingNode);
2302
+ if (node.values.length + siblingNode.values.length < this.order) {
2270
2303
  if (!isPredecessor) {
2271
- const pTemp = pointer;
2272
- pointer = node;
2304
+ const pTemp = siblingNode;
2305
+ siblingNode = node;
2273
2306
  node = pTemp;
2274
2307
  }
2275
- pointer.keys.push(...node.keys);
2308
+ siblingNode.keys.push(...node.keys);
2276
2309
  if (!node.leaf) {
2277
- pointer.values.push(guess);
2310
+ siblingNode.values.push(guess);
2278
2311
  } else {
2279
- pointer.next = node.next;
2280
- if (pointer.next) {
2281
- const n = this._cloneNode(this.getNode(pointer.next));
2282
- n.prev = pointer.id;
2312
+ siblingNode.next = node.next;
2313
+ if (siblingNode.next) {
2314
+ const n = this._cloneNode(this.getNode(siblingNode.next));
2315
+ n.prev = siblingNode.id;
2283
2316
  this._updateNode(n);
2284
2317
  }
2285
2318
  }
2286
- pointer.values.push(...node.values);
2287
- if (!pointer.leaf) {
2288
- const keys = pointer.keys;
2319
+ siblingNode.values.push(...node.values);
2320
+ if (!siblingNode.leaf) {
2321
+ const keys = siblingNode.keys;
2289
2322
  for (const key2 of keys) {
2290
2323
  const node2 = this._cloneNode(this.getNode(key2));
2291
- node2.parent = pointer.id;
2324
+ node2.parent = siblingNode.id;
2292
2325
  this._updateNode(node2);
2293
2326
  }
2294
2327
  }
2295
2328
  this._deleteNode(node);
2296
- this._updateNode(pointer);
2297
- this._deleteEntry(this._cloneNode(this.getNode(node.parent)), node.id);
2329
+ this._updateNode(siblingNode);
2330
+ this._deleteEntry(this.getNode(node.parent), node.id);
2298
2331
  } else {
2299
2332
  if (isPredecessor) {
2300
2333
  let pointerPm;
2301
2334
  let pointerKm;
2302
2335
  if (!node.leaf) {
2303
- pointerPm = pointer.keys.splice(-1)[0];
2304
- pointerKm = pointer.values.splice(-1)[0];
2336
+ pointerPm = siblingNode.keys.splice(-1)[0];
2337
+ pointerKm = siblingNode.values.splice(-1)[0];
2305
2338
  node.keys = [pointerPm, ...node.keys];
2306
2339
  node.values = [guess, ...node.values];
2307
2340
  parentNode = this._cloneNode(this.getNode(node.parent));
@@ -2311,8 +2344,8 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2311
2344
  this._updateNode(parentNode);
2312
2345
  }
2313
2346
  } else {
2314
- pointerPm = pointer.keys.splice(-1)[0];
2315
- pointerKm = pointer.values.splice(-1)[0];
2347
+ pointerPm = siblingNode.keys.splice(-1)[0];
2348
+ pointerKm = siblingNode.values.splice(-1)[0];
2316
2349
  node.keys = [pointerPm, ...node.keys];
2317
2350
  node.values = [pointerKm, ...node.values];
2318
2351
  parentNode = this._cloneNode(this.getNode(node.parent));
@@ -2323,40 +2356,40 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2323
2356
  }
2324
2357
  }
2325
2358
  this._updateNode(node);
2326
- this._updateNode(pointer);
2359
+ this._updateNode(siblingNode);
2327
2360
  } else {
2328
2361
  let pointerP0;
2329
2362
  let pointerK0;
2330
2363
  if (!node.leaf) {
2331
- pointerP0 = pointer.keys.splice(0, 1)[0];
2332
- pointerK0 = pointer.values.splice(0, 1)[0];
2364
+ pointerP0 = siblingNode.keys.splice(0, 1)[0];
2365
+ pointerK0 = siblingNode.values.splice(0, 1)[0];
2333
2366
  node.keys = [...node.keys, pointerP0];
2334
2367
  node.values = [...node.values, guess];
2335
2368
  parentNode = this._cloneNode(this.getNode(node.parent));
2336
- const pointerIndex = parentNode.keys.indexOf(pointer.id);
2369
+ const pointerIndex = parentNode.keys.indexOf(siblingNode.id);
2337
2370
  if (pointerIndex > 0) {
2338
2371
  parentNode.values[pointerIndex - 1] = pointerK0;
2339
2372
  this._updateNode(parentNode);
2340
2373
  }
2341
2374
  } else {
2342
- pointerP0 = pointer.keys.splice(0, 1)[0];
2343
- pointerK0 = pointer.values.splice(0, 1)[0];
2375
+ pointerP0 = siblingNode.keys.splice(0, 1)[0];
2376
+ pointerK0 = siblingNode.values.splice(0, 1)[0];
2344
2377
  node.keys = [...node.keys, pointerP0];
2345
2378
  node.values = [...node.values, pointerK0];
2346
2379
  parentNode = this._cloneNode(this.getNode(node.parent));
2347
- const pointerIndex = parentNode.keys.indexOf(pointer.id);
2380
+ const pointerIndex = parentNode.keys.indexOf(siblingNode.id);
2348
2381
  if (pointerIndex > 0) {
2349
- parentNode.values[pointerIndex - 1] = pointer.values[0];
2382
+ parentNode.values[pointerIndex - 1] = siblingNode.values[0];
2350
2383
  this._updateNode(parentNode);
2351
2384
  }
2352
2385
  }
2353
2386
  this._updateNode(node);
2354
- this._updateNode(pointer);
2387
+ this._updateNode(siblingNode);
2355
2388
  }
2356
- if (!pointer.leaf) {
2357
- for (const key2 of pointer.keys) {
2389
+ if (!siblingNode.leaf) {
2390
+ for (const key2 of siblingNode.keys) {
2358
2391
  const n = this._cloneNode(this.getNode(key2));
2359
- n.parent = pointer.id;
2392
+ n.parent = siblingNode.id;
2360
2393
  this._updateNode(n);
2361
2394
  }
2362
2395
  }
@@ -2376,14 +2409,13 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2376
2409
  }
2377
2410
  }
2378
2411
  } else {
2379
- this._updateNode(node);
2412
+ this._updateNode(this._cloneNode(node));
2380
2413
  }
2381
2414
  return node;
2382
2415
  }
2383
2416
  delete(key, value) {
2384
2417
  let node = this.insertableNodeByPrimary(value);
2385
2418
  let found = false;
2386
- node = this._cloneNode(node);
2387
2419
  while (true) {
2388
2420
  let i = node.values.length;
2389
2421
  while (i--) {
@@ -2392,8 +2424,10 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2392
2424
  const keys = node.keys[i];
2393
2425
  const keyIndex = keys.indexOf(key);
2394
2426
  if (keyIndex !== -1) {
2395
- keys.splice(keyIndex, 1);
2396
- if (keys.length === 0) {
2427
+ node = this._cloneNode(node);
2428
+ const freshKeys = node.keys[i];
2429
+ freshKeys.splice(keyIndex, 1);
2430
+ if (freshKeys.length === 0) {
2397
2431
  node.keys.splice(i, 1);
2398
2432
  node.values.splice(i, 1);
2399
2433
  }
@@ -2848,7 +2882,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2848
2882
  if (this.mvcc.isDeleted(node.id)) {
2849
2883
  return;
2850
2884
  }
2851
- await this.mvcc.write(node.id, this._cloneNode(node));
2885
+ await this.mvcc.write(node.id, node);
2852
2886
  }
2853
2887
  async _deleteNode(node) {
2854
2888
  if (this.mvcc.isDeleted(node.id)) {
@@ -2861,15 +2895,14 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2861
2895
  }
2862
2896
  async _writeHead(head) {
2863
2897
  if (!await this.mvcc.exists("__HEAD__")) {
2864
- await this.mvcc.create("__HEAD__", this._cloneNode(head));
2898
+ await this.mvcc.create("__HEAD__", head);
2865
2899
  } else {
2866
- await this.mvcc.write("__HEAD__", this._cloneNode(head));
2900
+ await this.mvcc.write("__HEAD__", head);
2867
2901
  }
2868
2902
  this.rootId = head.root;
2869
2903
  }
2870
2904
  async _insertAtLeaf(node, key, value) {
2871
2905
  let leaf = node;
2872
- leaf = this._cloneNode(leaf);
2873
2906
  if (leaf.values.length) {
2874
2907
  for (let i = 0, len = leaf.values.length; i < len; i++) {
2875
2908
  const nValue = leaf.values[i];
@@ -2878,15 +2911,18 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2878
2911
  if (keys.includes(key)) {
2879
2912
  break;
2880
2913
  }
2881
- keys.push(key);
2914
+ leaf = this._cloneNode(leaf);
2915
+ leaf.keys[i].push(key);
2882
2916
  await this._updateNode(leaf);
2883
2917
  return leaf;
2884
2918
  } else if (this.comparator.isLower(value, nValue)) {
2919
+ leaf = this._cloneNode(leaf);
2885
2920
  leaf.values.splice(i, 0, value);
2886
2921
  leaf.keys.splice(i, 0, [key]);
2887
2922
  await this._updateNode(leaf);
2888
2923
  return leaf;
2889
2924
  } else if (i + 1 === leaf.values.length) {
2925
+ leaf = this._cloneNode(leaf);
2890
2926
  leaf.values.push(value);
2891
2927
  leaf.keys.push([key]);
2892
2928
  await this._updateNode(leaf);
@@ -2894,6 +2930,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2894
2930
  }
2895
2931
  }
2896
2932
  } else {
2933
+ leaf = this._cloneNode(leaf);
2897
2934
  leaf.values = [value];
2898
2935
  leaf.keys = [[key]];
2899
2936
  await this._updateNode(leaf);
@@ -2901,17 +2938,17 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2901
2938
  }
2902
2939
  return leaf;
2903
2940
  }
2904
- async _insertInParent(node, value, pointer) {
2905
- node = this._cloneNode(node);
2906
- pointer = this._cloneNode(pointer);
2941
+ async _insertInParent(node, value, newSiblingNode) {
2907
2942
  if (this.rootId === node.id) {
2908
- const root = await this._createNode(false, [node.id, pointer.id], [value]);
2943
+ node = this._cloneNode(node);
2944
+ newSiblingNode = this._cloneNode(newSiblingNode);
2945
+ const root = await this._createNode(false, [node.id, newSiblingNode.id], [value]);
2909
2946
  this.rootId = root.id;
2910
2947
  node.parent = root.id;
2911
- pointer.parent = root.id;
2912
- if (pointer.leaf) {
2913
- node.next = pointer.id;
2914
- pointer.prev = node.id;
2948
+ newSiblingNode.parent = root.id;
2949
+ if (newSiblingNode.leaf) {
2950
+ node.next = newSiblingNode.id;
2951
+ newSiblingNode.prev = node.id;
2915
2952
  }
2916
2953
  await this._writeHead({
2917
2954
  root: root.id,
@@ -2919,7 +2956,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2919
2956
  data: this.strategy.head.data
2920
2957
  });
2921
2958
  await this._updateNode(node);
2922
- await this._updateNode(pointer);
2959
+ await this._updateNode(newSiblingNode);
2923
2960
  return;
2924
2961
  }
2925
2962
  const parentNode = this._cloneNode(await this.getNode(node.parent));
@@ -2928,29 +2965,30 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2928
2965
  throw new Error(`Node ${node.id} not found in parent ${parentNode.id}`);
2929
2966
  }
2930
2967
  parentNode.values.splice(nodeIndex, 0, value);
2931
- parentNode.keys.splice(nodeIndex + 1, 0, pointer.id);
2932
- pointer.parent = parentNode.id;
2933
- if (pointer.leaf) {
2934
- const leftSibling = node;
2968
+ parentNode.keys.splice(nodeIndex + 1, 0, newSiblingNode.id);
2969
+ newSiblingNode = this._cloneNode(newSiblingNode);
2970
+ newSiblingNode.parent = parentNode.id;
2971
+ if (newSiblingNode.leaf) {
2972
+ const leftSibling = this._cloneNode(node);
2935
2973
  const oldNextId = leftSibling.next;
2936
- pointer.prev = leftSibling.id;
2937
- pointer.next = oldNextId;
2938
- leftSibling.next = pointer.id;
2974
+ newSiblingNode.prev = leftSibling.id;
2975
+ newSiblingNode.next = oldNextId;
2976
+ leftSibling.next = newSiblingNode.id;
2939
2977
  await this._updateNode(leftSibling);
2940
2978
  if (oldNextId) {
2941
2979
  const oldNext = this._cloneNode(await this.getNode(oldNextId));
2942
- oldNext.prev = pointer.id;
2980
+ oldNext.prev = newSiblingNode.id;
2943
2981
  await this._updateNode(oldNext);
2944
2982
  }
2945
2983
  }
2946
2984
  await this._updateNode(parentNode);
2947
- await this._updateNode(pointer);
2985
+ await this._updateNode(newSiblingNode);
2948
2986
  if (parentNode.keys.length > this.order) {
2949
- const parentPointer = await this._createNode(false, [], []);
2950
- parentPointer.parent = parentNode.parent;
2987
+ const newSiblingNodeRecursive = await this._createNode(false, [], []);
2988
+ newSiblingNodeRecursive.parent = parentNode.parent;
2951
2989
  const mid = Math.ceil(this.order / 2) - 1;
2952
- parentPointer.values = parentNode.values.slice(mid + 1);
2953
- parentPointer.keys = parentNode.keys.slice(mid + 1);
2990
+ newSiblingNodeRecursive.values = parentNode.values.slice(mid + 1);
2991
+ newSiblingNodeRecursive.keys = parentNode.keys.slice(mid + 1);
2954
2992
  const midValue = parentNode.values[mid];
2955
2993
  parentNode.values = parentNode.values.slice(0, mid);
2956
2994
  parentNode.keys = parentNode.keys.slice(0, mid + 1);
@@ -2959,13 +2997,13 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2959
2997
  n.parent = parentNode.id;
2960
2998
  await this._updateNode(n);
2961
2999
  }
2962
- for (const k of parentPointer.keys) {
3000
+ for (const k of newSiblingNodeRecursive.keys) {
2963
3001
  const n = this._cloneNode(await this.getNode(k));
2964
- n.parent = parentPointer.id;
3002
+ n.parent = newSiblingNodeRecursive.id;
2965
3003
  await this._updateNode(n);
2966
3004
  }
2967
3005
  await this._updateNode(parentNode);
2968
- await this._insertInParent(parentNode, midValue, parentPointer);
3006
+ await this._insertInParent(parentNode, midValue, newSiblingNodeRecursive);
2969
3007
  }
2970
3008
  }
2971
3009
  async insertableNode(value) {
@@ -3264,7 +3302,6 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3264
3302
  });
3265
3303
  }
3266
3304
  async _deleteEntry(node, key) {
3267
- node = this._cloneNode(node);
3268
3305
  if (!node.leaf) {
3269
3306
  let keyIndex = -1;
3270
3307
  for (let i = 0, len = node.keys.length; i < len; i++) {
@@ -3274,6 +3311,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3274
3311
  }
3275
3312
  }
3276
3313
  if (keyIndex !== -1) {
3314
+ node = this._cloneNode(node);
3277
3315
  node.keys.splice(keyIndex, 1);
3278
3316
  const valueIndex = keyIndex > 0 ? keyIndex - 1 : 0;
3279
3317
  node.values.splice(valueIndex, 1);
@@ -3313,73 +3351,75 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3313
3351
  const nKey = parentNode.keys[i];
3314
3352
  if (nKey === node.id) {
3315
3353
  if (i > 0) {
3316
- prevNode = this._cloneNode(await this.getNode(parentNode.keys[i - 1]));
3354
+ prevNode = await this.getNode(parentNode.keys[i - 1]);
3317
3355
  prevValue = parentNode.values[i - 1];
3318
3356
  }
3319
3357
  if (i < parentNode.keys.length - 1) {
3320
- nextNode = this._cloneNode(await this.getNode(parentNode.keys[i + 1]));
3358
+ nextNode = await this.getNode(parentNode.keys[i + 1]);
3321
3359
  postValue = parentNode.values[i];
3322
3360
  }
3323
3361
  }
3324
3362
  }
3325
- let pointer;
3363
+ let siblingNode;
3326
3364
  let guess;
3327
3365
  if (prevNode === null) {
3328
- pointer = nextNode;
3366
+ siblingNode = nextNode;
3329
3367
  guess = postValue;
3330
3368
  } else if (nextNode === null) {
3331
3369
  isPredecessor = true;
3332
- pointer = prevNode;
3370
+ siblingNode = prevNode;
3333
3371
  guess = prevValue;
3334
3372
  } else {
3335
3373
  if (node.values.length + nextNode.values.length < this.order) {
3336
- pointer = nextNode;
3374
+ siblingNode = nextNode;
3337
3375
  guess = postValue;
3338
3376
  } else {
3339
3377
  isPredecessor = true;
3340
- pointer = prevNode;
3378
+ siblingNode = prevNode;
3341
3379
  guess = prevValue;
3342
3380
  }
3343
3381
  }
3344
- if (!pointer) {
3382
+ if (!siblingNode) {
3345
3383
  return node;
3346
3384
  }
3347
- if (node.values.length + pointer.values.length < this.order) {
3385
+ node = this._cloneNode(node);
3386
+ siblingNode = this._cloneNode(siblingNode);
3387
+ if (node.values.length + siblingNode.values.length < this.order) {
3348
3388
  if (!isPredecessor) {
3349
- const pTemp = pointer;
3350
- pointer = node;
3389
+ const pTemp = siblingNode;
3390
+ siblingNode = node;
3351
3391
  node = pTemp;
3352
3392
  }
3353
- pointer.keys.push(...node.keys);
3393
+ siblingNode.keys.push(...node.keys);
3354
3394
  if (!node.leaf) {
3355
- pointer.values.push(guess);
3395
+ siblingNode.values.push(guess);
3356
3396
  } else {
3357
- pointer.next = node.next;
3358
- if (pointer.next) {
3359
- const n = this._cloneNode(await this.getNode(pointer.next));
3360
- n.prev = pointer.id;
3397
+ siblingNode.next = node.next;
3398
+ if (siblingNode.next) {
3399
+ const n = this._cloneNode(await this.getNode(siblingNode.next));
3400
+ n.prev = siblingNode.id;
3361
3401
  await this._updateNode(n);
3362
3402
  }
3363
3403
  }
3364
- pointer.values.push(...node.values);
3365
- if (!pointer.leaf) {
3366
- const keys = pointer.keys;
3404
+ siblingNode.values.push(...node.values);
3405
+ if (!siblingNode.leaf) {
3406
+ const keys = siblingNode.keys;
3367
3407
  for (const key2 of keys) {
3368
3408
  const node2 = this._cloneNode(await this.getNode(key2));
3369
- node2.parent = pointer.id;
3409
+ node2.parent = siblingNode.id;
3370
3410
  await this._updateNode(node2);
3371
3411
  }
3372
3412
  }
3373
3413
  this._deleteNode(node);
3374
- await this._updateNode(pointer);
3375
- await this._deleteEntry(this._cloneNode(await this.getNode(node.parent)), node.id);
3414
+ await this._updateNode(siblingNode);
3415
+ await this._deleteEntry(await this.getNode(node.parent), node.id);
3376
3416
  } else {
3377
3417
  if (isPredecessor) {
3378
3418
  let pointerPm;
3379
3419
  let pointerKm;
3380
3420
  if (!node.leaf) {
3381
- pointerPm = pointer.keys.splice(-1)[0];
3382
- pointerKm = pointer.values.splice(-1)[0];
3421
+ pointerPm = siblingNode.keys.splice(-1)[0];
3422
+ pointerKm = siblingNode.values.splice(-1)[0];
3383
3423
  node.keys = [pointerPm, ...node.keys];
3384
3424
  node.values = [guess, ...node.values];
3385
3425
  parentNode = this._cloneNode(await this.getNode(node.parent));
@@ -3389,8 +3429,8 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3389
3429
  await this._updateNode(parentNode);
3390
3430
  }
3391
3431
  } else {
3392
- pointerPm = pointer.keys.splice(-1)[0];
3393
- pointerKm = pointer.values.splice(-1)[0];
3432
+ pointerPm = siblingNode.keys.splice(-1)[0];
3433
+ pointerKm = siblingNode.values.splice(-1)[0];
3394
3434
  node.keys = [pointerPm, ...node.keys];
3395
3435
  node.values = [pointerKm, ...node.values];
3396
3436
  parentNode = this._cloneNode(await this.getNode(node.parent));
@@ -3401,40 +3441,40 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3401
3441
  }
3402
3442
  }
3403
3443
  await this._updateNode(node);
3404
- await this._updateNode(pointer);
3444
+ await this._updateNode(siblingNode);
3405
3445
  } else {
3406
3446
  let pointerP0;
3407
3447
  let pointerK0;
3408
3448
  if (!node.leaf) {
3409
- pointerP0 = pointer.keys.splice(0, 1)[0];
3410
- pointerK0 = pointer.values.splice(0, 1)[0];
3449
+ pointerP0 = siblingNode.keys.splice(0, 1)[0];
3450
+ pointerK0 = siblingNode.values.splice(0, 1)[0];
3411
3451
  node.keys = [...node.keys, pointerP0];
3412
3452
  node.values = [...node.values, guess];
3413
3453
  parentNode = this._cloneNode(await this.getNode(node.parent));
3414
- const pointerIndex = parentNode.keys.indexOf(pointer.id);
3454
+ const pointerIndex = parentNode.keys.indexOf(siblingNode.id);
3415
3455
  if (pointerIndex > 0) {
3416
3456
  parentNode.values[pointerIndex - 1] = pointerK0;
3417
3457
  await this._updateNode(parentNode);
3418
3458
  }
3419
3459
  } else {
3420
- pointerP0 = pointer.keys.splice(0, 1)[0];
3421
- pointerK0 = pointer.values.splice(0, 1)[0];
3460
+ pointerP0 = siblingNode.keys.splice(0, 1)[0];
3461
+ pointerK0 = siblingNode.values.splice(0, 1)[0];
3422
3462
  node.keys = [...node.keys, pointerP0];
3423
3463
  node.values = [...node.values, pointerK0];
3424
3464
  parentNode = this._cloneNode(await this.getNode(node.parent));
3425
- const pointerIndex = parentNode.keys.indexOf(pointer.id);
3465
+ const pointerIndex = parentNode.keys.indexOf(siblingNode.id);
3426
3466
  if (pointerIndex > 0) {
3427
- parentNode.values[pointerIndex - 1] = pointer.values[0];
3467
+ parentNode.values[pointerIndex - 1] = siblingNode.values[0];
3428
3468
  await this._updateNode(parentNode);
3429
3469
  }
3430
3470
  }
3431
3471
  await this._updateNode(node);
3432
- await this._updateNode(pointer);
3472
+ await this._updateNode(siblingNode);
3433
3473
  }
3434
- if (!pointer.leaf) {
3435
- for (const key2 of pointer.keys) {
3474
+ if (!siblingNode.leaf) {
3475
+ for (const key2 of siblingNode.keys) {
3436
3476
  const n = this._cloneNode(await this.getNode(key2));
3437
- n.parent = pointer.id;
3477
+ n.parent = siblingNode.id;
3438
3478
  await this._updateNode(n);
3439
3479
  }
3440
3480
  }
@@ -3454,16 +3494,14 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3454
3494
  }
3455
3495
  }
3456
3496
  } else {
3457
- await this._updateNode(node);
3497
+ await this._updateNode(this._cloneNode(node));
3458
3498
  }
3459
- await this._updateNode(node);
3460
3499
  return node;
3461
3500
  }
3462
3501
  async delete(key, value) {
3463
3502
  return this.writeLock(0, async () => {
3464
3503
  let node = await this.insertableNodeByPrimary(value);
3465
3504
  let found = false;
3466
- node = this._cloneNode(node);
3467
3505
  while (true) {
3468
3506
  let i = node.values.length;
3469
3507
  while (i--) {
@@ -3472,8 +3510,10 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3472
3510
  const keys = node.keys[i];
3473
3511
  const keyIndex = keys.indexOf(key);
3474
3512
  if (keyIndex !== -1) {
3475
- keys.splice(keyIndex, 1);
3476
- if (keys.length === 0) {
3513
+ node = this._cloneNode(node);
3514
+ const freshKeys = node.keys[i];
3515
+ freshKeys.splice(keyIndex, 1);
3516
+ if (freshKeys.length === 0) {
3477
3517
  node.keys.splice(i, 1);
3478
3518
  node.values.splice(i, 1);
3479
3519
  }