serializable-bptree 5.1.3 → 5.1.6

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.
@@ -290,9 +290,10 @@ var CacheEntanglement = class {
290
290
  lifespan;
291
291
  dependencies;
292
292
  caches;
293
- assignments;
294
293
  parameters;
294
+ assignments;
295
295
  dependencyProperties;
296
+ updateRequirements;
296
297
  constructor(creation, option) {
297
298
  option = option ?? {};
298
299
  const {
@@ -306,9 +307,10 @@ var CacheEntanglement = class {
306
307
  this.lifespan = this._normalizeMs(lifespan ?? 0);
307
308
  this.assignments = [];
308
309
  this.caches = new InvertedWeakMap({ lifespan: this.lifespan });
310
+ this.parameters = /* @__PURE__ */ new Map();
309
311
  this.dependencies = dependencies ?? {};
310
312
  this.dependencyProperties = Object.keys(this.dependencies);
311
- this.parameters = {};
313
+ this.updateRequirements = /* @__PURE__ */ new Set();
312
314
  for (const name in this.dependencies) {
313
315
  const dependency = this.dependencies[name];
314
316
  if (!dependency.assignments.includes(this)) {
@@ -322,16 +324,30 @@ var CacheEntanglement = class {
322
324
  }
323
325
  return time;
324
326
  }
327
+ bubbleUpdateSignal(key) {
328
+ this.updateRequirements.add(key);
329
+ for (let i = 0, len = this.assignments.length; i < len; i++) {
330
+ const t = this.assignments[i];
331
+ const instance = t;
332
+ for (const cacheKey of instance.caches.keys()) {
333
+ if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
334
+ instance.bubbleUpdateSignal(cacheKey);
335
+ }
336
+ }
337
+ }
338
+ }
325
339
  dependencyKey(key) {
326
- const tokens = key.split("/");
327
- tokens.pop();
328
- return tokens.join("/");
340
+ const i = key.lastIndexOf("/");
341
+ if (i === -1) {
342
+ return key;
343
+ }
344
+ return key.substring(0, i);
329
345
  }
330
346
  /**
331
347
  * Returns all keys stored in the instance.
332
348
  */
333
349
  keys() {
334
- return this.caches.keys();
350
+ return this.parameters.keys();
335
351
  }
336
352
  /**
337
353
  * Deletes all cache values stored in the instance.
@@ -346,17 +362,33 @@ var CacheEntanglement = class {
346
362
  * @param key The key to search.
347
363
  */
348
364
  exists(key) {
349
- return this.caches.has(key);
365
+ return this.parameters.has(key);
350
366
  }
351
367
  /**
352
- * Returns the cache value stored in the key within the instance. If the cached value is not present, an error is thrown.
368
+ * Checks if there is a cache value stored in the key within the instance.
369
+ * This method is an alias for `exists`.
353
370
  * @param key The key to search.
354
371
  */
355
- get(key) {
356
- if (!this.caches.has(key)) {
357
- throw new Error(`Cache value not found: ${key}`);
372
+ has(key) {
373
+ return this.exists(key);
374
+ }
375
+ /**
376
+ * Deletes the cache value stored in the key within the instance.
377
+ * @param key The key to delete.
378
+ */
379
+ delete(key) {
380
+ this.caches.delete(key);
381
+ this.parameters.delete(key);
382
+ this.updateRequirements.delete(key);
383
+ for (let i = 0, len = this.assignments.length; i < len; i++) {
384
+ const t = this.assignments[i];
385
+ const instance = t;
386
+ for (const cacheKey of instance.keys()) {
387
+ if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
388
+ instance.delete(cacheKey);
389
+ }
390
+ }
358
391
  }
359
- return this.caches.get(key);
360
392
  }
361
393
  };
362
394
  var CacheData = class _CacheData {
@@ -407,6 +439,15 @@ var CacheEntanglementSync = class extends CacheEntanglement {
407
439
  constructor(creation, option) {
408
440
  super(creation, option);
409
441
  }
442
+ recache(key) {
443
+ if (!this.parameters.has(key)) {
444
+ return;
445
+ }
446
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
447
+ this.resolve(key, ...this.parameters.get(key));
448
+ }
449
+ return this.caches.get(key);
450
+ }
410
451
  resolve(key, ...parameter) {
411
452
  const resolved = {};
412
453
  const dependencyKey = this.dependencyKey(key);
@@ -414,59 +455,55 @@ var CacheEntanglementSync = class extends CacheEntanglement {
414
455
  for (let i = 0, len = this.dependencyProperties.length; i < len; i++) {
415
456
  const name = this.dependencyProperties[i];
416
457
  const dependency = this.dependencies[name];
417
- if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
458
+ if (!dependency.exists(key) && !dependency.exists(dependencyKey)) {
418
459
  throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name.toString()}'.`, {
419
460
  cause: {
420
461
  from: this
421
462
  }
422
463
  });
423
464
  }
424
- const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
465
+ const dependencyValue = dependency.recache(key) ?? dependency.recache(dependencyKey);
425
466
  resolved[name] = dependencyValue;
426
467
  }
427
- this.parameters[key] = parameter;
428
468
  const value = new CacheData(this.creation(key, resolved, ...parameter));
469
+ this.updateRequirements.delete(key);
470
+ this.parameters.set(key, parameter);
429
471
  this.caches.set(key, value);
430
472
  return value;
431
473
  }
474
+ get(key) {
475
+ if (!this.parameters.has(key)) {
476
+ throw new Error(`Cache value not found: ${key}`);
477
+ }
478
+ return this.cache(key, ...this.parameters.get(key));
479
+ }
432
480
  cache(key, ...parameter) {
433
- if (!this.caches.has(key)) {
434
- this.update(key, ...parameter);
481
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
482
+ this.resolve(key, ...parameter);
435
483
  } else {
436
484
  this.caches.extendExpire(key);
437
485
  }
438
486
  return this.caches.get(key);
439
487
  }
440
488
  update(key, ...parameter) {
489
+ this.bubbleUpdateSignal(key);
441
490
  this.resolve(key, ...parameter);
442
- for (let i = 0, len = this.assignments.length; i < len; i++) {
443
- const t = this.assignments[i];
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
491
  return this.caches.get(key);
452
492
  }
453
- delete(key) {
454
- this.caches.delete(key);
455
- for (let i = 0, len = this.assignments.length; i < len; i++) {
456
- const t = this.assignments[i];
457
- const instance = t;
458
- for (const cacheKey of instance.caches.keys()) {
459
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
460
- instance.delete(cacheKey);
461
- }
462
- }
463
- }
464
- }
465
493
  };
466
494
  var CacheEntanglementAsync = class extends CacheEntanglement {
467
495
  constructor(creation, option) {
468
496
  super(creation, option);
469
497
  }
498
+ async recache(key) {
499
+ if (!this.parameters.has(key)) {
500
+ return;
501
+ }
502
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
503
+ await this.resolve(key, ...this.parameters.get(key));
504
+ }
505
+ return this.caches.get(key);
506
+ }
470
507
  async resolve(key, ...parameter) {
471
508
  const resolved = {};
472
509
  const dependencyKey = this.dependencyKey(key);
@@ -474,23 +511,30 @@ var CacheEntanglementAsync = class extends CacheEntanglement {
474
511
  for (let i = 0, len = this.dependencyProperties.length; i < len; i++) {
475
512
  const name = this.dependencyProperties[i];
476
513
  const dependency = this.dependencies[name];
477
- if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
514
+ if (!dependency.exists(key) && !dependency.exists(dependencyKey)) {
478
515
  throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name.toString()}'.`, {
479
516
  cause: {
480
517
  from: this
481
518
  }
482
519
  });
483
520
  }
484
- const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
521
+ const dependencyValue = await dependency.recache(key) ?? await dependency.recache(dependencyKey);
485
522
  resolved[name] = dependencyValue;
486
523
  }
487
- this.parameters[key] = parameter;
488
524
  const value = new CacheData(await this.creation(key, resolved, ...parameter));
525
+ this.updateRequirements.delete(key);
526
+ this.parameters.set(key, parameter);
489
527
  this.caches.set(key, value);
490
528
  return value;
491
529
  }
