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.
- package/README.md +4 -0
- package/dist/cjs/index.cjs +456 -36
- package/dist/esm/index.mjs +456 -36
- package/dist/types/BPTreeAsync.d.ts +4 -2
- package/dist/types/BPTreeSync.d.ts +4 -2
- package/dist/types/base/BPTree.d.ts +21 -3
- package/package.json +5 -2
- package/dist/types/utils/InvertedWeakMap.d.ts +0 -12
package/README.md
CHANGED
|
@@ -64,6 +64,8 @@ tree.where({ gt: 1 }) // Map([{ key: 'c', value: 3 }])
|
|
|
64
64
|
tree.where({ lt: 2 }) // Map([{ key: 'a', value: 1 }])
|
|
65
65
|
tree.where({ gt: 0, lt: 4 }) // Map([{ key: 'a', value: 1 }, { key: 'c', value: 3 }])
|
|
66
66
|
tree.where({ or: [3, 1] }) // Map([{ key: 'a', value: 1 }, { key: 'c', value: 3 }])
|
|
67
|
+
|
|
68
|
+
tree.clear()
|
|
67
69
|
```
|
|
68
70
|
|
|
69
71
|
## Why use a `serializable-bptree`?
|
|
@@ -461,6 +463,8 @@ await tree.where({ equal: 1 }) // Map([{ key: 'a', value: 1 }])
|
|
|
461
463
|
await tree.where({ gt: 1 }) // Map([{ key: 'c', value: 3 }])
|
|
462
464
|
await tree.where({ lt: 2 }) // Map([{ key: 'a', value: 1 }])
|
|
463
465
|
await tree.where({ gt: 0, lt: 4 }) // Map([{ key: 'a', value: 1 }, { key: 'c', value: 3 }])
|
|
466
|
+
|
|
467
|
+
tree.clear()
|
|
464
468
|
```
|
|
465
469
|
|
|
466
470
|
The implementation method for asynchronous operations is not significantly different. The **-Async** suffix is used instead of the **-Sync** suffix in the **BPTree** and **SerializeStrategy** classes. The only difference is that the methods become asynchronous. The **ValueComparator** class and similar value comparators do not use asynchronous operations.
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -61,16 +61,167 @@ var StringComparator = class extends ValueComparator {
|
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
-
//
|
|
64
|
+
// node_modules/cache-entanglement/dist/esm/index.mjs
|
|
65
|
+
var __create = Object.create;
|
|
66
|
+
var __defProp2 = Object.defineProperty;
|
|
67
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
68
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
69
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
70
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
71
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
72
|
+
return mod || (0, cb[__getOwnPropNames2(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
73
|
+
};
|
|
74
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
75
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
76
|
+
for (let key of __getOwnPropNames2(from))
|
|
77
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
78
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
79
|
+
}
|
|
80
|
+
return to;
|
|
81
|
+
};
|
|
82
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps2(
|
|
83
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
84
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
85
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
86
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
87
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target,
|
|
88
|
+
mod
|
|
89
|
+
));
|
|
90
|
+
var require_ms = __commonJS({
|
|
91
|
+
"node_modules/ms/index.js"(exports, module2) {
|
|
92
|
+
var s = 1e3;
|
|
93
|
+
var m = s * 60;
|
|
94
|
+
var h = m * 60;
|
|
95
|
+
var d = h * 24;
|
|
96
|
+
var w = d * 7;
|
|
97
|
+
var y = d * 365.25;
|
|
98
|
+
module2.exports = function(val, options) {
|
|
99
|
+
options = options || {};
|
|
100
|
+
var type = typeof val;
|
|
101
|
+
if (type === "string" && val.length > 0) {
|
|
102
|
+
return parse(val);
|
|
103
|
+
} else if (type === "number" && isFinite(val)) {
|
|
104
|
+
return options.long ? fmtLong(val) : fmtShort(val);
|
|
105
|
+
}
|
|
106
|
+
throw new Error(
|
|
107
|
+
"val is not a non-empty string or a valid number. val=" + JSON.stringify(val)
|
|
108
|
+
);
|
|
109
|
+
};
|
|
110
|
+
function parse(str) {
|
|
111
|
+
str = String(str);
|
|
112
|
+
if (str.length > 100) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
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(
|
|
116
|
+
str
|
|
117
|
+
);
|
|
118
|
+
if (!match) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
var n = parseFloat(match[1]);
|
|
122
|
+
var type = (match[2] || "ms").toLowerCase();
|
|
123
|
+
switch (type) {
|
|
124
|
+
case "years":
|
|
125
|
+
case "year":
|
|
126
|
+
case "yrs":
|
|
127
|
+
case "yr":
|
|
128
|
+
case "y":
|
|
129
|
+
return n * y;
|
|
130
|
+
case "weeks":
|
|
131
|
+
case "week":
|
|
132
|
+
case "w":
|
|
133
|
+
return n * w;
|
|
134
|
+
case "days":
|
|
135
|
+
case "day":
|
|
136
|
+
case "d":
|
|
137
|
+
return n * d;
|
|
138
|
+
case "hours":
|
|
139
|
+
case "hour":
|
|
140
|
+
case "hrs":
|
|
141
|
+
case "hr":
|
|
142
|
+
case "h":
|
|
143
|
+
return n * h;
|
|
144
|
+
case "minutes":
|
|
145
|
+
case "minute":
|
|
146
|
+
case "mins":
|
|
147
|
+
case "min":
|
|
148
|
+
case "m":
|
|
149
|
+
return n * m;
|
|
150
|
+
case "seconds":
|
|
151
|
+
case "second":
|
|
152
|
+
case "secs":
|
|
153
|
+
case "sec":
|
|
154
|
+
case "s":
|
|
155
|
+
return n * s;
|
|
156
|
+
case "milliseconds":
|
|
157
|
+
case "millisecond":
|
|
158
|
+
case "msecs":
|
|
159
|
+
case "msec":
|
|
160
|
+
case "ms":
|
|
161
|
+
return n;
|
|
162
|
+
default:
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function fmtShort(ms2) {
|
|
167
|
+
var msAbs = Math.abs(ms2);
|
|
168
|
+
if (msAbs >= d) {
|
|
169
|
+
return Math.round(ms2 / d) + "d";
|
|
170
|
+
}
|
|
171
|
+
if (msAbs >= h) {
|
|
172
|
+
return Math.round(ms2 / h) + "h";
|
|
173
|
+
}
|
|
174
|
+
if (msAbs >= m) {
|
|
175
|
+
return Math.round(ms2 / m) + "m";
|
|
176
|
+
}
|
|
177
|
+
if (msAbs >= s) {
|
|
178
|
+
return Math.round(ms2 / s) + "s";
|
|
179
|
+
}
|
|
180
|
+
return ms2 + "ms";
|
|
181
|
+
}
|
|
182
|
+
function fmtLong(ms2) {
|
|
183
|
+
var msAbs = Math.abs(ms2);
|
|
184
|
+
if (msAbs >= d) {
|
|
185
|
+
return plural(ms2, msAbs, d, "day");
|
|
186
|
+
}
|
|
187
|
+
if (msAbs >= h) {
|
|
188
|
+
return plural(ms2, msAbs, h, "hour");
|
|
189
|
+
}
|
|
190
|
+
if (msAbs >= m) {
|
|
191
|
+
return plural(ms2, msAbs, m, "minute");
|
|
192
|
+
}
|
|
193
|
+
if (msAbs >= s) {
|
|
194
|
+
return plural(ms2, msAbs, s, "second");
|
|
195
|
+
}
|
|
196
|
+
return ms2 + " ms";
|
|
197
|
+
}
|
|
198
|
+
function plural(ms2, msAbs, n, name) {
|
|
199
|
+
var isPlural = msAbs >= n * 1.5;
|
|
200
|
+
return Math.round(ms2 / n) + " " + name + (isPlural ? "s" : "");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
var import_ms = __toESM(require_ms());
|
|
65
205
|
var InvertedWeakMap = class {
|
|
66
206
|
_map;
|
|
207
|
+
_keepAlive;
|
|
208
|
+
_timeouts;
|
|
67
209
|
_registry;
|
|
68
|
-
|
|
210
|
+
_lifespan;
|
|
211
|
+
constructor(option) {
|
|
212
|
+
const { lifespan } = option;
|
|
213
|
+
this._lifespan = lifespan;
|
|
69
214
|
this._map = /* @__PURE__ */ new Map();
|
|
70
|
-
this.
|
|
215
|
+
this._keepAlive = /* @__PURE__ */ new Map();
|
|
216
|
+
this._timeouts = /* @__PURE__ */ new Map();
|
|
217
|
+
this._registry = new FinalizationRegistry((key) => {
|
|
218
|
+
this._stopExpire(key, true);
|
|
219
|
+
this._map.delete(key);
|
|
220
|
+
});
|
|
71
221
|
}
|
|
72
222
|
clear() {
|
|
73
|
-
|
|
223
|
+
this._keepAlive.clear();
|
|
224
|
+
this._map.clear();
|
|
74
225
|
}
|
|
75
226
|
delete(key) {
|
|
76
227
|
const ref = this._map.get(key);
|
|
@@ -80,6 +231,8 @@ var InvertedWeakMap = class {
|
|
|
80
231
|
this._registry.unregister(raw);
|
|
81
232
|
}
|
|
82
233
|
}
|
|
234
|
+
this._stopExpire(key, true);
|
|
235
|
+
this._keepAlive.delete(key);
|
|
83
236
|
return this._map.delete(key);
|
|
84
237
|
}
|
|
85
238
|
get(key) {
|
|
@@ -91,8 +244,39 @@ var InvertedWeakMap = class {
|
|
|
91
244
|
set(key, value) {
|
|
92
245
|
this._map.set(key, new WeakRef(value));
|
|
93
246
|
this._registry.register(value, key);
|
|
247
|
+
if (this._lifespan > 0) {
|
|
248
|
+
this._stopExpire(key, true);
|
|
249
|
+
this._startExpire(key, value);
|
|
250
|
+
}
|
|
94
251
|
return this;
|
|
95
252
|
}
|
|
253
|
+
extendExpire(key) {
|
|
254
|
+
if (!(this._lifespan > 0)) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (!this._keepAlive.has(key)) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
this._stopExpire(key, false);
|
|
261
|
+
this._startExpire(key, this._keepAlive.get(key));
|
|
262
|
+
}
|
|
263
|
+
_startExpire(key, value) {
|
|
264
|
+
this._keepAlive.set(key, value);
|
|
265
|
+
this._timeouts.set(key, setTimeout(() => {
|
|
266
|
+
this._keepAlive.delete(key);
|
|
267
|
+
}, this._lifespan));
|
|
268
|
+
}
|
|
269
|
+
_stopExpire(key, removeKeepAlive) {
|
|
270
|
+
if (!this._timeouts.has(key)) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const timeout = this._timeouts.get(key);
|
|
274
|
+
this._timeouts.delete(key);
|
|
275
|
+
clearTimeout(timeout);
|
|
276
|
+
if (removeKeepAlive) {
|
|
277
|
+
this._keepAlive.delete(key);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
96
280
|
get size() {
|
|
97
281
|
return this._map.size;
|
|
98
282
|
}
|
|
@@ -100,13 +284,226 @@ var InvertedWeakMap = class {
|
|
|
100
284
|
return this._map.keys();
|
|
101
285
|
}
|
|
102
286
|
};
|
|
287
|
+
var CacheEntanglement = class {
|
|
288
|
+
creation;
|
|
289
|
+
beforeUpdateHook;
|
|
290
|
+
lifespan;
|
|
291
|
+
dependencies;
|
|
292
|
+
caches;
|
|
293
|
+
assignments;
|
|
294
|
+
parameters;
|
|
295
|
+
constructor(creation, option) {
|
|
296
|
+
option = option ?? {};
|
|
297
|
+
const {
|
|
298
|
+
dependencies,
|
|
299
|
+
lifespan,
|
|
300
|
+
beforeUpdateHook
|
|
301
|
+
} = option;
|
|
302
|
+
this.creation = creation;
|
|
303
|
+
this.beforeUpdateHook = beforeUpdateHook ?? (() => {
|
|
304
|
+
});
|
|
305
|
+
this.lifespan = this._normalizeMs(lifespan ?? 0);
|
|
306
|
+
this.assignments = [];
|
|
307
|
+
this.caches = new InvertedWeakMap({ lifespan: this.lifespan });
|
|
308
|
+
this.dependencies = dependencies ?? {};
|
|
309
|
+
this.parameters = {};
|
|
310
|
+
for (const name in this.dependencies) {
|
|
311
|
+
const dependency = this.dependencies[name];
|
|
312
|
+
if (!dependency.assignments.includes(this)) {
|
|
313
|
+
dependency.assignments.push(this);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
_normalizeMs(time) {
|
|
318
|
+
if (typeof time === "string") {
|
|
319
|
+
return (0, import_ms.default)(time);
|
|
320
|
+
}
|
|
321
|
+
return time;
|
|
322
|
+
}
|
|
323
|
+
dependencyKey(key) {
|
|
324
|
+
const tokens = key.split("/");
|
|
325
|
+
tokens.pop();
|
|
326
|
+
return tokens.join("/");
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Returns all keys stored in the instance.
|
|
330
|
+
*/
|
|
331
|
+
keys() {
|
|
332
|
+
return this.caches.keys();
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Deletes all cache values stored in the instance.
|
|
336
|
+
*/
|
|
337
|
+
clear() {
|
|
338
|
+
for (const key of this.keys()) {
|
|
339
|
+
this.delete(key);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Checks if there is a cache value stored in the key within the instance.
|
|
344
|
+
* @param key The key to search.
|
|
345
|
+
*/
|
|
346
|
+
exists(key) {
|
|
347
|
+
return this.caches.has(key);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Returns the cache value stored in the key within the instance. If the cached value is not present, an error is thrown.
|
|
351
|
+
* @param key The key to search.
|
|
352
|
+
*/
|
|
353
|
+
get(key) {
|
|
354
|
+
if (!this.caches.has(key)) {
|
|
355
|
+
throw new Error(`Cache value not found: ${key}`);
|
|
356
|
+
}
|
|
357
|
+
return this.caches.get(key);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Deletes the cache value stored in the key within the instance.
|
|
361
|
+
* @param key The key to delete.
|
|
362
|
+
*/
|
|
363
|
+
delete(key) {
|
|
364
|
+
this.caches.delete(key);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
var CacheData = class _CacheData {
|
|
368
|
+
static StructuredClone = globalThis.structuredClone.bind(globalThis);
|
|
369
|
+
_value;
|
|
370
|
+
constructor(value) {
|
|
371
|
+
this._value = value;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* This is cached data.
|
|
375
|
+
* It was generated at the time of caching, so there is a risk of modification if it's an object due to shallow copying.
|
|
376
|
+
* 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.
|
|
377
|
+
*/
|
|
378
|
+
get raw() {
|
|
379
|
+
return this._value;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* The method returns a copied value of the cached data.
|
|
383
|
+
* You can pass a function as a parameter to copy the value. This parameter function should return the copied value.
|
|
384
|
+
*
|
|
385
|
+
* If no parameter is passed, it defaults to using Javascript's or \@ungap/structured-clone's `structuredClone` function to copy the value.
|
|
386
|
+
* If you prefer shallow copying instead of deep copying,
|
|
387
|
+
* you can use the default options `array-shallow-copy`, `object-shallow-copy` and `deep-copy`,
|
|
388
|
+
* which are replaced with functions to shallow copy arrays and objects, respectively. This is a syntactic sugar.
|
|
389
|
+
* @param strategy The function that returns the copied value.
|
|
390
|
+
* If you want to perform a shallow copy, simply pass the strings `array-shallow-copy` or `object-shallow-copy` for easy use.
|
|
391
|
+
* The default is `structuredClone`.
|
|
392
|
+
*/
|
|
393
|
+
clone(strategy = "deep-copy") {
|
|
394
|
+
if (strategy && typeof strategy !== "string") {
|
|
395
|
+
return strategy(this.raw);
|
|
396
|
+
}
|
|
397
|
+
switch (strategy) {
|
|
398
|
+
case "array-shallow-copy":
|
|
399
|
+
return [].concat(this.raw);
|
|
400
|
+
case "object-shallow-copy":
|
|
401
|
+
return Object.assign({}, this.raw);
|
|
402
|
+
case "deep-copy":
|
|
403
|
+
default:
|
|
404
|
+
return _CacheData.StructuredClone(this.raw);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
var CacheEntanglementSync = class extends CacheEntanglement {
|
|
409
|
+
constructor(creation, option) {
|
|
410
|
+
super(creation, option);
|
|
411
|
+
}
|
|
412
|
+
resolve(key, ...parameter) {
|
|
413
|
+
const resolved = {};
|
|
414
|
+
const dependencyKey = this.dependencyKey(key);
|
|
415
|
+
this.beforeUpdateHook(key, dependencyKey, ...parameter);
|
|
416
|
+
for (const name in this.dependencies) {
|
|
417
|
+
const dependency = this.dependencies[name];
|
|
418
|
+
if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
|
|
419
|
+
throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name}'.`, {
|
|
420
|
+
cause: {
|
|
421
|
+
from: this
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
|
|
426
|
+
resolved[name] = dependencyValue;
|
|
427
|
+
}
|
|
428
|
+
this.parameters[key] = parameter;
|
|
429
|
+
const value = new CacheData(this.creation(key, resolved, ...parameter));
|
|
430
|
+
this.caches.set(key, value);
|
|
431
|
+
return value;
|
|
432
|
+
}
|
|
433
|
+
cache(key, ...parameter) {
|
|
434
|
+
if (!this.caches.has(key)) {
|
|
435
|
+
this.resolve(key, ...parameter);
|
|
436
|
+
} else {
|
|
437
|
+
this.caches.extendExpire(key);
|
|
438
|
+
}
|
|
439
|
+
return this.caches.get(key);
|
|
440
|
+
}
|
|
441
|
+
update(key, ...parameter) {
|
|
442
|
+
this.resolve(key, ...parameter);
|
|
443
|
+
for (const t of this.assignments) {
|
|
444
|
+
const instance = t;
|
|
445
|
+
for (const cacheKey of instance.caches.keys()) {
|
|
446
|
+
if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
|
|
447
|
+
instance.update(cacheKey, ...instance.parameters[cacheKey]);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return this.caches.get(key);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
var CacheEntanglementAsync = class extends CacheEntanglement {
|
|
455
|
+
constructor(creation, option) {
|
|
456
|
+
super(creation, option);
|
|
457
|
+
}
|
|
458
|
+
async resolve(key, ...parameter) {
|
|
459
|
+
const resolved = {};
|
|
460
|
+
const dependencyKey = this.dependencyKey(key);
|
|
461
|
+
await this.beforeUpdateHook(key, dependencyKey, ...parameter);
|
|
462
|
+
for (const name in this.dependencies) {
|
|
463
|
+
const dependency = this.dependencies[name];
|
|
464
|
+
if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
|
|
465
|
+
throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name}'.`, {
|
|
466
|
+
cause: {
|
|
467
|
+
from: this
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
|
|
472
|
+
resolved[name] = dependencyValue;
|
|
473
|
+
}
|
|
474
|
+
this.parameters[key] = parameter;
|
|
475
|
+
const value = new CacheData(await this.creation(key, resolved, ...parameter));
|
|
476
|
+
this.caches.set(key, value);
|
|
477
|
+
return value;
|
|
478
|
+
}
|
|
479
|
+
async cache(key, ...parameter) {
|
|
480
|
+
if (!this.caches.has(key)) {
|
|
481
|
+
await this.resolve(key, ...parameter);
|
|
482
|
+
} else {
|
|
483
|
+
this.caches.extendExpire(key);
|
|
484
|
+
}
|
|
485
|
+
return this.caches.get(key);
|
|
486
|
+
}
|
|
487
|
+
async update(key, ...parameter) {
|
|
488
|
+
await this.resolve(key, ...parameter);
|
|
489
|
+
for (const t of this.assignments) {
|
|
490
|
+
const instance = t;
|
|
491
|
+
for (const cacheKey of instance.caches.keys()) {
|
|
492
|
+
if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
|
|
493
|
+
await instance.update(cacheKey, ...instance.parameters[cacheKey]);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return this.caches.get(key);
|
|
498
|
+
}
|
|
499
|
+
};
|
|
103
500
|
|
|
104
501
|
// src/base/BPTree.ts
|
|
105
502
|
var BPTree = class {
|
|
106
503
|
_cachedRegexp;
|
|
107
504
|
strategy;
|
|
108
505
|
comparator;
|
|
109
|
-
|
|
506
|
+
option;
|
|
110
507
|
order;
|
|
111
508
|
root;
|
|
112
509
|
_strategyDirty;
|
|
@@ -124,12 +521,8 @@ var BPTree = class {
|
|
|
124
521
|
like: (nv, v) => {
|
|
125
522
|
const nodeValue = this.comparator.match(nv);
|
|
126
523
|
const value = this.comparator.match(v);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const regexp2 = new RegExp(`^${pattern}$`, "i");
|
|
130
|
-
this._cachedRegexp.set(value, regexp2);
|
|
131
|
-
}
|
|
132
|
-
const regexp = this._cachedRegexp.get(value);
|
|
524
|
+
const cache = this._cachedRegexp.cache(value);
|
|
525
|
+
const regexp = cache.raw;
|
|
133
526
|
return regexp.test(nodeValue);
|
|
134
527
|
}
|
|
135
528
|
};
|
|
@@ -166,15 +559,24 @@ var BPTree = class {
|
|
|
166
559
|
or: 1,
|
|
167
560
|
like: 1
|
|
168
561
|
};
|
|
169
|
-
constructor(strategy, comparator) {
|
|
562
|
+
constructor(strategy, comparator, option) {
|
|
563
|
+
this.strategy = strategy;
|
|
564
|
+
this.comparator = comparator;
|
|
565
|
+
this.option = option ?? {};
|
|
170
566
|
this._strategyDirty = false;
|
|
171
|
-
this._cachedRegexp = new InvertedWeakMap();
|
|
172
567
|
this._nodeCreateBuffer = /* @__PURE__ */ new Map();
|
|
173
568
|
this._nodeUpdateBuffer = /* @__PURE__ */ new Map();
|
|
174
569
|
this._nodeDeleteBuffer = /* @__PURE__ */ new Map();
|
|
175
|
-
this.
|
|
176
|
-
|
|
177
|
-
|
|
570
|
+
this._cachedRegexp = this._createCachedRegexp();
|
|
571
|
+
}
|
|
572
|
+
_createCachedRegexp() {
|
|
573
|
+
return new CacheEntanglementSync((key) => {
|
|
574
|
+
const pattern = key.replace(/%/g, ".*").replace(/_/g, ".");
|
|
575
|
+
const regexp = new RegExp(`^${pattern}$`, "i");
|
|
576
|
+
return regexp;
|
|
577
|
+
}, {
|
|
578
|
+
lifespan: this.option.lifespan ?? "3m"
|
|
579
|
+
});
|
|
178
580
|
}
|
|
179
581
|
ensureValues(v) {
|
|
180
582
|
if (!Array.isArray(v)) {
|
|
@@ -248,12 +650,28 @@ var BPTree = class {
|
|
|
248
650
|
getHeadData() {
|
|
249
651
|
return this.strategy.head.data;
|
|
250
652
|
}
|
|
653
|
+
/**
|
|
654
|
+
* Clears all cached nodes.
|
|
655
|
+
* This method is useful for freeing up memory when the tree is no longer needed.
|
|
656
|
+
*/
|
|
657
|
+
clear() {
|
|
658
|
+
this._cachedRegexp.clear();
|
|
659
|
+
this.nodes.clear();
|
|
660
|
+
}
|
|
251
661
|
};
|
|
252
662
|
|
|
253
663
|
// src/BPTreeSync.ts
|
|
254
664
|
var BPTreeSync = class extends BPTree {
|
|
255
|
-
constructor(strategy, comparator) {
|
|
256
|
-
super(strategy, comparator);
|
|
665
|
+
constructor(strategy, comparator, option) {
|
|
666
|
+
super(strategy, comparator, option);
|
|
667
|
+
this.nodes = this._createCachedNode();
|
|
668
|
+
}
|
|
669
|
+
_createCachedNode() {
|
|
670
|
+
return new CacheEntanglementSync((key) => {
|
|
671
|
+
return this.strategy.read(key);
|
|
672
|
+
}, {
|
|
673
|
+
lifespan: this.option.lifespan ?? "3m"
|
|
674
|
+
});
|
|
257
675
|
}
|
|
258
676
|
getPairsRightToLeft(value, startNode, endNode, comparator) {
|
|
259
677
|
const pairs = [];
|
|
@@ -338,7 +756,7 @@ var BPTreeSync = class extends BPTree {
|
|
|
338
756
|
next,
|
|
339
757
|
prev
|
|
340
758
|
};
|
|
341
|
-
this.
|
|
759
|
+
this._nodeCreateBuffer.set(id, node);
|
|
342
760
|
return node;
|
|
343
761
|
}
|
|
344
762
|
_deleteEntry(node, key, value) {
|
|
@@ -552,7 +970,6 @@ var BPTreeSync = class extends BPTree {
|
|
|
552
970
|
this.strategy.head.root = root.id;
|
|
553
971
|
node.parent = root.id;
|
|
554
972
|
pointer.parent = root.id;
|
|
555
|
-
this.bufferForNodeCreate(root);
|
|
556
973
|
this.bufferForNodeUpdate(node);
|
|
557
974
|
this.bufferForNodeUpdate(pointer);
|
|
558
975
|
return;
|
|
@@ -588,7 +1005,6 @@ var BPTreeSync = class extends BPTree {
|
|
|
588
1005
|
this.bufferForNodeUpdate(node2);
|
|
589
1006
|
}
|
|
590
1007
|
this._insertInParent(parentNode, midValue, parentPointer);
|
|
591
|
-
this.bufferForNodeCreate(parentPointer);
|
|
592
1008
|
this.bufferForNodeUpdate(parentNode);
|
|
593
1009
|
}
|
|
594
1010
|
}
|
|
@@ -600,7 +1016,6 @@ var BPTreeSync = class extends BPTree {
|
|
|
600
1016
|
this.order = this.strategy.order;
|
|
601
1017
|
this.root = this._createNode(true, [], [], true);
|
|
602
1018
|
this.strategy.head.root = this.root.id;
|
|
603
|
-
this.bufferForNodeCreate(this.root);
|
|
604
1019
|
this.commitHeadBuffer();
|
|
605
1020
|
this.commitNodeCreateBuffer();
|
|
606
1021
|
} else {
|
|
@@ -614,10 +1029,11 @@ var BPTreeSync = class extends BPTree {
|
|
|
614
1029
|
}
|
|
615
1030
|
}
|
|
616
1031
|
getNode(id) {
|
|
617
|
-
if (
|
|
618
|
-
|
|
1032
|
+
if (this._nodeCreateBuffer.has(id)) {
|
|
1033
|
+
return this._nodeCreateBuffer.get(id);
|
|
619
1034
|
}
|
|
620
|
-
|
|
1035
|
+
const cache = this.nodes.cache(id);
|
|
1036
|
+
return cache.raw;
|
|
621
1037
|
}
|
|
622
1038
|
insertableNode(value) {
|
|
623
1039
|
let node = this.root;
|
|
@@ -773,7 +1189,6 @@ var BPTreeSync = class extends BPTree {
|
|
|
773
1189
|
this.bufferForNodeUpdate(node);
|
|
774
1190
|
}
|
|
775
1191
|
this._insertInParent(before, after.values[0], after);
|
|
776
|
-
this.bufferForNodeCreate(after);
|
|
777
1192
|
this.bufferForNodeUpdate(before);
|
|
778
1193
|
}
|
|
779
1194
|
this.commitHeadBuffer();
|
|
@@ -839,8 +1254,16 @@ var BPTreeSync = class extends BPTree {
|
|
|
839
1254
|
|
|
840
1255
|
// src/BPTreeAsync.ts
|
|
841
1256
|
var BPTreeAsync = class extends BPTree {
|
|
842
|
-
constructor(strategy, comparator) {
|
|
843
|
-
super(strategy, comparator);
|
|
1257
|
+
constructor(strategy, comparator, option) {
|
|
1258
|
+
super(strategy, comparator, option);
|
|
1259
|
+
this.nodes = this._createCachedNode();
|
|
1260
|
+
}
|
|
1261
|
+
_createCachedNode() {
|
|
1262
|
+
return new CacheEntanglementAsync(async (key) => {
|
|
1263
|
+
return await this.strategy.read(key);
|
|
1264
|
+
}, {
|
|
1265
|
+
lifespan: this.option.lifespan ?? "3m"
|
|
1266
|
+
});
|
|
844
1267
|
}
|
|
845
1268
|
async getPairsRightToLeft(value, startNode, endNode, comparator) {
|
|
846
1269
|
const pairs = [];
|
|
@@ -925,7 +1348,7 @@ var BPTreeAsync = class extends BPTree {
|
|
|
925
1348
|
next,
|
|
926
1349
|
prev
|
|
927
1350
|
};
|
|
928
|
-
this.
|
|
1351
|
+
this._nodeCreateBuffer.set(id, node);
|
|
929
1352
|
return node;
|
|
930
1353
|
}
|
|
931
1354
|
async _deleteEntry(node, key, value) {
|
|
@@ -1139,7 +1562,6 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1139
1562
|
this.strategy.head.root = root.id;
|
|
1140
1563
|
node.parent = root.id;
|
|
1141
1564
|
pointer.parent = root.id;
|
|
1142
|
-
this.bufferForNodeCreate(root);
|
|
1143
1565
|
this.bufferForNodeUpdate(node);
|
|
1144
1566
|
this.bufferForNodeUpdate(pointer);
|
|
1145
1567
|
return;
|
|
@@ -1175,7 +1597,6 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1175
1597
|
this.bufferForNodeUpdate(node2);
|
|
1176
1598
|
}
|
|
1177
1599
|
await this._insertInParent(parentNode, midValue, parentPointer);
|
|
1178
|
-
this.bufferForNodeCreate(parentPointer);
|
|
1179
1600
|
this.bufferForNodeUpdate(parentNode);
|
|
1180
1601
|
}
|
|
1181
1602
|
}
|
|
@@ -1187,7 +1608,6 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1187
1608
|
this.order = this.strategy.order;
|
|
1188
1609
|
this.root = await this._createNode(true, [], [], true);
|
|
1189
1610
|
this.strategy.head.root = this.root.id;
|
|
1190
|
-
this.bufferForNodeCreate(this.root);
|
|
1191
1611
|
await this.commitHeadBuffer();
|
|
1192
1612
|
await this.commitNodeCreateBuffer();
|
|
1193
1613
|
} else {
|
|
@@ -1201,10 +1621,11 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1201
1621
|
}
|
|
1202
1622
|
}
|
|
1203
1623
|
async getNode(id) {
|
|
1204
|
-
if (
|
|
1205
|
-
|
|
1624
|
+
if (this._nodeCreateBuffer.has(id)) {
|
|
1625
|
+
return this._nodeCreateBuffer.get(id);
|
|
1206
1626
|
}
|
|
1207
|
-
|
|
1627
|
+
const cache = await this.nodes.cache(id);
|
|
1628
|
+
return cache.raw;
|
|
1208
1629
|
}
|
|
1209
1630
|
async insertableNode(value) {
|
|
1210
1631
|
let node = this.root;
|
|
@@ -1360,7 +1781,6 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1360
1781
|
this.bufferForNodeUpdate(node);
|
|
1361
1782
|
}
|
|
1362
1783
|
await this._insertInParent(before, after.values[0], after);
|
|
1363
|
-
this.bufferForNodeCreate(after);
|
|
1364
1784
|
this.bufferForNodeUpdate(before);
|
|
1365
1785
|
}
|
|
1366
1786
|
await this.commitHeadBuffer();
|