serializable-bptree 3.2.3 → 3.2.4
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.js +419 -91
- package/dist/esm/index.js +419 -91
- package/dist/typings/BPTreeAsync.d.ts +3 -3
- package/dist/typings/BPTreeSync.d.ts +0 -1
- package/dist/typings/base/BPTree.d.ts +5 -5
- package/dist/typings/base/SerializeStrategy.d.ts +1 -0
- package/package.json +11 -1
- package/dist/typings/utils/CacheStorage.d.ts +0 -3
package/dist/cjs/index.js
CHANGED
|
@@ -61,19 +61,374 @@ var StringComparator = class extends ValueComparator {
|
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
-
//
|
|
65
|
-
var
|
|
64
|
+
// node_modules/cachebranch/dist/esm/index.js
|
|
65
|
+
var CacheBranch = class {
|
|
66
|
+
data;
|
|
67
|
+
branches;
|
|
68
|
+
constructor(data) {
|
|
69
|
+
this.data = data;
|
|
70
|
+
this.branches = /* @__PURE__ */ new Map();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The method splits the provided key value into tokens and returns them as an array.
|
|
74
|
+
* For instance, if you pass `'user/name/middle'` as the parameter, it will return `['user', 'name', 'middle']` as the split values.
|
|
75
|
+
* @param key The key value to split.
|
|
76
|
+
*/
|
|
77
|
+
tokens(key) {
|
|
78
|
+
return key.split("/");
|
|
79
|
+
}
|
|
80
|
+
to(key, getNextBranch) {
|
|
81
|
+
const tokens = this.tokens(key);
|
|
82
|
+
let current = this;
|
|
83
|
+
for (let i = 0, len = tokens.length; i < len; i++) {
|
|
84
|
+
const last = i === len - 1;
|
|
85
|
+
const key2 = tokens[i];
|
|
86
|
+
const next = getNextBranch(current, key2, i, last);
|
|
87
|
+
if (next === null) {
|
|
88
|
+
return void 0;
|
|
89
|
+
}
|
|
90
|
+
current = next;
|
|
91
|
+
}
|
|
92
|
+
return current;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
var VOID = -1;
|
|
96
|
+
var PRIMITIVE = 0;
|
|
97
|
+
var ARRAY = 1;
|
|
98
|
+
var OBJECT = 2;
|
|
99
|
+
var DATE = 3;
|
|
100
|
+
var REGEXP = 4;
|
|
101
|
+
var MAP = 5;
|
|
102
|
+
var SET = 6;
|
|
103
|
+
var ERROR = 7;
|
|
104
|
+
var BIGINT = 8;
|
|
105
|
+
var env = typeof self === "object" ? self : globalThis;
|
|
106
|
+
var deserializer = ($, _) => {
|
|
107
|
+
const as = (out, index) => {
|
|
108
|
+
$.set(index, out);
|
|
109
|
+
return out;
|
|
110
|
+
};
|
|
111
|
+
const unpair = (index) => {
|
|
112
|
+
if ($.has(index))
|
|
113
|
+
return $.get(index);
|
|
114
|
+
const [type, value] = _[index];
|
|
115
|
+
switch (type) {
|
|
116
|
+
case PRIMITIVE:
|
|
117
|
+
case VOID:
|
|
118
|
+
return as(value, index);
|
|
119
|
+
case ARRAY: {
|
|
120
|
+
const arr = as([], index);
|
|
121
|
+
for (const index2 of value)
|
|
122
|
+
arr.push(unpair(index2));
|
|
123
|
+
return arr;
|
|
124
|
+
}
|
|
125
|
+
case OBJECT: {
|
|
126
|
+
const object = as({}, index);
|
|
127
|
+
for (const [key, index2] of value)
|
|
128
|
+
object[unpair(key)] = unpair(index2);
|
|
129
|
+
return object;
|
|
130
|
+
}
|
|
131
|
+
case DATE:
|
|
132
|
+
return as(new Date(value), index);
|
|
133
|
+
case REGEXP: {
|
|
134
|
+
const { source, flags } = value;
|
|
135
|
+
return as(new RegExp(source, flags), index);
|
|
136
|
+
}
|
|
137
|
+
case MAP: {
|
|
138
|
+
const map = as(/* @__PURE__ */ new Map(), index);
|
|
139
|
+
for (const [key, index2] of value)
|
|
140
|
+
map.set(unpair(key), unpair(index2));
|
|
141
|
+
return map;
|
|
142
|
+
}
|
|
143
|
+
case SET: {
|
|
144
|
+
const set = as(/* @__PURE__ */ new Set(), index);
|
|
145
|
+
for (const index2 of value)
|
|
146
|
+
set.add(unpair(index2));
|
|
147
|
+
return set;
|
|
148
|
+
}
|
|
149
|
+
case ERROR: {
|
|
150
|
+
const { name, message } = value;
|
|
151
|
+
return as(new env[name](message), index);
|
|
152
|
+
}
|
|
153
|
+
case BIGINT:
|
|
154
|
+
return as(BigInt(value), index);
|
|
155
|
+
case "BigInt":
|
|
156
|
+
return as(Object(BigInt(value)), index);
|
|
157
|
+
}
|
|
158
|
+
return as(new env[type](value), index);
|
|
159
|
+
};
|
|
160
|
+
return unpair;
|
|
161
|
+
};
|
|
162
|
+
var deserialize = (serialized) => deserializer(/* @__PURE__ */ new Map(), serialized)(0);
|
|
163
|
+
var EMPTY = "";
|
|
164
|
+
var { toString } = {};
|
|
165
|
+
var { keys } = Object;
|
|
166
|
+
var typeOf = (value) => {
|
|
167
|
+
const type = typeof value;
|
|
168
|
+
if (type !== "object" || !value)
|
|
169
|
+
return [PRIMITIVE, type];
|
|
170
|
+
const asString = toString.call(value).slice(8, -1);
|
|
171
|
+
switch (asString) {
|
|
172
|
+
case "Array":
|
|
173
|
+
return [ARRAY, EMPTY];
|
|
174
|
+
case "Object":
|
|
175
|
+
return [OBJECT, EMPTY];
|
|
176
|
+
case "Date":
|
|
177
|
+
return [DATE, EMPTY];
|
|
178
|
+
case "RegExp":
|
|
179
|
+
return [REGEXP, EMPTY];
|
|
180
|
+
case "Map":
|
|
181
|
+
return [MAP, EMPTY];
|
|
182
|
+
case "Set":
|
|
183
|
+
return [SET, EMPTY];
|
|
184
|
+
}
|
|
185
|
+
if (asString.includes("Array"))
|
|
186
|
+
return [ARRAY, asString];
|
|
187
|
+
if (asString.includes("Error"))
|
|
188
|
+
return [ERROR, asString];
|
|
189
|
+
return [OBJECT, asString];
|
|
190
|
+
};
|
|
191
|
+
var shouldSkip = ([TYPE, type]) => TYPE === PRIMITIVE && (type === "function" || type === "symbol");
|
|
192
|
+
var serializer = (strict, json, $, _) => {
|
|
193
|
+
const as = (out, value) => {
|
|
194
|
+
const index = _.push(out) - 1;
|
|
195
|
+
$.set(value, index);
|
|
196
|
+
return index;
|
|
197
|
+
};
|
|
198
|
+
const pair = (value) => {
|
|
199
|
+
if ($.has(value))
|
|
200
|
+
return $.get(value);
|
|
201
|
+
let [TYPE, type] = typeOf(value);
|
|
202
|
+
switch (TYPE) {
|
|
203
|
+
case PRIMITIVE: {
|
|
204
|
+
let entry = value;
|
|
205
|
+
switch (type) {
|
|
206
|
+
case "bigint":
|
|
207
|
+
TYPE = BIGINT;
|
|
208
|
+
entry = value.toString();
|
|
209
|
+
break;
|
|
210
|
+
case "function":
|
|
211
|
+
case "symbol":
|
|
212
|
+
if (strict)
|
|
213
|
+
throw new TypeError("unable to serialize " + type);
|
|
214
|
+
entry = null;
|
|
215
|
+
break;
|
|
216
|
+
case "undefined":
|
|
217
|
+
return as([VOID], value);
|
|
218
|
+
}
|
|
219
|
+
return as([TYPE, entry], value);
|
|
220
|
+
}
|
|
221
|
+
case ARRAY: {
|
|
222
|
+
if (type)
|
|
223
|
+
return as([type, [...value]], value);
|
|
224
|
+
const arr = [];
|
|
225
|
+
const index = as([TYPE, arr], value);
|
|
226
|
+
for (const entry of value)
|
|
227
|
+
arr.push(pair(entry));
|
|
228
|
+
return index;
|
|
229
|
+
}
|
|
230
|
+
case OBJECT: {
|
|
231
|
+
if (type) {
|
|
232
|
+
switch (type) {
|
|
233
|
+
case "BigInt":
|
|
234
|
+
return as([type, value.toString()], value);
|
|
235
|
+
case "Boolean":
|
|
236
|
+
case "Number":
|
|
237
|
+
case "String":
|
|
238
|
+
return as([type, value.valueOf()], value);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (json && "toJSON" in value)
|
|
242
|
+
return pair(value.toJSON());
|
|
243
|
+
const entries = [];
|
|
244
|
+
const index = as([TYPE, entries], value);
|
|
245
|
+
for (const key of keys(value)) {
|
|
246
|
+
if (strict || !shouldSkip(typeOf(value[key])))
|
|
247
|
+
entries.push([pair(key), pair(value[key])]);
|
|
248
|
+
}
|
|
249
|
+
return index;
|
|
250
|
+
}
|
|
251
|
+
case DATE:
|
|
252
|
+
return as([TYPE, value.toISOString()], value);
|
|
253
|
+
case REGEXP: {
|
|
254
|
+
const { source, flags } = value;
|
|
255
|
+
return as([TYPE, { source, flags }], value);
|
|
256
|
+
}
|
|
257
|
+
case MAP: {
|
|
258
|
+
const entries = [];
|
|
259
|
+
const index = as([TYPE, entries], value);
|
|
260
|
+
for (const [key, entry] of value) {
|
|
261
|
+
if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry))))
|
|
262
|
+
entries.push([pair(key), pair(entry)]);
|
|
263
|
+
}
|
|
264
|
+
return index;
|
|
265
|
+
}
|
|
266
|
+
case SET: {
|
|
267
|
+
const entries = [];
|
|
268
|
+
const index = as([TYPE, entries], value);
|
|
269
|
+
for (const entry of value) {
|
|
270
|
+
if (strict || !shouldSkip(typeOf(entry)))
|
|
271
|
+
entries.push(pair(entry));
|
|
272
|
+
}
|
|
273
|
+
return index;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const { message } = value;
|
|
277
|
+
return as([TYPE, { name: type, message }], value);
|
|
278
|
+
};
|
|
279
|
+
return pair;
|
|
280
|
+
};
|
|
281
|
+
var serialize = (value, { json, lossy } = {}) => {
|
|
282
|
+
const _ = [];
|
|
283
|
+
return serializer(!(json || lossy), !!json, /* @__PURE__ */ new Map(), _)(value), _;
|
|
284
|
+
};
|
|
285
|
+
var esm_default = typeof structuredClone === "function" ? (
|
|
286
|
+
/* c8 ignore start */
|
|
287
|
+
(any, options) => options && ("json" in options || "lossy" in options) ? deserialize(serialize(any, options)) : structuredClone(any)
|
|
288
|
+
) : (any, options) => deserialize(serialize(any, options));
|
|
289
|
+
var CacheData = class _CacheData {
|
|
290
|
+
static IsDirty(data) {
|
|
291
|
+
return data.dirty;
|
|
292
|
+
}
|
|
293
|
+
static SetDirty(data, value) {
|
|
294
|
+
data.dirty = value;
|
|
295
|
+
return data;
|
|
296
|
+
}
|
|
297
|
+
static StructuredClone = globalThis.structuredClone ?? esm_default;
|
|
298
|
+
_raw;
|
|
299
|
+
generator;
|
|
300
|
+
dirty;
|
|
301
|
+
constructor(generator) {
|
|
302
|
+
this._raw = void 0;
|
|
303
|
+
this.dirty = false;
|
|
304
|
+
this.generator = generator;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* This is cached data.
|
|
308
|
+
* It was generated at the time of caching, so there is a risk of modification if it's an object due to shallow copying.
|
|
309
|
+
* 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.
|
|
310
|
+
*/
|
|
311
|
+
get raw() {
|
|
312
|
+
if (!this.dirty) {
|
|
313
|
+
throw new Error(`The data is not initialized and cannot be accessed. Please use the 'ensure' or 'set' method to create the data first.`);
|
|
314
|
+
}
|
|
315
|
+
return this._raw;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* The method returns a copied value of the cached data.
|
|
319
|
+
* You can pass a function as a parameter to copy the value. This parameter function should return the copied value.
|
|
320
|
+
*
|
|
321
|
+
* If no parameter is passed, it defaults to using Javascript's or \@ungap/structured-clone's `structuredClone` function to copy the value.
|
|
322
|
+
* If you prefer shallow copying instead of deep copying,
|
|
323
|
+
* you can use the default options `array-shallow-copy`, `object-shallow-copy` and `deep-copy`,
|
|
324
|
+
* which are replaced with functions to shallow copy arrays and objects, respectively. This is a syntactic sugar.
|
|
325
|
+
* @param strategy The function that returns the copied value.
|
|
326
|
+
* If you want to perform a shallow copy, simply pass the strings `array-shallow-copy` or `object-shallow-copy` for easy use.
|
|
327
|
+
* The default is `structuredClone`.
|
|
328
|
+
*/
|
|
329
|
+
clone(strategy = "deep-copy") {
|
|
330
|
+
if (strategy && typeof strategy !== "string") {
|
|
331
|
+
return strategy(this.raw);
|
|
332
|
+
}
|
|
333
|
+
switch (strategy) {
|
|
334
|
+
case "array-shallow-copy":
|
|
335
|
+
return [].concat(this.raw);
|
|
336
|
+
case "object-shallow-copy":
|
|
337
|
+
return Object.assign({}, this.raw);
|
|
338
|
+
case "deep-copy":
|
|
339
|
+
default:
|
|
340
|
+
return _CacheData.StructuredClone(this.raw);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
var CacheDataSync = class extends CacheData {
|
|
345
|
+
static EmptyDataGenerator = () => void 0;
|
|
346
|
+
static Update(branch, data, generator) {
|
|
347
|
+
data._raw = generator(branch);
|
|
348
|
+
data.generator = generator;
|
|
349
|
+
data.dirty = true;
|
|
350
|
+
return data;
|
|
351
|
+
}
|
|
352
|
+
static Cache(branch, data) {
|
|
353
|
+
data._raw = data.generator(branch);
|
|
354
|
+
return data;
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
var CacheBranchSync = class _CacheBranchSync extends CacheBranch {
|
|
358
|
+
root;
|
|
359
|
+
constructor(generator = CacheDataSync.EmptyDataGenerator, root) {
|
|
360
|
+
super(new CacheDataSync(generator));
|
|
361
|
+
this.root = root ?? this;
|
|
362
|
+
}
|
|
363
|
+
ensureBranch(key, generator) {
|
|
364
|
+
return this.to(key, (b, k) => {
|
|
365
|
+
const branch = b;
|
|
366
|
+
if (!branch.branches.has(k)) {
|
|
367
|
+
branch.branches.set(k, new _CacheBranchSync(generator, this.root));
|
|
368
|
+
}
|
|
369
|
+
return branch.branches.get(k);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
getBranch(key) {
|
|
373
|
+
return this.to(key, (b, k) => {
|
|
374
|
+
const branch = b;
|
|
375
|
+
if (!branch.branches.has(k)) {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
return branch.branches.get(k);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
cache(key, recursive) {
|
|
382
|
+
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
383
|
+
if (!branch) {
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
if (recursive === "bottom-up") {
|
|
387
|
+
for (const key2 of branch.branches.keys()) {
|
|
388
|
+
branch.cache(key2, recursive);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
CacheDataSync.Cache(this.root, branch.data);
|
|
392
|
+
if (recursive === "top-down") {
|
|
393
|
+
for (const key2 of branch.branches.keys()) {
|
|
394
|
+
branch.cache(key2, recursive);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return this;
|
|
398
|
+
}
|
|
66
399
|
ensure(key, generator) {
|
|
67
|
-
|
|
68
|
-
|
|
400
|
+
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
401
|
+
if (!CacheDataSync.IsDirty(branch.data)) {
|
|
402
|
+
CacheDataSync.Update(this.root, branch.data, generator);
|
|
403
|
+
}
|
|
404
|
+
return branch.data;
|
|
405
|
+
}
|
|
406
|
+
get(key) {
|
|
407
|
+
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
408
|
+
if (!CacheDataSync.IsDirty(branch.data)) {
|
|
409
|
+
return void 0;
|
|
410
|
+
}
|
|
411
|
+
return branch.data;
|
|
412
|
+
}
|
|
413
|
+
set(key, generator) {
|
|
414
|
+
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
415
|
+
CacheDataSync.Update(this.root, branch.data, generator);
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
418
|
+
delete(key) {
|
|
419
|
+
const branch = this.ensureBranch(key, CacheDataSync.EmptyDataGenerator);
|
|
420
|
+
if (branch) {
|
|
421
|
+
branch.branches.clear();
|
|
422
|
+
CacheDataSync.Update(this.root, branch.data, CacheDataSync.EmptyDataGenerator);
|
|
423
|
+
CacheDataSync.SetDirty(branch.data, false);
|
|
69
424
|
}
|
|
70
|
-
return this
|
|
425
|
+
return this;
|
|
71
426
|
}
|
|
72
427
|
};
|
|
73
428
|
|
|
74
429
|
// src/base/BPTree.ts
|
|
75
430
|
var BPTree = class {
|
|
76
|
-
|
|
431
|
+
_cachedRegexp;
|
|
77
432
|
strategy;
|
|
78
433
|
comparator;
|
|
79
434
|
nodes;
|
|
@@ -91,10 +446,10 @@ var BPTree = class {
|
|
|
91
446
|
like: (nv, v) => {
|
|
92
447
|
const nodeValue = this.comparator.match(nv);
|
|
93
448
|
const value = this.comparator.match(v);
|
|
94
|
-
const regexp = this.
|
|
449
|
+
const regexp = this._cachedRegexp.ensure(value, () => {
|
|
95
450
|
const pattern = value.replace(/%/g, ".*").replace(/_/g, ".");
|
|
96
451
|
return new RegExp(`^${pattern}$`, "i");
|
|
97
|
-
});
|
|
452
|
+
}).raw;
|
|
98
453
|
return regexp.test(nodeValue);
|
|
99
454
|
}
|
|
100
455
|
};
|
|
@@ -126,7 +481,7 @@ var BPTree = class {
|
|
|
126
481
|
like: true
|
|
127
482
|
};
|
|
128
483
|
constructor(strategy, comparator) {
|
|
129
|
-
this.
|
|
484
|
+
this._cachedRegexp = new CacheBranchSync();
|
|
130
485
|
this._nodeCreateBuffer = /* @__PURE__ */ new Map();
|
|
131
486
|
this._nodeUpdateBuffer = /* @__PURE__ */ new Map();
|
|
132
487
|
this.nodes = /* @__PURE__ */ new Map();
|
|
@@ -138,8 +493,8 @@ var BPTree = class {
|
|
|
138
493
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
139
494
|
const nValue = node.values[i];
|
|
140
495
|
if (this.comparator.isSame(value, nValue)) {
|
|
141
|
-
const
|
|
142
|
-
|
|
496
|
+
const keys2 = node.keys[i];
|
|
497
|
+
keys2.push(key);
|
|
143
498
|
this.bufferForNodeUpdate(node);
|
|
144
499
|
break;
|
|
145
500
|
} else if (this.comparator.isLower(value, nValue)) {
|
|
@@ -190,12 +545,12 @@ var BPTreeSync = class extends BPTree {
|
|
|
190
545
|
let i = node.values.length;
|
|
191
546
|
while (i--) {
|
|
192
547
|
const nValue = node.values[i];
|
|
193
|
-
const
|
|
548
|
+
const keys2 = node.keys[i];
|
|
194
549
|
if (comparator(nValue, value)) {
|
|
195
550
|
found = true;
|
|
196
|
-
let j =
|
|
551
|
+
let j = keys2.length;
|
|
197
552
|
while (j--) {
|
|
198
|
-
pairs.push({ key:
|
|
553
|
+
pairs.push({ key: keys2[j], value: nValue });
|
|
199
554
|
}
|
|
200
555
|
} else if (found && !fullScan) {
|
|
201
556
|
done = true;
|
|
@@ -218,10 +573,10 @@ var BPTreeSync = class extends BPTree {
|
|
|
218
573
|
while (!done) {
|
|
219
574
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
220
575
|
const nValue = node.values[i];
|
|
221
|
-
const
|
|
576
|
+
const keys2 = node.keys[i];
|
|
222
577
|
if (comparator(nValue, value)) {
|
|
223
578
|
found = true;
|
|
224
|
-
for (const key of
|
|
579
|
+
for (const key of keys2) {
|
|
225
580
|
pairs.push({ key, value: nValue });
|
|
226
581
|
}
|
|
227
582
|
} else if (found && !fullScan) {
|
|
@@ -254,11 +609,11 @@ var BPTreeSync = class extends BPTree {
|
|
|
254
609
|
}
|
|
255
610
|
return id;
|
|
256
611
|
}
|
|
257
|
-
_createNode(
|
|
612
|
+
_createNode(keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
|
|
258
613
|
const id = this._createNodeId();
|
|
259
614
|
const node = {
|
|
260
615
|
id,
|
|
261
|
-
keys,
|
|
616
|
+
keys: keys2,
|
|
262
617
|
values,
|
|
263
618
|
leaf,
|
|
264
619
|
parent,
|
|
@@ -288,8 +643,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
288
643
|
}
|
|
289
644
|
}
|
|
290
645
|
if (this.root === node && node.keys.length === 1) {
|
|
291
|
-
const
|
|
292
|
-
this.root = this.getNode(
|
|
646
|
+
const keys2 = node.keys;
|
|
647
|
+
this.root = this.getNode(keys2[0]);
|
|
293
648
|
this.root.parent = 0;
|
|
294
649
|
this.strategy.head.root = this.root.id;
|
|
295
650
|
this.bufferForNodeUpdate(this.root);
|
|
@@ -363,8 +718,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
363
718
|
}
|
|
364
719
|
pointer.values.push(...node.values);
|
|
365
720
|
if (!pointer.leaf) {
|
|
366
|
-
const
|
|
367
|
-
for (const key2 of
|
|
721
|
+
const keys2 = pointer.keys;
|
|
722
|
+
for (const key2 of keys2) {
|
|
368
723
|
const node2 = this.getNode(key2);
|
|
369
724
|
node2.parent = pointer.id;
|
|
370
725
|
this.bufferForNodeUpdate(node2);
|
|
@@ -466,33 +821,6 @@ var BPTreeSync = class extends BPTree {
|
|
|
466
821
|
}
|
|
467
822
|
}
|
|
468
823
|
}
|
|
469
|
-
_insertAtLeaf(node, key, value) {
|
|
470
|
-
if (node.values.length) {
|
|
471
|
-
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
472
|
-
const nValue = node.values[i];
|
|
473
|
-
if (this.comparator.isSame(value, nValue)) {
|
|
474
|
-
const keys = node.keys[i];
|
|
475
|
-
keys.push(key);
|
|
476
|
-
this.bufferForNodeUpdate(node);
|
|
477
|
-
break;
|
|
478
|
-
} else if (this.comparator.isLower(value, nValue)) {
|
|
479
|
-
node.values.splice(i, 0, value);
|
|
480
|
-
node.keys.splice(i, 0, [key]);
|
|
481
|
-
this.bufferForNodeUpdate(node);
|
|
482
|
-
break;
|
|
483
|
-
} else if (i + 1 === node.values.length) {
|
|
484
|
-
node.values.push(value);
|
|
485
|
-
node.keys.push([key]);
|
|
486
|
-
this.bufferForNodeUpdate(node);
|
|
487
|
-
break;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
} else {
|
|
491
|
-
node.values = [value];
|
|
492
|
-
node.keys = [[key]];
|
|
493
|
-
this.bufferForNodeUpdate(node);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
824
|
_insertInParent(node, value, pointer) {
|
|
497
825
|
if (this.root === node) {
|
|
498
826
|
const root = this._createNode([node.id, pointer.id], [value]);
|
|
@@ -590,8 +918,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
590
918
|
leftestNode() {
|
|
591
919
|
let node = this.root;
|
|
592
920
|
while (!node.leaf) {
|
|
593
|
-
const
|
|
594
|
-
node = this.getNode(
|
|
921
|
+
const keys2 = node.keys;
|
|
922
|
+
node = this.getNode(keys2[0]);
|
|
595
923
|
}
|
|
596
924
|
return node;
|
|
597
925
|
}
|
|
@@ -695,17 +1023,17 @@ var BPTreeSync = class extends BPTree {
|
|
|
695
1023
|
while (i--) {
|
|
696
1024
|
const nValue = node.values[i];
|
|
697
1025
|
if (this.comparator.isSame(value, nValue)) {
|
|
698
|
-
const
|
|
699
|
-
if (
|
|
700
|
-
if (
|
|
701
|
-
|
|
1026
|
+
const keys2 = node.keys[i];
|
|
1027
|
+
if (keys2.includes(key)) {
|
|
1028
|
+
if (keys2.length > 1) {
|
|
1029
|
+
keys2.splice(keys2.indexOf(key), 1);
|
|
702
1030
|
this.bufferForNodeUpdate(node);
|
|
703
1031
|
} else if (node === this.root) {
|
|
704
1032
|
node.values.splice(i, 1);
|
|
705
1033
|
node.keys.splice(i, 1);
|
|
706
1034
|
this.bufferForNodeUpdate(node);
|
|
707
1035
|
} else {
|
|
708
|
-
|
|
1036
|
+
keys2.splice(keys2.indexOf(key), 1);
|
|
709
1037
|
node.keys.splice(i, 1);
|
|
710
1038
|
node.values.splice(node.values.indexOf(value), 1);
|
|
711
1039
|
this._deleteEntry(node, key, value);
|
|
@@ -723,8 +1051,8 @@ var BPTreeSync = class extends BPTree {
|
|
|
723
1051
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
724
1052
|
const nValue = node.values[i];
|
|
725
1053
|
if (this.comparator.isSame(value, nValue)) {
|
|
726
|
-
const
|
|
727
|
-
return
|
|
1054
|
+
const keys2 = node.keys[i];
|
|
1055
|
+
return keys2.includes(key);
|
|
728
1056
|
}
|
|
729
1057
|
}
|
|
730
1058
|
return false;
|
|
@@ -734,13 +1062,13 @@ var BPTreeSync = class extends BPTree {
|
|
|
734
1062
|
this.commitHeadBuffer();
|
|
735
1063
|
}
|
|
736
1064
|
forceUpdate() {
|
|
737
|
-
const
|
|
1065
|
+
const keys2 = [...this.nodes.keys()];
|
|
738
1066
|
this.nodes.clear();
|
|
739
1067
|
this.init();
|
|
740
|
-
for (const key of
|
|
1068
|
+
for (const key of keys2) {
|
|
741
1069
|
this.getNode(key);
|
|
742
1070
|
}
|
|
743
|
-
return
|
|
1071
|
+
return keys2.length;
|
|
744
1072
|
}
|
|
745
1073
|
};
|
|
746
1074
|
|
|
@@ -749,7 +1077,7 @@ var BPTreeAsync = class extends BPTree {
|
|
|
749
1077
|
constructor(strategy, comparator) {
|
|
750
1078
|
super(strategy, comparator);
|
|
751
1079
|
}
|
|
752
|
-
async _getPairsRightToLeft(value, startNode,
|
|
1080
|
+
async _getPairsRightToLeft(value, startNode, fullScan, comparator) {
|
|
753
1081
|
const pairs = [];
|
|
754
1082
|
let node = startNode;
|
|
755
1083
|
let done = false;
|
|
@@ -758,14 +1086,14 @@ var BPTreeAsync = class extends BPTree {
|
|
|
758
1086
|
let i = node.values.length;
|
|
759
1087
|
while (i--) {
|
|
760
1088
|
const nValue = node.values[i];
|
|
761
|
-
const
|
|
1089
|
+
const keys2 = node.keys[i];
|
|
762
1090
|
if (comparator(nValue, value)) {
|
|
763
1091
|
found = true;
|
|
764
|
-
let j =
|
|
1092
|
+
let j = keys2.length;
|
|
765
1093
|
while (j--) {
|
|
766
|
-
pairs.push({ key:
|
|
1094
|
+
pairs.push({ key: keys2[j], value: nValue });
|
|
767
1095
|
}
|
|
768
|
-
} else if (found && !
|
|
1096
|
+
} else if (found && !fullScan) {
|
|
769
1097
|
done = true;
|
|
770
1098
|
break;
|
|
771
1099
|
}
|
|
@@ -778,7 +1106,7 @@ var BPTreeAsync = class extends BPTree {
|
|
|
778
1106
|
}
|
|
779
1107
|
return pairs.reverse();
|
|
780
1108
|
}
|
|
781
|
-
async _getPairsLeftToRight(value, startNode,
|
|
1109
|
+
async _getPairsLeftToRight(value, startNode, fullScan, comparator) {
|
|
782
1110
|
const pairs = [];
|
|
783
1111
|
let node = startNode;
|
|
784
1112
|
let done = false;
|
|
@@ -786,13 +1114,13 @@ var BPTreeAsync = class extends BPTree {
|
|
|
786
1114
|
while (!done) {
|
|
787
1115
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
788
1116
|
const nValue = node.values[i];
|
|
789
|
-
const
|
|
1117
|
+
const keys2 = node.keys[i];
|
|
790
1118
|
if (comparator(nValue, value)) {
|
|
791
1119
|
found = true;
|
|
792
|
-
for (const key of
|
|
1120
|
+
for (const key of keys2) {
|
|
793
1121
|
pairs.push({ key, value: nValue });
|
|
794
1122
|
}
|
|
795
|
-
} else if (found && !
|
|
1123
|
+
} else if (found && !fullScan) {
|
|
796
1124
|
done = true;
|
|
797
1125
|
break;
|
|
798
1126
|
}
|
|
@@ -805,12 +1133,12 @@ var BPTreeAsync = class extends BPTree {
|
|
|
805
1133
|
}
|
|
806
1134
|
return pairs;
|
|
807
1135
|
}
|
|
808
|
-
async getPairs(value, startNode,
|
|
1136
|
+
async getPairs(value, startNode, fullScan, comparator, direction) {
|
|
809
1137
|
switch (direction) {
|
|
810
1138
|
case -1:
|
|
811
|
-
return await this._getPairsRightToLeft(value, startNode,
|
|
1139
|
+
return await this._getPairsRightToLeft(value, startNode, fullScan, comparator);
|
|
812
1140
|
case 1:
|
|
813
|
-
return await this._getPairsLeftToRight(value, startNode,
|
|
1141
|
+
return await this._getPairsLeftToRight(value, startNode, fullScan, comparator);
|
|
814
1142
|
default:
|
|
815
1143
|
throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
|
|
816
1144
|
}
|
|
@@ -822,11 +1150,11 @@ var BPTreeAsync = class extends BPTree {
|
|
|
822
1150
|
}
|
|
823
1151
|
return id;
|
|
824
1152
|
}
|
|
825
|
-
async _createNode(
|
|
1153
|
+
async _createNode(keys2, values, leaf = false, parent = 0, next = 0, prev = 0) {
|
|
826
1154
|
const id = await this._createNodeId();
|
|
827
1155
|
const node = {
|
|
828
1156
|
id,
|
|
829
|
-
keys,
|
|
1157
|
+
keys: keys2,
|
|
830
1158
|
values,
|
|
831
1159
|
leaf,
|
|
832
1160
|
parent,
|
|
@@ -856,8 +1184,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
856
1184
|
}
|
|
857
1185
|
}
|
|
858
1186
|
if (this.root === node && node.keys.length === 1) {
|
|
859
|
-
const
|
|
860
|
-
this.root = await this.getNode(
|
|
1187
|
+
const keys2 = node.keys;
|
|
1188
|
+
this.root = await this.getNode(keys2[0]);
|
|
861
1189
|
this.root.parent = 0;
|
|
862
1190
|
this.strategy.head.root = this.root.id;
|
|
863
1191
|
this.bufferForNodeUpdate(this.root);
|
|
@@ -931,8 +1259,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
931
1259
|
}
|
|
932
1260
|
pointer.values.push(...node.values);
|
|
933
1261
|
if (!pointer.leaf) {
|
|
934
|
-
const
|
|
935
|
-
for (const key2 of
|
|
1262
|
+
const keys2 = pointer.keys;
|
|
1263
|
+
for (const key2 of keys2) {
|
|
936
1264
|
const node2 = await this.getNode(key2);
|
|
937
1265
|
node2.parent = pointer.id;
|
|
938
1266
|
this.bufferForNodeUpdate(node2);
|
|
@@ -1131,8 +1459,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1131
1459
|
async leftestNode() {
|
|
1132
1460
|
let node = this.root;
|
|
1133
1461
|
while (!node.leaf) {
|
|
1134
|
-
const
|
|
1135
|
-
node = await this.getNode(
|
|
1462
|
+
const keys2 = node.keys;
|
|
1463
|
+
node = await this.getNode(keys2[0]);
|
|
1136
1464
|
}
|
|
1137
1465
|
return node;
|
|
1138
1466
|
}
|
|
@@ -1236,17 +1564,17 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1236
1564
|
while (i--) {
|
|
1237
1565
|
const nValue = node.values[i];
|
|
1238
1566
|
if (this.comparator.isSame(value, nValue)) {
|
|
1239
|
-
const
|
|
1240
|
-
if (
|
|
1241
|
-
if (
|
|
1242
|
-
|
|
1567
|
+
const keys2 = node.keys[i];
|
|
1568
|
+
if (keys2.includes(key)) {
|
|
1569
|
+
if (keys2.length > 1) {
|
|
1570
|
+
keys2.splice(keys2.indexOf(key), 1);
|
|
1243
1571
|
this.bufferForNodeUpdate(node);
|
|
1244
1572
|
} else if (node === this.root) {
|
|
1245
1573
|
node.values.splice(i, 1);
|
|
1246
1574
|
node.keys.splice(i, 1);
|
|
1247
1575
|
this.bufferForNodeUpdate(node);
|
|
1248
1576
|
} else {
|
|
1249
|
-
|
|
1577
|
+
keys2.splice(keys2.indexOf(key), 1);
|
|
1250
1578
|
node.keys.splice(i, 1);
|
|
1251
1579
|
node.values.splice(node.values.indexOf(value), 1);
|
|
1252
1580
|
await this._deleteEntry(node, key, value);
|
|
@@ -1264,8 +1592,8 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1264
1592
|
for (let i = 0, len = node.values.length; i < len; i++) {
|
|
1265
1593
|
const nValue = node.values[i];
|
|
1266
1594
|
if (this.comparator.isSame(value, nValue)) {
|
|
1267
|
-
const
|
|
1268
|
-
return
|
|
1595
|
+
const keys2 = node.keys[i];
|
|
1596
|
+
return keys2.includes(key);
|
|
1269
1597
|
}
|
|
1270
1598
|
}
|
|
1271
1599
|
return false;
|
|
@@ -1275,13 +1603,13 @@ var BPTreeAsync = class extends BPTree {
|
|
|
1275
1603
|
await this.commitHeadBuffer();
|
|
1276
1604
|
}
|
|
1277
1605
|
async forceUpdate() {
|
|
1278
|
-
const
|
|
1606
|
+
const keys2 = [...this.nodes.keys()];
|
|
1279
1607
|
this.nodes.clear();
|
|
1280
1608
|
await this.init();
|
|
1281
|
-
for (const key of
|
|
1609
|
+
for (const key of keys2) {
|
|
1282
1610
|
await this.getNode(key);
|
|
1283
1611
|
}
|
|
1284
|
-
return
|
|
1612
|
+
return keys2.length;
|
|
1285
1613
|
}
|
|
1286
1614
|
};
|
|
1287
1615
|
|