dataply 0.0.26-alpha.1 → 0.0.26-alpha.2

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 +239 -45
  2. package/package.json +3 -3
package/dist/cjs/index.js CHANGED
@@ -1954,30 +1954,22 @@ var BPTreeTransaction = class _BPTreeTransaction {
1954
1954
  */
1955
1955
  _insertValueIntoLeaf(leaf, key, value) {
1956
1956
  if (leaf.values.length) {
1957
- for (let i = 0, len = leaf.values.length; i < len; i++) {
1958
- const nValue = leaf.values[i];
1959
- if (this.comparator.isSame(value, nValue)) {
1960
- if (leaf.keys[i].includes(key)) {
1961
- return false;
1962
- }
1963
- leaf.keys[i].push(key);
1964
- return true;
1965
- } else if (this.comparator.isLower(value, nValue)) {
1966
- leaf.values.splice(i, 0, value);
1967
- leaf.keys.splice(i, 0, [key]);
1968
- return true;
1969
- } else if (i + 1 === leaf.values.length) {
1970
- leaf.values.push(value);
1971
- leaf.keys.push([key]);
1972
- return true;
1957
+ const { index, found } = this._binarySearchValues(leaf.values, value);
1958
+ if (found) {
1959
+ if (leaf.keys[index].includes(key)) {
1960
+ return false;
1973
1961
  }
1962
+ leaf.keys[index].push(key);
1963
+ return true;
1974
1964
  }
1965
+ leaf.values.splice(index, 0, value);
1966
+ leaf.keys.splice(index, 0, [key]);
1967
+ return true;
1975
1968
  } else {
1976
1969
  leaf.values = [value];
1977
1970
  leaf.keys = [[key]];
1978
1971
  return true;
1979
1972
  }
1980
- return false;
1981
1973
  }
1982
1974
  _cloneNode(node) {
1983
1975
  return JSON.parse(JSON.stringify(node));
@@ -2268,12 +2260,8 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2268
2260
  const midValue = parentNode.values[mid];
2269
2261
  parentNode.values = parentNode.values.slice(0, mid);
2270
2262
  parentNode.keys = parentNode.keys.slice(0, mid + 1);
2271
- for (const k of parentNode.keys) {
2272
- const n = this._cloneNode(this.getNode(k));
2273
- n.parent = parentNode.id;
2274
- this._updateNode(n);
2275
- }
2276
- for (const k of newSiblingNodeRecursive.keys) {
2263
+ for (let i = 0, len = newSiblingNodeRecursive.keys.length; i < len; i++) {
2264
+ const k = newSiblingNodeRecursive.keys[i];
2277
2265
  const n = this._cloneNode(this.getNode(k));
2278
2266
  n.parent = newSiblingNodeRecursive.id;
2279
2267
  this._updateNode(n);
@@ -2355,7 +2343,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2355
2343
  for (let i = 0; i < len; i++) {
2356
2344
  const nValue = node.values[i];
2357
2345
  const keys = node.keys[i];
2358
- for (let j = 0, kLen = keys.length; j < kLen; j++) {
2346
+ for (let j = 0, len2 = keys.length; j < len2; j++) {
2359
2347
  yield [keys[j], nValue];
2360
2348
  }
2361
2349
  }
@@ -2434,7 +2422,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2434
2422
  while (true) {
2435
2423
  for (let i = 0, len = node.values.length; i < len; i++) {
2436
2424
  const keys = node.keys[i];
2437
- for (let j = 0, kLen = keys.length; j < kLen; j++) {
2425
+ for (let j = 0, len2 = keys.length; j < len2; j++) {
2438
2426
  if (keys[j] === key) {
2439
2427
  return node.values[i];
2440
2428
  }
@@ -2543,8 +2531,16 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2543
2531
  const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
2544
2532
  let currentLeaf = null;
2545
2533
  let modified = false;
2546
- for (const [key, value] of sorted) {
2547
- const targetLeaf = this.locateLeaf(value);
2534
+ let cachedLeafId = null;
2535
+ let cachedLeafMaxValue = null;
2536
+ for (let i = 0, len = sorted.length; i < len; i++) {
2537
+ const [key, value] = sorted[i];
2538
+ let targetLeaf;
2539
+ if (cachedLeafId !== null && cachedLeafMaxValue !== null && currentLeaf !== null && (this.comparator.isLower(value, cachedLeafMaxValue) || this.comparator.isSame(value, cachedLeafMaxValue))) {
2540
+ targetLeaf = currentLeaf;
2541
+ } else {
2542
+ targetLeaf = this.locateLeaf(value);
2543
+ }
2548
2544
  if (currentLeaf !== null && currentLeaf.id === targetLeaf.id) {
2549
2545
  } else {
2550
2546
  if (currentLeaf !== null && modified) {
@@ -2553,8 +2549,10 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2553
2549
  currentLeaf = this._cloneNode(targetLeaf);
2554
2550
  modified = false;
2555
2551
  }
2552
+ cachedLeafId = currentLeaf.id;
2556
2553
  const changed = this._insertValueIntoLeaf(currentLeaf, key, value);
2557
2554
  modified = modified || changed;
2555
+ cachedLeafMaxValue = currentLeaf.values[currentLeaf.values.length - 1];
2558
2556
  if (currentLeaf.values.length === this.order) {
2559
2557
  this._updateNode(currentLeaf);
2560
2558
  let after = this._createNode(
@@ -2575,6 +2573,8 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2575
2573
  this._updateNode(after);
2576
2574
  this._insertInParent(currentLeaf, after.values[0], after);
2577
2575
  currentLeaf = null;
2576
+ cachedLeafId = null;
2577
+ cachedLeafMaxValue = null;
2578
2578
  modified = false;
2579
2579
  }
2580
2580
  }
@@ -2582,6 +2582,85 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2582
2582
  this._updateNode(currentLeaf);
2583
2583
  }
2584
2584
  }
2585
+ bulkLoad(entries) {
2586
+ if (entries.length === 0) return;
2587
+ const root = this.getNode(this.rootId);
2588
+ if (!root.leaf || root.values.length > 0) {
2589
+ throw new Error("bulkLoad can only be called on an empty tree. Use batchInsert for non-empty trees.");
2590
+ }
2591
+ const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
2592
+ const grouped = [];
2593
+ for (let i = 0, len = sorted.length; i < len; i++) {
2594
+ const [key, value] = sorted[i];
2595
+ const last = grouped[grouped.length - 1];
2596
+ if (last && this.comparator.isSame(last.value, value)) {
2597
+ if (!last.keys.includes(key)) {
2598
+ last.keys.push(key);
2599
+ }
2600
+ } else {
2601
+ grouped.push({ keys: [key], value });
2602
+ }
2603
+ }
2604
+ this._deleteNode(root);
2605
+ const maxLeafSize = this.order - 1;
2606
+ const leaves = [];
2607
+ for (let i = 0, len = grouped.length; i < len; i += maxLeafSize) {
2608
+ const chunk = grouped.slice(i, i + maxLeafSize);
2609
+ const leafKeys = chunk.map((g) => g.keys);
2610
+ const leafValues = chunk.map((g) => g.value);
2611
+ const leaf = this._createNode(
2612
+ true,
2613
+ leafKeys,
2614
+ leafValues,
2615
+ null,
2616
+ null,
2617
+ null
2618
+ );
2619
+ leaves.push(leaf);
2620
+ }
2621
+ for (let i = 0, len = leaves.length; i < len; i++) {
2622
+ if (i > 0) {
2623
+ leaves[i].prev = leaves[i - 1].id;
2624
+ }
2625
+ if (i < len - 1) {
2626
+ leaves[i].next = leaves[i + 1].id;
2627
+ }
2628
+ this._updateNode(leaves[i]);
2629
+ }
2630
+ let currentLevel = leaves;
2631
+ while (currentLevel.length > 1) {
2632
+ const nextLevel = [];
2633
+ for (let i = 0, len = currentLevel.length; i < len; i += this.order) {
2634
+ const children = currentLevel.slice(i, i + this.order);
2635
+ const childIds = children.map((c) => c.id);
2636
+ const separators = [];
2637
+ for (let j = 1, len2 = children.length; j < len2; j++) {
2638
+ separators.push(children[j].values[0]);
2639
+ }
2640
+ const internalNode = this._createNode(
2641
+ false,
2642
+ childIds,
2643
+ separators,
2644
+ null,
2645
+ null,
2646
+ null
2647
+ );
2648
+ for (let j = 0, len2 = children.length; j < len2; j++) {
2649
+ const child = children[j];
2650
+ child.parent = internalNode.id;
2651
+ this._updateNode(child);
2652
+ }
2653
+ nextLevel.push(internalNode);
2654
+ }
2655
+ currentLevel = nextLevel;
2656
+ }
2657
+ const newRoot = currentLevel[0];
2658
+ this._writeHead({
2659
+ root: newRoot.id,
2660
+ order: this.order,
2661
+ data: this.strategy.head.data
2662
+ });
2663
+ }
2585
2664
  _deleteEntry(node, key) {
2586
2665
  if (!node.leaf) {
2587
2666
  let keyIndex = -1;
@@ -2685,7 +2764,8 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2685
2764
  siblingNode.values.push(...node.values);
2686
2765
  if (!siblingNode.leaf) {
2687
2766
  const keys = siblingNode.keys;
2688
- for (const key2 of keys) {
2767
+ for (let i = 0, len = keys.length; i < len; i++) {
2768
+ const key2 = keys[i];
2689
2769
  const node2 = this._cloneNode(this.getNode(key2));
2690
2770
  node2.parent = siblingNode.id;
2691
2771
  this._updateNode(node2);
@@ -2753,21 +2833,24 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2753
2833
  this._updateNode(siblingNode);
2754
2834
  }
2755
2835
  if (!siblingNode.leaf) {
2756
- for (const key2 of siblingNode.keys) {
2836
+ for (let i = 0, len = siblingNode.keys.length; i < len; i++) {
2837
+ const key2 = siblingNode.keys[i];
2757
2838
  const n = this._cloneNode(this.getNode(key2));
2758
2839
  n.parent = siblingNode.id;
2759
2840
  this._updateNode(n);
2760
2841
  }
2761
2842
  }
2762
2843
  if (!node.leaf) {
2763
- for (const key2 of node.keys) {
2844
+ for (let i = 0, len = node.keys.length; i < len; i++) {
2845
+ const key2 = node.keys[i];
2764
2846
  const n = this._cloneNode(this.getNode(key2));
2765
2847
  n.parent = node.id;
2766
2848
  this._updateNode(n);
2767
2849
  }
2768
2850
  }
2769
2851
  if (!parentNode.leaf) {
2770
- for (const key2 of parentNode.keys) {
2852
+ for (let i = 0, len = parentNode.keys.length; i < len; i++) {
2853
+ const key2 = parentNode.keys[i];
2771
2854
  const n = this._cloneNode(this.getNode(key2));
2772
2855
  n.parent = parentNode.id;
2773
2856
  this._updateNode(n);
@@ -2941,6 +3024,14 @@ var BPTreeSync = class extends BPTreeSyncTransaction {
2941
3024
  throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
2942
3025
  }
2943
3026
  }
3027
+ bulkLoad(entries) {
3028
+ const tx = this.createTransaction();
3029
+ tx.bulkLoad(entries);
3030
+ const result = tx.commit();
3031
+ if (!result.success) {
3032
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
3033
+ }
3034
+ }
2944
3035
  };
2945
3036
  var Ryoiki2 = class _Ryoiki2 {
2946
3037
  readings;
@@ -3354,12 +3445,8 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3354
3445
  const midValue = parentNode.values[mid];
3355
3446
  parentNode.values = parentNode.values.slice(0, mid);
3356
3447
  parentNode.keys = parentNode.keys.slice(0, mid + 1);
3357
- for (const k of parentNode.keys) {
3358
- const n = this._cloneNode(await this.getNode(k));
3359
- n.parent = parentNode.id;
3360
- await this._updateNode(n);
3361
- }
3362
- for (const k of newSiblingNodeRecursive.keys) {
3448
+ for (let i = 0, len = newSiblingNodeRecursive.keys.length; i < len; i++) {
3449
+ const k = newSiblingNodeRecursive.keys[i];
3363
3450
  const n = this._cloneNode(await this.getNode(k));
3364
3451
  n.parent = newSiblingNodeRecursive.id;
3365
3452
  await this._updateNode(n);
@@ -3451,7 +3538,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3451
3538
  for (let i = 0; i < len; i++) {
3452
3539
  const nValue = node.values[i];
3453
3540
  const keys = node.keys[i];
3454
- for (let j = 0, kLen = keys.length; j < kLen; j++) {
3541
+ for (let j = 0, len2 = keys.length; j < len2; j++) {
3455
3542
  yield [keys[j], nValue];
3456
3543
  }
3457
3544
  }
@@ -3529,7 +3616,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3529
3616
  while (true) {
3530
3617
  for (let i = 0, len = node.values.length; i < len; i++) {
3531
3618
  const keys = node.keys[i];
3532
- for (let j = 0, kLen = keys.length; j < kLen; j++) {
3619
+ for (let j = 0, len2 = keys.length; j < len2; j++) {
3533
3620
  if (keys[j] === key) {
3534
3621
  return node.values[i];
3535
3622
  }
@@ -3641,8 +3728,16 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3641
3728
  const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
3642
3729
  let currentLeaf = null;
3643
3730
  let modified = false;
3644
- for (const [key, value] of sorted) {
3645
- const targetLeaf = await this.locateLeaf(value);
3731
+ let cachedLeafId = null;
3732
+ let cachedLeafMaxValue = null;
3733
+ for (let i = 0, len = sorted.length; i < len; i++) {
3734
+ const [key, value] = sorted[i];
3735
+ let targetLeaf;
3736
+ if (cachedLeafId !== null && cachedLeafMaxValue !== null && currentLeaf !== null && (this.comparator.isLower(value, cachedLeafMaxValue) || this.comparator.isSame(value, cachedLeafMaxValue))) {
3737
+ targetLeaf = currentLeaf;
3738
+ } else {
3739
+ targetLeaf = await this.locateLeaf(value);
3740
+ }
3646
3741
  if (currentLeaf !== null && currentLeaf.id === targetLeaf.id) {
3647
3742
  } else {
3648
3743
  if (currentLeaf !== null && modified) {
@@ -3651,8 +3746,10 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3651
3746
  currentLeaf = this._cloneNode(targetLeaf);
3652
3747
  modified = false;
3653
3748
  }
3749
+ cachedLeafId = currentLeaf.id;
3654
3750
  const changed = this._insertValueIntoLeaf(currentLeaf, key, value);
3655
3751
  modified = modified || changed;
3752
+ cachedLeafMaxValue = currentLeaf.values[currentLeaf.values.length - 1];
3656
3753
  if (currentLeaf.values.length === this.order) {
3657
3754
  await this._updateNode(currentLeaf);
3658
3755
  let after = await this._createNode(
@@ -3673,6 +3770,8 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3673
3770
  await this._updateNode(after);
3674
3771
  await this._insertInParent(currentLeaf, after.values[0], after);
3675
3772
  currentLeaf = null;
3773
+ cachedLeafId = null;
3774
+ cachedLeafMaxValue = null;
3676
3775
  modified = false;
3677
3776
  }
3678
3777
  }
@@ -3681,6 +3780,87 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3681
3780
  }
3682
3781
  });
3683
3782
  }
3783
+ async bulkLoad(entries) {
3784
+ if (entries.length === 0) return;
3785
+ return this.writeLock(0, async () => {
3786
+ const root = await this.getNode(this.rootId);
3787
+ if (!root.leaf || root.values.length > 0) {
3788
+ throw new Error("bulkLoad can only be called on an empty tree. Use batchInsert for non-empty trees.");
3789
+ }
3790
+ const sorted = [...entries].sort((a, b) => this.comparator.asc(a[1], b[1]));
3791
+ const grouped = [];
3792
+ for (let i = 0, len = sorted.length; i < len; i++) {
3793
+ const [key, value] = sorted[i];
3794
+ const last = grouped[grouped.length - 1];
3795
+ if (last && this.comparator.isSame(last.value, value)) {
3796
+ if (!last.keys.includes(key)) {
3797
+ last.keys.push(key);
3798
+ }
3799
+ } else {
3800
+ grouped.push({ keys: [key], value });
3801
+ }
3802
+ }
3803
+ await this._deleteNode(root);
3804
+ const maxLeafSize = this.order - 1;
3805
+ const leaves = [];
3806
+ for (let i = 0, len = grouped.length; i < len; i += maxLeafSize) {
3807
+ const chunk = grouped.slice(i, i + maxLeafSize);
3808
+ const leafKeys = chunk.map((g) => g.keys);
3809
+ const leafValues = chunk.map((g) => g.value);
3810
+ const leaf = await this._createNode(
3811
+ true,
3812
+ leafKeys,
3813
+ leafValues,
3814
+ null,
3815
+ null,
3816
+ null
3817
+ );
3818
+ leaves.push(leaf);
3819
+ }
3820
+ for (let i = 0, len = leaves.length; i < len; i++) {
3821
+ if (i > 0) {
3822
+ leaves[i].prev = leaves[i - 1].id;
3823
+ }
3824
+ if (i < len - 1) {
3825
+ leaves[i].next = leaves[i + 1].id;
3826
+ }
3827
+ await this._updateNode(leaves[i]);
3828
+ }
3829
+ let currentLevel = leaves;
3830
+ while (currentLevel.length > 1) {
3831
+ const nextLevel = [];
3832
+ for (let i = 0, len = currentLevel.length; i < len; i += this.order) {
3833
+ const children = currentLevel.slice(i, i + this.order);
3834
+ const childIds = children.map((c) => c.id);
3835
+ const separators = [];
3836
+ for (let j = 1, len2 = children.length; j < len2; j++) {
3837
+ separators.push(children[j].values[0]);
3838
+ }
3839
+ const internalNode = await this._createNode(
3840
+ false,
3841
+ childIds,
3842
+ separators,
3843
+ null,
3844
+ null,
3845
+ null
3846
+ );
3847
+ for (let j = 0, len2 = children.length; j < len2; j++) {
3848
+ const child = children[j];
3849
+ child.parent = internalNode.id;
3850
+ await this._updateNode(child);
3851
+ }
3852
+ nextLevel.push(internalNode);
3853
+ }
3854
+ currentLevel = nextLevel;
3855
+ }
3856
+ const newRoot = currentLevel[0];
3857
+ await this._writeHead({
3858
+ root: newRoot.id,
3859
+ order: this.order,
3860
+ data: this.strategy.head.data
3861
+ });
3862
+ });
3863
+ }
3684
3864
  async _deleteEntry(node, key) {
3685
3865
  if (!node.leaf) {
3686
3866
  let keyIndex = -1;
@@ -3784,7 +3964,8 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3784
3964
  siblingNode.values.push(...node.values);
3785
3965
  if (!siblingNode.leaf) {
3786
3966
  const keys = siblingNode.keys;
3787
- for (const key2 of keys) {
3967
+ for (let i = 0, len = keys.length; i < len; i++) {
3968
+ const key2 = keys[i];
3788
3969
  const node2 = this._cloneNode(await this.getNode(key2));
3789
3970
  node2.parent = siblingNode.id;
3790
3971
  await this._updateNode(node2);
@@ -3852,21 +4033,24 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3852
4033
  await this._updateNode(siblingNode);
3853
4034
  }
3854
4035
  if (!siblingNode.leaf) {
3855
- for (const key2 of siblingNode.keys) {
4036
+ for (let i = 0, len = siblingNode.keys.length; i < len; i++) {
4037
+ const key2 = siblingNode.keys[i];
3856
4038
  const n = this._cloneNode(await this.getNode(key2));
3857
4039
  n.parent = siblingNode.id;
3858
4040
  await this._updateNode(n);
3859
4041
  }
3860
4042
  }
3861
4043
  if (!node.leaf) {
3862
- for (const key2 of node.keys) {
4044
+ for (let i = 0, len = node.keys.length; i < len; i++) {
4045
+ const key2 = node.keys[i];
3863
4046
  const n = this._cloneNode(await this.getNode(key2));
3864
4047
  n.parent = node.id;
3865
4048
  await this._updateNode(n);
3866
4049
  }
3867
4050
  }
3868
4051
  if (!parentNode.leaf) {
3869
- for (const key2 of parentNode.keys) {
4052
+ for (let i = 0, len = parentNode.keys.length; i < len; i++) {
4053
+ const key2 = parentNode.keys[i];
3870
4054
  const n = this._cloneNode(await this.getNode(key2));
3871
4055
  n.parent = parentNode.id;
3872
4056
  await this._updateNode(n);
@@ -4048,6 +4232,16 @@ var BPTreeAsync = class extends BPTreeAsyncTransaction {
4048
4232
  }
4049
4233
  });
4050
4234
  }
4235
+ async bulkLoad(entries) {
4236
+ return this.writeLock(1, async () => {
4237
+ const tx = await this.createTransaction();
4238
+ await tx.bulkLoad(entries);
4239
+ const result = await tx.commit();
4240
+ if (!result.success) {
4241
+ throw new Error(`Transaction failed: ${result.error || "Commit failed due to conflict"}`);
4242
+ }
4243
+ });
4244
+ }
4051
4245
  };
4052
4246
  var SerializeStrategy = class {
4053
4247
  order;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.26-alpha.1",
3
+ "version": "0.0.26-alpha.2",
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.5",
51
51
  "ryoiki": "^1.2.0",
52
- "serializable-bptree": "^8.3.5"
52
+ "serializable-bptree": "^8.4.0"
53
53
  }
54
- }
54
+ }