serializable-bptree 5.2.1 → 6.0.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.
package/README.md CHANGED
@@ -15,7 +15,7 @@ import {
15
15
 
16
16
  class FileStoreStrategySync extends SerializeStrategySync<K, V> {
17
17
  id(): string {
18
- return this.autoIncrement('index', 1).toString()
18
+ return crypto.randomUUID()
19
19
  }
20
20
 
21
21
  read(id: string): BPTreeNode<K, V> {
@@ -105,10 +105,19 @@ import {
105
105
  ValueComparator,
106
106
  NumericComparator,
107
107
  StringComparator
108
- } from 'https://cdn.jsdelivr.net/npm/serializable-bptree@5/+esm'
108
+ } from 'https://cdn.jsdelivr.com/npm/serializable-bptree@6/+esm'
109
109
  </script>
110
110
  ```
111
111
 
112
+ ## Migration from v5.x.x to v6.0.0
113
+
114
+ Version 6.0.0 includes a critical fix for how internal nodes are sorted.
115
+
116
+ > [!IMPORTANT]
117
+ > **Breaking Changes & Incompatibility**
118
+ > In previous versions, internal nodes were not strictly sorted by value magnitude, which could lead to incorrect traversals and failed queries (especially for the last nodes in a branch).
119
+ > v6.0.0 enforces strict value sorting. **Data structures created with v5.x.x or earlier may be incompatible** with v6.0.0 if they contain unsorted internal nodes. It is highly recommended to rebuild your tree from scratch when upgrading.
120
+
112
121
  ## Conceptualization
113
122
 
114
123
  ### Value comparator
@@ -199,21 +208,11 @@ What does this method mean? And why do we need to construct such a method?
199
208
 
200
209
  When a node is created in the B+tree, the node needs a unique value to represent itself. This is the **node.id** attribute, and you can specify this attribute yourself.
201
210
 
202
- Typically, such an **id** value increases sequentially, and it would be beneficial to store such a value separately within the tree. For that purpose, the **setHeadData** and **getHeadData** methods are available. These methods are responsible for storing arbitrary data in the tree's header or retrieving stored data. Below is an example of usage:
203
-
204
- ```typescript
205
- id(isLeaf: boolean): string {
206
- const current = this.getHeadData('index', 1) as number
207
- this.setHeadData('index', current+1)
208
- return current.toString()
209
- }
210
- ```
211
-
212
- Additionally, there is a more dev-friendly usage of this code.
211
+ Typically, such an **id** value can be a unique string like a UUID. Below is an example of usage:
213
212
 
214
213
  ```typescript
215
214
  id(isLeaf: boolean): string {
216
- return this.autoIncrement('index', 1).toString()
215
+ return crypto.randomUUID()
217
216
  }
218
217
  ```
219
218
 
@@ -415,7 +414,7 @@ import {
415
414
 
416
415
  class FileStoreStrategyAsync extends SerializeStrategyAsync<K, V> {
417
416
  async id(isLeaf: boolean): Promise<string> {
418
- return await this.autoIncrement('index', 1).toString()
417
+ return crypto.randomUUID()
419
418
  }
420
419
 
421
420
  async read(id: string): Promise<BPTreeNode<K, V>> {
@@ -473,9 +472,11 @@ The implementation method for asynchronous operations is not significantly diffe
473
472
 
474
473
  ### Synchronization Issue
475
474
 
476
- The serializable-bptree minimizes file I/O by storing loaded nodes in-memory. This approach works well in situations where there is a 1:1 relationship between the remote storage and the client. However, in a 1:n scenario, where multiple clients read from and write to a single remote storage, data inconsistency between the remote storage and the clients can occur.
475
+ The serializable-bptree minimizes file I/O by storing loaded nodes in-memory (caching). This approach works perfectly when a single tree instance is used for a given storage.
476
+
477
+ However, if **multiple BPTree instances** (e.g., across different processes or servers) read from and write to a **single shared storage**, data inconsistency can occur. This is because each instance maintains its own independent in-memory cache, and changes made by one instance are not automatically reflected in the others.
477
478
 
478
- To solve this problem, it's necessary to update the cached nodes. The forceUpdate method was created for this purpose. It fetches the node data cached in the tree instance again. To use this feature, when you save data to the remote storage, you must send a signal to all clients connected to that remote storage indicating that the node has been updated. Clients must receive this signal and configure logic to call the **forceUpdate** method; however, this goes beyond the scope of the library, so you must implement it yourself.
479
+ To solve this problem, you must synchronize the cached nodes across all instances. The `forceUpdate` method can be used to refresh the nodes cached in a tree instance. When one instance saves data to the shared storage, you should implement a signaling mechanism (e.g., via Pub/Sub or WebSockets) to notify other instances that a node has been updated. Upon receiving this signal, the other instances should call the `forceUpdate` method to ensure they are working with the latest data.
479
480
 
480
481
  ### Concurrency Issue in Asynchronous Trees
481
482
 
@@ -769,6 +769,9 @@ var BPTreeSync = class extends BPTree {
769
769
  guess = prevValue;
770
770
  }
771
771
  }
772
+ if (!pointer) {
773
+ return;
774
+ }
772
775
  if (node.values.length + pointer.values.length < this.order) {
773
776
  if (!isPredecessor) {
774
777
  const pTemp = pointer;
@@ -913,39 +916,40 @@ var BPTreeSync = class extends BPTree {
913
916
  return;
914
917
  }
915
918
  const parentNode = this.getNode(node.parent);
916
- for (let i = 0, len = parentNode.keys.length; i < len; i++) {
917
- const nKeys = parentNode.keys[i];
918
- if (nKeys === node.id) {
919
- parentNode.values.splice(i, 0, value);
920
- parentNode.keys.splice(i + 1, 0, pointer.id);
921
- this.bufferForNodeUpdate(parentNode);
922
- if (parentNode.keys.length > this.order) {
923
- const parentPointer = this._createNode(false, [], []);
924
- parentPointer.parent = parentNode.parent;
925
- const mid = Math.ceil(this.order / 2) - 1;
926
- parentPointer.values = parentNode.values.slice(mid + 1);
927
- parentPointer.keys = parentNode.keys.slice(mid + 1);
928
- const midValue = parentNode.values[mid];
929
- if (mid === 0) {
930
- parentNode.values = parentNode.values.slice(0, mid + 1);
931
- } else {
932
- parentNode.values = parentNode.values.slice(0, mid);
933
- }
934
- parentNode.keys = parentNode.keys.slice(0, mid + 1);
935
- for (const k of parentNode.keys) {
936
- const node2 = this.getNode(k);
937
- node2.parent = parentNode.id;
938
- this.bufferForNodeUpdate(node2);
939
- }
940
- for (const k of parentPointer.keys) {
941
- const node2 = this.getNode(k);
942
- node2.parent = parentPointer.id;
943
- this.bufferForNodeUpdate(node2);
944
- }
945
- this._insertInParent(parentNode, midValue, parentPointer);
946
- this.bufferForNodeUpdate(parentNode);
947
- }
919
+ let insertIndex = 0;
920
+ for (let i = 0; i < parentNode.values.length; i++) {
921
+ if (this.comparator.asc(value, parentNode.values[i]) > 0) {
922
+ insertIndex = i + 1;
923
+ } else {
924
+ break;
925
+ }
926
+ }
927
+ parentNode.values.splice(insertIndex, 0, value);
928
+ parentNode.keys.splice(insertIndex + 1, 0, pointer.id);
929
+ pointer.parent = parentNode.id;
930
+ this.bufferForNodeUpdate(parentNode);
931
+ this.bufferForNodeUpdate(pointer);
932
+ if (parentNode.keys.length > this.order) {
933
+ const parentPointer = this._createNode(false, [], []);
934
+ parentPointer.parent = parentNode.parent;
935
+ const mid = Math.ceil(this.order / 2) - 1;
936
+ parentPointer.values = parentNode.values.slice(mid + 1);
937
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
938
+ const midValue = parentNode.values[mid];
939
+ parentNode.values = parentNode.values.slice(0, mid);
940
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
941
+ for (const k of parentNode.keys) {
942
+ const node2 = this.getNode(k);
943
+ node2.parent = parentNode.id;
944
+ this.bufferForNodeUpdate(node2);
945
+ }
946
+ for (const k of parentPointer.keys) {
947
+ const node2 = this.getNode(k);
948
+ node2.parent = parentPointer.id;
949
+ this.bufferForNodeUpdate(node2);
948
950
  }
951
+ this._insertInParent(parentNode, midValue, parentPointer);
952
+ this.bufferForNodeUpdate(parentNode);
949
953
  }
950
954
  }
951
955
  init() {
@@ -1639,6 +1643,9 @@ var BPTreeAsync = class extends BPTree {
1639
1643
  guess = prevValue;
1640
1644
  }
1641
1645
  }
1646
+ if (!pointer) {
1647
+ return;
1648
+ }
1642
1649
  if (node.values.length + pointer.values.length < this.order) {
1643
1650
  if (!isPredecessor) {
1644
1651
  const pTemp = pointer;
@@ -1783,39 +1790,40 @@ var BPTreeAsync = class extends BPTree {
1783
1790
  return;
1784
1791
  }
1785
1792
  const parentNode = await this.getNode(node.parent);
1786
- for (let i = 0, len = parentNode.keys.length; i < len; i++) {
1787
- const nKeys = parentNode.keys[i];
1788
- if (nKeys === node.id) {
1789
- parentNode.values.splice(i, 0, value);
1790
- parentNode.keys.splice(i + 1, 0, pointer.id);
1791
- this.bufferForNodeUpdate(parentNode);
1792
- if (parentNode.keys.length > this.order) {
1793
- const parentPointer = await this._createNode(false, [], []);
1794
- parentPointer.parent = parentNode.parent;
1795
- const mid = Math.ceil(this.order / 2) - 1;
1796
- parentPointer.values = parentNode.values.slice(mid + 1);
1797
- parentPointer.keys = parentNode.keys.slice(mid + 1);
1798
- const midValue = parentNode.values[mid];
1799
- if (mid === 0) {
1800
- parentNode.values = parentNode.values.slice(0, mid + 1);
1801
- } else {
1802
- parentNode.values = parentNode.values.slice(0, mid);
1803
- }
1804
- parentNode.keys = parentNode.keys.slice(0, mid + 1);
1805
- for (const k of parentNode.keys) {
1806
- const node2 = await this.getNode(k);
1807
- node2.parent = parentNode.id;
1808
- this.bufferForNodeUpdate(node2);
1809
- }
1810
- for (const k of parentPointer.keys) {
1811
- const node2 = await this.getNode(k);
1812
- node2.parent = parentPointer.id;
1813
- this.bufferForNodeUpdate(node2);
1814
- }
1815
- await this._insertInParent(parentNode, midValue, parentPointer);
1816
- this.bufferForNodeUpdate(parentNode);
1817
- }
1793
+ let insertIndex = 0;
1794
+ for (let i = 0; i < parentNode.values.length; i++) {
1795
+ if (this.comparator.asc(value, parentNode.values[i]) > 0) {
1796
+ insertIndex = i + 1;
1797
+ } else {
1798
+ break;
1799
+ }
1800
+ }
1801
+ parentNode.values.splice(insertIndex, 0, value);
1802
+ parentNode.keys.splice(insertIndex + 1, 0, pointer.id);
1803
+ pointer.parent = parentNode.id;
1804
+ this.bufferForNodeUpdate(parentNode);
1805
+ this.bufferForNodeUpdate(pointer);
1806
+ if (parentNode.keys.length > this.order) {
1807
+ const parentPointer = await this._createNode(false, [], []);
1808
+ parentPointer.parent = parentNode.parent;
1809
+ const mid = Math.ceil(this.order / 2) - 1;
1810
+ parentPointer.values = parentNode.values.slice(mid + 1);
1811
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
1812
+ const midValue = parentNode.values[mid];
1813
+ parentNode.values = parentNode.values.slice(0, mid);
1814
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
1815
+ for (const k of parentNode.keys) {
1816
+ const node2 = await this.getNode(k);
1817
+ node2.parent = parentNode.id;
1818
+ this.bufferForNodeUpdate(node2);
1819
+ }
1820
+ for (const k of parentPointer.keys) {
1821
+ const node2 = await this.getNode(k);
1822
+ node2.parent = parentPointer.id;
1823
+ this.bufferForNodeUpdate(node2);
1818
1824
  }
1825
+ await this._insertInParent(parentNode, midValue, parentPointer);
1826
+ this.bufferForNodeUpdate(parentNode);
1819
1827
  }
1820
1828
  }
1821
1829
  async init() {
@@ -735,6 +735,9 @@ var BPTreeSync = class extends BPTree {
735
735
  guess = prevValue;
736
736
  }
737
737
  }
738
+ if (!pointer) {
739
+ return;
740
+ }
738
741
  if (node.values.length + pointer.values.length < this.order) {
739
742
  if (!isPredecessor) {
740
743
  const pTemp = pointer;
@@ -879,39 +882,40 @@ var BPTreeSync = class extends BPTree {
879
882
  return;
880
883
  }
881
884
  const parentNode = this.getNode(node.parent);
882
- for (let i = 0, len = parentNode.keys.length; i < len; i++) {
883
- const nKeys = parentNode.keys[i];
884
- if (nKeys === node.id) {
885
- parentNode.values.splice(i, 0, value);
886
- parentNode.keys.splice(i + 1, 0, pointer.id);
887
- this.bufferForNodeUpdate(parentNode);
888
- if (parentNode.keys.length > this.order) {
889
- const parentPointer = this._createNode(false, [], []);
890
- parentPointer.parent = parentNode.parent;
891
- const mid = Math.ceil(this.order / 2) - 1;
892
- parentPointer.values = parentNode.values.slice(mid + 1);
893
- parentPointer.keys = parentNode.keys.slice(mid + 1);
894
- const midValue = parentNode.values[mid];
895
- if (mid === 0) {
896
- parentNode.values = parentNode.values.slice(0, mid + 1);
897
- } else {
898
- parentNode.values = parentNode.values.slice(0, mid);
899
- }
900
- parentNode.keys = parentNode.keys.slice(0, mid + 1);
901
- for (const k of parentNode.keys) {
902
- const node2 = this.getNode(k);
903
- node2.parent = parentNode.id;
904
- this.bufferForNodeUpdate(node2);
905
- }
906
- for (const k of parentPointer.keys) {
907
- const node2 = this.getNode(k);
908
- node2.parent = parentPointer.id;
909
- this.bufferForNodeUpdate(node2);
910
- }
911
- this._insertInParent(parentNode, midValue, parentPointer);
912
- this.bufferForNodeUpdate(parentNode);
913
- }
885
+ let insertIndex = 0;
886
+ for (let i = 0; i < parentNode.values.length; i++) {
887
+ if (this.comparator.asc(value, parentNode.values[i]) > 0) {
888
+ insertIndex = i + 1;
889
+ } else {
890
+ break;
891
+ }
892
+ }
893
+ parentNode.values.splice(insertIndex, 0, value);
894
+ parentNode.keys.splice(insertIndex + 1, 0, pointer.id);
895
+ pointer.parent = parentNode.id;
896
+ this.bufferForNodeUpdate(parentNode);
897
+ this.bufferForNodeUpdate(pointer);
898
+ if (parentNode.keys.length > this.order) {
899
+ const parentPointer = this._createNode(false, [], []);
900
+ parentPointer.parent = parentNode.parent;
901
+ const mid = Math.ceil(this.order / 2) - 1;
902
+ parentPointer.values = parentNode.values.slice(mid + 1);
903
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
904
+ const midValue = parentNode.values[mid];
905
+ parentNode.values = parentNode.values.slice(0, mid);
906
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
907
+ for (const k of parentNode.keys) {
908
+ const node2 = this.getNode(k);
909
+ node2.parent = parentNode.id;
910
+ this.bufferForNodeUpdate(node2);
911
+ }
912
+ for (const k of parentPointer.keys) {
913
+ const node2 = this.getNode(k);
914
+ node2.parent = parentPointer.id;
915
+ this.bufferForNodeUpdate(node2);
914
916
  }
917
+ this._insertInParent(parentNode, midValue, parentPointer);
918
+ this.bufferForNodeUpdate(parentNode);
915
919
  }
916
920
  }
917
921
  init() {
@@ -1605,6 +1609,9 @@ var BPTreeAsync = class extends BPTree {
1605
1609
  guess = prevValue;
1606
1610
  }
1607
1611
  }
1612
+ if (!pointer) {
1613
+ return;
1614
+ }
1608
1615
  if (node.values.length + pointer.values.length < this.order) {
1609
1616
  if (!isPredecessor) {
1610
1617
  const pTemp = pointer;
@@ -1749,39 +1756,40 @@ var BPTreeAsync = class extends BPTree {
1749
1756
  return;
1750
1757
  }
1751
1758
  const parentNode = await this.getNode(node.parent);
1752
- for (let i = 0, len = parentNode.keys.length; i < len; i++) {
1753
- const nKeys = parentNode.keys[i];
1754
- if (nKeys === node.id) {
1755
- parentNode.values.splice(i, 0, value);
1756
- parentNode.keys.splice(i + 1, 0, pointer.id);
1757
- this.bufferForNodeUpdate(parentNode);
1758
- if (parentNode.keys.length > this.order) {
1759
- const parentPointer = await this._createNode(false, [], []);
1760
- parentPointer.parent = parentNode.parent;
1761
- const mid = Math.ceil(this.order / 2) - 1;
1762
- parentPointer.values = parentNode.values.slice(mid + 1);
1763
- parentPointer.keys = parentNode.keys.slice(mid + 1);
1764
- const midValue = parentNode.values[mid];
1765
- if (mid === 0) {
1766
- parentNode.values = parentNode.values.slice(0, mid + 1);
1767
- } else {
1768
- parentNode.values = parentNode.values.slice(0, mid);
1769
- }
1770
- parentNode.keys = parentNode.keys.slice(0, mid + 1);
1771
- for (const k of parentNode.keys) {
1772
- const node2 = await this.getNode(k);
1773
- node2.parent = parentNode.id;
1774
- this.bufferForNodeUpdate(node2);
1775
- }
1776
- for (const k of parentPointer.keys) {
1777
- const node2 = await this.getNode(k);
1778
- node2.parent = parentPointer.id;
1779
- this.bufferForNodeUpdate(node2);
1780
- }
1781
- await this._insertInParent(parentNode, midValue, parentPointer);
1782
- this.bufferForNodeUpdate(parentNode);
1783
- }
1759
+ let insertIndex = 0;
1760
+ for (let i = 0; i < parentNode.values.length; i++) {
1761
+ if (this.comparator.asc(value, parentNode.values[i]) > 0) {
1762
+ insertIndex = i + 1;
1763
+ } else {
1764
+ break;
1765
+ }
1766
+ }
1767
+ parentNode.values.splice(insertIndex, 0, value);
1768
+ parentNode.keys.splice(insertIndex + 1, 0, pointer.id);
1769
+ pointer.parent = parentNode.id;
1770
+ this.bufferForNodeUpdate(parentNode);
1771
+ this.bufferForNodeUpdate(pointer);
1772
+ if (parentNode.keys.length > this.order) {
1773
+ const parentPointer = await this._createNode(false, [], []);
1774
+ parentPointer.parent = parentNode.parent;
1775
+ const mid = Math.ceil(this.order / 2) - 1;
1776
+ parentPointer.values = parentNode.values.slice(mid + 1);
1777
+ parentPointer.keys = parentNode.keys.slice(mid + 1);
1778
+ const midValue = parentNode.values[mid];
1779
+ parentNode.values = parentNode.values.slice(0, mid);
1780
+ parentNode.keys = parentNode.keys.slice(0, mid + 1);
1781
+ for (const k of parentNode.keys) {
1782
+ const node2 = await this.getNode(k);
1783
+ node2.parent = parentNode.id;
1784
+ this.bufferForNodeUpdate(node2);
1785
+ }
1786
+ for (const k of parentPointer.keys) {
1787
+ const node2 = await this.getNode(k);
1788
+ node2.parent = parentPointer.id;
1789
+ this.bufferForNodeUpdate(node2);
1784
1790
  }
1791
+ await this._insertInParent(parentNode, midValue, parentPointer);
1792
+ this.bufferForNodeUpdate(parentNode);
1785
1793
  }
1786
1794
  }
1787
1795
  async init() {
@@ -8,8 +8,8 @@ export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
8
8
  private readonly lock;
9
9
  constructor(strategy: SerializeStrategyAsync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
10
10
  private _createCachedNode;
11
- private readLock;
12
- private writeLock;
11
+ protected readLock<T>(callback: () => Promise<T>): Promise<T>;
12
+ protected writeLock<T>(callback: () => Promise<T>): Promise<T>;
13
13
  protected getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): Promise<BPTreePair<K, V>>;
14
14
  protected getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): Promise<BPTreePair<K, V>>;
15
15
  protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): Promise<BPTreePair<K, V>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "5.2.1",
3
+ "version": "6.0.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",