serializable-bptree 7.0.1 → 7.0.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.
@@ -7,10 +7,13 @@ export declare abstract class SerializeStrategyAsync<K, V> extends SerializeStra
7
7
  abstract delete(id: string): Promise<void>;
8
8
  abstract readHead(): Promise<SerializeStrategyHead | null>;
9
9
  abstract writeHead(head: SerializeStrategyHead): Promise<void>;
10
+ private lock;
11
+ acquireLock<T>(action: () => Promise<T>): Promise<T>;
10
12
  getHeadData(key: string, defaultValue: Json): Promise<Json>;
11
13
  setHeadData(key: string, data: Json): Promise<void>;
12
14
  autoIncrement(key: string, defaultValue: number): Promise<number>;
13
- compareAndSwapHead(oldRoot: string | null, newRoot: string): Promise<boolean>;
15
+ getLastCommittedTransactionId(): Promise<number>;
16
+ compareAndSwapHead(newRoot: string, newTxId: number): Promise<void>;
14
17
  }
15
18
  export declare class InMemoryStoreStrategyAsync<K, V> extends SerializeStrategyAsync<K, V> {
16
19
  protected readonly node: Record<string, BPTreeNode<K, V>>;
@@ -10,7 +10,8 @@ export declare abstract class SerializeStrategySync<K, V> extends SerializeStrat
10
10
  getHeadData(key: string, defaultValue: Json): Json;
11
11
  setHeadData(key: string, data: Json): void;
12
12
  autoIncrement(key: string, defaultValue: number): number;
13
- compareAndSwapHead(oldRoot: string | null, newRoot: string): boolean;
13
+ getLastCommittedTransactionId(): number;
14
+ compareAndSwapHead(newRoot: string, newTxId: number): void;
14
15
  }