530
+ async get(key) {
531
+ if (!this.parameters.has(key)) {
532
+ throw new Error(`Cache value not found: ${key}`);
533
+ }
534
+ return this.cache(key, ...this.parameters.get(key));
535
+ }
492
536
  async cache(key, ...parameter) {
493
- if (!this.caches.has(key)) {
537
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
494
538
  await this.update(key, ...parameter);
495
539
  } else {
496
540
  this.caches.extendExpire(key);
@@ -498,30 +542,10 @@ var CacheEntanglementAsync = class extends CacheEntanglement {
498
542
  return this.caches.get(key);
499
543
  }
500
544
  async update(key, ...parameter) {
545
+ this.bubbleUpdateSignal(key);
501
546
  await this.resolve(key, ...parameter);
502
- for (let i = 0, len = this.assignments.length; i < len; i++) {
503
- const t = this.assignments[i];
504
- const instance = t;
505
- for (const cacheKey of instance.caches.keys()) {
506
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
507
- await instance.update(cacheKey, ...instance.parameters[cacheKey]);
508
- }
509
- }
510
- }
511
547
  return this.caches.get(key);
512
548
  }
513
- async delete(key) {
514
- this.caches.delete(key);
515
- for (let i = 0, len = this.assignments.length; i < len; i++) {
516
- const t = this.assignments[i];
517
- const instance = t;
518
- for (const cacheKey of instance.caches.keys()) {
519
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
520
- await instance.delete(cacheKey);
521
- }
522
- }
523
- }
524
- }
525
549
  };
