serializable-bptree 6.1.1 → 6.2.1

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;
@@ -21,7 +21,6 @@ export type BPTreeCondition<V> = Partial<{
21
21
  /** Searches for pairs that satisfy at least one of the conditions. */
22
22
  or: Partial<V>[];
23
23
  /** Searches for values matching the given pattern. '%' matches zero or more characters, and '_' matches exactly one character. */
24
- like: Partial<V>;
25
24
  /**
26
25
  * Searches for pairs where the primary field equals the given value.
27
26
  * Uses `primaryAsc` method for comparison, which compares only the primary sorting field.
@@ -40,6 +39,12 @@ export type BPTreeCondition<V> = Partial<{
40
39
  primaryNotEqual: Partial<V>;
41
40
  /** Searches for pairs where the primary field matches at least one of the given values. */
42
41
  primaryOr: Partial<V>[];
42
+ /**
43
+ * Searches for values matching the given pattern on the primary field.
44
+ * Uses `match` method for getting string representation.
45
+ * '%' matches zero or more characters, and '_' matches exactly one character.
46
+ */
47
+ like: string;
43
48
  }>;
44
49
  export type BPTreePair<K, V> = Map<K, V>;
45
50
  export type BPTreeUnknownNode<K, V> = BPTreeInternalNode<K, V> | BPTreeLeafNode<K, V>;
@@ -90,11 +95,45 @@ export declare abstract class BPTree<K, V> {
90
95
  * Only applicable for conditions that guarantee contiguous matches in a sorted B+Tree.
91
96
  */
92
97
  protected readonly verifierEarlyTerminate: Record<keyof BPTreeCondition<V>, boolean>;
98
+ /**
99
+ * Priority map for condition types.
100
+ * Higher value = higher selectivity (fewer expected results).
101
+ * Used by `chooseDriver` to select the most selective index.
102
+ */
103
+ protected static readonly conditionPriority: Record<keyof BPTreeCondition<unknown>, number>;
104
+ /**
105
+ * Selects the best driver tree from multiple tree/condition pairs.
106
+ * Uses rule-based optimization to choose the tree with highest estimated selectivity.
107
+ *
108
+ * @param candidates Array of { tree, condition } pairs to evaluate
109
+ * @returns The candidate with highest priority condition, or null if empty
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const driver = BPTreeSync.chooseDriver([
114
+ * { tree: idxId, condition: { equal: 100 } },
115
+ * { tree: idxAge, condition: { gt: 20 } }
116
+ * ])
117
+ * // Returns { tree: idxId, condition: { equal: 100 } } because 'equal' has higher priority
118
+ * ```
119
+ */
120
+ static ChooseDriver<T>(candidates: Array<{
121
+ tree: T;
122
+ condition: BPTreeCondition<unknown>;
123
+ }>): {
124
+ tree: T;
125
+ condition: BPTreeCondition<unknown>;
126
+ } | null;
127
+ /**
128
+ * Verified if the value satisfies the condition.
129
+ *
130
+ * @param nodeValue The value to verify.
131
+ * @param condition The condition to verify against.
132
+ * @returns Returns true if the value satisfies the condition.
133
+ */
134
+ verify(nodeValue: V, condition: BPTreeCondition<V>): boolean;
93
135
  protected constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
94
136
  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
137
  protected abstract _createNodeId(isLeaf: boolean): Deferred<string>;
99
138
  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
139
  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.1",
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",