dataply 0.0.23-alpha.1 → 0.0.23

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.
Files changed (2) hide show
  1. package/dist/cjs/index.js +87 -20
  2. package/package.json +2 -2
package/dist/cjs/index.js CHANGED
@@ -1726,6 +1726,39 @@ var BPTreeTransaction = class _BPTreeTransaction {
1726
1726
  }
1727
1727
  return true;
1728
1728
  }
1729
+ /**
1730
+ * Inserts a key-value pair into an already-cloned leaf node in-place.
1731
+ * Unlike _insertAtLeaf, this does NOT clone or update the node via MVCC.
1732
+ * Used by batchInsert to batch multiple insertions with a single clone/update.
1733
+ * @returns true if the leaf was modified, false if the key already exists.
1734
+ */
1735
+ _insertValueIntoLeaf(leaf, key, value) {
1736
+ if (leaf.values.length) {
1737
+ for (let i = 0, len = leaf.values.length; i < len; i++) {
1738
+ const nValue = leaf.values[i];
1739
+ if (this.comparator.isSame(value, nValue)) {
1740
+ if (leaf.keys[i].includes(key)) {
1741
+ return false;
1742
+ }
1743
+ leaf.keys[i].push(key);
1744
+ return true;
1745
+ } else if (this.comparator.isLower(value, nValue)) {
1746
+ leaf.values.splice(i, 0, value);
1747
+ leaf.keys.splice(i, 0, [key]);
1748
+ return true;
1749
+ } else if (i + 1 === leaf.values.length) {
1750
+ leaf.values.push(value);
1751
+ leaf.keys.push([key]);
1752
+ return true;
1753
+ }
1754
+ }
1755
+ } else {
1756
+ leaf.values = [value];
1757
+ leaf.keys = [[key]];
1758
+ return true;
1759
+ }
1760
+ return false;
1761
+ }
1729
1762
  _cloneNode(node) {
1730
1763
  return JSON.parse(JSON.stringify(node));
1731
1764
  }
