serializable-bptree 4.0.1 → 4.0.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.
- package/dist/cjs/index.cjs +54 -417
- package/dist/esm/index.mjs +54 -417
- package/dist/{typings → types}/base/BPTree.d.ts +3 -3
- package/package.json +10 -13
- /package/dist/{typings → types}/BPTreeAsync.d.ts +0 -0
- /package/dist/{typings → types}/BPTreeSync.d.ts +0 -0
- /package/dist/{typings → types}/SerializeStrategyAsync.d.ts +0 -0
- /package/dist/{typings → types}/SerializeStrategySync.d.ts +0 -0
- /package/dist/{typings → types}/base/SerializeStrategy.d.ts +0 -0
- /package/dist/{typings → types}/base/ValueComparator.d.ts +0 -0
- /package/dist/{typings → types}/index.d.ts +0 -0
- /package/dist/{typings → types}/utils/InvertedWeakMap.d.ts +0 -0
- /package/dist/{typings → types}/utils/types.d.ts +0 -0
package/dist/esm/index.mjs
CHANGED
|
@@ -27,371 +27,6 @@ var StringComparator = class extends ValueComparator {
|
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
// node_modules/cachebranch/dist/esm/index.mjs
|
|
31
|
-
var CacheBranch = class {
|
|
32
|
-
data;
|
|
33
|
-
branches;
|
|
34
|
-
constructor(data) {
|
|
35
|
-
this.data = data;
|
|
36
|
-
this.branches = /* @__PURE__ */ new Map();
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* The method splits the provided key value into tokens and returns them as an array.
|
|
40
|
-
* For instance, if you pass `'user/name/middle'` as the parameter, it will return `['user', 'name', 'middle']` as the split values.
|
|
41
|
-
* @param key The key value to split.
|
|
42
|
-
*/
|
|
43
|
-
tokens(key) {
|
|
44
|
-
return key.split("/");
|
|
45
|
-
}
|
|
46
|
-
to(key, getNextBranch) {
|
|
47
|
-
const tokens = this.tokens(key);
|
|
48
|
-
let current = this;
|
|
49
|
-
for (let i = 0, len = tokens.length; i < len; i++) {
|
|
50
|
-
const last = i === len - 1;
|
|
51
|
-
const key2 = tokens[i];
|
|
52
|
-
const next = getNextBranch(current, key2, i, last);
|
|
53
|
-
if (next === null) {
|
|
54
|
-
return void 0;
|
|
55
|
-
}
|
|
56
|
-
current = next;
|
|
57
|
-
}
|
|
58
|
-
return current;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
var VOID = -1;
|
|
62
|
-
var PRIMITIVE = 0;
|
|
63
|
-
var ARRAY = 1;
|
|
64
|
-
var OBJECT = 2;
|
|
65
|
-
var DATE = 3;
|
|
66
|
-
var REGEXP = 4;
|
|
67
|
-
var MAP = 5;
|
|
68
|
-
var SET = 6;
|
|
69
|
-
var ERROR = 7;
|
|
70
|
-
var BIGINT = 8;
|
|
71
|
-
var env = typeof self === "object" ? self : globalThis;
|
|
72
|
-
var deserializer = ($, _) => {
|
|
73
|
-
const as = (out, index) => {
|
|
74
|
-
$.set(index, out);
|
|
75
|
-
return out;
|
|
76
|
-
};
|
|
77
|
-
const unpair = (index) => {
|
|
78
|
-
if ($.has(index))
|
|
79
|
-
return $.get(index);
|
|
80
|
-
const [type, value] = _[index];
|
|
81
|
-
switch (type) {
|
|
82
|
-
case PRIMITIVE:
|
|
83
|
-
case VOID:
|
|
84
|
-
return as(value, index);
|
|
85
|
-
case ARRAY: {
|
|
86
|
-
const arr = as([], index);
|
|
87
|
-
for (const index2 of value)
|
|
88
|
-
arr.push(unpair(index2));
|
|
89
|
-
return arr;
|
|
90
|
-
}
|
|
91
|
-
case OBJECT: {
|
|
92
|
-
const object = as({}, index);
|
|
93
|
-
for (const [key, index2] of value)
|
|
94
|
-
object[unpair(key)] = unpair(index2);
|
|
95
|
-
return object;
|
|
96
|
-
}
|
|
97
|
-
case DATE:
|
|
98
|
-
return as(new Date(value), index);
|
|
99
|
-
case REGEXP: {
|
|
100
|
-
const { source, flags } = value;
|
|
101
|
-
return as(new RegExp(source, flags), index);
|
|
102
|
-
}
|
|
103
|
-
case MAP: {
|
|
104
|
-
const map = as(/* @__PURE__ */ new Map(), index);
|
|
105
|
-
for (const [key, index2] of value)
|
|
106
|
-
map.set(unpair(key), unpair(index2));
|
|
107
|
-
return map;
|
|
108
|
-
}
|
|
109
|
-
case SET: {
|
|
110
|
-
const set = as(/* @__PURE__ */ new Set(), index);
|
|
111
|
-
for (const index2 of value)
|
|
112
|
-
set.add(unpair(index2));
|
|
113
|
-
return set;
|
|
114
|
-
}
|
|
115
|
-
case ERROR: {
|
|
116
|
-
const { name, message } = value;
|
|
117
|
-
return as(new env[name](message), index);
|
|
118
|
-
}
|
|
119
|
-
case BIGINT:
|
|
120
|
-
return as(BigInt(value), index);
|
|
121
|
-
case "BigInt":
|
|
122
|
-
return as(Object(BigInt(value)), index);
|
|
123
|
-
}
|
|
124
|
-
return as(new env[type](value), index);
|
|
125
|
-
};
|
|
126
|
-
return unpair;
|
|
127
|
-
};
|
|
128
|
-
var deserialize = (serialized) => deserializer(/* @__PURE__ */ new Map(), serialized)(0);
|
|
129
|
-
var EMPTY = "";
|
|
130
|
-
var { toString } = {};
|
|
131
|
-
var { keys } = Object;
|
|
132
|
-
var typeOf = (value) => {
|
|
133
|
-
const type = typeof value;
|
|
134
|
-
if (type !== "object" || !value)
|
|
135
|
-
return [PRIMITIVE, type];
|
|
136
|
-
const asString = toString.call(value).slice(8, -1);
|
|
137
|
-
switch (asString) {
|
|
138
|
-
case "Array":
|
|
139
|
-
return [ARRAY, EMPTY];
|
|
140
|
-
case "Object":
|
|
141
|
-
return [OBJECT, EMPTY];
|
|
142
|
-
case "Date":
|
|
143
|
-
return [DATE, EMPTY];
|
|
144
|
-
case "RegExp":
|
|
145
|
-
return [REGEXP, EMPTY];
|
|
146
|
-
case "Map":
|
|
147
|
-
return [MAP, EMPTY];
|
|
148
|
-
case "Set":
|
|
149
|
-
return [SET, EMPTY];
|
|
150
|
-
}
|
|
151
|
-
if (asString.includes("Array"))
|
|
152
|
-
return [ARRAY, asString];
|
|
153
|
-
if (asString.includes("Error"))
|
|
154
|
-
return [ERROR, asString];
|
|
155
|
-
return [OBJECT, asString];
|
|
156
|
-
};
|
|
157
|
-
var shouldSkip = ([TYPE, type]) => TYPE === PRIMITIVE && (type === "function" || type === "symbol");
|
|
158
|
-
var serializer = (strict, json, $, _) => {
|
|
159
|
-
const as = (out, value) => {
|
|
160
|
-
const index = _.push(out) - 1;
|
|
161
|
-
$.set(value, index);
|
|
162
|
-
return index;
|
|
163
|
-
};
|
|
164
|
-
const pair = (value) => {
|
|
165
|
-
if ($.has(value))
|
|
166
|
-
return $.get(value);
|
|
167
|
-
let [TYPE, type] = typeOf(value);
|
|
168
|
-
switch (TYPE) {
|
|
169
|
-
case PRIMITIVE: {
|
|
170
|
-
let entry = value;
|
|
171
|
-
switch (type) {
|
|
172
|
-
case "bigint":
|
|
173
|
-
TYPE = BIGINT;
|
|
174
|
-
entry = value.toString();
|
|
175
|
-
break;
|
|
176
|
-
case "function":
|
|
177
|
-
case "symbol":
|
|
178
|
-
if (strict)
|
|
179
|
-
throw new TypeError("unable to serialize " + type);
|
|
180
|
-
entry = null;
|
|
181
|
-
break;
|
|
182
|
-
case "undefined":
|
|
183
|
-
return as([VOID], value);
|
|
184
|
-
}
|
|
185
|
-
return as([TYPE, entry], value);
|
|
186
|
-
}
|
|
187
|
-
case ARRAY: {
|
|
188
|
-
if (type)
|
|
189
|
-
return as([type, [...value]], value);
|
|
190
|
-
const arr = [];
|
|
191
|
-
const index = as([TYPE, arr], value);
|
|
192
|
-
for (const entry of value)
|
|
193
|
-
arr.push(pair(entry));
|
|
194
|
-
return index;
|
|
195
|
-
}
|
|
196
|
-
case OBJECT: {
|
|
197
|
-
if (type) {
|
|
198
|
-
switch (type) {
|
|
199
|
-
case "BigInt":
|
|
200
|
-
return as([type, value.toString()], value);
|
|
201
|
-
case "Boolean":
|
|
202
|
-
case "Number":
|
|
203
|
-
case "String":
|
|
204
|
-
return as([type, value.valueOf()], value);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (json && "toJSON" in value)
|
|
208
|
-
return pair(value.toJSON());
|
|
209
|
-
const entries = [];
|
|
210
|
-
const index = as([TYPE, entries], value);
|
|
211
|
-
for (const key of keys(value)) {
|
|
212
|
-
if (strict || !shouldSkip(typeOf(value[key])))
|
|
213
|
-
entries.push([pair(key), pair(value[key])]);
|
|
214
|
-
}
|
|
215
|
-
return index;
|
|
216
|
-
}
|
|
217
|
-
case DATE:
|
|
218
|
-
return as([TYPE, value.toISOString()], value);
|
|
219
|
-
case REGEXP: {
|
|
220
|
-
const { source, flags } = value;
|
|
221
|
-
return as([TYPE, { source, flags }], value);
|
|
222
|
-
}
|
|
223
|
-
case MAP: {
|
|
224
|
-
const entries = [];
|
|
225
|
-
const index = as([TYPE, entries], value);
|
|
226
|
-
for (const [key, entry] of value) {
|
|
227
|
-
if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry))))
|
|
228
|
-
entries.push([pair(key), pair(entry)]);
|
|
229
|
-
}
|
|
230
|
-
return index;
|
|
231
|
-
}
|
|
232
|
-
case SET: {
|
|
233
|
-
const entries = [];
|
|
234
|
-
const index = as([TYPE, entries], value);
|
|
235
|
-
for (const entry of value) {
|
|
236
|
-
if (strict || !shouldSkip(typeOf(entry)))
|
|
237
|
-
entries.push(pair(entry));
|
|
238
|
-
}
|
|
239
|
-
return index;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
const { message } = value;
|
|
243
|
-
return as([TYPE, { name: type, message }], value);
|
|
244
|
-
};
|
|
245
|
-
return pair;
|
|
246
|
-
};
|
|
247
|
-
var serialize = (value, { json, lossy } = {}) => {
|
|
248
|
-
const _ = [];
|
|
249
|
-
return serializer(!(json || lossy), !!json, /* @__PURE__ */ new Map(), _)(value), _;
|
|
250
|
-
};
|
|
251
|
-
var esm_default = typeof structuredClone === "function" ? (
|
|
252
|
-
/* c8 ignore start */
|
|
253
|
-
(any, options) => options && ("json" in options || "lossy" in options) ? deserialize(serialize(any, options)) : structuredClone(any)
|
|
254
|
-
) : (any, options) => deserialize(serialize(any, options));
|
|
255
|
-
var CacheData = class _CacheData {
|
|
256
|
-
static IsDirty(data) {
|
|
257
|
-
return data.dirty;
|
|
258
|
-
}
|
|
259
|
-
static SetDirty(data, value) {
|
|
260
|
-
data.dirty = value;
|
|
261
|
-
return data;
|
|
262
|
-
}
|
|
263
|
-
static StructuredClone = globalThis.structuredClone ?? esm_default;
|
|
264
|
-
_raw;
|
|
265
|
-
generator;
|
|
266
|
-
dirty;
|
|
267
|
-
constructor(generator) {
|
|
268
|
-
this._raw = void 0;
|
|
269
|
-
this.dirty = false;
|
|
270
|
-
this.generator = generator;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* This is cached data.
|
|
274
|
-
* It was generated at the time of caching, so there is a risk of modification if it's an object due to shallow copying.
|
|
275
|
-
* 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.
|
|
276
|
-
*/
|
|
277
|
-
get raw() {
|
|
278
|
-
if (!this.dirty) {
|
|
279
|
-
throw new Error(`The data is not initialized and cannot be accessed. Please use the 'ensure' or 'set' method to create the data first.`);
|
|
280
|
-
}
|
|
281
|
-
return this._raw;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* The method returns a copied value of the cached data.
|
|
285
|
-
* You can pass a function as a parameter to copy the value. This parameter function should return the copied value.
|
|
286
|
-
*
|
|
287
|
-
* If no parameter is passed, it defaults to using Javascript's or \@ungap/structured-clone's `structuredClone` function to copy the value.
|
|
288
|
-
* If you prefer shallow copying instead of deep copying,
|
|
289
|
-
* you can use the default options `array-shallow-copy`, `object-shallow-copy` and `deep-copy`,
|
|
290
|
-
* which are replaced with functions to shallow copy arrays and objects, respectively. This is a syntactic sugar.
|
|
291
|
-
* @param strategy The function that returns the copied value.
|
|
292
|
-
* If you want to perform a shallow copy, simply pass the strings `array-shallow-copy` or `object-shallow-copy` for easy use.
|
|
293
|
-
* The default is `structuredClone`.
|
|
294
|
-
*/
|
|
295
|
-
clone(strategy = "deep-copy") {
|
|
296
|
-
if (strategy && typeof strategy !== "string") {
|
|
297
|
-
return strategy(this.raw);
|
|
298
|
-
}
|
|
299
|
-
switch (strategy) {
|
|
300
|
-
case "array-shallow-copy":
|
|
301
|
-
return [].concat(this.raw);
|
|
302
|
-
case "object-shallow-copy":
|
|
303
|
-
return Object.assign({}, this.raw);
|
|
304
|
-
case "deep-copy":
|
|
305
|
-
default:
|
|
306
|
-
return _CacheData.StructuredClone(this.raw);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
var CacheDataSync = class extends CacheData {
|
|
311
|
-
static EmptyDataGenerator = () => void 0;
|
|
312
|
-
static Update(branch, data, generator) {
|
|
313
|
-
data._raw = generator(branch);
|
|
314
|
-
data.generator = generator;
|
|
315
|
-
data.dirty = true;
|
|
316
|
-
return data;
|
|
317
|
-
}
|
|
318
|
-
static Cache(branch, data) {
|
|
319
|
-
data._raw = data.generator(branch);
|
|
320
|
-
return data;
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
var CacheBranchSync = class _CacheBranchSync extends CacheBranch {
|
|
324
|
-
root;
|
|
325
|
-
constructor(generator = CacheDataSync.EmptyDataGenerator, root) {
|
|
326
|
-
super(new CacheDataSync(generator));
|
|
327
|
-
this.root = root ?? this;
|
|
328
|
-
}
|
|
329
|
-
ensureBranch(key, generator) {
|
|
330
|
-
return this.to(key, (b, k) => {
|
|
331
|
-
const branch = b;
|
|
332
|
-
if (!branch.branches.has(k)) {
|
|
333
|
-
branch.branches.set(k, new _CacheBranchSync(generator, this.root));
|
|
334
|
-
}
|
|
335
|
-
return branch.branches.get(k);
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
getBranch(key) {
|
|
339
|
-
return this.to(key, (b, k) => {
|
|
340
|
-
const branch = b;
|
|
341
|
-
if (!branch.branches.has(k)) {
|
|
342
|
-
return null;
|
|
343
|
-
}
|
|
344
|
-
return branch.branches.get(k);
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
cache(key, recursive) {
|
|
348
|
-
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
349
|
-
if (!branch) {
|
|
350
|
-
return this;
|
|
351
|
-
}
|
|
352
|
-
if (recursive === "bottom-up") {
|
|
353
|
-
for (const key2 of branch.branches.keys()) {
|
|
354
|
-
branch.cache(key2, recursive);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
CacheDataSync.Cache(this.root, branch.data);
|
|
358
|
-
if (recursive === "top-down") {
|
|
359
|
-
for (const key2 of branch.branches.keys()) {
|
|
360
|
-
branch.cache(key2, recursive);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return this;
|
|
364
|
-
}
|
|
365
|
-
ensure(key, generator) {
|
|
366
|
-
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
367
|
-
if (!CacheDataSync.IsDirty(branch.data)) {
|
|
368
|
-
CacheDataSync.Update(this.root, branch.data, generator);
|
|
369
|
-
}
|
|
370
|
-
return branch.data;
|
|
371
|
-
}
|
|
372
|
-
get(key) {
|
|
373
|
-
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
374
|
-
if (!CacheDataSync.IsDirty(branch.data)) {
|
|
375
|
-
return void 0;
|
|
376
|
-
}
|
|
377
|
-
return branch.data;
|
|
378
|
-
}
|
|
379
|
-
set(key, generator) {
|
|
380
|
-
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
381
|
-
CacheDataSync.Update(this.root, branch.data, generator);
|
|
382
|
-
return this;
|
|
383
|
-
}
|
|
384
|
-
delete(key) {
|
|
385
|
-
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
386
|
-
if (branch) {
|
|
387
|
-
branch.branches.clear();
|
|
388
|
-
CacheDataSync.Update(this.root, branch.data, CacheDataSync.EmptyDataGenerator);
|
|
389
|
-
CacheDataSync.SetDirty(branch.data, false);
|
|
390
|
-
}
|
|
391
|
-
return this;
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
|
|
395
30
|
// src/utils/InvertedWeakMap.ts
|
|
396
31
|
var InvertedWeakMap = class {
|
|
397
32
|
_map;
|
|
@@ -446,10 +81,12 @@ var BPTree = class {
|
|
|
446
81
|
like: (nv, v) => {
|
|
447
82
|
const nodeValue = this.comparator.match(nv);
|
|
448
83
|
const value = this.comparator.match(v);
|
|
449
|
-
|
|
84
|
+
if (!this._cachedRegexp.has(value)) {
|
|
450
85
|
const pattern = value.replace(/%/g, ".*").replace(/_/g, ".");
|
|
451
|
-
|
|
452
|
-
|
|
86
|
+
const regexp2 = new RegExp(`^${pattern}$`, "i");
|
|
87
|
+
this._cachedRegexp.set(value, regexp2);
|
|
88
|
+
}
|
|
89
|
+
const regexp = this._cachedRegexp.get(value);
|
|
453
90
|
return regexp.test(nodeValue);
|
|
454
91
|
}
|
|
455
92
|
};
|
|
@@ -481,7 +118,7 @@ var BPTree = class {
|
|
|
481
118
|
like: true
|
|
482
119
|
};
|
|
483
120
|
constructor(strategy, comparator) {
|
|
484
|
-
this._cachedRegexp = new
|
|
121
|
+
this._cachedRegexp = new InvertedWeakMap();
|
|
485
122
|
this._nodeCreateBuffer = /* @__PURE__ */ new Map();
|
|
486
123
|
this._nodeUpdateBuffer = /* @__PURE__ */ new Map();
|
|
487
124
|
this._nodeDeleteBuffer = /* @__PURE__ */ new Map();
|
|
@@ -494,8 +131,8 @@ var BPTree = class {
|
|
|
494
131
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
495
132
|
const nValue = node.values[i];
|
|
496
133
|
if (this.comparator.isSame(value, nValue)) {
|
|
497
|
-
const
|
|
498
|
-
|
|
134
|
+
const keys = node.keys[i];
|
|
135
|
+
keys.push(key);
|
|
499
136
|
this.bufferForNodeUpdate(node);
|
|
500
137
|
break;
|
|
501
138
|
} else if (this.comparator.isLower(value, nValue)) {
|
|
@@ -549,12 +186,12 @@ var BPTreeSync = class extends BPTree {
|
|
|
549
186
|
let i = node.values.length;
|
|
550
187
|
while (i--) {
|
|
551
188
|
const nValue = node.values[i];
|
|
552
|
-
const
|
|
189
|
+
const keys = node.keys[i];
|
|
553
190
|
if (comparator(nValue, value)) {
|
|
554
191
|
found = true;
|
|
555
|
-
let j =
|
|
192
|
+
let j = keys.length;
|
|
556
193
|
while (j--) {
|
|
557
|
-
pairs.push({ key:
|
|
194
|
+
pairs.push({ key: keys[j], value: nValue });
|
|
558
195
|
}
|
|
559
196
|
} else if (found && !fullScan) {
|
|
560
197
|
done = true;
|
|
@@ -577,10 +214,10 @@ var BPTreeSync = class extends BPTree {
|
|
|
577
214
|
while (!done) {
|
|
578
215
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
579
216
|
const nValue = node.values[i];
|
|
580
|
-
const
|
|
217
|
+
const keys = node.keys[i];
|
|
581
218
|
if (comparator(nValue, value)) {
|
|
582
219
|
found = true;
|
|
583
|
-
for (const key of
|
|
220
|
+
for (const key of keys) {
|
|
584
221
|
pairs.push({ key, value: nValue });
|
|
585
222
|
}
|
|
586
223
|
} else if (found && !fullScan) {
|
|
@@ -613,11 +250,11 @@ var BPTreeSync = class extends BPTree {
|
|
|
613
250
|
}
|
|
614
251
|
return id;
|
|
615
252
|
}
|
|
616
|
-
_createNode(isLeaf,
|
|
253
|
+
_createNode(isLeaf, keys, values, leaf = false, parent = null, next = null, prev = null) {
|
|
617
254
|
const id = this._createNodeId(isLeaf);
|
|
618
255
|
const node = {
|
|
619
256
|
id,
|
|
620
|
-
keys
|
|
257
|
+
keys,
|
|
621
258
|
values,
|
|
622
259
|
leaf,
|
|
623
260
|
parent,
|
|
@@ -647,9 +284,9 @@ var BPTreeSync = class extends BPTree {
|
|
|
647
284
|
}
|
|
648
285
|
}
|
|
649
286
|
if (this.root === node && node.keys.length === 1) {
|
|
650
|
-
const
|
|
287
|
+
const keys = node.keys;
|
|
651
288
|
this.bufferForNodeDelete(this.root);
|
|
652
|
-
this.root = this.getNode(
|
|
289
|
+
this.root = this.getNode(keys[0]);
|
|
653
290
|
this.root.parent = null;
|
|
654
291
|
this.strategy.head.root = this.root.id;
|
|
655
292
|
this.bufferForNodeUpdate(this.root);
|
|
@@ -723,8 +360,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
723
360
|
}
|
|
724
361
|
pointer.values.push(...node.values);
|
|
725
362
|
if (!pointer.leaf) {
|
|
726
|
-
const
|
|
727
|
-
for (const key2 of
|
|
363
|
+
const keys = pointer.keys;
|
|
364
|
+
for (const key2 of keys) {
|
|
728
365
|
const node2 = this.getNode(key2);
|
|
729
366
|
node2.parent = pointer.id;
|
|
730
367
|
this.bufferForNodeUpdate(node2);
|
|
@@ -924,8 +561,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
924
561
|
leftestNode() {
|
|
925
562
|
let node = this.root;
|
|
926
563
|
while (!node.leaf) {
|
|
927
|
-
const
|
|
928
|
-
node = this.getNode(
|
|
564
|
+
const keys = node.keys;
|
|
565
|
+
node = this.getNode(keys[0]);
|
|
929
566
|
}
|
|
930
567
|
return node;
|
|
931
568
|
}
|
|
@@ -1036,17 +673,17 @@ var BPTreeSync = class extends BPTree {
|
|
|
1036
673
|
while (i--) {
|
|
1037
674
|
const nValue = node.values[i];
|
|
1038
675
|
if (this.comparator.isSame(value, nValue)) {
|
|
1039
|
-
const
|
|
1040
|
-
if (
|
|
1041
|
-
if (
|
|
1042
|
-
|
|
676
|
+
const keys = node.keys[i];
|
|
677
|
+
if (keys.includes(key)) {
|
|
678
|
+
if (keys.length > 1) {
|
|
679
|
+
keys.splice(keys.indexOf(key), 1);
|
|
1043
680
|
this.bufferForNodeUpdate(node);
|
|
1044
681
|
} else if (node === this.root) {
|
|
1045
682
|
node.values.splice(i, 1);
|
|
1046
683
|
node.keys.splice(i, 1);
|
|
1047
684
|
this.bufferForNodeUpdate(node);
|
|
1048
685
|
} else {
|
|
1049
|
-
|
|
686
|
+
keys.splice(keys.indexOf(key), 1);
|
|
1050
687
|
node.keys.splice(i, 1);
|
|
1051
688
|
node.values.splice(node.values.indexOf(value), 1);
|
|
1052
689
|
this._deleteEntry(node, key, value);
|
|
@@ -1065,8 +702,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
1065
702
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
1066
703
|
const nValue = node.values[i];
|
|
1067
704
|
if (this.comparator.isSame(value, nValue)) {
|
|
1068
|
-
const
|
|
1069
|
-
return
|
|
705
|
+
const keys = node.keys[i];
|
|
706
|
+
return keys.includes(key);
|
|
1070
707
|
}
|
|
1071
708
|
}
|
|
1072
709
|
return false;
|
|
@@ -1076,13 +713,13 @@ var BPTreeSync = class extends BPTree {
|
|
|
1076
713
|
this.commitHeadBuffer();
|
|
1077
714
|
}
|
|
1078
715
|
forceUpdate() {
|
|
1079
|
-
const
|
|
716
|
+
const keys = [...this.nodes.keys()];
|
|
1080
717
|
this.nodes.clear();
|
|
1081
718
|
this.init();
|
|
1082
|
-
for (const key of
|
|
719
|
+
for (const key of keys) {
|
|
1083
720
|
this.getNode(key);
|
|
1084
721
|
}
|
|
1085
|
-
return
|
|
722
|
+
return keys.length;
|
|
1086
723
|
}
|
|
1087
724
|
};
|
|
1088
725
|
|
|
@@ -1100,12 +737,12 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1100
737
|
let i = node.values.length;
|
|
1101
738
|
while (i--) {
|
|
1102
739
|
const nValue = node.values[i];
|
|
1103
|
-
const
|
|
740
|
+
const keys = node.keys[i];
|
|
1104
741
|
if (comparator(nValue, value)) {
|
|
1105
742
|
found = true;
|
|
1106
|
-
let j =
|
|
743
|
+
let j = keys.length;
|
|
1107
744
|
while (j--) {
|
|
1108
|
-
pairs.push({ key:
|
|
745
|
+
pairs.push({ key: keys[j], value: nValue });
|
|
1109
746
|
}
|
|
1110
747
|
} else if (found && !fullScan) {
|
|
1111
748
|
done = true;
|
|
@@ -1128,10 +765,10 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1128
765
|
while (!done) {
|
|
1129
766
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
1130
767
|
const nValue = node.values[i];
|
|
1131
|
-
const
|
|
768
|
+
const keys = node.keys[i];
|
|
1132
769
|
if (comparator(nValue, value)) {
|
|
1133
770
|
found = true;
|
|
1134
|
-
for (const key of
|
|
771
|
+
for (const key of keys) {
|
|
1135
772
|
pairs.push({ key, value: nValue });
|
|
1136
773
|
}
|
|
1137
774
|
} else if (found && !fullScan) {
|
|
@@ -1164,11 +801,11 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1164
801
|
}
|
|
1165
802
|
return id;
|
|
1166
803
|
}
|
|
1167
|
-
async _createNode(isLeaf,
|
|
804
|
+
async _createNode(isLeaf, keys, values, leaf = false, parent = null, next = null, prev = null) {
|
|
1168
805
|
const id = await this._createNodeId(isLeaf);
|
|
1169
806
|
const node = {
|
|
1170
807
|
id,
|
|
1171
|
-
keys
|
|
808
|
+
keys,
|
|
1172
809
|
values,
|
|
1173
810
|
leaf,
|
|
1174
811
|
parent,
|
|
@@ -1198,9 +835,9 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1198
835
|
}
|
|
1199
836
|
}
|
|
1200
837
|
if (this.root === node && node.keys.length === 1) {
|
|
1201
|
-
const
|
|
838
|
+
const keys = node.keys;
|
|
1202
839
|
this.bufferForNodeDelete(this.root);
|
|
1203
|
-
this.root = await this.getNode(
|
|
840
|
+
this.root = await this.getNode(keys[0]);
|
|
1204
841
|
this.root.parent = null;
|
|
1205
842
|
this.strategy.head.root = this.root.id;
|
|
1206
843
|
this.bufferForNodeUpdate(this.root);
|
|
@@ -1274,8 +911,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1274
911
|
}
|
|
1275
912
|
pointer.values.push(...node.values);
|
|
1276
913
|
if (!pointer.leaf) {
|
|
1277
|
-
const
|
|
1278
|
-
for (const key2 of
|
|
914
|
+
const keys = pointer.keys;
|
|
915
|
+
for (const key2 of keys) {
|
|
1279
916
|
const node2 = await this.getNode(key2);
|
|
1280
917
|
node2.parent = pointer.id;
|
|
1281
918
|
this.bufferForNodeUpdate(node2);
|
|
@@ -1475,8 +1112,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1475
1112
|
async leftestNode() {
|
|
1476
1113
|
let node = this.root;
|
|
1477
1114
|
while (!node.leaf) {
|
|
1478
|
-
const
|
|
1479
|
-
node = await this.getNode(
|
|
1115
|
+
const keys = node.keys;
|
|
1116
|
+
node = await this.getNode(keys[0]);
|
|
1480
1117
|
}
|
|
1481
1118
|
return node;
|
|
1482
1119
|
}
|
|
@@ -1587,17 +1224,17 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1587
1224
|
while (i--) {
|
|
1588
1225
|
const nValue = node.values[i];
|
|
1589
1226
|
if (this.comparator.isSame(value, nValue)) {
|
|
1590
|
-
const
|
|
1591
|
-
if (
|
|
1592
|
-
if (
|
|
1593
|
-
|
|
1227
|
+
const keys = node.keys[i];
|
|
1228
|
+
if (keys.includes(key)) {
|
|
1229
|
+
if (keys.length > 1) {
|
|
1230
|
+
keys.splice(keys.indexOf(key), 1);
|
|
1594
1231
|
this.bufferForNodeUpdate(node);
|
|
1595
1232
|
} else if (node === this.root) {
|
|
1596
1233
|
node.values.splice(i, 1);
|
|
1597
1234
|
node.keys.splice(i, 1);
|
|
1598
1235
|
this.bufferForNodeUpdate(node);
|
|
1599
1236
|
} else {
|
|
1600
|
-
|
|
1237
|
+
keys.splice(keys.indexOf(key), 1);
|
|
1601
1238
|
node.keys.splice(i, 1);
|
|
1602
1239
|
node.values.splice(node.values.indexOf(value), 1);
|
|
1603
1240
|
await this._deleteEntry(node, key, value);
|
|
@@ -1616,8 +1253,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1616
1253
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
1617
1254
|
const nValue = node.values[i];
|
|
1618
1255
|
if (this.comparator.isSame(value, nValue)) {
|
|
1619
|
-
const
|
|
1620
|
-
return
|
|
1256
|
+
const keys = node.keys[i];
|
|
1257
|
+
return keys.includes(key);
|
|
1621
1258
|
}
|
|
1622
1259
|
}
|
|
1623
1260
|
return false;
|
|
@@ -1627,13 +1264,13 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1627
1264
|
await this.commitHeadBuffer();
|
|
1628
1265
|
}
|
|
1629
1266
|
async forceUpdate() {
|
|
1630
|
-
const
|
|
1267
|
+
const keys = [...this.nodes.keys()];
|
|
1631
1268
|
this.nodes.clear();
|
|
1632
1269
|
await this.init();
|
|
1633
|
-
for (const key of
|
|
1270
|
+
for (const key of keys) {
|
|
1634
1271
|
await this.getNode(key);
|
|
1635
1272
|
}
|
|
1636
|
-
return
|
|
1273
|
+
return keys.length;
|
|
1637
1274
|
}
|
|
1638
1275
|
};
|
|
1639
1276
|
|
|
@@ -96,19 +96,19 @@ export declare abstract class BPTree<K, V> {
|
|
|
96
96
|
/**
|
|
97
97
|
* You enter the key and value as a pair. You can later search for the pair by value.
|
|
98
98
|
* This data is stored in the tree, sorted in ascending order of value.
|
|
99
|
-
* @param key The key of the pair.
|
|
99
|
+
* @param key The key of the pair. This key must be unique.
|
|
100
100
|
* @param value The value of the pair.
|
|
101
101
|
*/
|
|
102
102
|
abstract insert(key: K, value: V): Deferred<void>;
|
|
103
103
|
/**
|
|
104
104
|
* Deletes the pair that matches the key and value.
|
|
105
|
-
* @param key The key of the pair.
|
|
105
|
+
* @param key The key of the pair. This key must be unique.
|
|
106
106
|
* @param value The value of the pair.
|
|
107
107
|
*/
|
|
108
108
|
abstract delete(key: K, value: V): Deferred<void>;
|
|
109
109
|
/**
|
|
110
110
|
* It returns whether there is a value in the tree.
|
|
111
|
-
* @param key The key value to search for.
|
|
111
|
+
* @param key The key value to search for. This key must be unique.
|
|
112
112
|
* @param value The value to search for.
|
|
113
113
|
*/
|
|
114
114
|
abstract exists(key: K, value: V): Deferred<boolean>;
|