serializable-bptree 6.2.1 → 6.2.3

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.
@@ -472,7 +472,7 @@ var BPTree = class _BPTree {
472
472
  comparator;
473
473
  option;
474
474
  order;
475
- root;
475
+ rootId;
476
476
  _strategyDirty;
477
477
  _nodeCreateBuffer;
478
478
  _nodeUpdateBuffer;
@@ -501,13 +501,13 @@ var BPTree = class _BPTree {
501
501
  }
502
502
  };
503
503
  verifierStartNode = {
504
- gt: (v) => this.insertableNode(v),
505
- gte: (v) => this.insertableNode(v),
506
- lt: (v) => this.insertableNode(v),
507
- lte: (v) => this.insertableNode(v),
508
- equal: (v) => this.insertableNode(v),
504
+ gt: (v) => this.insertableNodeByPrimary(v),
505
+ gte: (v) => this.insertableNodeByPrimary(v),
506
+ lt: (v) => this.insertableNodeByPrimary(v),
507
+ lte: (v) => this.insertableRightestNodeByPrimary(v),
508
+ equal: (v) => this.insertableNodeByPrimary(v),
509
509
  notEqual: (v) => this.leftestNode(),
510
- or: (v) => this.insertableNode(this.lowestValue(this.ensureValues(v))),
510
+ or: (v) => this.insertableNodeByPrimary(this.lowestPrimaryValue(this.ensureValues(v))),
511
511
  primaryGt: (v) => this.insertableNodeByPrimary(v),
512
512
  primaryGte: (v) => this.insertableNodeByPrimary(v),
513
513
  primaryLt: (v) => this.insertableNodeByPrimary(v),
@@ -532,7 +532,7 @@ var BPTree = class _BPTree {
532
532
  primaryGte: (v) => null,
533
533
  primaryLt: (v) => null,
534
534
  primaryLte: (v) => null,
535
- primaryEqual: (v) => null,
535
+ primaryEqual: (v) => this.insertableRightestEndNodeByPrimary(v),
536
536
  primaryNotEqual: (v) => null,
537
537
  primaryOr: (v) => this.insertableRightestEndNodeByPrimary(
538
538
  this.highestPrimaryValue(this.ensureValues(v))
@@ -726,19 +726,19 @@ var BPTree = class _BPTree {
726
726
  }
727
727
  }
728
728
  bufferForNodeCreate(node) {
729
- if (node.id === this.root.id) {
729
+ if (node.id === this.rootId) {
730
730
  this._strategyDirty = true;
731
731
  }
732
732
  this._nodeCreateBuffer.set(node.id, node);
733
733
  }
734
734
  bufferForNodeUpdate(node) {
735
- if (node.id === this.root.id) {
735
+ if (node.id === this.rootId) {
736
736
  this._strategyDirty = true;
737
737
  }
738
738
  this._nodeUpdateBuffer.set(node.id, node);
739
739
  }
740
740
  bufferForNodeDelete(node) {
741
- if (node.id === this.root.id) {
741
+ if (node.id === this.rootId) {
742
742
  this._strategyDirty = true;
743
743
  }
744
744
  this._nodeDeleteBuffer.set(node.id, node);
@@ -871,16 +871,18 @@ var BPTreeSync = class extends BPTree {
871
871
  }
872
872
  }
873
873
  }
874
- if (this.root.id === node.id && node.keys.length === 1) {
874
+ if (this.rootId === node.id && node.keys.length === 1) {
875
875
  const keys = node.keys;
876
- this.bufferForNodeDelete(this.root);
877
- this.root = this.getNode(keys[0]);
878
- this.root.parent = null;
879
- this.strategy.head.root = this.root.id;
880
- this.bufferForNodeUpdate(this.root);
876
+ this.bufferForNodeDelete(node);
877
+ const newRoot = this.getNode(keys[0]);
878
+ this.rootId = newRoot.id;
879
+ newRoot.parent = null;
880
+ this.strategy.head.root = this.rootId;
881
+ this.bufferForNodeUpdate(newRoot);
881
882
  return;
882
- } else if (this.root.id === node.id) {
883
- this.bufferForNodeUpdate(this.root);
883
+ } else if (this.rootId === node.id) {
884
+ const root = this.getNode(this.rootId);
885
+ this.bufferForNodeUpdate(root);
884
886
  return;
885
887
  } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
886
888
  if (node.parent === null) {
@@ -1060,9 +1062,9 @@ var BPTreeSync = class extends BPTree {
1060
1062
  }
1061
1063
  }
1062
1064
  _insertInParent(node, value, pointer) {
1063
- if (this.root.id === node.id) {
1065
+ if (this.rootId === node.id) {
1064
1066
  const root = this._createNode(false, [node.id, pointer.id], [value]);
1065
- this.root = root;
1067
+ this.rootId = root.id;
1066
1068
  this.strategy.head.root = root.id;
1067
1069
  node.parent = root.id;
1068
1070
  pointer.parent = root.id;
@@ -1131,15 +1133,16 @@ var BPTreeSync = class extends BPTree {
1131
1133
  const head = this.strategy.readHead();
1132
1134
  if (head === null) {
1133
1135
  this.order = this.strategy.order;
1134
- this.root = this._createNode(true, [], [], true);
1135
- this.strategy.head.root = this.root.id;
1136
+ const root = this._createNode(true, [], [], true);
1137
+ this.rootId = root.id;
1138
+ this.strategy.head.root = this.rootId;
1136
1139
  this.commitHeadBuffer();
1137
1140
  this.commitNodeCreateBuffer();
1138
1141
  } else {
1139
1142
  const { root, order } = head;
1140
1143
  this.strategy.head = head;
1141
1144
  this.order = order;
1142
- this.root = this.getNode(root);
1145
+ this.rootId = root;
1143
1146
  }
1144
1147
  if (this.order < 3) {
1145
1148
  throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
@@ -1156,7 +1159,7 @@ var BPTreeSync = class extends BPTree {
1156
1159
  return cache.raw;
1157
1160
  }
1158
1161
  insertableNode(value) {
1159
- let node = this.getNode(this.root.id);
1162
+ let node = this.getNode(this.rootId);
1160
1163
  while (!node.leaf) {
1161
1164
  for (let i = 0, len = node.values.length; i < len; i++) {
1162
1165
  const nValue = node.values[i];
@@ -1180,7 +1183,7 @@ var BPTreeSync = class extends BPTree {
1180
1183
  * This allows finding nodes by primary value only, ignoring unique identifiers.
1181
1184
  */
1182
1185
  insertableNodeByPrimary(value) {
1183
- let node = this.getNode(this.root.id);
1186
+ let node = this.getNode(this.rootId);
1184
1187
  while (!node.leaf) {
1185
1188
  for (let i = 0, len = node.values.length; i < len; i++) {
1186
1189
  const nValue = node.values[i];
@@ -1200,7 +1203,7 @@ var BPTreeSync = class extends BPTree {
1200
1203
  return node;
1201
1204
  }
1202
1205
  insertableRightestNodeByPrimary(value) {
1203
- let node = this.getNode(this.root.id);
1206
+ let node = this.getNode(this.rootId);
1204
1207
  while (!node.leaf) {
1205
1208
  for (let i = 0, len = node.values.length; i < len; i++) {
1206
1209
  const nValue = node.values[i];
@@ -1244,7 +1247,7 @@ var BPTreeSync = class extends BPTree {
1244
1247
  return this.getNode(guessNode);
1245
1248
  }
1246
1249
  leftestNode() {
1247
- let node = this.root;
1250
+ let node = this.getNode(this.rootId);
1248
1251
  while (!node.leaf) {
1249
1252
  const keys = node.keys;
1250
1253
  node = this.getNode(keys[0]);
@@ -1252,7 +1255,7 @@ var BPTreeSync = class extends BPTree {
1252
1255
  return node;
1253
1256
  }
1254
1257
  rightestNode() {
1255
- let node = this.root;
1258
+ let node = this.getNode(this.rootId);
1256
1259
  while (!node.leaf) {
1257
1260
  const keys = node.keys;
1258
1261
  node = this.getNode(keys[keys.length - 1]);
@@ -1265,6 +1268,9 @@ var BPTreeSync = class extends BPTree {
1265
1268
  }
1266
1269
  this._strategyDirty = false;
1267
1270
  this.strategy.writeHead(this.strategy.head);
1271
+ if (this.strategy.head.root) {
1272
+ this.nodes.delete(this.strategy.head.root);
1273
+ }
1268
1274
  }
1269
1275
  commitNodeCreateBuffer() {
1270
1276
  for (const node of this._nodeCreateBuffer.values()) {
@@ -1275,12 +1281,14 @@ var BPTreeSync = class extends BPTree {
1275
1281
  commitNodeUpdateBuffer() {
1276
1282
  for (const node of this._nodeUpdateBuffer.values()) {
1277
1283
  this.strategy.write(node.id, node);
1284
+ this.nodes.delete(node.id);
1278
1285
  }
1279
1286
  this._nodeUpdateBuffer.clear();
1280
1287
  }
1281
1288
  commitNodeDeleteBuffer() {
1282
1289
  for (const node of this._nodeDeleteBuffer.values()) {
1283
1290
  this.strategy.delete(node.id);
1291
+ this.nodes.delete(node.id);
1284
1292
  }
1285
1293
  this._nodeDeleteBuffer.clear();
1286
1294
  }
@@ -1424,7 +1432,7 @@ var BPTreeSync = class extends BPTree {
1424
1432
  if (keys.length > 1) {
1425
1433
  keys.splice(keys.indexOf(key), 1);
1426
1434
  this.bufferForNodeUpdate(node);
1427
- } else if (node.id === this.root.id) {
1435
+ } else if (node.id === this.rootId) {
1428
1436
  node.values.splice(i, 1);
1429
1437
  node.keys.splice(i, 1);
1430
1438
  this.bufferForNodeUpdate(node);
@@ -1858,16 +1866,18 @@ var BPTreeAsync = class extends BPTree {
1858
1866
  }
1859
1867
  }
1860
1868
  }
1861
- if (this.root.id === node.id && node.keys.length === 1) {
1869
+ if (this.rootId === node.id && node.keys.length === 1) {
1862
1870
  const keys = node.keys;
1863
- this.bufferForNodeDelete(this.root);
1864
- this.root = await this.getNode(keys[0]);
1865
- this.root.parent = null;
1866
- this.strategy.head.root = this.root.id;
1867
- this.bufferForNodeUpdate(this.root);
1871
+ this.bufferForNodeDelete(node);
1872
+ const newRoot = await this.getNode(keys[0]);
1873
+ this.rootId = newRoot.id;
1874
+ newRoot.parent = null;
1875
+ this.strategy.head.root = this.rootId;
1876
+ this.bufferForNodeUpdate(newRoot);
1868
1877
  return;
1869
- } else if (this.root.id === node.id) {
1870
- this.bufferForNodeUpdate(this.root);
1878
+ } else if (this.rootId === node.id) {
1879
+ const root = await this.getNode(this.rootId);
1880
+ this.bufferForNodeUpdate(root);
1871
1881
  return;
1872
1882
  } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
1873
1883
  if (node.parent === null) {
@@ -2047,9 +2057,9 @@ var BPTreeAsync = class extends BPTree {
2047
2057
  }
2048
2058
  }
2049
2059
  async _insertInParent(node, value, pointer) {
2050
- if (this.root.id === node.id) {
2060
+ if (this.rootId === node.id) {
2051
2061
  const root = await this._createNode(false, [node.id, pointer.id], [value]);
2052
- this.root = root;
2062
+ this.rootId = root.id;
2053
2063
  this.strategy.head.root = root.id;
2054
2064
  node.parent = root.id;
2055
2065
  pointer.parent = root.id;
@@ -2118,15 +2128,16 @@ var BPTreeAsync = class extends BPTree {
2118
2128
  const head = await this.strategy.readHead();
2119
2129
  if (head === null) {
2120
2130
  this.order = this.strategy.order;
2121
- this.root = await this._createNode(true, [], [], true);
2122
- this.strategy.head.root = this.root.id;
2131
+ const root = await this._createNode(true, [], [], true);
2132
+ this.rootId = root.id;
2133
+ this.strategy.head.root = this.rootId;
2123
2134
  await this.commitHeadBuffer();
2124
2135
  await this.commitNodeCreateBuffer();
2125
2136
  } else {
2126
2137
  const { root, order } = head;
2127
2138
  this.strategy.head = head;
2128
2139
  this.order = order;
2129
- this.root = await this.getNode(root);
2140
+ this.rootId = root;
2130
2141
  }
2131
2142
  if (this.order < 3) {
2132
2143
  throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
@@ -2143,7 +2154,7 @@ var BPTreeAsync = class extends BPTree {
2143
2154
  return cache.raw;
2144
2155
  }
2145
2156
  async insertableNode(value) {
2146
- let node = await this.getNode(this.root.id);
2157
+ let node = await this.getNode(this.rootId);
2147
2158
  while (!node.leaf) {
2148
2159
  for (let i = 0, len = node.values.length; i < len; i++) {
2149
2160
  const nValue = node.values[i];
@@ -2167,7 +2178,7 @@ var BPTreeAsync = class extends BPTree {
2167
2178
  * This allows finding nodes by primary value only, ignoring unique identifiers.
2168
2179
  */
2169
2180
  async insertableNodeByPrimary(value) {
2170
- let node = await this.getNode(this.root.id);
2181
+ let node = await this.getNode(this.rootId);
2171
2182
  while (!node.leaf) {
2172
2183
  for (let i = 0, len = node.values.length; i < len; i++) {
2173
2184
  const nValue = node.values[i];
@@ -2187,7 +2198,7 @@ var BPTreeAsync = class extends BPTree {
2187
2198
  return node;
2188
2199
  }
2189
2200
  async insertableRightestNodeByPrimary(value) {
2190
- let node = await this.getNode(this.root.id);
2201
+ let node = await this.getNode(this.rootId);
2191
2202
  while (!node.leaf) {
2192
2203
  for (let i = 0, len = node.values.length; i < len; i++) {
2193
2204
  const nValue = node.values[i];
@@ -2231,7 +2242,7 @@ var BPTreeAsync = class extends BPTree {
2231
2242
  return await this.getNode(guessNode);
2232
2243
  }
2233
2244
  async leftestNode() {
2234
- let node = this.root;
2245
+ let node = await this.getNode(this.rootId);
2235
2246
  while (!node.leaf) {
2236
2247
  const keys = node.keys;
2237
2248
  node = await this.getNode(keys[0]);
@@ -2239,7 +2250,7 @@ var BPTreeAsync = class extends BPTree {
2239
2250
  return node;
2240
2251
  }
2241
2252
  async rightestNode() {
2242
- let node = this.root;
2253
+ let node = await this.getNode(this.rootId);
2243
2254
  while (!node.leaf) {
2244
2255
  const keys = node.keys;
2245
2256
  node = await this.getNode(keys[keys.length - 1]);
@@ -2252,6 +2263,9 @@ var BPTreeAsync = class extends BPTree {
2252
2263
  }
2253
2264
  this._strategyDirty = false;
2254
2265
  await this.strategy.writeHead(this.strategy.head);
2266
+ if (this.strategy.head.root) {
2267
+ this.nodes.delete(this.strategy.head.root);
2268
+ }
2255
2269
  }
2256
2270
  async commitNodeCreateBuffer() {
2257
2271
  for (const node of this._nodeCreateBuffer.values()) {
@@ -2262,12 +2276,14 @@ var BPTreeAsync = class extends BPTree {
2262
2276
  async commitNodeUpdateBuffer() {
2263
2277
  for (const node of this._nodeUpdateBuffer.values()) {
2264
2278
  await this.strategy.write(node.id, node);
2279
+ this.nodes.delete(node.id);
2265
2280
  }
2266
2281
  this._nodeUpdateBuffer.clear();
2267
2282
  }
2268
2283
  async commitNodeDeleteBuffer() {
2269
2284
  for (const node of this._nodeDeleteBuffer.values()) {
2270
2285
  await this.strategy.delete(node.id);
2286
+ this.nodes.delete(node.id);
2271
2287
  }
2272
2288
  this._nodeDeleteBuffer.clear();
2273
2289
  }
@@ -2420,7 +2436,7 @@ var BPTreeAsync = class extends BPTree {
2420
2436
  if (keys.length > 1) {
2421
2437
  keys.splice(keys.indexOf(key), 1);
2422
2438
  this.bufferForNodeUpdate(node);
2423
- } else if (node.id === this.root.id) {
2439
+ } else if (node.id === this.rootId) {
2424
2440
  node.values.splice(i, 1);
2425
2441
  node.keys.splice(i, 1);
2426
2442
  this.bufferForNodeUpdate(node);
@@ -438,7 +438,7 @@ var BPTree = class _BPTree {
438
438
  comparator;
439
439
  option;
440
440
  order;
441
- root;
441
+ rootId;
442
442
  _strategyDirty;
443
443
  _nodeCreateBuffer;
444
444
  _nodeUpdateBuffer;
@@ -467,13 +467,13 @@ var BPTree = class _BPTree {
467
467
  }
468
468
  };
469
469
  verifierStartNode = {
470
- gt: (v) => this.insertableNode(v),
471
- gte: (v) => this.insertableNode(v),
472
- lt: (v) => this.insertableNode(v),
473
- lte: (v) => this.insertableNode(v),
474
- equal: (v) => this.insertableNode(v),
470
+ gt: (v) => this.insertableNodeByPrimary(v),
471
+ gte: (v) => this.insertableNodeByPrimary(v),
472
+ lt: (v) => this.insertableNodeByPrimary(v),
473
+ lte: (v) => this.insertableRightestNodeByPrimary(v),
474
+ equal: (v) => this.insertableNodeByPrimary(v),
475
475
  notEqual: (v) => this.leftestNode(),
476
- or: (v) => this.insertableNode(this.lowestValue(this.ensureValues(v))),
476
+ or: (v) => this.insertableNodeByPrimary(this.lowestPrimaryValue(this.ensureValues(v))),
477
477
  primaryGt: (v) => this.insertableNodeByPrimary(v),
478
478
  primaryGte: (v) => this.insertableNodeByPrimary(v),
479
479
  primaryLt: (v) => this.insertableNodeByPrimary(v),
@@ -498,7 +498,7 @@ var BPTree = class _BPTree {
498
498
  primaryGte: (v) => null,
499
499
  primaryLt: (v) => null,
500
500
  primaryLte: (v) => null,
501
- primaryEqual: (v) => null,
501
+ primaryEqual: (v) => this.insertableRightestEndNodeByPrimary(v),
502
502
  primaryNotEqual: (v) => null,
503
503
  primaryOr: (v) => this.insertableRightestEndNodeByPrimary(
504
504
  this.highestPrimaryValue(this.ensureValues(v))
@@ -692,19 +692,19 @@ var BPTree = class _BPTree {
692
692
  }
693
693
  }
694
694
  bufferForNodeCreate(node) {
695
- if (node.id === this.root.id) {
695
+ if (node.id === this.rootId) {
696
696
  this._strategyDirty = true;
697
697
  }
698
698
  this._nodeCreateBuffer.set(node.id, node);
699
699
  }
700
700
  bufferForNodeUpdate(node) {
701
- if (node.id === this.root.id) {
701
+ if (node.id === this.rootId) {
702
702
  this._strategyDirty = true;
703
703
  }
704
704
  this._nodeUpdateBuffer.set(node.id, node);
705
705
  }
706
706
  bufferForNodeDelete(node) {
707
- if (node.id === this.root.id) {
707
+ if (node.id === this.rootId) {
708
708
  this._strategyDirty = true;
709
709
  }
710
710
  this._nodeDeleteBuffer.set(node.id, node);
@@ -837,16 +837,18 @@ var BPTreeSync = class extends BPTree {
837
837
  }
838
838
  }
839
839
  }
840
- if (this.root.id === node.id && node.keys.length === 1) {
840
+ if (this.rootId === node.id && node.keys.length === 1) {
841
841
  const keys = node.keys;
842
- this.bufferForNodeDelete(this.root);
843
- this.root = this.getNode(keys[0]);
844
- this.root.parent = null;
845
- this.strategy.head.root = this.root.id;
846
- this.bufferForNodeUpdate(this.root);
842
+ this.bufferForNodeDelete(node);
843
+ const newRoot = this.getNode(keys[0]);
844
+ this.rootId = newRoot.id;
845
+ newRoot.parent = null;
846
+ this.strategy.head.root = this.rootId;
847
+ this.bufferForNodeUpdate(newRoot);
847
848
  return;
848
- } else if (this.root.id === node.id) {
849
- this.bufferForNodeUpdate(this.root);
849
+ } else if (this.rootId === node.id) {
850
+ const root = this.getNode(this.rootId);
851
+ this.bufferForNodeUpdate(root);
850
852
  return;
851
853
  } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
852
854
  if (node.parent === null) {
@@ -1026,9 +1028,9 @@ var BPTreeSync = class extends BPTree {
1026
1028
  }
1027
1029
  }
1028
1030
  _insertInParent(node, value, pointer) {
1029
- if (this.root.id === node.id) {
1031
+ if (this.rootId === node.id) {
1030
1032
  const root = this._createNode(false, [node.id, pointer.id], [value]);
1031
- this.root = root;
1033
+ this.rootId = root.id;
1032
1034
  this.strategy.head.root = root.id;
1033
1035
  node.parent = root.id;
1034
1036
  pointer.parent = root.id;
@@ -1097,15 +1099,16 @@ var BPTreeSync = class extends BPTree {
1097
1099
  const head = this.strategy.readHead();
1098
1100
  if (head === null) {
1099
1101
  this.order = this.strategy.order;
1100
- this.root = this._createNode(true, [], [], true);
1101
- this.strategy.head.root = this.root.id;
1102
+ const root = this._createNode(true, [], [], true);
1103
+ this.rootId = root.id;
1104
+ this.strategy.head.root = this.rootId;
1102
1105
  this.commitHeadBuffer();
1103
1106
  this.commitNodeCreateBuffer();
1104
1107
  } else {
1105
1108
  const { root, order } = head;
1106
1109
  this.strategy.head = head;
1107
1110
  this.order = order;
1108
- this.root = this.getNode(root);
1111
+ this.rootId = root;
1109
1112
  }
1110
1113
  if (this.order < 3) {
1111
1114
  throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
@@ -1122,7 +1125,7 @@ var BPTreeSync = class extends BPTree {
1122
1125
  return cache.raw;
1123
1126
  }
1124
1127
  insertableNode(value) {
1125
- let node = this.getNode(this.root.id);
1128
+ let node = this.getNode(this.rootId);
1126
1129
  while (!node.leaf) {
1127
1130
  for (let i = 0, len = node.values.length; i < len; i++) {
1128
1131
  const nValue = node.values[i];
@@ -1146,7 +1149,7 @@ var BPTreeSync = class extends BPTree {
1146
1149
  * This allows finding nodes by primary value only, ignoring unique identifiers.
1147
1150
  */
1148
1151
  insertableNodeByPrimary(value) {
1149
- let node = this.getNode(this.root.id);
1152
+ let node = this.getNode(this.rootId);
1150
1153
  while (!node.leaf) {
1151
1154
  for (let i = 0, len = node.values.length; i < len; i++) {
1152
1155
  const nValue = node.values[i];
@@ -1166,7 +1169,7 @@ var BPTreeSync = class extends BPTree {
1166
1169
  return node;
1167
1170
  }
1168
1171
  insertableRightestNodeByPrimary(value) {
1169
- let node = this.getNode(this.root.id);
1172
+ let node = this.getNode(this.rootId);
1170
1173
  while (!node.leaf) {
1171
1174
  for (let i = 0, len = node.values.length; i < len; i++) {
1172
1175
  const nValue = node.values[i];
@@ -1210,7 +1213,7 @@ var BPTreeSync = class extends BPTree {
1210
1213
  return this.getNode(guessNode);
1211
1214
  }
1212
1215
  leftestNode() {
1213
- let node = this.root;
1216
+ let node = this.getNode(this.rootId);
1214
1217
  while (!node.leaf) {
1215
1218
  const keys = node.keys;
1216
1219
  node = this.getNode(keys[0]);
@@ -1218,7 +1221,7 @@ var BPTreeSync = class extends BPTree {
1218
1221
  return node;
1219
1222
  }
1220
1223
  rightestNode() {
1221
- let node = this.root;
1224
+ let node = this.getNode(this.rootId);
1222
1225
  while (!node.leaf) {
1223
1226
  const keys = node.keys;
1224
1227
  node = this.getNode(keys[keys.length - 1]);
@@ -1231,6 +1234,9 @@ var BPTreeSync = class extends BPTree {
1231
1234
  }
1232
1235
  this._strategyDirty = false;
1233
1236
  this.strategy.writeHead(this.strategy.head);
1237
+ if (this.strategy.head.root) {
1238
+ this.nodes.delete(this.strategy.head.root);
1239
+ }
1234
1240
  }
1235
1241
  commitNodeCreateBuffer() {
1236
1242
  for (const node of this._nodeCreateBuffer.values()) {
@@ -1241,12 +1247,14 @@ var BPTreeSync = class extends BPTree {
1241
1247
  commitNodeUpdateBuffer() {
1242
1248
  for (const node of this._nodeUpdateBuffer.values()) {
1243
1249
  this.strategy.write(node.id, node);
1250
+ this.nodes.delete(node.id);
1244
1251
  }
1245
1252
  this._nodeUpdateBuffer.clear();
1246
1253
  }
1247
1254
  commitNodeDeleteBuffer() {
1248
1255
  for (const node of this._nodeDeleteBuffer.values()) {
1249
1256
  this.strategy.delete(node.id);
1257
+ this.nodes.delete(node.id);
1250
1258
  }
1251
1259
  this._nodeDeleteBuffer.clear();
1252
1260
  }
@@ -1390,7 +1398,7 @@ var BPTreeSync = class extends BPTree {
1390
1398
  if (keys.length > 1) {
1391
1399
  keys.splice(keys.indexOf(key), 1);
1392
1400
  this.bufferForNodeUpdate(node);
1393
- } else if (node.id === this.root.id) {
1401
+ } else if (node.id === this.rootId) {
1394
1402
  node.values.splice(i, 1);
1395
1403
  node.keys.splice(i, 1);
1396
1404
  this.bufferForNodeUpdate(node);
@@ -1824,16 +1832,18 @@ var BPTreeAsync = class extends BPTree {
1824
1832
  }
1825
1833
  }
1826
1834
  }
1827
- if (this.root.id === node.id && node.keys.length === 1) {
1835
+ if (this.rootId === node.id && node.keys.length === 1) {
1828
1836
  const keys = node.keys;
1829
- this.bufferForNodeDelete(this.root);
1830
- this.root = await this.getNode(keys[0]);
1831
- this.root.parent = null;
1832
- this.strategy.head.root = this.root.id;
1833
- this.bufferForNodeUpdate(this.root);
1837
+ this.bufferForNodeDelete(node);
1838
+ const newRoot = await this.getNode(keys[0]);
1839
+ this.rootId = newRoot.id;
1840
+ newRoot.parent = null;
1841
+ this.strategy.head.root = this.rootId;
1842
+ this.bufferForNodeUpdate(newRoot);
1834
1843
  return;
1835
- } else if (this.root.id === node.id) {
1836
- this.bufferForNodeUpdate(this.root);
1844
+ } else if (this.rootId === node.id) {
1845
+ const root = await this.getNode(this.rootId);
1846
+ this.bufferForNodeUpdate(root);
1837
1847
  return;
1838
1848
  } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
1839
1849
  if (node.parent === null) {
@@ -2013,9 +2023,9 @@ var BPTreeAsync = class extends BPTree {
2013
2023
  }
2014
2024
  }
2015
2025
  async _insertInParent(node, value, pointer) {
2016
- if (this.root.id === node.id) {
2026
+ if (this.rootId === node.id) {
2017
2027
  const root = await this._createNode(false, [node.id, pointer.id], [value]);
2018
- this.root = root;
2028
+ this.rootId = root.id;
2019
2029
  this.strategy.head.root = root.id;
2020
2030
  node.parent = root.id;
2021
2031
  pointer.parent = root.id;
@@ -2084,15 +2094,16 @@ var BPTreeAsync = class extends BPTree {
2084
2094
  const head = await this.strategy.readHead();
2085
2095
  if (head === null) {
2086
2096
  this.order = this.strategy.order;
2087
- this.root = await this._createNode(true, [], [], true);
2088
- this.strategy.head.root = this.root.id;
2097
+ const root = await this._createNode(true, [], [], true);
2098
+ this.rootId = root.id;
2099
+ this.strategy.head.root = this.rootId;
2089
2100
  await this.commitHeadBuffer();
2090
2101
  await this.commitNodeCreateBuffer();
2091
2102
  } else {
2092
2103
  const { root, order } = head;
2093
2104
  this.strategy.head = head;
2094
2105
  this.order = order;
2095
- this.root = await this.getNode(root);
2106
+ this.rootId = root;
2096
2107
  }
2097
2108
  if (this.order < 3) {
2098
2109
  throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
@@ -2109,7 +2120,7 @@ var BPTreeAsync = class extends BPTree {
2109
2120
  return cache.raw;
2110
2121
  }
2111
2122
  async insertableNode(value) {
2112
- let node = await this.getNode(this.root.id);
2123
+ let node = await this.getNode(this.rootId);
2113
2124
  while (!node.leaf) {
2114
2125
  for (let i = 0, len = node.values.length; i < len; i++) {
2115
2126
  const nValue = node.values[i];
@@ -2133,7 +2144,7 @@ var BPTreeAsync = class extends BPTree {
2133
2144
  * This allows finding nodes by primary value only, ignoring unique identifiers.
2134
2145
  */
2135
2146
  async insertableNodeByPrimary(value) {
2136
- let node = await this.getNode(this.root.id);
2147
+ let node = await this.getNode(this.rootId);
2137
2148
  while (!node.leaf) {
2138
2149
  for (let i = 0, len = node.values.length; i < len; i++) {
2139
2150
  const nValue = node.values[i];
@@ -2153,7 +2164,7 @@ var BPTreeAsync = class extends BPTree {
2153
2164
  return node;
2154
2165
  }
2155
2166
  async insertableRightestNodeByPrimary(value) {
2156
- let node = await this.getNode(this.root.id);
2167
+ let node = await this.getNode(this.rootId);
2157
2168
  while (!node.leaf) {
2158
2169
  for (let i = 0, len = node.values.length; i < len; i++) {
2159
2170
  const nValue = node.values[i];
@@ -2197,7 +2208,7 @@ var BPTreeAsync = class extends BPTree {
2197
2208
  return await this.getNode(guessNode);
2198
2209
  }
2199
2210
  async leftestNode() {
2200
- let node = this.root;
2211
+ let node = await this.getNode(this.rootId);
2201
2212
  while (!node.leaf) {
2202
2213
  const keys = node.keys;
2203
2214
  node = await this.getNode(keys[0]);
@@ -2205,7 +2216,7 @@ var BPTreeAsync = class extends BPTree {
2205
2216
  return node;
2206
2217
  }
2207
2218
  async rightestNode() {
2208
- let node = this.root;
2219
+ let node = await this.getNode(this.rootId);
2209
2220
  while (!node.leaf) {
2210
2221
  const keys = node.keys;
2211
2222
  node = await this.getNode(keys[keys.length - 1]);
@@ -2218,6 +2229,9 @@ var BPTreeAsync = class extends BPTree {
2218
2229
  }
2219
2230
  this._strategyDirty = false;
2220
2231
  await this.strategy.writeHead(this.strategy.head);
2232
+ if (this.strategy.head.root) {
2233
+ this.nodes.delete(this.strategy.head.root);
2234
+ }
2221
2235
  }
2222
2236
  async commitNodeCreateBuffer() {
2223
2237
  for (const node of this._nodeCreateBuffer.values()) {
@@ -2228,12 +2242,14 @@ var BPTreeAsync = class extends BPTree {
2228
2242
  async commitNodeUpdateBuffer() {
2229
2243
  for (const node of this._nodeUpdateBuffer.values()) {
2230
2244
  await this.strategy.write(node.id, node);
2245
+ this.nodes.delete(node.id);
2231
2246
  }
2232
2247
  this._nodeUpdateBuffer.clear();
2233
2248
  }
2234
2249
  async commitNodeDeleteBuffer() {
2235
2250
  for (const node of this._nodeDeleteBuffer.values()) {
2236
2251
  await this.strategy.delete(node.id);
2252
+ this.nodes.delete(node.id);
2237
2253
  }
2238
2254
  this._nodeDeleteBuffer.clear();
2239
2255
  }
@@ -2386,7 +2402,7 @@ var BPTreeAsync = class extends BPTree {
2386
2402
  if (keys.length > 1) {
2387
2403
  keys.splice(keys.indexOf(key), 1);
2388
2404
  this.bufferForNodeUpdate(node);
2389
- } else if (node.id === this.root.id) {
2405
+ } else if (node.id === this.rootId) {
2390
2406
  node.values.splice(i, 1);
2391
2407
  node.keys.splice(i, 1);
2392
2408
  this.bufferForNodeUpdate(node);
@@ -80,7 +80,7 @@ export declare abstract class BPTree<K, V> {
80
80
  protected readonly comparator: ValueComparator<V>;
81
81
  protected readonly option: BPTreeConstructorOption;
82
82
  protected order: number;
83
- protected root: BPTreeUnknownNode<K, V>;
83
+ protected rootId: string;
84
84
  protected _strategyDirty: boolean;
85
85
  protected readonly _nodeCreateBuffer: Map<string, BPTreeUnknownNode<K, V>>;
86
86
  protected readonly _nodeUpdateBuffer: Map<string, BPTreeUnknownNode<K, V>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "6.2.1",
3
+ "version": "6.2.3",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "main": "./dist/cjs/index.cjs",