serializable-bptree 6.1.0 → 6.2.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 +34 -2
- package/dist/cjs/index.cjs +443 -207
- package/dist/esm/index.mjs +443 -207
- package/dist/types/BPTreeAsync.d.ts +13 -3
- package/dist/types/BPTreeSync.d.ts +13 -3
- package/dist/types/base/BPTree.d.ts +53 -3
- package/package.json +1 -1
|
@@ -10,9 +10,7 @@ export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
|
|
|
10
10
|
private _createCachedNode;
|
|
11
11
|
protected readLock<T>(callback: () => Promise<T>): Promise<T>;
|
|
12
12
|
protected writeLock<T>(callback: () => Promise<T>): Promise<T>;
|
|
13
|
-
protected
|
|
14
|
-
protected getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, earlyTerminate: boolean): Promise<BPTreePair<K, V>>;
|
|
15
|
-
protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1, earlyTerminate: boolean): Promise<BPTreePair<K, V>>;
|
|
13
|
+
protected getPairsGenerator(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1, earlyTerminate: boolean): AsyncGenerator<[K, V]>;
|
|
16
14
|
protected _createNodeId(isLeaf: boolean): Promise<string>;
|
|
17
15
|
protected _createNode(isLeaf: boolean, keys: string[] | K[][], values: V[], leaf?: boolean, parent?: string | null, next?: string | null, prev?: string | null): Promise<BPTreeUnknownNode<K, V>>;
|
|
18
16
|
protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): Promise<void>;
|
|
@@ -25,6 +23,8 @@ export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
|
|
|
25
23
|
* This allows finding nodes by primary value only, ignoring unique identifiers.
|
|
26
24
|
*/
|
|
27
25
|
protected insertableNodeByPrimary(value: V): Promise<BPTreeLeafNode<K, V>>;
|
|
26
|
+
protected insertableRightestNodeByPrimary(value: V): Promise<BPTreeLeafNode<K, V>>;
|
|
27
|
+
protected insertableRightestEndNodeByPrimary(value: V): Promise<BPTreeLeafNode<K, V> | null>;
|
|
28
28
|
protected insertableEndNode(value: V, direction: 1 | -1): Promise<BPTreeLeafNode<K, V> | null>;
|
|
29
29
|
protected leftestNode(): Promise<BPTreeLeafNode<K, V>>;
|
|
30
30
|
protected rightestNode(): Promise<BPTreeLeafNode<K, V>>;
|
|
@@ -32,6 +32,16 @@ export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
|
|
|
32
32
|
protected commitNodeCreateBuffer(): Promise<void>;
|
|
33
33
|
protected commitNodeUpdateBuffer(): Promise<void>;
|
|
34
34
|
protected commitNodeDeleteBuffer(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the value associated with the given key (PK).
|
|
37
|
+
* Note: This method performs a full scan of leaf nodes as the tree is ordered by Value, not Key.
|
|
38
|
+
*
|
|
39
|
+
* @param key The key to search for.
|
|
40
|
+
* @returns The value associated with the key, or undefined if not found.
|
|
41
|
+
*/
|
|
42
|
+
get(key: K): Promise<V | undefined>;
|
|
43
|
+
keysStream(condition: BPTreeCondition<V>, filterValues?: Set<K>, limit?: number): AsyncGenerator<K>;
|
|
44
|
+
whereStream(condition: BPTreeCondition<V>, limit?: number): AsyncGenerator<[K, V]>;
|
|
35
45
|
keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Promise<Set<K>>;
|
|
36
46
|
where(condition: BPTreeCondition<V>): Promise<BPTreePair<K, V>>;
|
|
37
47
|
insert(key: K, value: V): Promise<void>;
|
|
@@ -7,9 +7,7 @@ export declare class BPTreeSync<K, V> extends BPTree<K, V> {
|
|
|
7
7
|
protected readonly nodes: ReturnType<typeof this._createCachedNode>;
|
|
8
8
|
constructor(strategy: SerializeStrategySync<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
|
|
9
9
|
private _createCachedNode;
|
|
10
|
-
protected
|
|
11
|
-
protected getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, earlyTerminate: boolean): BPTreePair<K, V>;
|
|
12
|
-
protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1, earlyTerminate: boolean): BPTreePair<K, V>;
|
|
10
|
+
protected getPairsGenerator(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1, earlyTerminate: boolean): Generator<[K, V]>;
|
|
13
11
|
protected _createNodeId(isLeaf: boolean): string;
|
|
14
12
|
protected _createNode(isLeaf: boolean, keys: string[] | K[][], values: V[], leaf?: boolean, parent?: string | null, next?: string | null, prev?: string | null): BPTreeUnknownNode<K, V>;
|
|
15
13
|
protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): void;
|
|
@@ -22,6 +20,8 @@ export declare class BPTreeSync<K, V> extends BPTree<K, V> {
|
|
|
22
20
|
* This allows finding nodes by primary value only, ignoring unique identifiers.
|
|
23
21
|
*/
|
|
24
22
|
protected insertableNodeByPrimary(value: V): BPTreeLeafNode<K, V>;
|
|
23
|
+
protected insertableRightestNodeByPrimary(value: V): BPTreeLeafNode<K, V>;
|
|
24
|
+
protected insertableRightestEndNodeByPrimary(value: V): BPTreeLeafNode<K, V> | null;
|
|
25
25
|
protected insertableEndNode(value: V, direction: 1 | -1): BPTreeLeafNode<K, V> | null;
|
|
26
26
|
protected leftestNode(): BPTreeLeafNode<K, V>;
|
|
27
27
|
protected rightestNode(): BPTreeLeafNode<K, V>;
|
|
@@ -29,6 +29,16 @@ export declare class BPTreeSync<K, V> extends BPTree<K, V> {
|
|
|
29
29
|
protected commitNodeCreateBuffer(): void;
|
|
30
30
|
protected commitNodeUpdateBuffer(): void;
|
|
31
31
|
protected commitNodeDeleteBuffer(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Retrieves the value associated with the given key (PK).
|
|
34
|
+
* Note: This method performs a full scan of leaf nodes as the tree is ordered by Value, not Key.
|
|
35
|
+
*
|
|
36
|
+
* @param key The key to search for.
|
|
37
|
+
* @returns The value associated with the key, or undefined if not found.
|
|
38
|
+
*/
|
|
39
|
+
get(key: K): V | undefined;
|
|
40
|
+
keysStream(condition: BPTreeCondition<V>, filterValues?: Set<K>, limit?: number): Generator<K>;
|
|
41
|
+
whereStream(condition: BPTreeCondition<V>, limit?: number): Generator<[K, V]>;
|
|
32
42
|
keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Set<K>;
|
|
33
43
|
where(condition: BPTreeCondition<V>): BPTreePair<K, V>;
|
|
34
44
|
insert(key: K, value: V): void;
|
|
@@ -28,6 +28,18 @@ export type BPTreeCondition<V> = Partial<{
|
|
|
28
28
|
* Useful for composite values where you want to find all entries with the same primary value.
|
|
29
29
|
*/
|
|
30
30
|
primaryEqual: Partial<V>;
|
|
31
|
+
/** Searches for pairs where the primary field is greater than the given value. */
|
|
32
|
+
primaryGt: Partial<V>;
|
|
33
|
+
/** Searches for pairs where the primary field is greater than or equal to the given value. */
|
|
34
|
+
primaryGte: Partial<V>;
|
|
35
|
+
/** Searches for pairs where the primary field is less than the given value. */
|
|
36
|
+
primaryLt: Partial<V>;
|
|
37
|
+
/** Searches for pairs where the primary field is less than or equal to the given value. */
|
|
38
|
+
primaryLte: Partial<V>;
|
|
39
|
+
/** Searches for pairs where the primary field is not equal to the given value. */
|
|
40
|
+
primaryNotEqual: Partial<V>;
|
|
41
|
+
/** Searches for pairs where the primary field matches at least one of the given values. */
|
|
42
|
+
primaryOr: Partial<V>[];
|
|
31
43
|
}>;
|
|
32
44
|
export type BPTreePair<K, V> = Map<K, V>;
|
|
33
45
|
export type BPTreeUnknownNode<K, V> = BPTreeInternalNode<K, V> | BPTreeLeafNode<K, V>;
|
|
@@ -78,11 +90,45 @@ export declare abstract class BPTree<K, V> {
|
|
|
78
90
|
* Only applicable for conditions that guarantee contiguous matches in a sorted B+Tree.
|
|
79
91
|
*/
|
|
80
92
|
protected readonly verifierEarlyTerminate: Record<keyof BPTreeCondition<V>, boolean>;
|
|
93
|
+
/**
|
|
94
|
+
* Priority map for condition types.
|
|
95
|
+
* Higher value = higher selectivity (fewer expected results).
|
|
96
|
+
* Used by `chooseDriver` to select the most selective index.
|
|
97
|
+
*/
|
|
98
|
+
protected static readonly conditionPriority: Record<keyof BPTreeCondition<unknown>, number>;
|
|
99
|
+
/**
|
|
100
|
+
* Selects the best driver tree from multiple tree/condition pairs.
|
|
101
|
+
* Uses rule-based optimization to choose the tree with highest estimated selectivity.
|
|
102
|
+
*
|
|
103
|
+
* @param candidates Array of { tree, condition } pairs to evaluate
|
|
104
|
+
* @returns The candidate with highest priority condition, or null if empty
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const driver = BPTreeSync.chooseDriver([
|
|
109
|
+
* { tree: idxId, condition: { equal: 100 } },
|
|
110
|
+
* { tree: idxAge, condition: { gt: 20 } }
|
|
111
|
+
* ])
|
|
112
|
+
* // Returns { tree: idxId, condition: { equal: 100 } } because 'equal' has higher priority
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
static ChooseDriver<T>(candidates: Array<{
|
|
116
|
+
tree: T;
|
|
117
|
+
condition: BPTreeCondition<unknown>;
|
|
118
|
+
}>): {
|
|
119
|
+
tree: T;
|
|
120
|
+
condition: BPTreeCondition<unknown>;
|
|
121
|
+
} | null;
|
|
122
|
+
/**
|
|
123
|
+
* Verified if the value satisfies the condition.
|
|
124
|
+
*
|
|
125
|
+
* @param nodeValue The value to verify.
|
|
126
|
+
* @param condition The condition to verify against.
|
|
127
|
+
* @returns Returns true if the value satisfies the condition.
|
|
128
|
+
*/
|
|
129
|
+
verify(nodeValue: V, condition: BPTreeCondition<V>): boolean;
|
|
81
130
|
protected constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
|
|
82
131
|
private _createCachedRegexp;
|
|
83
|
-
protected abstract getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, earlyTerminate: boolean): Deferred<BPTreePair<K, V>>;
|
|
84
|
-
protected abstract getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, earlyTerminate: boolean): Deferred<BPTreePair<K, V>>;
|
|
85
|
-
protected abstract getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: -1 | 1, earlyTerminate: boolean): Deferred<BPTreePair<K, V>>;
|
|
86
132
|
protected abstract _createNodeId(isLeaf: boolean): Deferred<string>;
|
|
87
133
|
protected abstract _createNode(isLeaf: boolean, keys: string[] | K[][], values: V[], leaf?: boolean, parent?: string | null, next?: string | null, prev?: string | null): Deferred<BPTreeUnknownNode<K, V>>;
|
|
88
134
|
protected abstract _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): Deferred<void>;
|
|
@@ -90,6 +136,8 @@ export declare abstract class BPTree<K, V> {
|
|
|
90
136
|
protected abstract getNode(id: string): Deferred<BPTreeUnknownNode<K, V>>;
|
|
91
137
|
protected abstract insertableNode(value: V): Deferred<BPTreeLeafNode<K, V>>;
|
|
92
138
|
protected abstract insertableNodeByPrimary(value: V): Deferred<BPTreeLeafNode<K, V>>;
|
|
139
|
+
protected abstract insertableRightestNodeByPrimary(value: V): Deferred<BPTreeLeafNode<K, V>>;
|
|
140
|
+
protected abstract insertableRightestEndNodeByPrimary(value: V): Deferred<BPTreeLeafNode<K, V> | null>;
|
|
93
141
|
protected abstract insertableEndNode(value: V, direction: 1 | -1): Deferred<BPTreeLeafNode<K, V> | null>;
|
|
94
142
|
protected abstract leftestNode(): Deferred<BPTreeLeafNode<K, V>>;
|
|
95
143
|
protected abstract rightestNode(): Deferred<BPTreeLeafNode<K, V>>;
|
|
@@ -153,6 +201,8 @@ export declare abstract class BPTree<K, V> {
|
|
|
153
201
|
protected ensureValues(v: V | V[]): V[];
|
|
154
202
|
protected lowestValue(v: V[]): V;
|
|
155
203
|
protected highestValue(v: V[]): V;
|
|
204
|
+
protected lowestPrimaryValue(v: V[]): V;
|
|
205
|
+
protected highestPrimaryValue(v: V[]): V;
|
|
156
206
|
protected _insertAtLeaf(node: BPTreeLeafNode<K, V>, key: K, value: V): void;
|
|
157
207
|
protected bufferForNodeCreate(node: BPTreeUnknownNode<K, V>): void;
|
|
158
208
|
protected bufferForNodeUpdate(node: BPTreeUnknownNode<K, V>): void;
|