mvcc-api 1.0.3 → 1.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 +156 -110
- package/dist/cjs/index.cjs +519 -275
- package/dist/esm/index.mjs +519 -266
- package/dist/types/core/async/Transaction.d.ts +15 -3
- package/dist/types/core/async/index.d.ts +0 -1
- package/dist/types/core/base/Transaction.d.ts +59 -19
- package/dist/types/core/base/index.d.ts +0 -1
- package/dist/types/core/sync/Transaction.d.ts +13 -3
- package/dist/types/core/sync/index.d.ts +0 -1
- package/dist/types/types/index.d.ts +11 -0
- package/package.json +2 -2
- package/dist/types/core/async/Manager.d.ts +0 -14
- package/dist/types/core/base/Manager.d.ts +0 -59
- package/dist/types/core/sync/Manager.d.ts +0 -11
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import type { AsyncMVCCStrategy } from './Strategy';
|
|
2
|
-
import type {
|
|
2
|
+
import type { TransactionResult } from '../../types';
|
|
3
3
|
import { MVCCTransaction } from '../base';
|
|
4
|
-
export declare class AsyncMVCCTransaction<S extends AsyncMVCCStrategy<K, T>, K, T
|
|
4
|
+
export declare class AsyncMVCCTransaction<S extends AsyncMVCCStrategy<K, T>, K, T> extends MVCCTransaction<S, K, T> {
|
|
5
|
+
private lock;
|
|
6
|
+
private writeLock;
|
|
7
|
+
create(key: K, value: T): Promise<this>;
|
|
8
|
+
write(key: K, value: T): Promise<this>;
|
|
9
|
+
delete(key: K): Promise<this>;
|
|
10
|
+
createNested(): this;
|
|
5
11
|
read(key: K): Promise<T | null>;
|
|
6
|
-
|
|
12
|
+
_readSnapshot(key: K, snapshotVersion: number, snapshotLocalVersion?: number): Promise<T | null>;
|
|
13
|
+
commit(): Promise<TransactionResult<K, T>>;
|
|
14
|
+
_merge(child: MVCCTransaction<S, K, T>): Promise<string | null>;
|
|
15
|
+
_diskWrite(key: K, value: T, version: number): Promise<void>;
|
|
16
|
+
_diskRead(key: K, snapshotVersion: number): Promise<T | null>;
|
|
17
|
+
_diskDelete(key: K, snapshotVersion: number): Promise<void>;
|
|
18
|
+
_cleanupDeletedCache(): void;
|
|
7
19
|
}
|
|
@@ -1,50 +1,70 @@
|
|
|
1
|
-
import type { Deferred } from '../../types';
|
|
2
|
-
import type { MVCCManager } from './Manager';
|
|
1
|
+
import type { Deferred, TransactionResult } from '../../types';
|
|
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 S The strategy type used by the
|
|
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.
|
|
9
8
|
* @template K The type of key used for data storage (e.g., string).
|
|
10
9
|
* @template T The type of data stored (e.g., string, Buffer, object).
|
|
11
|
-
* @template M The manager type that created this transaction.
|
|
12
10
|
*/
|
|
13
|
-
export declare abstract class MVCCTransaction<S extends MVCCStrategy<K, T>, K, T
|
|
14
|
-
|
|
15
|
-
protected committed: boolean;
|
|
11
|
+
export declare abstract class MVCCTransaction<S extends MVCCStrategy<K, T>, K, T> {
|
|
12
|
+
committed: boolean;
|
|
16
13
|
readonly snapshotVersion: number;
|
|
14
|
+
readonly snapshotLocalVersion: number;
|
|
17
15
|
readonly writeBuffer: Map<K, T>;
|
|
18
16
|
readonly deleteBuffer: Set<K>;
|
|
19
|
-
|
|
17
|
+
readonly createdKeys: Set<K>;
|
|
18
|
+
readonly deletedValues: Map<K, T>;
|
|
19
|
+
readonly parent?: MVCCTransaction<S, K, T>;
|
|
20
|
+
localVersion: number;
|
|
21
|
+
readonly keyVersions: Map<K, number>;
|
|
22
|
+
readonly root: MVCCTransaction<S, K, T>;
|
|
23
|
+
protected strategy?: S;
|
|
24
|
+
protected version: number;
|
|
25
|
+
protected versionIndex: Map<K, Array<{
|
|
26
|
+
version: number;
|
|
27
|
+
exists: boolean;
|
|
28
|
+
}>>;
|
|
29
|
+
protected deletedCache: Map<K, Array<{
|
|
30
|
+
value: T;
|
|
31
|
+
deletedAtVersion: number;
|
|
32
|
+
}>>;
|
|
33
|
+
protected activeTransactions: Set<MVCCTransaction<S, K, T>>;
|
|
34
|
+
constructor(strategy?: S, parent?: MVCCTransaction<S, K, T>, snapshotVersion?: number);
|
|
35
|
+
isRoot(): boolean;
|
|
20
36
|
/**
|
|
21
37
|
* Schedules a creation (insert) of a key-value pair.
|
|
22
|
-
* Throws if the
|
|
38
|
+
* Throws if the key already exists.
|
|
23
39
|
* @param key The key to create.
|
|
24
40
|
* @param value The value to store.
|
|
25
41
|
* @returns The transaction instance for chaining.
|
|
26
42
|
*/
|
|
27
|
-
create(key: K, value: T): this
|
|
43
|
+
abstract create(key: K, value: T): Deferred<this>;
|
|
28
44
|
/**
|
|
29
45
|
* Schedules a write (update) of a key-value pair.
|
|
30
|
-
*
|
|
46
|
+
* Throws if the key does not exist.
|
|
31
47
|
* @param key The key to write.
|
|
32
48
|
* @param value The value to store.
|
|
33
49
|
* @returns The transaction instance for chaining.
|
|
34
50
|
*/
|
|
35
|
-
write(key: K, value: T): this
|
|
51
|
+
abstract write(key: K, value: T): Deferred<this>;
|
|
36
52
|
/**
|
|
37
53
|
* Schedules a deletion of a key.
|
|
54
|
+
* Throws if the key does not exist.
|
|
38
55
|
* @param key The key to delete.
|
|
39
56
|
* @returns The transaction instance for chaining.
|
|
40
57
|
*/
|
|
41
|
-
delete(key: K): this
|
|
58
|
+
abstract delete(key: K): Deferred<this>;
|
|
59
|
+
protected _bufferCreate(key: K, value: T): void;
|
|
60
|
+
protected _bufferWrite(key: K, value: T): void;
|
|
61
|
+
protected _bufferDelete(key: K): void;
|
|
42
62
|
/**
|
|
43
63
|
* Rolls back the transaction.
|
|
44
64
|
* Clears all buffers and marks the transaction as finished.
|
|
45
|
-
* @returns The
|
|
65
|
+
* @returns The result object with success, created, updated, and deleted keys.
|
|
46
66
|
*/
|
|
47
|
-
rollback():
|
|
67
|
+
rollback(): TransactionResult<K, T>;
|
|
48
68
|
/**
|
|
49
69
|
* Reads a value respecting the transaction's snapshot and local changes.
|
|
50
70
|
* @param key The key to read.
|
|
@@ -53,8 +73,28 @@ export declare abstract class MVCCTransaction<S extends MVCCStrategy<K, T>, K, T
|
|
|
53
73
|
abstract read(key: K): Deferred<T | null>;
|
|
54
74
|
/**
|
|
55
75
|
* Commits the transaction.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
76
|
+
* If root, persists to storage.
|
|
77
|
+
* If nested, merges to parent.
|
|
78
|
+
* @returns The result object with success, created, and obsolete keys.
|
|
58
79
|
*/
|
|
59
|
-
abstract commit(): Deferred<
|
|
80
|
+
abstract commit(): Deferred<TransactionResult<K, T>>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a nested transaction (child) from this transaction.
|
|
83
|
+
* @returns A new nested transaction instance.
|
|
84
|
+
*/
|
|
85
|
+
abstract createNested(): MVCCTransaction<S, K, T>;
|
|
86
|
+
/**
|
|
87
|
+
* Merges a child transaction's changes into this transaction.
|
|
88
|
+
* @param child The committed child transaction.
|
|
89
|
+
* @returns Error message if conflict, null if success.
|
|
90
|
+
*/
|
|
91
|
+
abstract _merge(child: MVCCTransaction<S, K, T>): Deferred<string | null>;
|
|
92
|
+
/**
|
|
93
|
+
* Reads a value at a specific snapshot version.
|
|
94
|
+
* Used by child transactions to read from parent respecting the child's snapshot.
|
|
95
|
+
* @param key The key to read.
|
|
96
|
+
* @param snapshotVersion The global version to read at.
|
|
97
|
+
* @param snapshotLocalVersion The local version within the parent's buffer to read at.
|
|
98
|
+
*/
|
|
99
|
+
abstract _readSnapshot(key: K, snapshotVersion: number, snapshotLocalVersion?: number): Deferred<T | null>;
|
|
60
100
|
}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import type { SyncMVCCStrategy } from './Strategy';
|
|
2
|
-
import type {
|
|
2
|
+
import type { TransactionResult } from '../../types';
|
|
3
3
|
import { MVCCTransaction } from '../base';
|
|
4
|
-
export declare class SyncMVCCTransaction<S extends SyncMVCCStrategy<K, T>, K, T
|
|
4
|
+
export declare class SyncMVCCTransaction<S extends SyncMVCCStrategy<K, T>, K, T> extends MVCCTransaction<S, K, T> {
|
|
5
|
+
create(key: K, value: T): this;
|
|
6
|
+
write(key: K, value: T): this;
|
|
7
|
+
delete(key: K): this;
|
|
8
|
+
createNested(): this;
|
|
5
9
|
read(key: K): T | null;
|
|
6
|
-
|
|
10
|
+
_readSnapshot(key: K, snapshotVersion: number, snapshotLocalVersion?: number): T | null;
|
|
11
|
+
commit(): TransactionResult<K, T>;
|
|
12
|
+
_merge(child: MVCCTransaction<S, K, T>): string | null;
|
|
13
|
+
_diskWrite(key: K, value: T, version: number): void;
|
|
14
|
+
_diskRead(key: K, snapshotVersion: number): T | null;
|
|
15
|
+
_diskDelete(key: K, snapshotVersion: number): void;
|
|
16
|
+
_cleanupDeletedCache(): void;
|
|
7
17
|
}
|
|
@@ -3,3 +3,14 @@ export type DeleteEntry<T> = {
|
|
|
3
3
|
value: T;
|
|
4
4
|
deletedAtVersion: number;
|
|
5
5
|
};
|
|
6
|
+
export type TransactionEntry<K, T> = {
|
|
7
|
+
key: K;
|
|
8
|
+
data: T;
|
|
9
|
+
};
|
|
10
|
+
export type TransactionResult<K, T> = {
|
|
11
|
+
success: boolean;
|
|
12
|
+
error?: string;
|
|
13
|
+
created: TransactionEntry<K, T>[];
|
|
14
|
+
updated: TransactionEntry<K, T>[];
|
|
15
|
+
deleted: TransactionEntry<K, T>[];
|
|
16
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mvcc-api",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.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<S extends AsyncMVCCStrategy<K, T>, K, T> extends MVCCManager<S, K, T> {
|
|
6
|
-
readonly lock: Ryoiki;
|
|
7
|
-
constructor(strategy: S);
|
|
8
|
-
createTransaction(): AsyncMVCCTransaction<S, K, T, this>;
|
|
9
|
-
writeLock<R>(fn: () => Promise<R>): Promise<R>;
|
|
10
|
-
_diskWrite(key: K, value: T, version: number): Promise<void>;
|
|
11
|
-
_diskRead(key: K, snapshotVersion: number): Promise<T | null>;
|
|
12
|
-
_diskDelete(key: K, snapshotVersion: number): Promise<void>;
|
|
13
|
-
_commit(tx: AsyncMVCCTransaction<S, K, T, this>): Promise<void>;
|
|
14
|
-
}
|
|
@@ -1,59 +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 S The strategy type used for persistence.
|
|
9
|
-
* @template K The type of key used for data storage (e.g., string).
|
|
10
|
-
* @template T The type of data stored (e.g., string, Buffer, object).
|
|
11
|
-
*/
|
|
12
|
-
export declare abstract class MVCCManager<S extends MVCCStrategy<K, T>, K, T> {
|
|
13
|
-
version: number;
|
|
14
|
-
readonly strategy: S;
|
|
15
|
-
protected readonly versionIndex: Map<K, {
|
|
16
|
-
version: number;
|
|
17
|
-
exists: boolean;
|
|
18
|
-
}[]>;
|
|
19
|
-
protected readonly activeTransactions: Set<MVCCTransaction<S, K, T, this>>;
|
|
20
|
-
protected readonly deletedCache: Map<K, DeleteEntry<T>[]>;
|
|
21
|
-
constructor(strategy: S);
|
|
22
|
-
/**
|
|
23
|
-
* Creates a new transaction with the current version.
|
|
24
|
-
* @returns A new accessible transaction instance.
|
|
25
|
-
*/
|
|
26
|
-
abstract createTransaction(): MVCCTransaction<S, K, T, this>;
|
|
27
|
-
/**
|
|
28
|
-
* Writes data to the persistent storage and indexes the new version.
|
|
29
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
30
|
-
* @param key The key to write.
|
|
31
|
-
* @param value The value to write.
|
|
32
|
-
* @param version The version number for this write.
|
|
33
|
-
*/
|
|
34
|
-
abstract _diskWrite(key: K, value: T, version: number): Deferred<void>;
|
|
35
|
-
/**
|
|
36
|
-
* Reads data from the persistent storage for a specific snapshot version.
|
|
37
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
38
|
-
* @param key The key to read.
|
|
39
|
-
* @param shapshotVersion The transaction's snapshot version.
|
|
40
|
-
* @returns The data visible to the snapshot version, or null.
|
|
41
|
-
*/
|
|
42
|
-
abstract _diskRead(key: K, shapshotVersion: number): Deferred<T | null>;
|
|
43
|
-
/**
|
|
44
|
-
* Deletes data from the persistent storage (records a deletion version).
|
|
45
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
46
|
-
* @param key The key to delete.
|
|
47
|
-
* @param snapshotVersion The version at which the deletion occurs.
|
|
48
|
-
*/
|
|
49
|
-
abstract _diskDelete(key: K, snapshotVersion: number): Deferred<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Commits a transaction.
|
|
52
|
-
* Validates conflicts and applies the transaction's changes.
|
|
53
|
-
* @internal This method is for internal use only and should not be called directly.
|
|
54
|
-
* @param tx The transaction to commit.
|
|
55
|
-
*/
|
|
56
|
-
abstract _commit(tx: MVCCTransaction<S, K, T, this>): Deferred<void>;
|
|
57
|
-
_removeTransaction(tx: MVCCTransaction<S, K, T, this>): void;
|
|
58
|
-
protected _cleanupDeletedCache(): void;
|
|
59
|
-
}
|
|
@@ -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<S extends SyncMVCCStrategy<K, T>, K, T> extends MVCCManager<S, K, T> {
|
|
5
|
-
constructor(strategy: S);
|
|
6
|
-
createTransaction(): SyncMVCCTransaction<S, K, T, this>;
|
|
7
|
-
_diskWrite(key: K, value: T, version: number): void;
|
|
8
|
-
_diskRead(key: K, snapshotVersion: number): T | null;
|
|
9
|
-
_diskDelete(key: K, snapshotVersion: number): void;
|
|
10
|
-
_commit(tx: SyncMVCCTransaction<S, K, T, this>): void;
|
|
11
|
-
}
|