serializable-bptree 5.1.0 → 5.1.2

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.
@@ -1,6 +1,6 @@
1
+ import { CacheEntanglementSync, CacheEntanglementAsync, type StringValue } from 'cache-entanglement';
1
2
  import { ValueComparator } from './ValueComparator';
2
3
  import { SerializableData, SerializeStrategy } from './SerializeStrategy';
3
- import { InvertedWeakMap } from '../utils/InvertedWeakMap';
4
4
  type Sync<T> = T;
5
5
  type Async<T> = Promise<T>;
6
6
  type Deferred<T> = Sync<T> | Async<T>;
@@ -25,6 +25,17 @@ export type BPTreeCondition<V> = Partial<{
25
25
  }>;
26
26
  export type BPTreePair<K, V> = Map<K, V>;
27
27
  export type BPTreeUnknownNode<K, V> = BPTreeInternalNode<K, V> | BPTreeLeafNode<K, V>;
28
+ export interface BPTreeConstructorOption {
29
+ /**
30
+ * The lifespan of the cached node.
31
+ * This value is used to determine how long a cached node should be kept in memory.
32
+ * If the lifespan is set to a positive number, the cached node will expire after the specified number of milliseconds.
33
+ * If the lifespan is set to `0` or a negative number, the cache will not prevent garbage collection.
34
+ * If the lifespan is set to a string, the string will be parsed as a time duration.
35
+ * For example, '1m' means 1 minute, '1h' means 1 hour, '1d' means 1 day, '1w' means 1 week.
36
+ */
37
+ lifespan?: StringValue | number;
38
+ }
28
39
  export interface BPTreeNode<K, V> {
29
40
  id: string;
30
41
  keys: string[] | K[][];
@@ -44,9 +55,10 @@ export interface BPTreeLeafNode<K, V> extends BPTreeNode<K, V> {
44
55
  }
45
56
  export declare abstract class BPTree<K, V> {
46
57
  private readonly _cachedRegexp;
58
+ protected abstract readonly nodes: CacheEntanglementSync<any, any> | CacheEntanglementAsync<any, any>;
47
59
  protected readonly strategy: SerializeStrategy<K, V>;
48
60
  protected readonly comparator: ValueComparator<V>;
49
- protected readonly nodes: InvertedWeakMap<string, BPTreeUnknownNode<K, V>>;
61
+ protected readonly option: BPTreeConstructorOption;
50
62
  protected order: number;
51
63
  protected root: BPTreeUnknownNode<K, V>;
52
64
  protected _strategyDirty: boolean;
@@ -57,7 +69,8 @@ export declare abstract class BPTree<K, V> {
57
69
  protected readonly verifierStartNode: Record<keyof BPTreeCondition<V>, (value: V) => Deferred<BPTreeLeafNode<K, V>>>;
58
70
  protected readonly verifierEndNode: Record<keyof BPTreeCondition<V>, (value: V) => Deferred<BPTreeLeafNode<K, V> | null>>;
59
71
  protected readonly verifierDirection: Record<keyof BPTreeCondition<V>, -1 | 1>;
60
- protected constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>);
72
+ protected constructor(strategy: SerializeStrategy<K, V>, comparator: ValueComparator<V>, option?: BPTreeConstructorOption);
73
+ private _createCachedRegexp;
61
74
  protected abstract getPairsRightToLeft(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): Deferred<BPTreePair<K, V>>;
62
75
  protected abstract getPairsLeftToRight(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean): Deferred<BPTreePair<K, V>>;
63
76
  protected abstract getPairs(value: V, startNode: BPTreeLeafNode<K, V>, endNode: BPTreeLeafNode<K, V> | null, comparator: (nodeValue: V, value: V) => boolean, direction: -1 | 1): Deferred<BPTreePair<K, V>>;
@@ -140,5 +153,10 @@ export declare abstract class BPTree<K, V> {
140
153
  * @returns User-defined data stored in the head of the tree.
141
154
  */
142
155
  getHeadData(): SerializableData;
156
+ /**
157
+ * Clears all cached nodes.
158
+ * This method is useful for freeing up memory when the tree is no longer needed.
159
+ */
160
+ clear(): void;
143
161
  }
144
162
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "5.1.0",
3
+ "version": "5.1.2",
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",
@@ -37,9 +37,12 @@
37
37
  "license": "MIT",
38
38
  "devDependencies": {
39
39
  "@types/jest": "^29.5.14",
40
- "esbuild": "^0.25.4",
40
+ "esbuild": "^0.25.5",
41
41
  "jest": "^29.7.0",
42
- "ts-jest": "^29.3.3",
42
+ "ts-jest": "^29.3.4",
43
43
  "typescript": "^5.8.3"
44
+ },
45
+ "dependencies": {
46
+ "cache-entanglement": "^1.5.4"
44
47
  }
45
48
  }
@@ -1,12 +0,0 @@
1
- export declare class InvertedWeakMap<K extends string | number | symbol, V extends WeakKey> {
2
- private readonly _map;
3
- private readonly _registry;
4
- constructor();
5
- clear(): void;
6
- delete(key: K): boolean;
7
- get(key: K): V | undefined;
8
- has(key: K): boolean;
9
- set(key: K, value: V): this;
10
- get size(): number;
11
- keys(): IterableIterator<K>;
12
- }