15
16
  export declare class InMemoryStoreStrategySync<K, V> extends SerializeStrategySync<K, V> {
16
17
  protected readonly node: Record<string, BPTreeNode<K, V>>;
@@ -10,20 +10,16 @@ export declare abstract class BPTree<K, V> {
10
10
  protected readonly option: BPTreeConstructorOption;
11
11
  protected order: number;
12
12
  protected rootId: string;
13
- /**
14
- * Returns the ID of the root node.
15
- * @returns The root node ID.
16
- */
17
- getRootId(): string;
18
- /**
19
- * Returns the order of the B+Tree.
20
- * @returns The order of the tree.
21
- */
22
- getOrder(): number;
23
13
  protected _strategyDirty: boolean;
24
14
  protected readonly _nodeCreateBuffer: Map<string, BPTreeUnknownNode<K, V>>;
25
15
  protected readonly _nodeUpdateBuffer: Map<string, BPTreeUnknownNode<K, V>>;
26
16
  protected readonly _nodeDeleteBuffer: Map<string, BPTreeUnknownNode<K, V>>;
17
+ readonly sharedDeleteCache: Map<string, {
18
+ node: BPTreeUnknownNode<K, V>;
19
+ obsoleteAt: number;
20
+ }>;
21
+ protected readonly activeTransactions: Set<number>;
22
+ private lastTransactionId;
27
23
  protected readonly verifierMap: Record<keyof BPTreeCondition<V>, (nodeValue: V, value: V | V[]) => boolean>;
28
24
  protected readonly verifierStartNode: Record<keyof BPTreeCondition<V>, (value: V) => Deferred<BPTreeLeafNode<K, V>>>;
29
25
  protected readonly verifierEndNode: Record<keyof BPTreeCondition<V>, (value: V) => Deferred<BPTreeLeafNode<K, V> | null>>;
@@ -63,6 +59,16 @@ export declare abstract class BPTree<K, V> {
63
59
  tree: T;
64
60
  condition: BPTreeCondition<unknown>;
65
61
  } | null;
62
+ /**
63
+ * Returns the ID of the root node.
64
+ * @returns The root node ID.
65
+ */
66
+ getRootId(): string;
67
+ /**
68
+ * Returns the order of the B+Tree.
69
+ * @returns The order of the tree.
70
+ */
71
+ getOrder(): number;
66
72
  /**
67
73
  * Verified if the value satisfies the condition.
68
74
  *
@@ -164,4 +170,10 @@ export declare abstract class BPTree<K, V> {
164
170
  * This method is useful for freeing up memory when the tree is no longer needed.
165
171
  */
166
172
  clear(): void;
173
+ registerTransaction(txId: number): void;
174
+ unregisterTransaction(txId: number): void;
175
+ pruneObsoleteNodes(): void;
176
+ getObsoleteNode(id: string): BPTreeUnknownNode<K, V> | undefined;
177
+ addObsoleteNode(node: BPTreeUnknownNode<K, V>, obsoleteAt: number): void;
178
+ getNextTransactionId(): number;
167
179
  }
@@ -2,6 +2,7 @@ import type { BPTreeNode, Json, SerializeStrategyHead } from '../types';
2
2
  export declare abstract class SerializeStrategy<K, V> {
3
3
  readonly order: number;
4
4
  head: SerializeStrategyHead;
5
+ protected lastCommittedTransactionId: number;
5
6
  constructor(order: number);
6
7
  /**
7
8
  * The rule for generating node IDs is set.
@@ -44,13 +45,16 @@ export declare abstract class SerializeStrategy<K, V> {
44
45
  */
45
46
  abstract writeHead(head: SerializeStrategyHead): void | Promise<void>;
46
47
  /**
47
- * Atomically updates the head if the current root matches the expected value.
48
+ * Atomically updates the head with the new root and transaction ID.
48
49
  * Required for Optimistic Concurrency Control (CoW).
49
- * @param oldRoot The expected current root ID.
50
50
  * @param newRoot The new root ID to set.
51
- * @returns True if successful, False if the root has changed.
51
+ * @param newTxId The new transaction ID.
52
52
  */
53
- abstract compareAndSwapHead(oldRoot: string | null, newRoot: string): boolean | Promise<boolean>;
53
+ abstract compareAndSwapHead(newRoot: string, newTxId: number): void | Promise<void>;
54
+ /**
55
+ * Returns the last committed transaction ID (in-memory only).
56
+ */
57
+ abstract getLastCommittedTransactionId(): number | Promise<number>;
54
58
  /**
55
59
  * Retrieves the data stored in the tree.
56
60
  * If no value is stored in the tree, it stores a `defaultValue` and then returns that value.
@@ -10,7 +10,8 @@ export declare class BPTreeAsyncSnapshotStrategy<K, V> extends SerializeStrategy
10
10
  delete(id: string): Promise<void>;
11
11
  readHead(): Promise<SerializeStrategyHead | null>;
12
12
  writeHead(head: SerializeStrategyHead): Promise<void>;
13
- compareAndSwapHead(oldRoot: string | null, newRoot: string): Promise<boolean>;
13
+ compareAndSwapHead(newRoot: string, newTxId: number): Promise<void>;
14
+ getLastCommittedTransactionId(): Promise<number>;
14
15
  getHeadData(key: string, defaultValue: Json): Promise<Json>;
15
16
  setHeadData(key: string, data: Json): Promise<void>;
16
17
  autoIncrement(key: string, defaultValue: number): Promise<number>;
@@ -11,8 +11,12 @@ export declare class BPTreeAsyncTransaction<K, V> extends BPTreeAsyncBase<K, V>
11
11
  protected readonly dirtyIds: Set<string>;
12
12
  protected readonly createdInTx: Set<string>;
13
13
  protected readonly deletedIds: Set<string>;
14
+ readonly obsoleteNodes: Map<string, BPTreeUnknownNode<K, V>>;
15
+ private readonly originalNodes;
14
16
  private initialRootId;
15
17
  private transactionRootId;
18
+ private transactionId;
19
+ private initialLastCommittedTransactionId;
16
20
  constructor(baseTree: BPTreeAsyncBase<K, V>);
17
21
  /**
18
22
  * Initializes the transaction by capturing the current state of the tree.
@@ -28,14 +32,17 @@ export declare class BPTreeAsyncTransaction<K, V> extends BPTreeAsyncBase<K, V>
28
32
  * Attempts to commit the transaction.
29
33
  * Uses Optimistic Locking (Compare-And-Swap) on the root node ID to detect conflicts.
30
34
  *
35
+ * @param cleanup Whether to clean up obsolete nodes after commit. Defaults to true.
31
36
  * @returns A promise that resolves to the transaction result.
32
37
  */
33
- commit(): Promise<BPTreeTransactionResult>;
38
+ commit(cleanup?: boolean): Promise<BPTreeTransactionResult>;
34
39
  /**
35
40
  * Rolls back the transaction by clearing all buffered changes.
36
- * Internal use only.
41
+ * If cleanup is `true`, it also clears the transaction nodes.
42
+ * @param cleanup Whether to clear the transaction nodes.
43
+ * @returns The IDs of nodes that were created in this transaction.
37
44
  */
38
- protected rollback(): Promise<void>;
45
+ rollback(cleanup?: boolean): Promise<string[]>;
39
46
  protected readLock<T>(fn: () => Promise<T>): Promise<T>;
40
47
  protected writeLock<T>(fn: () => Promise<T>): Promise<T>;
41
48
  protected commitHeadBuffer(): Promise<void>;
@@ -10,7 +10,8 @@ export declare class BPTreeSyncSnapshotStrategy<K, V> extends SerializeStrategyS
10
10
  delete(id: string): void;
11
11
  readHead(): SerializeStrategyHead | null;
12
12
  writeHead(head: SerializeStrategyHead): void;
13
- compareAndSwapHead(oldRoot: string | null, newRoot: string): boolean;
13
+ compareAndSwapHead(newRoot: string, newTxId: number): void;
14
+ getLastCommittedTransactionId(): number;
14
15
  getHeadData(key: string, defaultValue: Json): Json;
15
16
  setHeadData(key: string, data: Json): void;
16
17
  autoIncrement(key: string, defaultValue: number): number;
@@ -11,8 +11,12 @@ export declare class BPTreeSyncTransaction<K, V> extends BPTreeSyncBase<K, V> {
11
11
  protected readonly dirtyIds: Set<string>;
12
12
  protected readonly createdInTx: Set<string>;
13
13
  protected readonly deletedIds: Set<string>;
14
+ readonly obsoleteNodes: Map<string, BPTreeUnknownNode<K, V>>;
15
+ private readonly originalNodes;
14
16
  private initialRootId;
15
17
  private transactionRootId;
18
+ private transactionId;
19
+ private initialLastCommittedTransactionId;
16
20
  constructor(baseTree: BPTreeSyncBase<K, V>);
17
21
  /**
18
22
  * Initializes the transaction by capturing the current state of the tree.
@@ -28,14 +32,19 @@ export declare class BPTreeSyncTransaction<K, V> extends BPTreeSyncBase<K, V> {
28
32
  * Attempts to commit the transaction.
29
33
  * Uses Optimistic Locking (Compare-And-Swap) on the root node ID to detect conflicts.
30
34
  *
35
+ * @param cleanup Whether to clean up obsolete nodes after commit. Defaults to true.
31
36
  * @returns The transaction result.
32
37
  */
33
- commit(): BPTreeTransactionResult;
38
+ commit(cleanup?: boolean): BPTreeTransactionResult;
34
39
  /**
35
40
  * Rolls back the transaction by clearing all buffered changes.
36
- * Internal use only.
41
+ * If cleanup is `true`, it also clears the transaction nodes.
42
+ * @param cleanup Whether to clear the transaction nodes.
43
+ * @returns The IDs of nodes that were created in this transaction.
37
44
  */
38
- protected rollback(): void;
45
+ rollback(cleanup?: boolean): string[];
46
+ protected readLock<T>(fn: () => T): T;
47
+ protected writeLock<T>(fn: () => T): T;
39
48
  protected commitHeadBuffer(): void;
40
49
  protected commitNodeCreateBuffer(): void;
41
50
  protected commitNodeUpdateBuffer(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
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",