serializable-bptree 9.0.4-alpha.2 → 9.0.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.
@@ -2309,8 +2309,8 @@ function deleteEntry(ops, ctx, node, key, comparator) {
2309
2309
  } else {
2310
2310
  pointerP0 = siblingNode.keys.splice(0, 1)[0];
2311
2311
  pointerK0 = siblingNode.values.splice(0, 1)[0];
2312
- node.keys = node.keys.concat(pointerP0);
2313
- node.values = node.values.concat(pointerK0);
2312
+ node.keys = [...node.keys, pointerP0];
2313
+ node.values = [...node.values, pointerK0];
2314
2314
  parentNode = cloneNode(ops.getNode(node.parent));
2315
2315
  const pointerIndex = parentNode.keys.indexOf(siblingNode.id);
2316
2316
  if (pointerIndex > 0) {
@@ -2390,6 +2390,13 @@ function deleteOp(ops, ctx, key, comparator, value) {
2390
2390
  break;
2391
2391
  }
2392
2392
  }
2393
+ function batchDeleteOp(ops, ctx, entries, comparator) {
2394
+ if (entries.length === 0) return;
2395
+ for (let i = 0, len = entries.length; i < len; i++) {
2396
+ const [key, value] = entries[i];
2397
+ deleteOp(ops, ctx, key, comparator, value);
2398
+ }
2399
+ }
2393
2400
  function batchInsertOp(ops, ctx, entries, comparator) {
2394
2401
  if (entries.length === 0) return;
2395
2402
  const sorted = [...entries].sort((a, b) => comparator.asc(a[1], b[1]));
@@ -3365,6 +3372,9 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
3365
3372
  delete(key, value) {
3366
3373
  deleteOp(this._ops, this._ctx, key, this.comparator, value);
3367
3374
  }
3375
+ batchDelete(entries) {
3376
+ batchDeleteOp(this._ops, this._ctx, entries, this.comparator);
3377
+ }
3368
3378
  // ─── Head Data ───────────────────────────────────────────────────
3369
3379
  getHeadData() {
3370
3380
  const head = this._readHead();
@@ -3490,6 +3500,14 @@ var BPTreeSync = class extends BPTreeSyncTransaction {
3490
3500
  throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3491
3501
  }
3492
3502
  }
3503
+ batchDelete(entries) {
3504
+ const tx = this.createTransaction();
3505
+ tx.batchDelete(entries);
3506
+ const result = tx.commit();
3507
+ if (!result.success) {
3508
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3509
+ }
3510
+ }
3493
3511
  batchInsert(entries) {
3494
3512
  const tx = this.createTransaction();
3495
3513
  tx.batchInsert(entries);
@@ -4312,8 +4330,8 @@ async function deleteEntryAsync(ops, ctx, node, key, comparator) {
4312
4330
  } else {
4313
4331
  pointerP0 = siblingNode.keys.splice(0, 1)[0];
4314
4332
  pointerK0 = siblingNode.values.splice(0, 1)[0];
4315
- node.keys = node.keys.concat(pointerP0);
4316
- node.values = node.values.concat(pointerK0);
4333
+ node.keys = [...node.keys, pointerP0];
4334
+ node.values = [...node.values, pointerK0];
4317
4335
  parentNode = cloneNode(await ops.getNode(node.parent));
4318
4336
  const pi = parentNode.keys.indexOf(siblingNode.id);
4319
4337
  if (pi > 0) {
@@ -4385,6 +4403,13 @@ async function deleteOpAsync(ops, ctx, key, comparator, value) {
4385
4403
  break;
4386
4404
  }
4387
4405
  }
4406
+ async function batchDeleteOpAsync(ops, ctx, entries, comparator) {
4407
+ if (entries.length === 0) return;
4408
+ for (let i = 0, len = entries.length; i < len; i++) {
4409
+ const [key, value] = entries[i];
4410
+ await deleteOpAsync(ops, ctx, key, comparator, value);
4411
+ }
4412
+ }
4388
4413
  async function batchInsertOpAsync(ops, ctx, entries, comparator) {
4389
4414
  if (entries.length === 0) return;
4390
4415
  const sorted = [...entries].sort((a, b) => comparator.asc(a[1], b[1]));
@@ -4809,6 +4834,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
4809
4834
  await deleteOpAsync(this._ops, this._ctx, key, this.comparator, value);
4810
4835
  });
4811
4836
  }
4837
+ async batchDelete(entries) {
4838
+ if (entries.length === 0) return;
4839
+ return this.writeLock(0, async () => {
4840
+ await batchDeleteOpAsync(this._ops, this._ctx, entries, this.comparator);
4841
+ });
4842
+ }
4812
4843
  // ─── Head Data ───────────────────────────────────────────────────
4813
4844
  async getHeadData() {
4814
4845
  const head = await this._readHead();
@@ -4938,6 +4969,16 @@ var BPTreeAsync = class extends BPTreeAsyncTransaction {
4938
4969
  }
4939
4970
  });
4940
4971
  }
