mvcc-api 1.0.2 → 1.1.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 +84 -89
- package/dist/cjs/index.cjs +343 -242
- package/dist/esm/index.mjs +343 -233
- package/dist/types/core/async/Strategy.d.ts +5 -5
- package/dist/types/core/async/Transaction.d.ts +11 -3
- package/dist/types/core/async/index.d.ts +0 -1
- package/dist/types/core/base/Strategy.d.ts +6 -5
- package/dist/types/core/base/Transaction.d.ts +50 -16
- package/dist/types/core/base/index.d.ts +0 -1
- package/dist/types/core/sync/Strategy.d.ts +5 -5
- package/dist/types/core/sync/Transaction.d.ts +9 -3
- package/dist/types/core/sync/index.d.ts +0 -1
- package/package.json +2 -2
- package/dist/types/core/async/Manager.d.ts +0 -14
- package/dist/types/core/base/Manager.d.ts +0 -58
- package/dist/types/core/sync/Manager.d.ts +0 -11
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MVCCStrategy } from '../base';
|
|
2
|
-
export declare abstract class AsyncMVCCStrategy<T> extends MVCCStrategy<T> {
|
|
3
|
-
abstract read(key:
|
|
4
|
-
abstract write(key:
|
|
5
|
-
abstract delete(key:
|
|
6
|
-
abstract exists(key:
|
|
2
|
+
export declare abstract class AsyncMVCCStrategy<K, T> extends MVCCStrategy<K, T> {
|
|
3
|
+
abstract read(key: K): Promise<T>;
|
|
4
|
+
abstract write(key: K, value: T): Promise<void>;
|
|
5
|
+
abstract delete(key: K): Promise<void>;
|
|
6
|
+
abstract exists(key: K): Promise<boolean>;
|
|
7
7
|
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import type { AsyncMVCCStrategy } from './Strategy';
|
|
2
|
-
import type { AsyncMVCCManager } from './Manager';
|
|
3
2
|
import { MVCCTransaction } from '../base';
|
|
4
|
-
export declare class AsyncMVCCTransaction<
|
|
5
|
-
|
|
3
|
+
export declare class AsyncMVCCTransaction<S extends AsyncMVCCStrategy<K, T>, K, T> extends MVCCTransaction<S, K, T> {
|
|
4
|
+
private lock;
|
|
5
|
+
private writeLock;
|
|
6
|
+
createNested(): this;
|
|
7
|
+
read(key: K): Promise<T | null>;
|
|
8
|
+
_readSnapshot(key: K, snapshotVersion: number, snapshotLocalVersion?: number): Promise<T | null>;
|
|
6
9
|
commit(): Promise<this>;
|
|
10
|
+
_merge(child: MVCCTransaction<S, K, T>): Promise<void>;
|
|
11
|
+
_diskWrite(key: K, value: T, version: number): Promise<void>;
|
|
12
|
+
_diskRead(key: K, snapshotVersion: number): Promise<T | null>;
|
|
13
|
+
_diskDelete(key: K, snapshotVersion: number): Promise<void>;
|
|
14
|
+
_cleanupDeletedCache(): void;
|
|
7
15
|
}
|
|
@@ -3,30 +3,31 @@ import type { Deferred } from '../../types';
|
|
|
3
3
|
* MVCC Strategy abstract class.
|
|
4
4
|
* Defines the interface for data storage strategies (e.g., File System, In-Memory).
|
|
5
5
|
* To implement a new storage backend, extend this class and implement the abstract methods.
|
|
6
|
+
* @template K The type of key used for data storage (e.g., string).
|
|
6
7
|
* @template T The type of data stored (e.g., string, Buffer, object).
|
|
7
8
|
*/
|
|
8
|
-
export declare abstract class MVCCStrategy<T> {
|
|
9
|
+
export declare abstract class MVCCStrategy<K, T> {
|
|
9
10
|
/**
|
|
10
11
|
* Reads a value from the storage.
|
|
11
12
|
* @param key The key to read.
|
|
12
13
|
* @returns The value corresponding to the key.
|
|
13
14
|
*/
|
|
14
|
-
abstract read(key:
|
|
15
|
+
abstract read(key: K): Deferred<T>;
|
|
15
16
|
/**
|
|
16
17
|
* Writes a value to the storage.
|
|
17
18
|
* @param key The key to write.
|
|
18
19
|
* @param value The value to write.
|
|
19
20
|
*/
|
|
20
|
-
abstract write(key:
|
|
21
|
+
abstract write(key: K, value: T): Deferred<void>;
|
|
21
22
|
/**
|
|
22
23
|
* Deletes a value from the storage.
|
|
23
24
|
* @param key The key to delete.
|
|
24
25
|
*/
|
|
25
|
-
abstract delete(key:
|
|
26
|
+
abstract delete(key: K): Deferred<void>;
|
|
26
27
|
/**
|
|
27
28
|
* Checks if a key exists in the storage.
|
|
28
29
|
* @param key The key to check.
|
|
29
30
|
* @returns True if the key exists, false otherwise.
|
|
30
31
|
*/
|
|
31
|
-
abstract exists(key:
|
|
32
|
+
abstract exists(key: K): Deferred<boolean>;
|
|
32
33
|
}
|
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
import type { Deferred } from '../../types';
|
|
2
|
-
import type { MVCCManager } from './Manager';
|
|
3
2
|
import type { MVCCStrategy } from './Strategy';
|
|
4
3
|
/**
|
|
5
4
|
* MVCC Transaction abstract class.
|
|
6
5
|
* Represents a logical unit of work that isolates changes until commit.
|
|
7
|
-
* It
|
|
8
|
-
* @template
|
|
9
|
-
* @template
|
|
10
|
-
* @template
|
|
6
|
+
* It can be a RootTransaction (interacting with storage) or a NestedTransaction (interacting with parent).
|
|
7
|
+
* @template S The strategy type used by the root transaction.
|
|
8
|
+
* @template K The type of key used for data storage (e.g., string).
|
|
9
|
+
* @template T The type of data stored (e.g., string, Buffer, object).
|
|
11
10
|
*/
|
|
12
|
-
export declare abstract class MVCCTransaction<
|
|
13
|
-
|
|
14
|
-
protected committed: boolean;
|
|
11
|
+
export declare abstract class MVCCTransaction<S extends MVCCStrategy<K, T>, K, T> {
|
|
12
|
+
committed: boolean;
|
|
15
13
|
readonly snapshotVersion: number;
|
|
16
|
-
readonly
|
|
17
|
-
readonly
|
|
18
|
-
|
|
14
|
+
readonly snapshotLocalVersion: number;
|
|
15
|
+
readonly writeBuffer: Map<K, T>;
|
|
16
|
+
readonly deleteBuffer: Set<K>;
|
|
17
|
+
readonly parent?: MVCCTransaction<S, K, T>;
|
|
18
|
+
localVersion: number;
|
|
19
|
+
readonly keyVersions: Map<K, number>;
|
|
20
|
+
readonly root: MVCCTransaction<S, K, T>;
|
|
21
|
+
protected strategy?: S;
|
|
22
|
+
protected version: number;
|
|
23
|
+
protected versionIndex: Map<K, Array<{
|
|
24
|
+
version: number;
|
|
25
|
+
exists: boolean;
|
|
26
|
+
}>>;
|
|
27
|
+
protected deletedCache: Map<K, Array<{
|
|
28
|
+
value: T;
|
|
29
|
+
deletedAtVersion: number;
|
|
30
|
+
}>>;
|
|
31
|
+
protected activeTransactions: Set<MVCCTransaction<S, K, T>>;
|
|
32
|
+
constructor(strategy?: S, parent?: MVCCTransaction<S, K, T>, snapshotVersion?: number);
|
|
33
|
+
isRoot(): boolean;
|
|
19
34
|
/**
|
|
20
35
|
* Schedules a creation (insert) of a key-value pair.
|
|
21
36
|
* Throws if the transaction is already committed.
|
|
@@ -23,7 +38,7 @@ export declare abstract class MVCCTransaction<T, S extends MVCCStrategy<T>, M ex
|
|
|
23
38
|
* @param value The value to store.
|
|
24
39
|
* @returns The transaction instance for chaining.
|
|
25
40
|
*/
|
|
26
|
-
create(key:
|
|
41
|
+
create(key: K, value: T): this;
|
|
27
42
|
/**
|
|
28
43
|
* Schedules a write (update) of a key-value pair.
|
|
29
44
|
* Overwrites any existing value in the buffer.
|
|
@@ -31,13 +46,13 @@ export declare abstract class MVCCTransaction<T, S extends MVCCStrategy<T>, M ex
|
|
|
31
46
|
* @param value The value to store.
|
|
32
47
|
* @returns The transaction instance for chaining.
|
|
33
48
|
*/
|
|
34
|
-
write(key:
|
|
49
|
+
write(key: K, value: T): this;
|
|
35
50
|
/**
|
|
36
51
|
* Schedules a deletion of a key.
|
|
37
52
|
* @param key The key to delete.
|
|
38
53
|
* @returns The transaction instance for chaining.
|
|
39
54
|
*/
|
|
40
|
-
delete(key:
|
|
55
|
+
delete(key: K): this;
|
|
41
56
|
/**
|
|
42
57
|
* Rolls back the transaction.
|
|
43
58
|
* Clears all buffers and marks the transaction as finished.
|
|
@@ -49,11 +64,30 @@ export declare abstract class MVCCTransaction<T, S extends MVCCStrategy<T>, M ex
|
|
|
49
64
|
* @param key The key to read.
|
|
50
65
|
* @returns The value, or null if not found.
|
|
51
66
|
*/
|
|
52
|
-
abstract read(key:
|
|
67
|
+
abstract read(key: K): Deferred<T | null>;
|
|
53
68
|
/**
|
|
54
69
|
* Commits the transaction.
|
|
55
|
-
*
|
|
70
|
+
* If root, persists to storage.
|
|
71
|
+
* If nested, merges to parent.
|
|
56
72
|
* @returns The transaction instance.
|
|
57
73
|
*/
|
|
58
74
|
abstract commit(): Deferred<this>;
|
|
75
|
+
/**
|
|
76
|
+
* Creates a nested transaction (child) from this transaction.
|
|
77
|
+
* @returns A new nested transaction instance.
|
|
78
|
+
*/
|
|
79
|
+
abstract createNested(): MVCCTransaction<S, K, T>;
|
|
80
|
+
/**
|
|
81
|
+
* Merges a child transaction's changes into this transaction.
|
|
82
|
+
* @param child The committed child transaction.
|
|
83
|
+
*/
|
|
84
|
+
abstract _merge(child: MVCCTransaction<S, K, T>): Deferred<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Reads a value at a specific snapshot version.
|
|
87
|
+
* Used by child transactions to read from parent respecting the child's snapshot.
|
|
88
|
+
* @param key The key to read.
|
|
89
|
+
* @param snapshotVersion The global version to read at.
|
|
90
|
+
* @param snapshotLocalVersion The local version within the parent's buffer to read at.
|
|
91
|
+
*/
|
|
92
|
+
abstract _readSnapshot(key: K, snapshotVersion: number, snapshotLocalVersion?: number): Deferred<T | null>;
|
|
59
93
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MVCCStrategy } from '../base';
|
|
2
|
-
export declare abstract class SyncMVCCStrategy<T> extends MVCCStrategy<T> {
|
|
3
|
-
abstract read(key:
|
|
4
|
-
abstract write(key:
|
|
5
|
-
abstract delete(key:
|
|
6
|
-
abstract exists(key:
|
|
2
|
+
export declare abstract class SyncMVCCStrategy<K, T> extends MVCCStrategy<K, T> {
|
|
3
|
+
abstract read(key: K): T;
|
|
4
|
+
abstract write(key: K, value: T): void;
|
|
5
|
+
abstract delete(key: K): void;
|
|
6
|
+
abstract exists(key: K): boolean;
|
|
7
7
|
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import type { SyncMVCCStrategy } from './Strategy';
|
|
2
|
-
import type { SyncMVCCManager } from './Manager';
|
|
3
2
|
import { MVCCTransaction } from '../base';
|
|
4
|
-
export declare class SyncMVCCTransaction<
|
|
5
|
-
|
|
3
|
+
export declare class SyncMVCCTransaction<S extends SyncMVCCStrategy<K, T>, K, T> extends MVCCTransaction<S, K, T> {
|
|
4
|
+
createNested(): this;
|
|
5
|
+
read(key: K): T | null;
|
|
6
|
+
_readSnapshot(key: K, snapshotVersion: number, snapshotLocalVersion?: number): T | null;
|
|
6
7
|
commit(): this;
|
|
8
|
+
_merge(child: MVCCTransaction<S, K, T>): void;
|
|
9
|
+
_diskWrite(key: K, value: T, version: number): void;
|
|
10
|
+
_diskRead(key: K, snapshotVersion: number): T | null;
|
|
11
|
+
_diskDelete(key: K, snapshotVersion: number): void;
|
|
12
|
+
_cleanupDeletedCache(): void;
|
|
7
13
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mvcc-api",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Multiversion Concurrency Control (MVCC) API for TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "izure <admin@izure.org>",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"test": "jest",
|
|
22
|
-
"build": "node build/index.
|
|
22
|
+
"build": "node build/index.js && tsc"
|
|
23
23
|
},
|
|
24
24
|
"keywords": [
|
|
25
25
|
"mvcc",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { AsyncMVCCStrategy } from './Strategy';
|
|
2
|
-
import { Ryoiki } from 'ryoiki';
|
|
3
|
-
import { MVCCManager } from '../base';
|
|
4
|
-
import { AsyncMVCCTransaction } from './Transaction';
|
|
5
|
-
export declare class AsyncMVCCManager<T, S extends AsyncMVCCStrategy<T>> extends MVCCManager<T, S> {
|
|
6
|
-
readonly lock: Ryoiki;
|
|
7
|
-
constructor(strategy: S);
|
|
8
|
-
createTransaction(): AsyncMVCCTransaction<T, S, this>;
|
|
9
|
-
writeLock<R>(fn: () => Promise<R>): Promise<R>;
|
|
10
|
-
_diskWrite(key: string, value: T, version: number): Promise<void>;
|
|
11
|
-
_diskRead(key: string, snapshotVersion: number): Promise<T | null>;
|
|
12
|
-
_diskDelete(key: string, snapshotVersion: number): Promise<void>;
|
|
13
|
-
_commit(tx: AsyncMVCCTransaction<T, S, this>): Promise<void>;
|
|
14
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { Deferred, DeleteEntry } from '../../types';
|
|
2
|
-
import type { MVCCStrategy } from './Strategy';
|
|
3
|
-
import type { MVCCTransaction } from './Transaction';
|
|
4
|
-
/**
|
|
5
|
-
* MVCC Manager abstract class.
|
|
6
|
-
* Orchestrates transactions, manages the version index, and handles garbage collection.
|
|
7
|
-
* It is responsible for maintaining the consistency of the data and version history.
|
|
8
|
-
* @template T The type of data stored.
|
|
9
|
-
* @template S The strategy type used for persistence.
|
|
10
|
-
*/
|
|
11
|
-
export declare abstract class MVCCManager<T, S extends MVCCStrategy<T>> {
|
|
12
|
-
version: number;
|
|
13
|
-
readonly strategy: S;
|
|
14
|
-
protected readonly versionIndex: Map<string, {
|
|
15
|
-
version: number;
|
|
16
|
-
exists: boolean;
|
|
17
|
-
}[]>;
|
|
18
|
-
protected readonly activeTransactions: Set<MVCCTransaction<T, S, this>>;
|
|
19
|
-
protected readonly deletedCache: Map<string, DeleteEntry<T>[]>;
|
|
20
|
-
constructor(strategy: S);
|
|
21
|
-
/**
|
|
22
|
-
* Creates a new transaction with the current version.
|
|
23
|
-
* @returns A new accessible transaction instance.
|
|
24
|
-
*/
|
|
25
|
-
abstract createTransaction(): MVCCTransaction<T, S, this>;
|
|
26
|
-
/**
|
|
27
|
-
* Writes data to the persistent storage and indexes the new version.
|
|
28
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
29
|
-
* @param key The key to write.
|
|
30
|
-
* @param value The value to write.
|
|
31
|
-
* @param version The version number for this write.
|
|
32
|
-
*/
|
|
33
|
-
abstract _diskWrite(key: string, value: T, version: number): Deferred<void>;
|
|
34
|
-
/**
|
|
35
|
-
* Reads data from the persistent storage for a specific snapshot version.
|
|
36
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
37
|
-
* @param key The key to read.
|
|
38
|
-
* @param shapshotVersion The transaction's snapshot version.
|
|
39
|
-
* @returns The data visible to the snapshot version, or null.
|
|
40
|
-
*/
|
|
41
|
-
abstract _diskRead(key: string, shapshotVersion: number): Deferred<T | null>;
|
|
42
|
-
/**
|
|
43
|
-
* Deletes data from the persistent storage (records a deletion version).
|
|
44
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
45
|
-
* @param key The key to delete.
|
|
46
|
-
* @param snapshotVersion The version at which the deletion occurs.
|
|
47
|
-
*/
|
|
48
|
-
abstract _diskDelete(key: string, snapshotVersion: number): Deferred<void>;
|
|
49
|
-
/**
|
|
50
|
-
* Commits a transaction.
|
|
51
|
-
* Validates conflicts and applies the transaction's changes.
|
|
52
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
53
|
-
* @param tx The transaction to commit.
|
|
54
|
-
*/
|
|
55
|
-
abstract _commit(tx: MVCCTransaction<T, S, this>): Deferred<void>;
|
|
56
|
-
_removeTransaction(tx: MVCCTransaction<T, S, this>): void;
|
|
57
|
-
protected _cleanupDeletedCache(): void;
|
|
58
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { SyncMVCCStrategy } from './Strategy';
|
|
2
|
-
import { MVCCManager } from '../base';
|
|
3
|
-
import { SyncMVCCTransaction } from './Transaction';
|
|
4
|
-
export declare class SyncMVCCManager<T, S extends SyncMVCCStrategy<T>> extends MVCCManager<T, S> {
|
|
5
|
-
constructor(strategy: S);
|
|
6
|
-
createTransaction(): SyncMVCCTransaction<T, S, this>;
|
|
7
|
-
_diskWrite(key: string, value: T, version: number): void;
|
|
8
|
-
_diskRead(key: string, snapshotVersion: number): T | null;
|
|
9
|
-
_diskDelete(key: string, snapshotVersion: number): void;
|
|
10
|
-
_commit(tx: SyncMVCCTransaction<T, S, this>): void;
|
|
11
|
-
}
|