@@ -2287,29 +2320,46 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2287
2320
  batchInsert(entries) {
2288
2321
  if (entries.length === 0) return;
2289
2322
  const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
2323
+ let currentLeaf = null;
2324
+ let modified = false;
2290
2325
  for (const [key, value] of sorted) {
2291
- let before = this.insertableNode(value);
2292
- before = this._insertAtLeaf(before, key, value);
2293
- if (before.values.length === this.order) {
2326
+ const targetLeaf = this.insertableNode(value);
2327
+ if (currentLeaf !== null && currentLeaf.id === targetLeaf.id) {
2328
+ } else {
2329
+ if (currentLeaf !== null && modified) {
2330
+ this._updateNode(currentLeaf);
2331
+ }
2332
+ currentLeaf = this._cloneNode(targetLeaf);
2333
+ modified = false;
2334
+ }
2335
+ const changed = this._insertValueIntoLeaf(currentLeaf, key, value);
2336
+ modified = modified || changed;
2337
+ if (currentLeaf.values.length === this.order) {
2338
+ this._updateNode(currentLeaf);
2294
2339
  let after = this._createNode(
2295
2340
  true,
2296
2341
  [],
2297
2342
  [],
2298
- before.parent,
2343
+ currentLeaf.parent,
2299
2344
  null,
2300
2345
  null
2301
2346
  );
2302
2347
  const mid = Math.ceil(this.order / 2) - 1;
2303
2348
  after = this._cloneNode(after);
2304
- after.values = before.values.slice(mid + 1);
2305
- after.keys = before.keys.slice(mid + 1);
2306
- before.values = before.values.slice(0, mid + 1);
2307
- before.keys = before.keys.slice(0, mid + 1);
2308
- this._updateNode(before);
2349
+ after.values = currentLeaf.values.slice(mid + 1);
2350
+ after.keys = currentLeaf.keys.slice(mid + 1);
2351
+ currentLeaf.values = currentLeaf.values.slice(0, mid + 1);
2352
+ currentLeaf.keys = currentLeaf.keys.slice(0, mid + 1);
2353
+ this._updateNode(currentLeaf);
2309
2354
  this._updateNode(after);
2310
- this._insertInParent(before, after.values[0], after);
2355
+ this._insertInParent(currentLeaf, after.values[0], after);
2356
+ currentLeaf = null;
2357
+ modified = false;
2311
2358
  }
2312
2359
  }
2360
+ if (currentLeaf !== null && modified) {
2361
+ this._updateNode(currentLeaf);
2362
+ }
2313
2363
  }
2314
2364
  _deleteEntry(node, key) {
2315
2365
  if (!node.leaf) {
@@ -3403,29 +3453,46 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3403
3453
  if (entries.length === 0) return;
3404
3454
  return this.writeLock(0, async () => {
3405
3455
  const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
3456
+ let currentLeaf = null;
3457
+ let modified = false;
3406
3458
  for (const [key, value] of sorted) {
3407
- let before = await this.insertableNode(value);
3408
- before = await this._insertAtLeaf(before, key, value);
3409
- if (before.values.length === this.order) {
3459
+ const targetLeaf = await this.insertableNode(value);
3460
+ if (currentLeaf !== null && currentLeaf.id === targetLeaf.id) {
3461
+ } else {
3462
+ if (currentLeaf !== null && modified) {
3463
+ await this._updateNode(currentLeaf);
3464
+ }
3465
+ currentLeaf = this._cloneNode(targetLeaf);
3466
+ modified = false;
3467
+ }
3468
+ const changed = this._insertValueIntoLeaf(currentLeaf, key, value);
3469
+ modified = modified || changed;
3470
+ if (currentLeaf.values.length === this.order) {
3471
+ await this._updateNode(currentLeaf);
3410
3472
  let after = await this._createNode(
3411
3473
  true,
3412
3474
  [],
3413
3475
  [],
3414
- before.parent,
3476
+ currentLeaf.parent,
3415
3477
  null,
3416
3478
  null
3417
3479
  );
3418
3480
  const mid = Math.ceil(this.order / 2) - 1;
3419
3481
  after = this._cloneNode(after);
3420
- after.values = before.values.slice(mid + 1);
3421
- after.keys = before.keys.slice(mid + 1);
3422
- before.values = before.values.slice(0, mid + 1);
3423
- before.keys = before.keys.slice(0, mid + 1);
3424
- await this._updateNode(before);
3482
+ after.values = currentLeaf.values.slice(mid + 1);
3483
+ after.keys = currentLeaf.keys.slice(mid + 1);
3484
+ currentLeaf.values = currentLeaf.values.slice(0, mid + 1);
3485
+ currentLeaf.keys = currentLeaf.keys.slice(0, mid + 1);
3486
+ await this._updateNode(currentLeaf);
3425
3487
  await this._updateNode(after);
3426
- await this._insertInParent(before, after.values[0], after);
3488
+ await this._insertInParent(currentLeaf, after.values[0], after);
3489
+ currentLeaf = null;
3490
+ modified = false;
3427
3491
  }
3428
3492
  }
3493
+ if (currentLeaf !== null && modified) {
3494
+ await this._updateNode(currentLeaf);
3495
+ }
3429
3496
  });
3430
3497
  }
3431
3498
  async _deleteEntry(node, key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.23-alpha.1",
3
+ "version": "0.0.23",
4
4
  "description": "A lightweight storage engine for Node.js with support for MVCC, WAL.",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",
@@ -49,6 +49,6 @@
49
49
  "hookall": "^2.2.0",
50
50
  "mvcc-api": "^1.3.4",
51
51
  "ryoiki": "^1.2.0",
52
- "serializable-bptree": "^8.3.0"
52
+ "serializable-bptree": "^8.3.1"
53
53
  }
54
54
  }