4972
+ async batchDelete(entries) {
4973
+ return this.writeLock(1, async () => {
4974
+ const tx = await this.createTransaction();
4975
+ await tx.batchDelete(entries);
4976
+ const result = await tx.commit();
4977
+ if (!result.success) {
4978
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
4979
+ }
4980
+ });
4981
+ }
4941
4982
  async batchInsert(entries) {
4942
4983
  return this.writeLock(1, async () => {
4943
4984
  const tx = await this.createTransaction();
@@ -5045,6 +5086,7 @@ var BPTreePureSync = class {
5045
5086
  };
5046
5087
  function flush() {
5047
5088
  for (const id of deleteBuffer) {
5089
+ writeBuffer.delete(id);
5048
5090
  strategy.delete(id);
5049
5091
  }
5050
5092
  for (const [id, node] of writeBuffer) {
@@ -5181,6 +5223,12 @@ var BPTreePureSync = class {
5181
5223
  deleteOp(ops, ctx, key, this.comparator, value);
5182
5224
  flush();
5183
5225
  }
5226
+ batchDelete(entries) {
5227
+ const { ops, flush } = this._createBufferedOps();
5228
+ const ctx = this._createCtx();
5229
+ batchDeleteOp(ops, ctx, entries, this.comparator);
5230
+ flush();
5231
+ }
5184
5232
  batchInsert(entries) {
5185
5233
  const { ops, flush } = this._createBufferedOps();
5186
5234
  const ctx = this._createCtx();
@@ -5299,6 +5347,7 @@ var BPTreePureAsync = class {
5299
5347
  };
5300
5348
  async function flush() {
5301
5349
  for (const id of deleteBuffer) {
5350
+ writeBuffer.delete(id);
5302
5351
  await strategy.delete(id);
5303
5352
  }
5304
5353
  for (const [id, node] of writeBuffer) {
@@ -5467,6 +5516,14 @@ var BPTreePureAsync = class {
5467
5516
  await flush();
5468
5517
  });
5469
5518
  }
5519
+ async batchDelete(entries) {
5520
+ return this.writeLock(async () => {
5521
+ const { ops, flush } = this._createBufferedOps();
5522
+ const ctx = await this._createCtx();
5523
+ await batchDeleteOpAsync(ops, ctx, entries, this.comparator);
5524
+ await flush();
5525
+ });
5526
+ }
5470
5527
  async bulkLoad(entries) {
5471
5528
  return this.writeLock(async () => {
5472
5529
  const { ops, flush } = this._createBufferedOps();
@@ -2271,8 +2271,8 @@ function deleteEntry(ops, ctx, node, key, comparator) {
2271
2271
  } else {
2272
2272
  pointerP0 = siblingNode.keys.splice(0, 1)[0];
2273
2273
  pointerK0 = siblingNode.values.splice(0, 1)[0];
2274
- node.keys = node.keys.concat(pointerP0);
2275
- node.values = node.values.concat(pointerK0);
2274
+ node.keys = [...node.keys, pointerP0];
2275
+ node.values = [...node.values, pointerK0];
2276
2276
  parentNode = cloneNode(ops.getNode(node.parent));
2277
2277
  const pointerIndex = parentNode.keys.indexOf(siblingNode.id);
2278
2278
  if (pointerIndex > 0) {
@@ -2352,6 +2352,13 @@ function deleteOp(ops, ctx, key, comparator, value) {
2352
2352
  break;
2353
2353
  }
2354
2354
  }
2355
+ function batchDeleteOp(ops, ctx, entries, comparator) {
2356
+ if (entries.length === 0) return;
2357
+ for (let i = 0, len = entries.length; i < len; i++) {
2358
+ const [key, value] = entries[i];
2359
+ deleteOp(ops, ctx, key, comparator, value);
2360
+ }
2361
+ }
2355
2362
  function batchInsertOp(ops, ctx, entries, comparator) {
2356
2363
  if (entries.length === 0) return;
2357
2364
  const sorted = [...entries].sort((a, b) => comparator.asc(a[1], b[1]));
@@ -3327,6 +3334,9 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
3327
3334
  delete(key, value) {
3328
3335
  deleteOp(this._ops, this._ctx, key, this.comparator, value);
3329
3336
  }
3337
+ batchDelete(entries) {
3338
+ batchDeleteOp(this._ops, this._ctx, entries, this.comparator);
3339
+ }
3330
3340
  // ─── Head Data ───────────────────────────────────────────────────
3331
3341
  getHeadData() {
3332
3342
  const head = this._readHead();
@@ -3452,6 +3462,14 @@ var BPTreeSync = class extends BPTreeSyncTransaction {
3452
3462
  throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3453
3463
  }
3454
3464
  }
3465
+ batchDelete(entries) {
3466
+ const tx = this.createTransaction();
3467
+ tx.batchDelete(entries);
3468
+ const result = tx.commit();
3469
+ if (!result.success) {
3470
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3471
+ }
3472
+ }
3455
3473
  batchInsert(entries) {
3456
3474
  const tx = this.createTransaction();
3457
3475
  tx.batchInsert(entries);
@@ -4274,8 +4292,8 @@ async function deleteEntryAsync(ops, ctx, node, key, comparator) {
4274
4292
  } else {
4275
4293
  pointerP0 = siblingNode.keys.splice(0, 1)[0];
4276
4294
  pointerK0 = siblingNode.values.splice(0, 1)[0];
4277
- node.keys = node.keys.concat(pointerP0);
4278
- node.values = node.values.concat(pointerK0);
4295
+ node.keys = [...node.keys, pointerP0];
4296
+ node.values = [...node.values, pointerK0];
4279
4297
  parentNode = cloneNode(await ops.getNode(node.parent));
4280
4298
  const pi = parentNode.keys.indexOf(siblingNode.id);
4281
4299
  if (pi > 0) {
@@ -4347,6 +4365,13 @@ async function deleteOpAsync(ops, ctx, key, comparator, value) {
4347
4365
  break;
4348
4366
  }
4349
4367
  }
4368
+ async function batchDeleteOpAsync(ops, ctx, entries, comparator) {
4369
+ if (entries.length === 0) return;
4370
+ for (let i = 0, len = entries.length; i < len; i++) {
4371
+ const [key, value] = entries[i];
4372
+ await deleteOpAsync(ops, ctx, key, comparator, value);
4373
+ }
4374
+ }
4350
4375
  async function batchInsertOpAsync(ops, ctx, entries, comparator) {
4351
4376
  if (entries.length === 0) return;
4352
4377
  const sorted = [...entries].sort((a, b) => comparator.asc(a[1], b[1]));
@@ -4771,6 +4796,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
4771
4796
  await deleteOpAsync(this._ops, this._ctx, key, this.comparator, value);
4772
4797
  });
4773
4798
  }
4799
+ async batchDelete(entries) {
4800
+ if (entries.length === 0) return;
4801
+ return this.writeLock(0, async () => {
4802
+ await batchDeleteOpAsync(this._ops, this._ctx, entries, this.comparator);
4803
+ });
4804
+ }
4774
4805
  // ─── Head Data ───────────────────────────────────────────────────
4775
4806
  async getHeadData() {
4776
4807
  const head = await this._readHead();
@@ -4900,6 +4931,16 @@ var BPTreeAsync = class extends BPTreeAsyncTransaction {
4900
4931
  }
4901
4932
  });
4902
4933
  }
4934
+ async batchDelete(entries) {
4935
+ return this.writeLock(1, async () => {
4936
+ const tx = await this.createTransaction();
4937
+ await tx.batchDelete(entries);
4938
+ const result = await tx.commit();
4939
+ if (!result.success) {
4940
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
4941
+ }
4942
+ });
4943
+ }
4903
4944
  async batchInsert(entries) {
4904
4945
  return this.writeLock(1, async () => {
4905
4946
  const tx = await this.createTransaction();
@@ -5007,6 +5048,7 @@ var BPTreePureSync = class {
5007
5048
  };
5008
5049
  function flush() {
5009
5050
  for (const id of deleteBuffer) {
5051
+ writeBuffer.delete(id);
5010
5052
  strategy.delete(id);
5011
5053
  }
5012
5054
  for (const [id, node] of writeBuffer) {
@@ -5143,6 +5185,12 @@ var BPTreePureSync = class {
5143
5185
  deleteOp(ops, ctx, key, this.comparator, value);
5144
5186
  flush();
5145
5187
  }
5188
+ batchDelete(entries) {
5189
+ const { ops, flush } = this._createBufferedOps();
5190
+ const ctx = this._createCtx();
5191
+ batchDeleteOp(ops, ctx, entries, this.comparator);
5192
+ flush();
5193
+ }
5146
5194
  batchInsert(entries) {
5147
5195
  const { ops, flush } = this._createBufferedOps();
5148
5196
  const ctx = this._createCtx();
@@ -5261,6 +5309,7 @@ var BPTreePureAsync = class {
5261
5309
  };
5262
5310
  async function flush() {
5263
5311
  for (const id of deleteBuffer) {
5312
+ writeBuffer.delete(id);
5264
5313
  await strategy.delete(id);
5265
5314
  }
5266
5315
  for (const [id, node] of writeBuffer) {
@@ -5429,6 +5478,14 @@ var BPTreePureAsync = class {
5429
5478
  await flush();
5430
5479
  });
5431
5480
  }
5481
+ async batchDelete(entries) {
5482
+ return this.writeLock(async () => {
5483
+ const { ops, flush } = this._createBufferedOps();
5484
+ const ctx = await this._createCtx();
5485
+ await batchDeleteOpAsync(ops, ctx, entries, this.comparator);
5486
+ await flush();
5487
+ });
5488
+ }
5432
5489
  async bulkLoad(entries) {
5433
5490
  return this.writeLock(async () => {
5434
5491
  const { ops, flush } = this._createBufferedOps();
@@ -11,6 +11,7 @@ export declare class BPTreeAsync<K, V> extends BPTreeAsyncTransaction<K, V> {
11
11
  createTransaction(): Promise<BPTreeAsyncTransaction<K, V>>;
12
12
  insert(key: K, value: V): Promise<void>;
13
13
  delete(key: K, value?: V): Promise<void>;
14
+ batchDelete(entries: [K, V?][]): Promise<void>;
14
15
  batchInsert(entries: [K, V][]): Promise<void>;
15
16
  bulkLoad(entries: [K, V][]): Promise<void>;
16
17
  }
@@ -32,6 +32,7 @@ export declare class BPTreePureAsync<K, V> implements IBPTree<K, V> {
32
32
  insert(key: K, value: V): Promise<void>;
33
33
  delete(key: K, value?: V): Promise<void>;
34
34
  batchInsert(entries: [K, V][]): Promise<void>;
35
+ batchDelete(entries: [K, V?][]): Promise<void>;
35
36
  bulkLoad(entries: [K, V][]): Promise<void>;
36
37
  getHeadData(): Promise<SerializableData>;
37
38
  setHeadData(data: SerializableData): Promise<void>;
@@ -28,6 +28,7 @@ export declare class BPTreePureSync<K, V> implements IBPTree<K, V> {
28
28
  where(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): BPTreePair<K, V>;
29
29
  insert(key: K, value: V): void;
30
30
  delete(key: K, value?: V): void;
31
+ batchDelete(entries: [K, V?][]): void;
31
32
  batchInsert(entries: [K, V][]): void;
32
33
  bulkLoad(entries: [K, V][]): void;
33
34
  getHeadData(): SerializableData;
@@ -11,6 +11,7 @@ export declare class BPTreeSync<K, V> extends BPTreeSyncTransaction<K, V> {
11
11
  createTransaction(): BPTreeSyncTransaction<K, V>;
12
12
  insert(key: K, value: V): void;
13
13
  delete(key: K, value?: V): void;
14
+ batchDelete(entries: [K, V?][]): void;
14
15
  batchInsert(entries: [K, V][]): void;
15
16
  bulkLoad(entries: [K, V][]): void;
16
17
  }
@@ -22,6 +22,7 @@ export declare function insertInParentAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>,
22
22
  export declare function insertOpAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, ctx: BPTreeAlgoContext<K, V>, key: K, value: V, comparator: ValueComparator<V>): Promise<void>;
23
23
  export declare function deleteEntryAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, ctx: BPTreeAlgoContext<K, V>, node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, comparator: ValueComparator<V>): Promise<BPTreeUnknownNode<K, V>>;
24
24
  export declare function deleteOpAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, ctx: BPTreeAlgoContext<K, V>, key: K, comparator: ValueComparator<V>, value?: V): Promise<void>;
25
+ export declare function batchDeleteOpAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, ctx: BPTreeAlgoContext<K, V>, entries: [K, V?][], comparator: ValueComparator<V>): Promise<void>;
25
26
  export declare function batchInsertOpAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, ctx: BPTreeAlgoContext<K, V>, entries: [K, V][], comparator: ValueComparator<V>): Promise<void>;
26
27
  export declare function bulkLoadOpAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, ctx: BPTreeAlgoContext<K, V>, entries: [K, V][], comparator: ValueComparator<V>): Promise<void>;
27
28
  export declare function existsOpAsync<K, V>(ops: BPTreeNodeOpsAsync<K, V>, rootId: string, key: K, value: V, comparator: ValueComparator<V>): Promise<boolean>;
@@ -35,6 +35,7 @@ export declare function insertInParent<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTr
35
35
  export declare function insertOp<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTreeAlgoContext<K, V>, key: K, value: V, comparator: ValueComparator<V>): void;
36
36
  export declare function deleteEntry<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTreeAlgoContext<K, V>, node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, comparator: ValueComparator<V>): BPTreeUnknownNode<K, V>;
37
37
  export declare function deleteOp<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTreeAlgoContext<K, V>, key: K, comparator: ValueComparator<V>, value?: V): void;
38
+ export declare function batchDeleteOp<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTreeAlgoContext<K, V>, entries: [K, V?][], comparator: ValueComparator<V>): void;
38
39
  export declare function batchInsertOp<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTreeAlgoContext<K, V>, entries: [K, V][], comparator: ValueComparator<V>): void;
39
40
  export declare function bulkLoadOp<K, V>(ops: BPTreeNodeOps<K, V>, ctx: BPTreeAlgoContext<K, V>, entries: [K, V][], comparator: ValueComparator<V>): void;
40
41
  export declare function existsOp<K, V>(ops: BPTreeNodeOps<K, V>, rootId: string, key: K, value: V, comparator: ValueComparator<V>): boolean;
@@ -111,6 +111,7 @@ export declare abstract class BPTreeTransaction<K, V> implements IBPTree<K, V> {
111
111
  abstract batchInsert(entries: [K, V][]): Deferred<void>;
112
112
  abstract bulkLoad(entries: [K, V][]): Deferred<void>;
113
113
  abstract delete(key: K, value?: V): Deferred<void>;
114
+ abstract batchDelete(entries: [K, V?][]): Deferred<void>;
114
115
  abstract exists(key: K, value: V): Deferred<boolean>;
115
116
  abstract setHeadData(data: SerializableData): Deferred<void>;
116
117
  abstract getHeadData(): Deferred<SerializableData>;
@@ -51,6 +51,7 @@ export declare class BPTreeAsyncTransaction<K, V> extends BPTreeTransaction<K, V
51
51
  bulkLoad(entries: [K, V][]): Promise<void>;
52
52
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>): Promise<BPTreeUnknownNode<K, V>>;
53
53
  delete(key: K, value?: V): Promise<void>;
54
+ batchDelete(entries: [K, V?][]): Promise<void>;
54
55
  getHeadData(): Promise<SerializableData>;
55
56
  setHeadData(data: SerializableData): Promise<void>;
56
57
  commit(label?: string): Promise<TransactionResult<string, BPTreeNode<K, V>>>;
@@ -48,6 +48,7 @@ export declare class BPTreeSyncTransaction<K, V> extends BPTreeTransaction<K, V>
48
48
  bulkLoad(entries: [K, V][]): void;
49
49
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>): BPTreeUnknownNode<K, V>;
50
50
  delete(key: K, value?: V): void;
51
+ batchDelete(entries: [K, V?][]): void;
51
52
  getHeadData(): SerializableData;
52
53
  setHeadData(data: SerializableData): void;
53
54
  commit(label?: string): TransactionResult<string, BPTreeNode<K, V>>;
@@ -208,6 +208,12 @@ export interface IBPTree<K, V> {
208
208
  * @warning If the 'value' is not specified, a full scan will be performed to find the value associated with the key, which may lead to performance degradation.
209
209
  */
210
210
  delete(key: K, value?: V): Deferred<void>;
211
+ /**
212
+ * Deletes multiple key-value pairs from the tree in a single batch operation.
213
+ * This is more efficient than calling delete() multiple times.
214
+ * @param entries Array of [key, value?] pairs to delete. If value is omitted, a full scan will be performed.
215
+ */
216
+ batchDelete(entries: [K, V?][]): Deferred<void>;
211
217
  /**
212
218
  * It returns whether there is a value in the tree.
213
219
  * @param key The key value to search for. This key must be unique.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "9.0.4-alpha.2",
3
+ "version": "9.0.4",
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",