serializable-bptree 3.2.1 → 3.2.3

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
@@ -130,7 +130,7 @@ class AgeComparator extends ValueComparator<MyObject> {
130
130
  }
131
131
 
132
132
  match(value: MyObject): string {
133
- return value.age
133
+ return value.age.toString()
134
134
  }
135
135
  }
136
136
  ```
@@ -141,7 +141,7 @@ The **asc** method should return values in ascending order. If the return value
141
141
 
142
142
  #### match
143
143
 
144
- The `match` method is used for the **LIKE** operator. This method specifies which value to test against a regular expression. For example, if you have a tree with values of the structure `{ country: string, capital: number }`, and you want to perform a **LIKE** operation based on the **capital** value, the method should return **value.capital**. In this case, you **CANNOT** perform a **LIKE** operation based on the **country** attribute. The returned value must be a string.
144
+ The `match` method is used for the **LIKE** operator. This method specifies which value to test against a regular expression. For example, if you have a tree with values of the structure `{ country: string, capital: string }`, and you want to perform a **LIKE** operation based on the **capital** value, the method should return **value.capital**. In this case, you **CANNOT** perform a **LIKE** operation based on the **country** attribute. The returned value must be a string.
145
145
 
146
146
  ```typescript
147
147
  interface MyObject {
package/dist/cjs/index.js CHANGED
@@ -181,7 +181,7 @@ var BPTreeSync = class extends BPTree {
181
181
  constructor(strategy, comparator) {
182
182
  super(strategy, comparator);
183
183
  }
184
- _getPairsRightToLeft(value, startNode, fullSearch, comparator) {
184
+ _getPairsRightToLeft(value, startNode, fullScan, comparator) {
185
185
  const pairs = [];
186
186
  let node = startNode;
187
187
  let done = false;
@@ -197,7 +197,7 @@ var BPTreeSync = class extends BPTree {
197
197
  while (j--) {
198
198
  pairs.push({ key: keys[j], value: nValue });
199
199
  }
200
- } else if (found && !fullSearch) {
200
+ } else if (found && !fullScan) {
201
201
  done = true;
202
202
  break;
203
203
  }
@@ -210,7 +210,7 @@ var BPTreeSync = class extends BPTree {
210
210
  }
211
211
  return pairs.reverse();
212
212
  }
213
- _getPairsLeftToRight(value, startNode, fullSearch, comparator) {
213
+ _getPairsLeftToRight(value, startNode, fullScan, comparator) {
214
214
  const pairs = [];
215
215
  let node = startNode;
216
216
  let done = false;
@@ -224,7 +224,7 @@ var BPTreeSync = class extends BPTree {
224
224
  for (const key of keys) {
225
225
  pairs.push({ key, value: nValue });
226
226
  }
227
- } else if (found && !fullSearch) {
227
+ } else if (found && !fullScan) {
228
228
  done = true;
229
229
  break;
230
230
  }
@@ -237,12 +237,12 @@ var BPTreeSync = class extends BPTree {
237
237
  }
238
238
  return pairs;
239
239
  }
240
- getPairs(value, startNode, fullSearch, comparator, direction) {
240
+ getPairs(value, startNode, fullScan, comparator, direction) {
241
241
  switch (direction) {
242
242
  case -1:
243
- return this._getPairsRightToLeft(value, startNode, fullSearch, comparator);
243
+ return this._getPairsRightToLeft(value, startNode, fullScan, comparator);
244
244
  case 1:
245
- return this._getPairsLeftToRight(value, startNode, fullSearch, comparator);
245
+ return this._getPairsLeftToRight(value, startNode, fullScan, comparator);
246
246
  default:
247
247
  throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
248
248
  }
@@ -610,8 +610,7 @@ var BPTreeSync = class extends BPTree {
610
610
  }
611
611
  this._nodeUpdateBuffer.clear();
612
612
  }
613
- keys(condition) {
614
- let result = null;
613
+ keys(condition, filterValues) {
615
614
  for (const k in condition) {
616
615
  const key = k;
617
616
  const value = condition[key];
@@ -620,13 +619,19 @@ var BPTreeSync = class extends BPTree {
620
619
  const fullScan = this.verifierFullScan[key];
621
620
  const comparator = this.verifierMap[key];
622
621
  const pairs = this.getPairs(value, startNode, fullScan, comparator, direction);
623
- if (result === null) {
624
- result = pairs.map((pair) => pair.key);
622
+ if (!filterValues) {
623
+ filterValues = new Set(pairs.map((pair) => pair.key));
625
624
  } else {
626
- result = result.filter((key2) => pairs.find((p) => p.key === key2));
625
+ const intersections = /* @__PURE__ */ new Set();
626
+ for (const pair of pairs) {
627
+ if (filterValues.has(pair.key)) {
628
+ intersections.add(pair.key);
629
+ }
630
+ }
631
+ filterValues = intersections;
627
632
  }
628
633
  }
629
- return new Set(result ?? []);
634
+ return filterValues ?? /* @__PURE__ */ new Set([]);
630
635
  }
631
636
  where(condition) {
632
637
  let result = null;
@@ -641,7 +646,13 @@ var BPTreeSync = class extends BPTree {
641
646
  if (result === null) {
642
647
  result = pairs;
643
648
  } else {
644
- result = result.filter((pair) => pairs.find((p) => p.key === pair.key));
649
+ const intersection = [];
650
+ for (const pair of pairs) {
651
+ if (result.find((p) => p.key === pair.key)) {
652
+ intersection.push(pair);
653
+ }
654
+ }
655
+ result = intersection;
645
656
  }
646
657
  }
647
658
  return result ?? [];
@@ -1140,8 +1151,7 @@ var BPTreeAsync = class extends BPTree {
1140
1151
  }
1141
1152
  this._nodeUpdateBuffer.clear();
1142
1153
  }
1143
- async keys(condition) {
1144
- let result = null;
1154
+ async keys(condition, filterValues) {
1145
1155
  for (const k in condition) {
1146
1156
  const key = k;
1147
1157
  const value = condition[key];
@@ -1150,13 +1160,19 @@ var BPTreeAsync = class extends BPTree {
1150
1160
  const fullScan = this.verifierFullScan[key];
1151
1161
  const comparator = this.verifierMap[key];
1152
1162
  const pairs = await this.getPairs(value, startNode, fullScan, comparator, direction);
1153
- if (result === null) {
1154
- result = pairs.map((pair) => pair.key);
1163
+ if (!filterValues) {
1164
+ filterValues = new Set(pairs.map((pair) => pair.key));
1155
1165
  } else {
1156
- result = result.filter((key2) => pairs.find((p) => p.key === key2));
1166
+ const intersections = /* @__PURE__ */ new Set();
1167
+ for (const pair of pairs) {
1168
+ if (filterValues.has(pair.key)) {
1169
+ intersections.add(pair.key);
1170
+ }
1171
+ }
1172
+ filterValues = intersections;
1157
1173
  }
1158
1174
  }
1159
- return new Set(result ?? []);
1175
+ return filterValues ?? /* @__PURE__ */ new Set([]);
1160
1176
  }
1161
1177
  async where(condition) {
1162
1178
  let result = null;
@@ -1171,7 +1187,13 @@ var BPTreeAsync = class extends BPTree {
1171
1187
  if (result === null) {
1172
1188
  result = pairs;
1173
1189
  } else {
1174
- result = result.filter((pair) => pairs.find((p) => p.key === pair.key));
1190
+ const intersection = [];
1191
+ for (const pair of pairs) {
1192
+ if (result.find((p) => p.key === pair.key)) {
1193
+ intersection.push(pair);
1194
+ }
1195
+ }
1196
+ result = intersection;
1175
1197
  }
1176
1198
  }
1177
1199
  return result ?? [];
package/dist/esm/index.js CHANGED
@@ -147,7 +147,7 @@ var BPTreeSync = class extends BPTree {
147
147
  constructor(strategy, comparator) {
148
148
  super(strategy, comparator);
149
149
  }
150
- _getPairsRightToLeft(value, startNode, fullSearch, comparator) {
150
+ _getPairsRightToLeft(value, startNode, fullScan, comparator) {
151
151
  const pairs = [];
152
152
  let node = startNode;
153
153
  let done = false;
@@ -163,7 +163,7 @@ var BPTreeSync = class extends BPTree {
163
163
  while (j--) {
164
164
  pairs.push({ key: keys[j], value: nValue });
165
165
  }
166
- } else if (found && !fullSearch) {
166
+ } else if (found && !fullScan) {
167
167
  done = true;
168
168
  break;
169
169
  }
@@ -176,7 +176,7 @@ var BPTreeSync = class extends BPTree {
176
176
  }
177
177
  return pairs.reverse();
178
178
  }
179
- _getPairsLeftToRight(value, startNode, fullSearch, comparator) {
179
+ _getPairsLeftToRight(value, startNode, fullScan, comparator) {
180
180
  const pairs = [];
181
181
  let node = startNode;
182
182
  let done = false;
@@ -190,7 +190,7 @@ var BPTreeSync = class extends BPTree {
190
190
  for (const key of keys) {
191
191
  pairs.push({ key, value: nValue });
192
192
  }
193
- } else if (found && !fullSearch) {
193
+ } else if (found && !fullScan) {
194
194
  done = true;
195
195
  break;
196
196
  }
@@ -203,12 +203,12 @@ var BPTreeSync = class extends BPTree {
203
203
  }
204
204
  return pairs;
205
205
  }
206
- getPairs(value, startNode, fullSearch, comparator, direction) {
206
+ getPairs(value, startNode, fullScan, comparator, direction) {
207
207
  switch (direction) {
208
208
  case -1:
209
- return this._getPairsRightToLeft(value, startNode, fullSearch, comparator);
209
+ return this._getPairsRightToLeft(value, startNode, fullScan, comparator);
210
210
  case 1:
211
- return this._getPairsLeftToRight(value, startNode, fullSearch, comparator);
211
+ return this._getPairsLeftToRight(value, startNode, fullScan, comparator);
212
212
  default:
213
213
  throw new Error(`Direction must be -1 or 1. but got a ${direction}`);
214
214
  }
@@ -576,8 +576,7 @@ var BPTreeSync = class extends BPTree {
576
576
  }
577
577
  this._nodeUpdateBuffer.clear();
578
578
  }
579
- keys(condition) {
580
- let result = null;
579
+ keys(condition, filterValues) {
581
580
  for (const k in condition) {
582
581
  const key = k;
583
582
  const value = condition[key];
@@ -586,13 +585,19 @@ var BPTreeSync = class extends BPTree {
586
585
  const fullScan = this.verifierFullScan[key];
587
586
  const comparator = this.verifierMap[key];
588
587
  const pairs = this.getPairs(value, startNode, fullScan, comparator, direction);
589
- if (result === null) {
590
- result = pairs.map((pair) => pair.key);
588
+ if (!filterValues) {
589
+ filterValues = new Set(pairs.map((pair) => pair.key));
591
590
  } else {
592
- result = result.filter((key2) => pairs.find((p) => p.key === key2));
591
+ const intersections = /* @__PURE__ */ new Set();
592
+ for (const pair of pairs) {
593
+ if (filterValues.has(pair.key)) {
594
+ intersections.add(pair.key);
595
+ }
596
+ }
597
+ filterValues = intersections;
593
598
  }
594
599
  }
595
- return new Set(result ?? []);
600
+ return filterValues ?? /* @__PURE__ */ new Set([]);
596
601
  }
597
602
  where(condition) {
598
603
  let result = null;
@@ -607,7 +612,13 @@ var BPTreeSync = class extends BPTree {
607
612
  if (result === null) {
608
613
  result = pairs;
609
614
  } else {
610
- result = result.filter((pair) => pairs.find((p) => p.key === pair.key));
615
+ const intersection = [];
616
+ for (const pair of pairs) {
617
+ if (result.find((p) => p.key === pair.key)) {
618
+ intersection.push(pair);
619
+ }
620
+ }
621
+ result = intersection;
611
622
  }
612
623
  }
613
624
  return result ?? [];
@@ -1106,8 +1117,7 @@ var BPTreeAsync = class extends BPTree {
1106
1117
  }
1107
1118
  this._nodeUpdateBuffer.clear();
1108
1119
  }
1109
- async keys(condition) {
1110
- let result = null;
1120
+ async keys(condition, filterValues) {
1111
1121
  for (const k in condition) {
1112
1122
  const key = k;
1113
1123
  const value = condition[key];
@@ -1116,13 +1126,19 @@ var BPTreeAsync = class extends BPTree {
1116
1126
  const fullScan = this.verifierFullScan[key];
1117
1127
  const comparator = this.verifierMap[key];
1118
1128
  const pairs = await this.getPairs(value, startNode, fullScan, comparator, direction);
1119
- if (result === null) {
1120
- result = pairs.map((pair) => pair.key);
1129
+ if (!filterValues) {
1130
+ filterValues = new Set(pairs.map((pair) => pair.key));
1121
1131
  } else {
1122
- result = result.filter((key2) => pairs.find((p) => p.key === key2));
1132
+ const intersections = /* @__PURE__ */ new Set();
1133
+ for (const pair of pairs) {
1134
+ if (filterValues.has(pair.key)) {
1135
+ intersections.add(pair.key);
1136
+ }
1137
+ }
1138
+ filterValues = intersections;
1123
1139
  }
1124
1140
  }
1125
- return new Set(result ?? []);
1141
+ return filterValues ?? /* @__PURE__ */ new Set([]);
1126
1142
  }
1127
1143
  async where(condition) {
1128
1144
  let result = null;
@@ -1137,7 +1153,13 @@ var BPTreeAsync = class extends BPTree {
1137
1153
  if (result === null) {
1138
1154
  result = pairs;
1139
1155
  } else {
1140
- result = result.filter((pair) => pairs.find((p) => p.key === pair.key));
1156
+ const intersection = [];
1157
+ for (const pair of pairs) {
1158
+ if (result.find((p) => p.key === pair.key)) {
1159
+ intersection.push(pair);
1160
+ }
1161
+ }
1162
+ result = intersection;
1141
1163
  }
1142
1164
  }
1143
1165
  return result ?? [];
@@ -19,7 +19,7 @@ export declare class BPTreeAsync<K, V> extends BPTree<K, V> {
19
19
  protected commitHeadBuffer(): Promise<void>;
20
20
  protected commitNodeCreateBuffer(): Promise<void>;
21
21
  protected commitNodeUpdateBuffer(): Promise<void>;
22
- keys(condition: BPTreeCondition<V>): Promise<Set<K>>;
22
+ keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Promise<Set<K>>;
23
23
  where(condition: BPTreeCondition<V>): Promise<BPTreePair<K, V>[]>;
24
24
  insert(key: K, value: V): Promise<void>;
25
25
  delete(key: K, value: V): Promise<void>;
@@ -5,9 +5,9 @@ import { SerializableData } from './base/SerializeStrategy';
5
5
  export declare class BPTreeSync<K, V> extends BPTree<K, V> {
6
6
  protected readonly strategy: SerializeStrategySync<K, V>;
7
7
  constructor(strategy: SerializeStrategySync<K, V>, comparator: ValueComparator<V>);
8
- protected _getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, fullSearch: boolean, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>[];
9
- protected _getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, fullSearch: boolean, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>[];
10
- protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, fullSearch: boolean, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): BPTreePair<K, V>[];
8
+ protected _getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>[];
9
+ protected _getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean): BPTreePair<K, V>[];
10
+ protected getPairs(value: V, startNode: BPTreeLeafNode<K, V>, fullScan: boolean, comparator: (nodeValue: V, value: V) => boolean, direction: 1 | -1): BPTreePair<K, V>[];
11
11
  protected _createNodeId(): number;
12
12
  protected _createNode(keys: number[] | K[][], values: V[], leaf?: boolean, parent?: number, next?: number, prev?: number): BPTreeUnknownNode<K, V>;
13
13
  protected _deleteEntry(node: BPTreeUnknownNode<K, V>, key: BPTreeNodeKey<K>, value: V): void;
@@ -20,7 +20,7 @@ export declare class BPTreeSync<K, V> extends BPTree<K, V> {
20
20
  protected commitHeadBuffer(): void;
21
21
  protected commitNodeCreateBuffer(): void;
22
22
  protected commitNodeUpdateBuffer(): void;
23
- keys(condition: BPTreeCondition<V>): Set<K>;
23
+ keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Set<K>;
24
24
  where(condition: BPTreeCondition<V>): BPTreePair<K, V>[];
25
25
  insert(key: K, value: V): void;
26
26
  delete(key: K, value: V): void;
@@ -80,8 +80,11 @@ export declare abstract class BPTree<K, V> {
80
80
  * The result is key set instance, and you can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
81
81
  * This method operates much faster than first searching with `where` and then retrieving only the key list.
82
82
  * @param condition You can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
83
+ * @param filterValues The `Set` containing values to check for intersection.
84
+ * Returns a `Set` containing values that are common to both the input `Set` and the intersection `Set`.
85
+ * If this parameter is not provided, it searches for all keys inserted into the tree.
83
86
  */
84
- abstract keys(condition: BPTreeCondition<V>): Deferred<Set<K>>;
87
+ abstract keys(condition: BPTreeCondition<V>, filterValues?: Set<K>): Deferred<Set<K>>;
85
88
  /**
86
89
  * It searches for a value within the tree. The result is returned as an array sorted in ascending order based on the value.
87
90
  * The result includes the key and value attributes, and you can use the `gt`, `lt`, `gte`, `lte`, `equal`, `notEqual`, `like` condition statements.
@@ -10,7 +10,7 @@ export declare abstract class ValueComparator<V> {
10
10
  * The `match` method is used for the **LIKE** operator.
11
11
  * This method specifies which value to test against a regular expression.
12
12
  *
13
- * For example, if you have a tree with values of the structure `{ country: string, capital: number }`,
13
+ * For example, if you have a tree with values of the structure `{ country: string, capital: string }`,
14
14
  * and you want to perform a **LIKE** operation based on the **capital** value, the method should return **value.capital**.
15
15
  * In this case, you **CANNOT** perform a **LIKE** operation based on the **country** attribute.
16
16
  * The returned value must be a string.
@@ -1,5 +1,5 @@
1
- export type { BPTreeNode } from './base/BPTree';
2
- export type { SerializeStrategyHead } from './base/SerializeStrategy';
1
+ export type { BPTreeNode, BPTreeCondition } from './base/BPTree';
2
+ export type { SerializeStrategyHead, SerializableData } from './base/SerializeStrategy';
3
3
  export { ValueComparator, NumericComparator, StringComparator } from './base/ValueComparator';
4
4
  export { BPTreeSync } from './BPTreeSync';
5
5
  export { BPTreeAsync } from './BPTreeAsync';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "3.2.1",
3
+ "version": "3.2.3",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",