serializable-bptree 5.1.0 → 5.1.1

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.
@@ -27,16 +27,167 @@ var StringComparator = class extends ValueComparator {
27
27
  }
28
28
  };
29
29
 
30
- // src/utils/InvertedWeakMap.ts
30
+ // node_modules/cache-entanglement/dist/esm/index.mjs
31
+ var __create = Object.create;
32
+ var __defProp = Object.defineProperty;
33
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
34
+ var __getOwnPropNames = Object.getOwnPropertyNames;
35
+ var __getProtoOf = Object.getPrototypeOf;
36
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
37
+ var __commonJS = (cb, mod) => function __require() {
38
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
39
+ };
40
+ var __copyProps = (to, from, except, desc) => {
41
+ if (from && typeof from === "object" || typeof from === "function") {
42
+ for (let key of __getOwnPropNames(from))
43
+ if (!__hasOwnProp.call(to, key) && key !== except)
44
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
45
+ }
46
+ return to;
47
+ };
48
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
49
+ // If the importer is in node compatibility mode or this is not an ESM
50
+ // file that has been converted to a CommonJS file using a Babel-
51
+ // compatible transform (i.e. "__esModule" has not been set), then set
52
+ // "default" to the CommonJS "module.exports" for node compatibility.
53
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
54
+ mod
55
+ ));
56
+ var require_ms = __commonJS({
57
+ "node_modules/ms/index.js"(exports, module) {
58
+ var s = 1e3;
59
+ var m = s * 60;
60
+ var h = m * 60;
61
+ var d = h * 24;
62
+ var w = d * 7;
63
+ var y = d * 365.25;
64
+ module.exports = function(val, options) {
65
+ options = options || {};
66
+ var type = typeof val;
67
+ if (type === "string" && val.length > 0) {
68
+ return parse(val);
69
+ } else if (type === "number" && isFinite(val)) {
70
+ return options.long ? fmtLong(val) : fmtShort(val);
71
+ }
72
+ throw new Error(
73
+ "val is not a non-empty string or a valid number. val=" + JSON.stringify(val)
74
+ );
75
+ };
76
+ function parse(str) {
77
+ str = String(str);
78
+ if (str.length > 100) {
79
+ return;
80
+ }
81
+ var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
82
+ str
83
+ );
84
+ if (!match) {
85
+ return;
86
+ }
87
+ var n = parseFloat(match[1]);
88
+ var type = (match[2] || "ms").toLowerCase();
89
+ switch (type) {
90
+ case "years":
91
+ case "year":
92
+ case "yrs":
93
+ case "yr":
94
+ case "y":
95
+ return n * y;
96
+ case "weeks":
97
+ case "week":
98
+ case "w":
99
+ return n * w;
100
+ case "days":
101
+ case "day":
102
+ case "d":
103
+ return n * d;
104
+ case "hours":
105
+ case "hour":
106
+ case "hrs":
107
+ case "hr":
108
+ case "h":
109
+ return n * h;
110
+ case "minutes":
111
+ case "minute":
112
+ case "mins":
113
+ case "min":
114
+ case "m":
115
+ return n * m;
116
+ case "seconds":
117
+ case "second":
118
+ case "secs":
119
+ case "sec":
120
+ case "s":
121
+ return n * s;
122
+ case "milliseconds":
123
+ case "millisecond":
124
+ case "msecs":
125
+ case "msec":
126
+ case "ms":
127
+ return n;
128
+ default:
129
+ return void 0;
130
+ }
131
+ }
132
+ function fmtShort(ms2) {
133
+ var msAbs = Math.abs(ms2);
134
+ if (msAbs >= d) {
135
+ return Math.round(ms2 / d) + "d";
136
+ }
137
+ if (msAbs >= h) {
138
+ return Math.round(ms2 / h) + "h";
139
+ }
140
+ if (msAbs >= m) {
141
+ return Math.round(ms2 / m) + "m";
142
+ }
143
+ if (msAbs >= s) {
144
+ return Math.round(ms2 / s) + "s";
145
+ }
146
+ return ms2 + "ms";
147
+ }
148
+ function fmtLong(ms2) {
149
+ var msAbs = Math.abs(ms2);
150
+ if (msAbs >= d) {
151
+ return plural(ms2, msAbs, d, "day");
152
+ }
153
+ if (msAbs >= h) {
154
+ return plural(ms2, msAbs, h, "hour");
155
+ }
156
+ if (msAbs >= m) {
157
+ return plural(ms2, msAbs, m, "minute");
158
+ }
159
+ if (msAbs >= s) {
160
+ return plural(ms2, msAbs, s, "second");
161
+ }
162
+ return ms2 + " ms";
163
+ }
164
+ function plural(ms2, msAbs, n, name) {
165
+ var isPlural = msAbs >= n * 1.5;
166
+ return Math.round(ms2 / n) + " " + name + (isPlural ? "s" : "");
167
+ }
168
+ }
169
+ });
170
+ var import_ms = __toESM(require_ms());
31
171
  var InvertedWeakMap = class {
32
172
  _map;
173
+ _keepAlive;
174
+ _timeouts;
33
175
  _registry;
34
- constructor() {
176
+ _lifespan;
177
+ constructor(option) {
178
+ const { lifespan } = option;
179
+ this._lifespan = lifespan;
35
180
  this._map = /* @__PURE__ */ new Map();
36
- this._registry = new FinalizationRegistry((key) => this._map.delete(key));
181
+ this._keepAlive = /* @__PURE__ */ new Map();
182
+ this._timeouts = /* @__PURE__ */ new Map();
183
+ this._registry = new FinalizationRegistry((key) => {
184
+ this._stopExpire(key, true);
185
+ this._map.delete(key);
186
+ });
37
187
  }
38
188
  clear() {
39
- return this._map.clear();
189
+ this._keepAlive.clear();
190
+ this._map.clear();
40
191
  }
41
192
  delete(key) {
42
193
  const ref = this._map.get(key);
@@ -46,6 +197,8 @@ var InvertedWeakMap = class {
46
197
  this._registry.unregister(raw);
47
198
  }
48
199
  }
200
+ this._stopExpire(key, true);
201
+ this._keepAlive.delete(key);
49
202
  return this._map.delete(key);
50
203
  }
51
204
  get(key) {
@@ -57,8 +210,39 @@ var InvertedWeakMap = class {
57
210
  set(key, value) {
58
211
  this._map.set(key, new WeakRef(value));
59
212
  this._registry.register(value, key);
213
+ if (this._lifespan > 0) {
214
+ this._stopExpire(key, true);
215
+ this._startExpire(key, value);
216
+ }
60
217
  return this;
61
218
  }
219
+ extendExpire(key) {
220
+ if (!(this._lifespan > 0)) {
221
+ return;
222
+ }
223
+ if (!this._keepAlive.has(key)) {
224
+ return;
225
+ }
226
+ this._stopExpire(key, false);
227
+ this._startExpire(key, this._keepAlive.get(key));
228
+ }
229
+ _startExpire(key, value) {
230
+ this._keepAlive.set(key, value);
231
+ this._timeouts.set(key, setTimeout(() => {
232
+ this._keepAlive.delete(key);
233
+ }, this._lifespan));
234
+ }
235
+ _stopExpire(key, removeKeepAlive) {
236
+ if (!this._timeouts.has(key)) {
237
+ return;
238
+ }
239
+ const timeout = this._timeouts.get(key);
240
+ this._timeouts.delete(key);
241
+ clearTimeout(timeout);
242
+ if (removeKeepAlive) {
243
+ this._keepAlive.delete(key);
244
+ }
245
+ }
62
246
  get size() {
63
247
  return this._map.size;
64
248
  }
@@ -66,13 +250,226 @@ var InvertedWeakMap = class {
66
250
  return this._map.keys();
67
251
  }
68
252
  };
253
+ var CacheEntanglement = class {
254
+ creation;
255
+ beforeUpdateHook;
256
+ lifespan;
257
+ dependencies;
258
+ caches;
259
+ assignments;
260
+ parameters;
261
+ constructor(creation, option) {
262
+ option = option ?? {};
263
+ const {
264
+ dependencies,
265
+ lifespan,
266
+ beforeUpdateHook
267
+ } = option;
268
+ this.creation = creation;
269
+ this.beforeUpdateHook = beforeUpdateHook ?? (() => {
270
+ });
271
+ this.lifespan = this._normalizeMs(lifespan ?? 0);
272
+ this.assignments = [];
273
+ this.caches = new InvertedWeakMap({ lifespan: this.lifespan });
274
+ this.dependencies = dependencies ?? {};
275
+ this.parameters = {};
276
+ for (const name in this.dependencies) {
277
+ const dependency = this.dependencies[name];
278
+ if (!dependency.assignments.includes(this)) {
279
+ dependency.assignments.push(this);
280
+ }
281
+ }
282
+ }
283
+ _normalizeMs(time) {
284
+ if (typeof time === "string") {
285
+ return (0, import_ms.default)(time);
286
+ }
287
+ return time;
288
+ }
289
+ dependencyKey(key) {
290
+ const tokens = key.split("/");
291
+ tokens.pop();
292
+ return tokens.join("/");
293
+ }
294
+ /**
295
+ * Returns all keys stored in the instance.
296
+ */
297
+ keys() {
298
+ return this.caches.keys();
299
+ }
300
+ /**
301
+ * Deletes all cache values stored in the instance.
302
+ */
303
+ clear() {
304
+ for (const key of this.keys()) {
305
+ this.delete(key);
306
+ }
307
+ }
308
+ /**
309
+ * Checks if there is a cache value stored in the key within the instance.
310
+ * @param key The key to search.
311
+ */
312
+ exists(key) {
313
+ return this.caches.has(key);
314
+ }
315
+ /**
316
+ * Returns the cache value stored in the key within the instance. If the cached value is not present, an error is thrown.
317
+ * @param key The key to search.
318
+ */
319
+ get(key) {
320
+ if (!this.caches.has(key)) {
321
+ throw new Error(`Cache value not found: ${key}`);
322
+ }
323
+ return this.caches.get(key);
324
+ }
325
+ /**
326
+ * Deletes the cache value stored in the key within the instance.
327
+ * @param key The key to delete.
328
+ */
329
+ delete(key) {
330
+ this.caches.delete(key);
331
+ }
332
+ };
333
+ var CacheData = class _CacheData {
334
+ static StructuredClone = globalThis.structuredClone.bind(globalThis);
335
+ _value;
336
+ constructor(value) {
337
+ this._value = value;
338
+ }
339
+ /**
340
+ * This is cached data.
341
+ * It was generated at the time of caching, so there is a risk of modification if it's an object due to shallow copying.
342
+ * Therefore, if it's not a primitive type, please avoid using this value directly and use the `clone` method to use a copied version of the data.
343
+ */
344
+ get raw() {
345
+ return this._value;
346
+ }
347
+ /**
348
+ * The method returns a copied value of the cached data.
349
+ * You can pass a function as a parameter to copy the value. This parameter function should return the copied value.
350
+ *
351
+ * If no parameter is passed, it defaults to using Javascript's or \@ungap/structured-clone's `structuredClone` function to copy the value.
352
+ * If you prefer shallow copying instead of deep copying,
353
+ * you can use the default options `array-shallow-copy`, `object-shallow-copy` and `deep-copy`,
354
+ * which are replaced with functions to shallow copy arrays and objects, respectively. This is a syntactic sugar.
355
+ * @param strategy The function that returns the copied value.
356
+ * If you want to perform a shallow copy, simply pass the strings `array-shallow-copy` or `object-shallow-copy` for easy use.
357
+ * The default is `structuredClone`.
358
+ */
359
+ clone(strategy = "deep-copy") {
360
+ if (strategy && typeof strategy !== "string") {
361
+ return strategy(this.raw);
362
+ }
363
+ switch (strategy) {
364
+ case "array-shallow-copy":
365
+ return [].concat(this.raw);
366
+ case "object-shallow-copy":
367
+ return Object.assign({}, this.raw);
368
+ case "deep-copy":
369
+ default:
370
+ return _CacheData.StructuredClone(this.raw);
371
+ }
372
+ }
373
+ };
374
+ var CacheEntanglementSync = class extends CacheEntanglement {
375
+ constructor(creation, option) {
376
+ super(creation, option);
377
+ }
378
+ resolve(key, ...parameter) {
379
+ const resolved = {};
380
+ const dependencyKey = this.dependencyKey(key);
381
+ this.beforeUpdateHook(key, dependencyKey, ...parameter);
382
+ for (const name in this.dependencies) {
383
+ const dependency = this.dependencies[name];
384
+ if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
385
+ throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name}'.`, {
386
+ cause: {
387
+ from: this
388
+ }
389
+ });
390
+ }
391
+ const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
392
+ resolved[name] = dependencyValue;
393
+ }
394
+ this.parameters[key] = parameter;
395
+ const value = new CacheData(this.creation(key, resolved, ...parameter));
396
+ this.caches.set(key, value);
397
+ return value;
398
+ }
399
+ cache(key, ...parameter) {
400
+ if (!this.caches.has(key)) {
401
+ this.resolve(key, ...parameter);
402
+ } else {
403
+ this.caches.extendExpire(key);
404
+ }
405
+ return this.caches.get(key);
406
+ }
407
+ update(key, ...parameter) {
408
+ this.resolve(key, ...parameter);
409
+ for (const t of this.assignments) {
410
+ const instance = t;
411
+ for (const cacheKey of instance.caches.keys()) {
412
+ if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
413
+ instance.update(cacheKey, ...instance.parameters[cacheKey]);
414
+ }
415
+ }
416
+ }
417
+ return this.caches.get(key);
418
+ }
419
+ };
420
+ var CacheEntanglementAsync = class extends CacheEntanglement {
421
+ constructor(creation, option) {
422
+ super(creation, option);
423
+ }
424
+ async resolve(key, ...parameter) {
425
+ const resolved = {};
426
+ const dependencyKey = this.dependencyKey(key);
427
+ await this.beforeUpdateHook(key, dependencyKey, ...parameter);
428
+ for (const name in this.dependencies) {
429
+ const dependency = this.dependencies[name];
430
+ if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
431
+ throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name}'.`, {
432
+ cause: {
433
+ from: this
434
+ }
435
+ });
436
+ }
437
+ const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
438
+ resolved[name] = dependencyValue;
439
+ }
440
+ this.parameters[key] = parameter;
441
+ const value = new CacheData(await this.creation(key, resolved, ...parameter));
442
+ this.caches.set(key, value);
443
+ return value;
444
+ }
445
+ async cache(key, ...parameter) {
446
+ if (!this.caches.has(key)) {
447
+ await this.resolve(key, ...parameter);
448
+ } else {
449
+ this.caches.extendExpire(key);
450
+ }
451
+ return this.caches.get(key);
452
+ }
453
+ async update(key, ...parameter) {
454
+ await this.resolve(key, ...parameter);
455
+ for (const t of this.assignments) {
456
+ const instance = t;
457
+ for (const cacheKey of instance.caches.keys()) {
458
+ if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
459
+ await instance.update(cacheKey, ...instance.parameters[cacheKey]);
460
+ }
461
+ }
462
+ }
463
+ return this.caches.get(key);
464
+ }
465
+ };
69
466
 
