serializable-bptree 8.2.0 → 8.3.0

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.
@@ -2256,6 +2256,33 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2256
2256
  this._insertInParent(before, after.values[0], after);
2257
2257
  }
2258
2258
  }
2259
+ batchInsert(entries) {
2260
+ if (entries.length === 0) return;
2261
+ const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
2262
+ for (const [key, value] of sorted) {
2263
+ let before = this.insertableNode(value);
2264
+ before = this._insertAtLeaf(before, key, value);
2265
+ if (before.values.length === this.order) {
2266
+ let after = this._createNode(
2267
+ true,
2268
+ [],
2269
+ [],
2270
+ before.parent,
2271
+ null,
2272
+ null
2273
+ );
2274
+ const mid = Math.ceil(this.order / 2) - 1;
2275
+ after = this._cloneNode(after);
2276
+ after.values = before.values.slice(mid + 1);
2277
+ after.keys = before.keys.slice(mid + 1);
2278
+ before.values = before.values.slice(0, mid + 1);
2279
+ before.keys = before.keys.slice(0, mid + 1);
2280
+ this._updateNode(before);
2281
+ this._updateNode(after);
2282
+ this._insertInParent(before, after.values[0], after);
2283
+ }
2284
+ }
2285
+ }
2259
2286
  _deleteEntry(node, key) {
2260
2287
  if (!node.leaf) {
2261
2288
  let keyIndex = -1;
@@ -2611,6 +2638,14 @@ var BPTreeSync = class extends BPTreeSyncTransaction {
2611
2638
  throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
2612
2639
  }
2613
2640
  }
2641
+ batchInsert(entries) {
2642
+ const tx = this.createTransaction();
2643
+ tx.batchInsert(entries);
2644
+ const result = tx.commit();
2645
+ if (!result.success) {
2646
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
2647
+ }
2648
+ }
2614
2649
  };
2615
2650
 
2616
2651
  // node_modules/ryoiki/dist/esm/index.mjs
@@ -3344,6 +3379,35 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3344
3379
  }
3345
3380
  });
3346
3381
  }
3382
+ async batchInsert(entries) {
3383
+ if (entries.length === 0) return;
3384
+ return this.writeLock(0, async () => {
3385
+ const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
3386
+ for (const [key, value] of sorted) {
3387
+ let before = await this.insertableNode(value);
3388
+ before = await this._insertAtLeaf(before, key, value);
3389
+ if (before.values.length === this.order) {
3390
+ let after = await this._createNode(
3391
+ true,
3392
+ [],
3393
+ [],
3394
+ before.parent,
3395
+ null,
3396
+ null
3397
+ );
3398
+ const mid = Math.ceil(this.order / 2) - 1;
3399
+ after = this._cloneNode(after);
3400
+ after.values = before.values.slice(mid + 1);
3401
+ after.keys = before.keys.slice(mid + 1);
3402
+ before.values = before.values.slice(0, mid + 1);
3403
+ before.keys = before.keys.slice(0, mid + 1);
3404
+ await this._updateNode(before);
3405
+ await this._updateNode(after);
3406
+ await this._insertInParent(before, after.values[0], after);
3407
+ }
3408
+ }
3409
+ });
3410
+ }
3347
3411
  async _deleteEntry(node, key) {
3348
3412
  if (!node.leaf) {
3349
3413
  let keyIndex = -1;
@@ -3705,6 +3769,16 @@ var BPTreeAsync = class extends BPTreeAsyncTransaction {
3705
3769
  }
3706
3770
  });
3707
3771
  }
3772
+ async batchInsert(entries) {
3773
+ return this.writeLock(1, async () => {
3774
+ const tx = await this.createTransaction();
3775
+ await tx.batchInsert(entries);
3776
+ const result = await tx.commit();
3777
+ if (!result.success) {
3778
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3779
+ }
3780
+ });
3781
+ }
3708
3782
  };
3709
3783
 
3710
3784
  // src/base/SerializeStrategy.ts
@@ -2220,6 +2220,33 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2220
2220
  this._insertInParent(before, after.values[0], after);
2221
2221
  }
2222
2222
  }
2223
+ batchInsert(entries) {
2224
+ if (entries.length === 0) return;
2225
+ const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
2226
+ for (const [key, value] of sorted) {
2227
+ let before = this.insertableNode(value);
2228
+ before = this._insertAtLeaf(before, key, value);
2229
+ if (before.values.length === this.order) {
2230
+ let after = this._createNode(
2231
+ true,
2232
+ [],
2233
+ [],
2234
+ before.parent,
2235
+ null,
2236
+ null
2237
+ );
2238
+ const mid = Math.ceil(this.order / 2) - 1;
2239
+ after = this._cloneNode(after);
2240
+ after.values = before.values.slice(mid + 1);
2241
+ after.keys = before.keys.slice(mid + 1);
2242
+ before.values = before.values.slice(0, mid + 1);
2243
+ before.keys = before.keys.slice(0, mid + 1);
2244
+ this._updateNode(before);
2245
+ this._updateNode(after);
2246
+ this._insertInParent(before, after.values[0], after);
2247
+ }
2248
+ }
2249
+ }
2223
2250
  _deleteEntry(node, key) {
2224
2251
  if (!node.leaf) {
2225
2252
  let keyIndex = -1;
@@ -2575,6 +2602,14 @@ var BPTreeSync = class extends BPTreeSyncTransaction {
2575
2602
  throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
2576
2603
  }
2577
2604
  }
2605
+ batchInsert(entries) {
2606
+ const tx = this.createTransaction();
2607
+ tx.batchInsert(entries);
2608
+ const result = tx.commit();
2609
+ if (!result.success) {
2610
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
2611
+ }
2612
+ }
2578
2613
  };
