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 +18 -17
- package/dist/cjs/index.cjs +72 -64
- package/dist/esm/index.mjs +72 -64
- package/dist/types/BPTreeAsync.d.ts +2 -2
- package/package.json +1 -1
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
|
|
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.
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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,
|
|
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
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -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
|
-
|
|
917
|
-
|
|
918
|
-
if (
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
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
|
-
|
|
1787
|
-
|
|
1788
|
-
if (
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
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() {
|
package/dist/esm/index.mjs
CHANGED
|
@@ -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
|
-
|
|
883
|
-
|
|
884
|
-
if (
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
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
|
-
|
|
1753
|
-
|
|
1754
|
-
if (
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
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
|
-
|
|
12
|
-
|
|
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>>;
|