serializable-bptree 6.1.1 → 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.
@@ -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 getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, earlyTerminate: boolean): BPTreePair<K, V>;
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;
@@ -31,6 +29,16 @@ export declare class BPTreeSync<K, V> extends BPTree<K, V> {
31
29
  protected commitNodeCreateBuffer(): void;
32
30
  protected commitNodeUpdateBuffer(): void;
33
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]>;
34
42
  keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Set<K>;
35
43
  where(condition: BPTreeCondition<V>): BPTreePair<K, V>;
36
44
  insert(key: K, value: V): void;
@@ -90,11 +90,45 @@ export declare abstract class BPTree<K, V> {
90
90
  * Only applicable for conditions that guarantee contiguous matches in a sorted B+Tree.
91
91
  */
92
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;
93
130
  protected constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
94
131
  private _createCachedRegexp;
95
- 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>>;
96
- 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>>;
97
- 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>>;
98
132
  protected abstract _createNodeId(isLeaf: boolean): Deferred<string>;
99
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>>;
100
134
  protected abstract _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): Deferred<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "6.1.1",
3
+ "version": "6.2.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",