70
467
  // src/base/BPTree.ts
71
468
  var BPTree = class {
72
469
  _cachedRegexp;
73
470
  strategy;
74
471
  comparator;
75
- nodes;
472
+ option;
76
473
  order;
77
474
  root;
78
475
  _strategyDirty;
@@ -90,12 +487,8 @@ var BPTree = class {
90
487
  like: (nv, v) => {
91
488
  const nodeValue = this.comparator.match(nv);
92
489
  const value = this.comparator.match(v);
93
- if (!this._cachedRegexp.has(value)) {
94
- const pattern = value.replace(/%/g, ".*").replace(/_/g, ".");
95
- const regexp2 = new RegExp(`^${pattern}$`, "i");
96
- this._cachedRegexp.set(value, regexp2);
97
- }
98
- const regexp = this._cachedRegexp.get(value);
490
+ const cache = this._cachedRegexp.cache(value);
491
+ const regexp = cache.raw;
99
492
  return regexp.test(nodeValue);
100
493
  }
101
494
  };
@@ -132,15 +525,24 @@ var BPTree = class {
132
525
  or: 1,
133
526
  like: 1
134
527
  };
135
- constructor(strategy, comparator) {
528
+ constructor(strategy, comparator, option) {
529
+ this.strategy = strategy;
530
+ this.comparator = comparator;
531
+ this.option = option ?? {};
136
532
  this._strategyDirty = false;
137
- this._cachedRegexp = new InvertedWeakMap();
138
533
  this._nodeCreateBuffer = /* @__PURE__ */ new Map();
139
534
  this._nodeUpdateBuffer = /* @__PURE__ */ new Map();
140
535
  this._nodeDeleteBuffer = /* @__PURE__ */ new Map();
141
- this.nodes = new InvertedWeakMap();
142
- this.strategy = strategy;
143
- this.comparator = comparator;
536
+ this._cachedRegexp = this._createCachedRegexp();
537
+ }
538
+ _createCachedRegexp() {
539
+ return new CacheEntanglementSync((key) => {
540
+ const pattern = key.replace(/%/g, ".*").replace(/_/g, ".");
541
+ const regexp = new RegExp(`^${pattern}$`, "i");
542
+ return regexp;
543
+ }, {
544
+ lifespan: this.option.lifespan ?? "3m"
545
+ });
144
546
  }
145
547
  ensureValues(v) {
146
548
  if (!Array.isArray(v)) {
@@ -214,12 +616,28 @@ var BPTree = class {
214
616
  getHeadData() {
215
617
  return this.strategy.head.data;
216
618
  }
619
+ /**
620
+ * Clears all cached nodes.
621
+ * This method is useful for freeing up memory when the tree is no longer needed.
622
+ */
623
+ clear() {
624
+ this._cachedRegexp.clear();
625
+ this.nodes.clear();
626
+ }
217
627
  };
218
628
 
219
629
  // src/BPTreeSync.ts
220
630
  var BPTreeSync = class extends BPTree {
221
- constructor(strategy, comparator) {
222
- super(strategy, comparator);
631
+ constructor(strategy, comparator, option) {
632
+ super(strategy, comparator, option);
633
+ this.nodes = this._createCachedNode();
634
+ }
635
+ _createCachedNode() {
636
+ return new CacheEntanglementSync((key) => {
637
+ return this.strategy.read(key);
638
+ }, {
639
+ lifespan: this.option.lifespan ?? "3m"
640
+ });
223
641
  }
224
642
  getPairsRightToLeft(value, startNode, endNode, comparator) {
225
643
  const pairs = [];
@@ -304,7 +722,7 @@ var BPTreeSync = class extends BPTree {
304
722
  next,
305
723
  prev
306
724
  };
307
- this.nodes.set(id, node);
725
+ this._nodeCreateBuffer.set(id, node);
308
726
  return node;
309
727
  }
310
728
  _deleteEntry(node, key, value) {
@@ -518,7 +936,6 @@ var BPTreeSync = class extends BPTree {
518
936
  this.strategy.head.root = root.id;
519
937
  node.parent = root.id;
520
938
  pointer.parent = root.id;
521
- this.bufferForNodeCreate(root);
522
939
  this.bufferForNodeUpdate(node);
523
940
  this.bufferForNodeUpdate(pointer);
524
941
  return;
@@ -554,7 +971,6 @@ var BPTreeSync = class extends BPTree {
554
971
  this.bufferForNodeUpdate(node2);
555
972
  }
556
973
  this._insertInParent(parentNode, midValue, parentPointer);
557
- this.bufferForNodeCreate(parentPointer);
558
974
  this.bufferForNodeUpdate(parentNode);
559
975
  }
560
976
  }
@@ -566,7 +982,6 @@ var BPTreeSync = class extends BPTree {
566
982
  this.order = this.strategy.order;
567
983
  this.root = this._createNode(true, [], [], true);
568
984
  this.strategy.head.root = this.root.id;
569
- this.bufferForNodeCreate(this.root);
570
985
  this.commitHeadBuffer();
571
986
  this.commitNodeCreateBuffer();
572
987
  } else {
@@ -580,10 +995,11 @@ var BPTreeSync = class extends BPTree {
580
995
  }
581
996
  }
582
997
  getNode(id) {
583
- if (!this.nodes.has(id)) {
584
- this.nodes.set(id, this.strategy.read(id));
998
+ if (this._nodeCreateBuffer.has(id)) {
999
+ return this._nodeCreateBuffer.get(id);
585
1000
  }
586
- return this.nodes.get(id);
1001
+ const cache = this.nodes.cache(id);
1002
+ return cache.raw;
587
1003
  }
588
1004
  insertableNode(value) {
589
1005
  let node = this.root;
@@ -739,7 +1155,6 @@ var BPTreeSync = class extends BPTree {
739
1155
  this.bufferForNodeUpdate(node);
740
1156
  }
741
1157
  this._insertInParent(before, after.values[0], after);
742
- this.bufferForNodeCreate(after);
743
1158
  this.bufferForNodeUpdate(before);
744
1159
  }
745
1160
  this.commitHeadBuffer();
@@ -805,8 +1220,16 @@ var BPTreeSync = class extends BPTree {
805
1220
 
806
1221
  // src/BPTreeAsync.ts
807
1222
  var BPTreeAsync = class extends BPTree {
808
- constructor(strategy, comparator) {
809
- super(strategy, comparator);
1223
+ constructor(strategy, comparator, option) {
1224
+ super(strategy, comparator, option);
1225
+ this.nodes = this._createCachedNode();
1226
+ }
1227
+ _createCachedNode() {
1228
+ return new CacheEntanglementAsync(async (key) => {
1229
+ return await this.strategy.read(key);
1230
+ }, {
1231
+ lifespan: this.option.lifespan ?? "3m"
1232
+ });
810
1233
  }
811
1234
  async getPairsRightToLeft(value, startNode, endNode, comparator) {
812
1235
  const pairs = [];
@@ -891,7 +1314,7 @@ var BPTreeAsync = class extends BPTree {
891
1314
  next,
892
1315
  prev
893
1316
  };
894
- this.nodes.set(id, node);
1317
+ this._nodeCreateBuffer.set(id, node);
895
1318
  return node;
896
1319
  }
897
1320
  async _deleteEntry(node, key, value) {
@@ -1105,7 +1528,6 @@ var BPTreeAsync = class extends BPTree {
1105
1528
  this.strategy.head.root = root.id;
1106
1529
  node.parent = root.id;
1107
1530
  pointer.parent = root.id;
1108
- this.bufferForNodeCreate(root);
1109
1531
  this.bufferForNodeUpdate(node);
1110
1532
  this.bufferForNodeUpdate(pointer);
1111
1533
  return;
@@ -1141,7 +1563,6 @@ var BPTreeAsync = class extends BPTree {
1141
1563
  this.bufferForNodeUpdate(node2);
1142
1564
  }
1143
1565
  await this._insertInParent(parentNode, midValue, parentPointer);
1144
- this.bufferForNodeCreate(parentPointer);
1145
1566
  this.bufferForNodeUpdate(parentNode);
1146
1567
  }
1147
1568
  }
@@ -1153,7 +1574,6 @@ var BPTreeAsync = class extends BPTree {
1153
1574
  this.order = this.strategy.order;
1154
1575
  this.root = await this._createNode(true, [], [], true);
1155
1576
  this.strategy.head.root = this.root.id;
1156
- this.bufferForNodeCreate(this.root);
1157
1577
  await this.commitHeadBuffer();
1158
1578
  await this.commitNodeCreateBuffer();
1159
1579
  } else {
@@ -1167,10 +1587,11 @@ var BPTreeAsync = class extends BPTree {
1167
1587
  }
1168
1588
  }
1169
1589
  async getNode(id) {
1170
- if (!this.nodes.has(id)) {
1171
- this.nodes.set(id, await this.strategy.read(id));
1590
+ if (this._nodeCreateBuffer.has(id)) {
1591
+ return this._nodeCreateBuffer.get(id);
1172
1592
  }
1173
- return this.nodes.get(id);
1593
+ const cache = await this.nodes.cache(id);
1594
+ return cache.raw;
1174
1595
  }
1175
1596
  async insertableNode(value) {
1176
1597
  let node = this.root;
@@ -1326,7 +1747,6 @@ var BPTreeAsync = class extends BPTree {
1326
1747
  this.bufferForNodeUpdate(node);
1327
1748
  }
1328
1749
  await this._insertInParent(before, after.values[0], after);
1329
- this.bufferForNodeCreate(after);
1330
1750
  this.bufferForNodeUpdate(before);
1331
1751
  }
1332
1752
  await this.commitHeadBuffer();
@@ -1,10 +1,12 @@
1
- import { BPTree, BPTreeCondition, BPTreeLeafNode, BPTreePair, BPTreeNodeKey, BPTreeUnknownNode } from './base/BPTree';
1
+ import { BPTree, BPTreeCondition, BPTreeLeafNode, BPTreePair, BPTreeNodeKey, BPTreeUnknownNode, BPTreeConstructorOption } from './base/BPTree';
2
2
  import { SerializeStrategyAsync } from './SerializeStrategyAsync';
3
3
  import { ValueComparator } from './base/ValueComparator';
4
4
  import { SerializableData } from './base/SerializeStrategy';
5
5
  export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
6
6
  protected readonly strategy: SerializeStrategyAsync<K, V>;
7
- constructor(strategy: SerializeStrategyAsync<K, V>, comparator: ValueComparator<V>);
7
+ protected readonly nodes: ReturnType<BPTreeAsync<K, V>['_createCachedNode']>;
8
+ constructor(strategy: SerializeStrategyAsync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
9
+ private _createCachedNode;
8
10
  protected getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): Promise<BPTreePair<K, V>>;
9
11
  protected getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): Promise<BPTreePair<K, V>>;
10
12
  protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): Promise<BPTreePair<K, V>>;
@@ -1,10 +1,12 @@
1
- import { BPTree, BPTreeCondition, BPTreeLeafNode, BPTreePair, BPTreeNodeKey, BPTreeUnknownNode } from './base/BPTree';
1
+ import { BPTree, BPTreeCondition, BPTreeLeafNode, BPTreePair, BPTreeNodeKey, BPTreeUnknownNode, BPTreeConstructorOption } from './base/BPTree';
2
2
  import { SerializeStrategySync } from './SerializeStrategySync';
3
3
  import { ValueComparator } from './base/ValueComparator';
4
4
  import { SerializableData } from './base/SerializeStrategy';
5
5
  export declare class BPTreeSync<K, V> extends BPTree<K, V> {
6
6
  protected readonly strategy: SerializeStrategySync<K, V>;
7
- constructor(strategy: SerializeStrategySync<K, V>, comparator: ValueComparator<V>);
7
+ protected readonly nodes: ReturnType<BPTreeSync<K, V>['_createCachedNode']>;
8
+ constructor(strategy: SerializeStrategySync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
9
+ private _createCachedNode;
8
10
  protected getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>;
9
11
  protected getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>;
10
12
  protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): BPTreePair<K, V>;