2579
2614
 
2580
2615
  // node_modules/ryoiki/dist/esm/index.mjs
@@ -3308,6 +3343,35 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3308
3343
  }
3309
3344
  });
3310
3345
  }
3346
+ async batchInsert(entries) {
3347
+ if (entries.length === 0) return;
3348
+ return this.writeLock(0, async () => {
3349
+ const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
3350
+ for (const [key, value] of sorted) {
3351
+ let before = await this.insertableNode(value);
3352
+ before = await this._insertAtLeaf(before, key, value);
3353
+ if (before.values.length === this.order) {
3354
+ let after = await this._createNode(
3355
+ true,
3356
+ [],
3357
+ [],
3358
+ before.parent,
3359
+ null,
3360
+ null
3361
+ );
3362
+ const mid = Math.ceil(this.order / 2) - 1;
3363
+ after = this._cloneNode(after);
3364
+ after.values = before.values.slice(mid + 1);
3365
+ after.keys = before.keys.slice(mid + 1);
3366
+ before.values = before.values.slice(0, mid + 1);
3367
+ before.keys = before.keys.slice(0, mid + 1);
3368
+ await this._updateNode(before);
3369
+ await this._updateNode(after);
3370
+ await this._insertInParent(before, after.values[0], after);
3371
+ }
3372
+ }
3373
+ });
3374
+ }
3311
3375
  async _deleteEntry(node, key) {
3312
3376
  if (!node.leaf) {
3313
3377
  let keyIndex = -1;
@@ -3669,6 +3733,16 @@ var BPTreeAsync = class extends BPTreeAsyncTransaction {
3669
3733
  }
3670
3734
  });
3671
3735
  }
3736
+ async batchInsert(entries) {
3737
+ return this.writeLock(1, async () => {
3738
+ const tx = await this.createTransaction();
3739
+ await tx.batchInsert(entries);
3740
+ const result = await tx.commit();
3741
+ if (!result.success) {
3742
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3743
+ }
3744
+ });
3745
+ }
3672
3746
  };
3673
3747
 
3674
3748
  // src/base/SerializeStrategy.ts
@@ -11,4 +11,5 @@ 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
+ batchInsert(entries: [K, V][]): Promise<void>;
14
15
  }
@@ -11,4 +11,5 @@ 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
+ batchInsert(entries: [K, V][]): void;
14
15
  }
@@ -152,6 +152,13 @@ export declare abstract class BPTreeTransaction<K, V> {
152
152
  * @param value The value of the pair.
153
153
  */
154
154
  abstract insert(key: K, value: V): Deferred<void>;
155
+ /**
156
+ * Inserts multiple key-value pairs into the tree in a single batch operation.
157
+ * Entries are sorted by value before insertion to optimize tree traversal.
158
+ * This is more efficient than calling insert() multiple times.
159
+ * @param entries Array of [key, value] pairs to insert.
160
+ */
161
+ abstract batchInsert(entries: [K, V][]): Deferred<void>;
155
162
  /**
156
163
  * Deletes the pair that matches the key and value.
157
164
  * @param key The key of the pair. This key must be unique.
@@ -42,6 +42,7 @@ export declare class BPTreeAsyncTransaction<K, V> extends BPTreeTransaction<K, V
42
42
  keys(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Promise<Set<K>>;
43
43
  where(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Promise<BPTreePair<K, V>>;
44
44
  insert(key: K, value: V): Promise<void>;
45
+ batchInsert(entries: [K, V][]): Promise<void>;
45
46
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>): Promise<BPTreeUnknownNode<K, V>>;
46
47
  delete(key: K, value?: V): Promise<void>;
47
48
  getHeadData(): Promise<SerializableData>;
@@ -39,6 +39,7 @@ export declare class BPTreeSyncTransaction<K, V> extends BPTreeTransaction<K, V>
39
39
  keys(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Set<K>;
40
40
  where(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): BPTreePair<K, V>;
41
41
  insert(key: K, value: V): void;
42
+ batchInsert(entries: [K, V][]): void;
42
43
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>): BPTreeUnknownNode<K, V>;
43
44
  delete(key: K, value?: V): void;
44
45
  getHeadData(): SerializableData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "8.2.0",
3
+ "version": "8.3.0",
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",