serializable-bptree 8.1.7 → 8.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 CHANGED
@@ -158,7 +158,7 @@ const others = candidates.filter((c) => driver.tree !== c.tree)
158
158
  // 2. Execute query using the selected driver
159
159
  let keys = driver.tree.keys(driver.condition)
160
160
  for (const { tree, condition } of others) {
161
- keys = tree.keys(condition, keys)
161
+ keys = tree.keys(condition, { filterValues: keys })
162
162
  }
163
163
 
164
164
  console.log('Found: ', keys)
@@ -2154,17 +2154,24 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2154
2154
  }
2155
2155
  return void 0;
2156
2156
  }
2157
- *keysStream(condition, filterValues, limit, order = "asc") {
2158
- const stream = this.whereStream(condition, limit, order);
2157
+ *keysStream(condition, options) {
2158
+ const { filterValues, limit, order = "asc" } = options ?? {};
2159
+ const stream = this.whereStream(condition, options);
2159
2160
  const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
2161
+ let count = 0;
2160
2162
  for (const [key] of stream) {
2161
2163
  if (intersection && !intersection.has(key)) {
2162
2164
  continue;
2163
2165
  }
2164
2166
  yield key;
2167
+ count++;
2168
+ if (limit !== void 0 && count >= limit) {
2169
+ break;
2170
+ }
2165
2171
  }
2166
2172
  }
2167
- *whereStream(condition, limit, order = "asc") {
2173
+ *whereStream(condition, options) {
2174
+ const { filterValues, limit, order = "asc" } = options ?? {};
2168
2175
  const driverKey = this.getDriverKey(condition);
2169
2176
  if (!driverKey) return;
2170
2177
  const value = condition[driverKey];
@@ -2187,8 +2194,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2187
2194
  earlyTerminate
2188
2195
  );
2189
2196
  let count = 0;
2197
+ const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
2190
2198
  for (const pair of generator) {
2191
2199
  const [k, v] = pair;
2200
+ if (intersection && !intersection.has(k)) {
2201
+ continue;
2202
+ }
2192
2203
  let isMatch = true;
2193
2204
  for (const key in condition) {
2194
2205
  if (key === driverKey) continue;
@@ -2208,16 +2219,16 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2208
2219
  }
2209
2220
  }
2210
2221
  }
2211
- keys(condition, filterValues, order = "asc") {
2222
+ keys(condition, options) {
2212
2223
  const set = /* @__PURE__ */ new Set();
2213
- for (const key of this.keysStream(condition, filterValues, void 0, order)) {
2224
+ for (const key of this.keysStream(condition, options)) {
2214
2225
  set.add(key);
2215
2226
  }
2216
2227
  return set;
2217
2228
  }
2218
- where(condition, order = "asc") {
2229
+ where(condition, options) {
2219
2230
  const map = /* @__PURE__ */ new Map();
2220
- for (const [key, value] of this.whereStream(condition, void 0, order)) {
2231
+ for (const [key, value] of this.whereStream(condition, options)) {
2221
2232
  map.set(key, value);
2222
2233
  }
2223
2234
  return map;
@@ -3229,17 +3240,24 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3229
3240
  }
3230
3241
  return void 0;
3231
3242
  }
3232
- async *keysStream(condition, filterValues, limit, order = "asc") {
3233
- const stream = this.whereStream(condition, limit, order);
3243
+ async *keysStream(condition, options) {
3244
+ const { filterValues, limit, order = "asc" } = options ?? {};
3245
+ const stream = this.whereStream(condition, options);
3234
3246
  const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
3247
+ let count = 0;
3235
3248
  for await (const [key] of stream) {
3236
3249
  if (intersection && !intersection.has(key)) {
3237
3250
  continue;
3238
3251
  }
3239
3252
  yield key;
3253
+ count++;
3254
+ if (limit !== void 0 && count >= limit) {
3255
+ break;
3256
+ }
3240
3257
  }
3241
3258
  }
3242
- async *whereStream(condition, limit, order = "asc") {
3259
+ async *whereStream(condition, options) {
3260
+ const { filterValues, limit, order = "asc" } = options ?? {};
3243
3261
  const driverKey = this.getDriverKey(condition);
3244
3262
  if (!driverKey) return;
3245
3263
  const value = condition[driverKey];
@@ -3262,8 +3280,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3262
3280
  earlyTerminate
3263
3281
  );
3264
3282
  let count = 0;
3283
+ const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
3265
3284
  for await (const pair of generator) {
3266
3285
  const [k, v] = pair;
3286
+ if (intersection && !intersection.has(k)) {
3287
+ continue;
3288
+ }
3267
3289
  let isMatch = true;
3268
3290
  for (const key in condition) {
3269
3291
  if (key === driverKey) continue;
@@ -3283,16 +3305,16 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3283
3305
  }
3284
3306
  }
3285
3307
  }
3286
- async keys(condition, filterValues, order = "asc") {
3308
+ async keys(condition, options) {
3287
3309
  const set = /* @__PURE__ */ new Set();
3288
- for await (const key of this.keysStream(condition, filterValues, void 0, order)) {
3310
+ for await (const key of this.keysStream(condition, options)) {
3289
3311
  set.add(key);
3290
3312
  }
3291
3313
  return set;
3292
3314
  }
3293
- async where(condition, order = "asc") {
3315
+ async where(condition, options) {
3294
3316
  const map = /* @__PURE__ */ new Map();
3295
- for await (const [key, value] of this.whereStream(condition, void 0, order)) {
3317
+ for await (const [key, value] of this.whereStream(condition, options)) {
3296
3318
  map.set(key, value);
3297
3319
  }
3298
3320
  return map;
@@ -2118,17 +2118,24 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2118
2118
  }
2119
2119
  return void 0;
2120
2120
  }
2121
- *keysStream(condition, filterValues, limit, order = "asc") {
2122
- const stream = this.whereStream(condition, limit, order);
2121
+ *keysStream(condition, options) {
2122
+ const { filterValues, limit, order = "asc" } = options ?? {};
2123
+ const stream = this.whereStream(condition, options);
2123
2124
  const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
2125
+ let count = 0;
2124
2126
  for (const [key] of stream) {
2125
2127
  if (intersection && !intersection.has(key)) {
2126
2128
  continue;
2127
2129
  }
2128
2130
  yield key;
2131
+ count++;
2132
+ if (limit !== void 0 && count >= limit) {
2133
+ break;
2134
+ }
2129
2135
  }
2130
2136
  }
2131
- *whereStream(condition, limit, order = "asc") {
2137
+ *whereStream(condition, options) {
2138
+ const { filterValues, limit, order = "asc" } = options ?? {};
2132
2139
  const driverKey = this.getDriverKey(condition);
2133
2140
  if (!driverKey) return;
2134
2141
  const value = condition[driverKey];
@@ -2151,8 +2158,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2151
2158
  earlyTerminate
2152
2159
  );
2153
2160
  let count = 0;
2161
+ const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
2154
2162
  for (const pair of generator) {
2155
2163
  const [k, v] = pair;
2164
+ if (intersection && !intersection.has(k)) {
2165
+ continue;
2166
+ }
2156
2167
  let isMatch = true;
2157
2168
  for (const key in condition) {
2158
2169
  if (key === driverKey) continue;
@@ -2172,16 +2183,16 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2172
2183
  }
2173
2184
  }
2174
2185
  }
2175
- keys(condition, filterValues, order = "asc") {
2186
+ keys(condition, options) {
2176
2187
  const set = /* @__PURE__ */ new Set();
2177
- for (const key of this.keysStream(condition, filterValues, void 0, order)) {
2188
+ for (const key of this.keysStream(condition, options)) {
2178
2189
  set.add(key);
2179
2190
  }
2180
2191
  return set;
2181
2192
  }
2182
- where(condition, order = "asc") {
2193
+ where(condition, options) {
2183
2194
  const map = /* @__PURE__ */ new Map();
2184
- for (const [key, value] of this.whereStream(condition, void 0, order)) {
2195
+ for (const [key, value] of this.whereStream(condition, options)) {
2185
2196
  map.set(key, value);
2186
2197
  }
2187
2198
  return map;
@@ -3193,17 +3204,24 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3193
3204
  }
3194
3205
  return void 0;
3195
3206
  }
3196
- async *keysStream(condition, filterValues, limit, order = "asc") {
3197
- const stream = this.whereStream(condition, limit, order);
3207
+ async *keysStream(condition, options) {
3208
+ const { filterValues, limit, order = "asc" } = options ?? {};
3209
+ const stream = this.whereStream(condition, options);
3198
3210
  const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
3211
+ let count = 0;
3199
3212
  for await (const [key] of stream) {
3200
3213
  if (intersection && !intersection.has(key)) {
3201
3214
  continue;
3202
3215
  }
3203
3216
  yield key;
3217
+ count++;
3218
+ if (limit !== void 0 && count >= limit) {
3219
+ break;
3220
+ }
3204
3221
  }
3205
3222
  }
3206
- async *whereStream(condition, limit, order = "asc") {
3223
+ async *whereStream(condition, options) {
3224
+ const { filterValues, limit, order = "asc" } = options ?? {};
3207
3225
  const driverKey = this.getDriverKey(condition);
3208
3226
  if (!driverKey) return;
3209
3227
  const value = condition[driverKey];
@@ -3226,8 +3244,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3226
3244
  earlyTerminate
3227
3245
  );
3228
3246
  let count = 0;
3247
+ const intersection = filterValues && filterValues.size > 0 ? filterValues : null;
3229
3248
  for await (const pair of generator) {
3230
3249
  const [k, v] = pair;
3250
+ if (intersection && !intersection.has(k)) {
3251
+ continue;
3252
+ }
3231
3253
  let isMatch = true;
3232
3254
  for (const key in condition) {
3233
3255
  if (key === driverKey) continue;
@@ -3247,16 +3269,16 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3247
3269
  }
3248
3270
  }
3249
3271
  }
3250
- async keys(condition, filterValues, order = "asc") {
3272
+ async keys(condition, options) {
3251
3273
  const set = /* @__PURE__ */ new Set();
3252
- for await (const key of this.keysStream(condition, filterValues, void 0, order)) {
3274
+ for await (const key of this.keysStream(condition, options)) {
3253
3275
  set.add(key);
3254
3276
  }
3255
3277
  return set;
3256
3278
  }
3257
- async where(condition, order = "asc") {
3279
+ async where(condition, options) {
3258
3280
  const map = /* @__PURE__ */ new Map();
3259
- for await (const [key, value] of this.whereStream(condition, void 0, order)) {
3281
+ for await (const [key, value] of this.whereStream(condition, options)) {
3260
3282
  map.set(key, value);
3261
3283
  }
3262
3284
  return map;
@@ -1,5 +1,5 @@
1
1
  import type { TransactionEntry, TransactionResult } from 'mvcc-api';
2
- import type { BPTreeCondition, BPTreeConstructorOption, BPTreeUnknownNode, Deferred, BPTreeLeafNode, BPTreeNodeKey, BPTreePair, SerializableData, BPTreeNode, BPTreeMVCC } from '../types';
2
+ import type { BPTreeCondition, BPTreeConstructorOption, BPTreeUnknownNode, Deferred, BPTreeLeafNode, BPTreeNodeKey, BPTreePair, SerializableData, BPTreeNode, BPTreeMVCC, BPTreeSearchOption } from '../types';
3
3
  import { ValueComparator } from './ValueComparator';
4
4
  import { SerializeStrategy } from './SerializeStrategy';
5
5
  export declare abstract class BPTreeTransaction<K, V> {
@@ -107,28 +107,44 @@ export declare abstract class BPTreeTransaction<K, V> {
107
107
  * This method is used to initialize the stored tree and recover data.
108
108
  * If it is not called, the tree will not function.
109
109
  */
110
+ abstract init(): Deferred<void>;
110
111
  /**
111
- * After creating a tree instance, it must be called.
112
- * This method is used to initialize the stored tree and recover data.
113
- * If it is not called, the tree will not function.
112
+ * Retrieves the value associated with the given key.
113
+ * @param key The key to search for.
114
+ * @returns A Deferred that resolves to the value if found, or undefined if not found.
114
115
  */
115
- abstract init(): Deferred<void>;
116
+ abstract get(key: K): Deferred<V | undefined>;
117
+ /**
118
+ * Returns a generator that yields keys satisfying the given condition.
119
+ * This is a memory-efficient way to iterate through keys when dealing with large result sets.
120
+ * @param condition The search condition (e.g., gt, lt, equal, like).
121
+ * @param options Search options including filterValues, limit, and order.
122
+ * @returns An async or synchronous generator yielding keys of type K.
123
+ */
124
+ abstract keysStream(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): AsyncGenerator<K> | Generator<K>;
125
+ /**
126
+ * Returns a generator that yields [key, value] pairs satisfying the given condition.
127
+ * This is a memory-efficient way to iterate through pairs when dealing with large result sets.
128
+ * @param condition The search condition (e.g., gt, lt, equal, like).
129
+ * @param options Search options including filterValues, limit, and order.
130
+ * @returns An async or synchronous generator yielding [K, V] tuples.
131
+ */
132
+ abstract whereStream(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): AsyncGenerator<[K, V]> | Generator<[K, V]>;
116
133
  /**
117
134
  * It searches for a key within the tree. The result is returned as an array sorted in ascending order based on the value.
118
135
  * The result is key set instance, and you can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
119
136
  * This method operates much faster than first searching with `where` and then retrieving only the key list.
120
137
  * @param condition You can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
121
- * @param filterValues The `Set` containing values to check for intersection.
122
- * Returns a `Set` containing values that are common to both the input `Set` and the intersection `Set`.
123
- * If this parameter is not provided, it searches for all keys inserted into the tree.
138
+ * @param options Search options including filterValues, limit, and order.
124
139
  */
125
- abstract keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Deferred<Set<K>>;
140
+ abstract keys(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Deferred<Set<K>>;
126
141
  /**
127
142
  * It searches for a value within the tree. The result is returned as an array sorted in ascending order based on the value.
128
143
  * The result includes the key and value attributes, and you can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
129
144
  * @param condition You can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
145
+ * @param options Search options including filterValues, limit, and order.
130
146
  */
131
- abstract where(condition: BPTreeCondition<V>): Deferred<BPTreePair<K, V>>;
147
+ abstract where(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Deferred<BPTreePair<K, V>>;
132
148
  /**
133
149
  * You enter the key and value as a pair. You can later search for the pair by value.
134
150
  * This data is stored in the tree, sorted in ascending order of value.
@@ -138,10 +154,8 @@ export declare abstract class BPTreeTransaction<K, V> {
138
154
  abstract insert(key: K, value: V): Deferred<void>;
139
155
  /**
140
156
  * Deletes the pair that matches the key and value.
141
- *
142
157
  * @param key The key of the pair. This key must be unique.
143
158
  * @param value The value of the pair.
144
- *
145
159
  * @warning If the 'value' is not specified, a full scan will be performed to find the value associated with the key, which may lead to performance degradation.
146
160
  */
147
161
  abstract delete(key: K, value?: V): Deferred<void>;
@@ -1,5 +1,5 @@
1
1
  import type { TransactionResult } from 'mvcc-api';
2
- import type { AsyncBPTreeMVCC, BPTreeCondition, BPTreeConstructorOption, BPTreeLeafNode, BPTreeNode, BPTreeNodeKey, BPTreeOrder, BPTreePair, BPTreeUnknownNode, SerializableData, SerializeStrategyHead } from '../types';
2
+ import type { AsyncBPTreeMVCC, BPTreeCondition, BPTreeConstructorOption, BPTreeLeafNode, BPTreeNode, BPTreeNodeKey, BPTreePair, BPTreeUnknownNode, SerializableData, SerializeStrategyHead, BPTreeSearchOption } from '../types';
3
3
  import { Ryoiki } from 'ryoiki';
4
4
  import { BPTreeTransaction } from '../base/BPTreeTransaction';
5
5
  import { SerializeStrategyAsync } from '../SerializeStrategyAsync';
@@ -37,10 +37,10 @@ export declare class BPTreeAsyncTransaction<K, V> extends BPTreeTransaction<K, V
37
37
  protected _initInternal(): Promise<void>;
38
38
  exists(key: K, value: V): Promise<boolean>;
39
39
  get(key: K): Promise<V | undefined>;
40
- keysStream(condition: BPTreeCondition<V>, filterValues?: Set<K>, limit?: number, order?: BPTreeOrder): AsyncGenerator<K>;
41
- whereStream(condition: BPTreeCondition<V>, limit?: number, order?: BPTreeOrder): AsyncGenerator<[K, V]>;
42
- keys(condition: BPTreeCondition<V>, filterValues?: Set<K>, order?: BPTreeOrder): Promise<Set<K>>;
43
- where(condition: BPTreeCondition<V>, order?: BPTreeOrder): Promise<BPTreePair<K, V>>;
40
+ keysStream(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): AsyncGenerator<K>;
41
+ whereStream(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): AsyncGenerator<[K, V]>;
42
+ keys(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Promise<Set<K>>;
43
+ where(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Promise<BPTreePair<K, V>>;
44
44
  insert(key: K, value: V): Promise<void>;
45
45
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>): Promise<BPTreeUnknownNode<K, V>>;
46
46
  delete(key: K, value?: V): Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import type { TransactionResult } from 'mvcc-api';
2
- import type { BPTreeCondition, BPTreeConstructorOption, BPTreeLeafNode, BPTreeNode, BPTreeNodeKey, BPTreeOrder, BPTreePair, BPTreeUnknownNode, SerializableData, SerializeStrategyHead, SyncBPTreeMVCC } from '../types';
2
+ import type { BPTreeCondition, BPTreeConstructorOption, BPTreeLeafNode, BPTreeNode, BPTreeNodeKey, BPTreePair, BPTreeUnknownNode, SerializableData, SerializeStrategyHead, SyncBPTreeMVCC, BPTreeSearchOption } from '../types';
3
3
  import { BPTreeTransaction } from '../base/BPTreeTransaction';
4
4
  import { SerializeStrategySync } from '../SerializeStrategySync';
5
5
  import { ValueComparator } from '../base/ValueComparator';
@@ -34,10 +34,10 @@ export declare class BPTreeSyncTransaction<K, V> extends BPTreeTransaction<K, V>
34
34
  protected _initInternal(): void;
35
35
  exists(key: K, value: V): boolean;
36
36
  get(key: K): V | undefined;
37
- keysStream(condition: BPTreeCondition<V>, filterValues?: Set<K>, limit?: number, order?: BPTreeOrder): Generator<K>;
38
- whereStream(condition: BPTreeCondition<V>, limit?: number, order?: BPTreeOrder): Generator<[K, V]>;
39
- keys(condition: BPTreeCondition<V>, filterValues?: Set<K>, order?: BPTreeOrder): Set<K>;
40
- where(condition: BPTreeCondition<V>, order?: BPTreeOrder): BPTreePair<K, V>;
37
+ keysStream(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Generator<K>;
38
+ whereStream(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Generator<[K, V]>;
39
+ keys(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): Set<K>;
40
+ where(condition: BPTreeCondition<V>, options?: BPTreeSearchOption<K>): BPTreePair<K, V>;
41
41
  insert(key: K, value: V): void;
42
42
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>): BPTreeUnknownNode<K, V>;
43
43
  delete(key: K, value?: V): void;
@@ -52,6 +52,11 @@ export type BPTreeCondition<V> = Partial<{
52
52
  * - `'desc'`: Descending order - traverses from right to left
53
53
  */
54
54
  export type BPTreeOrder = 'asc' | 'desc';
55
+ export interface BPTreeSearchOption<K> {
56
+ filterValues?: Set<K>;
57
+ limit?: number;
58
+ order?: BPTreeOrder;
59
+ }
55
60
  export type BPTreePair<K, V> = Map<K, V>;
56
61
  export type BPTreeUnknownNode<K, V> = BPTreeInternalNode<K, V> | BPTreeLeafNode<K, V>;
57
62
  export interface BPTreeConstructorOption {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "8.1.7",
3
+ "version": "8.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",