526
550
 
527
551
  // src/base/BPTree.ts
@@ -625,9 +649,7 @@ var BPTree = class {
625
649
  if (this.comparator.isSame(value, nValue)) {
626
650
  const keys = node.keys[i];
627
651
  if (keys.includes(key)) {
628
- throw new Error("The key already exists.", {
629
- cause: { key, value }
630
- });
652
+ break;
631
653
  }
632
654
  keys.push(key);
633
655
  this.bufferForNodeUpdate(node);
@@ -256,9 +256,10 @@ var CacheEntanglement = class {
256
256
  lifespan;
257
257
  dependencies;
258
258
  caches;
259
- assignments;
260
259
  parameters;
260
+ assignments;
261
261
  dependencyProperties;
262
+ updateRequirements;
262
263
  constructor(creation, option) {
263
264
  option = option ?? {};
264
265
  const {
@@ -272,9 +273,10 @@ var CacheEntanglement = class {
272
273
  this.lifespan = this._normalizeMs(lifespan ?? 0);
273
274
  this.assignments = [];
274
275
  this.caches = new InvertedWeakMap({ lifespan: this.lifespan });
276
+ this.parameters = /* @__PURE__ */ new Map();
275
277
  this.dependencies = dependencies ?? {};
276
278
  this.dependencyProperties = Object.keys(this.dependencies);
277
- this.parameters = {};
279
+ this.updateRequirements = /* @__PURE__ */ new Set();
278
280
  for (const name in this.dependencies) {
279
281
  const dependency = this.dependencies[name];
280
282
  if (!dependency.assignments.includes(this)) {
@@ -288,16 +290,30 @@ var CacheEntanglement = class {
288
290
  }
289
291
  return time;
290
292
  }
293
+ bubbleUpdateSignal(key) {
294
+ this.updateRequirements.add(key);
295
+ for (let i = 0, len = this.assignments.length; i < len; i++) {
296
+ const t = this.assignments[i];
297
+ const instance = t;
298
+ for (const cacheKey of instance.caches.keys()) {
299
+ if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
300
+ instance.bubbleUpdateSignal(cacheKey);
301
+ }
302
+ }
303
+ }
304
+ }
291
305
  dependencyKey(key) {
292
- const tokens = key.split("/");
293
- tokens.pop();
294
- return tokens.join("/");
306
+ const i = key.lastIndexOf("/");
307
+ if (i === -1) {
308
+ return key;
309
+ }
310
+ return key.substring(0, i);
295
311
  }
296
312
  /**
297
313
  * Returns all keys stored in the instance.
298
314
  */
299
315
  keys() {
300
- return this.caches.keys();
316
+ return this.parameters.keys();
301
317
  }
302
318
  /**
303
319
  * Deletes all cache values stored in the instance.
@@ -312,17 +328,33 @@ var CacheEntanglement = class {
312
328
  * @param key The key to search.
313
329
  */
314
330
  exists(key) {
315
- return this.caches.has(key);
331
+ return this.parameters.has(key);
316
332
  }
317
333
  /**
318
- * Returns the cache value stored in the key within the instance. If the cached value is not present, an error is thrown.
334
+ * Checks if there is a cache value stored in the key within the instance.
335
+ * This method is an alias for `exists`.
319
336
  * @param key The key to search.
320
337
  */
321
- get(key) {
322
- if (!this.caches.has(key)) {
323
- throw new Error(`Cache value not found: ${key}`);
338
+ has(key) {
339
+ return this.exists(key);
340
+ }
341
+ /**
342
+ * Deletes the cache value stored in the key within the instance.
343
+ * @param key The key to delete.
344
+ */
345
+ delete(key) {
346
+ this.caches.delete(key);
347
+ this.parameters.delete(key);
348
+ this.updateRequirements.delete(key);
349
+ for (let i = 0, len = this.assignments.length; i < len; i++) {
350
+ const t = this.assignments[i];
351
+ const instance = t;
352
+ for (const cacheKey of instance.keys()) {
353
+ if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
354
+ instance.delete(cacheKey);
355
+ }
356
+ }
324
357
  }
325
- return this.caches.get(key);
326
358
  }
327
359
  };
328
360
  var CacheData = class _CacheData {
@@ -373,6 +405,15 @@ var CacheEntanglementSync = class extends CacheEntanglement {
373
405
  constructor(creation, option) {
374
406
  super(creation, option);
375
407
  }
408
+ recache(key) {
409
+ if (!this.parameters.has(key)) {
410
+ return;
411
+ }
412
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
413
+ this.resolve(key, ...this.parameters.get(key));
414
+ }
415
+ return this.caches.get(key);
416
+ }
376
417
  resolve(key, ...parameter) {
377
418
  const resolved = {};
378
419
  const dependencyKey = this.dependencyKey(key);
@@ -380,59 +421,55 @@ var CacheEntanglementSync = class extends CacheEntanglement {
380
421
  for (let i = 0, len = this.dependencyProperties.length; i < len; i++) {
381
422
  const name = this.dependencyProperties[i];
382
423
  const dependency = this.dependencies[name];
383
- if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
424
+ if (!dependency.exists(key) && !dependency.exists(dependencyKey)) {
384
425
  throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name.toString()}'.`, {
385
426
  cause: {
386
427
  from: this
387
428
  }
388
429
  });
389
430
  }
390
- const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
431
+ const dependencyValue = dependency.recache(key) ?? dependency.recache(dependencyKey);
391
432
  resolved[name] = dependencyValue;
392
433
  }
393
- this.parameters[key] = parameter;
394
434
  const value = new CacheData(this.creation(key, resolved, ...parameter));
435
+ this.updateRequirements.delete(key);
436
+ this.parameters.set(key, parameter);
395
437
  this.caches.set(key, value);
396
438
  return value;
397
439
  }
440
+ get(key) {
441
+ if (!this.parameters.has(key)) {
442
+ throw new Error(`Cache value not found: ${key}`);
443
+ }
444
+ return this.cache(key, ...this.parameters.get(key));
445
+ }
398
446
  cache(key, ...parameter) {
399
- if (!this.caches.has(key)) {
400
- this.update(key, ...parameter);
447
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
448
+ this.resolve(key, ...parameter);
401
449
  } else {
402
450
  this.caches.extendExpire(key);
403
451
  }
404
452
  return this.caches.get(key);
405
453
  }
406
454
  update(key, ...parameter) {
455
+ this.bubbleUpdateSignal(key);
407
456
  this.resolve(key, ...parameter);
408
- for (let i = 0, len = this.assignments.length; i < len; i++) {
409
- const t = this.assignments[i];
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
457
  return this.caches.get(key);
418
458
  }
419
- delete(key) {
420
- this.caches.delete(key);
421
- for (let i = 0, len = this.assignments.length; i < len; i++) {
422
- const t = this.assignments[i];
423
- const instance = t;
424
- for (const cacheKey of instance.caches.keys()) {
425
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
426
- instance.delete(cacheKey);
427
- }
428
- }
429
- }
430
- }
431
459
  };
432
460
  var CacheEntanglementAsync = class extends CacheEntanglement {
433
461
  constructor(creation, option) {
434
462
  super(creation, option);
435
463
  }
464
+ async recache(key) {
465
+ if (!this.parameters.has(key)) {
466
+ return;
467
+ }
468
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
469
+ await this.resolve(key, ...this.parameters.get(key));
470
+ }
471
+ return this.caches.get(key);
472
+ }
436
473
  async resolve(key, ...parameter) {
437
474
  const resolved = {};
438
475
  const dependencyKey = this.dependencyKey(key);
@@ -440,23 +477,30 @@ var CacheEntanglementAsync = class extends CacheEntanglement {
440
477
  for (let i = 0, len = this.dependencyProperties.length; i < len; i++) {
441
478
  const name = this.dependencyProperties[i];
442
479
  const dependency = this.dependencies[name];
443
- if (!dependency.caches.has(key) && !dependency.caches.has(dependencyKey)) {
480
+ if (!dependency.exists(key) && !dependency.exists(dependencyKey)) {
444
481
  throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name.toString()}'.`, {
445
482
  cause: {
446
483
  from: this
447
484
  }
448
485
  });
449
486
  }
450
- const dependencyValue = dependency.caches.get(key) ?? dependency.caches.get(dependencyKey);
487
+ const dependencyValue = await dependency.recache(key) ?? await dependency.recache(dependencyKey);
451
488
  resolved[name] = dependencyValue;
452
489
  }
453
- this.parameters[key] = parameter;
454
490
  const value = new CacheData(await this.creation(key, resolved, ...parameter));
491
+ this.updateRequirements.delete(key);
492
+ this.parameters.set(key, parameter);
455
493
  this.caches.set(key, value);
456
494
  return value;
457
495
  }
496
+ async get(key) {
497
+ if (!this.parameters.has(key)) {
498
+ throw new Error(`Cache value not found: ${key}`);
499
+ }
500
+ return this.cache(key, ...this.parameters.get(key));
501
+ }
458
502
  async cache(key, ...parameter) {
459
- if (!this.caches.has(key)) {
503
+ if (!this.caches.has(key) || this.updateRequirements.has(key)) {
460
504
  await this.update(key, ...parameter);
461
505
  } else {
462
506
  this.caches.extendExpire(key);
@@ -464,30 +508,10 @@ var CacheEntanglementAsync = class extends CacheEntanglement {
464
508
  return this.caches.get(key);
465
509
  }
466
510
  async update(key, ...parameter) {
511
+ this.bubbleUpdateSignal(key);
467
512
  await this.resolve(key, ...parameter);
468
- for (let i = 0, len = this.assignments.length; i < len; i++) {
469
- const t = this.assignments[i];
470
- const instance = t;
471
- for (const cacheKey of instance.caches.keys()) {
472
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
473
- await instance.update(cacheKey, ...instance.parameters[cacheKey]);
474
- }
475
- }
476
- }
477
513
  return this.caches.get(key);
478
514
  }
479
- async delete(key) {
480
- this.caches.delete(key);
481
- for (let i = 0, len = this.assignments.length; i < len; i++) {
482
- const t = this.assignments[i];
483
- const instance = t;
484
- for (const cacheKey of instance.caches.keys()) {
485
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
486
- await instance.delete(cacheKey);
487
- }
488
- }
489
- }
490
- }
491
515
  };
492
516
 
493
517
  // src/base/BPTree.ts
@@ -591,9 +615,7 @@ var BPTree = class {
591
615
  if (this.comparator.isSame(value, nValue)) {
592
616
  const keys = node.keys[i];
593
617
  if (keys.includes(key)) {
594
- throw new Error("The key already exists.", {
595
- cause: { key, value }
596
- });
618
+ break;
597
619
  }
598
620
  keys.push(key);
599
621
  this.bufferForNodeUpdate(node);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "5.1.3",
3
+ "version": "5.1.6",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "main": "./dist/cjs/index.cjs",
@@ -39,10 +39,10 @@
39
39
  "@types/jest": "^29.5.14",
40
40
  "esbuild": "^0.25.5",
41
41
  "jest": "^29.7.0",
42
- "ts-jest": "^29.3.4",
42
+ "ts-jest": "^29.4.0",
43
43
  "typescript": "^5.8.3"
44
44
  },
45
45
  "dependencies": {
46
- "cache-entanglement": "^1.5.5"
46
+ "cache-entanglement": "^1.6.0"
47
47
  }
48
48
  }