mvcc-api 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 izure
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,158 @@
1
+ [![](https://data.jsdelivr.com/v1/package/npm/mvcc-api/badge)](https://www.jsdelivr.com/package/npm/mvcc-api)
2
+ ![Node.js workflow](https://github.com/izure1/mvcc-api/actions/workflows/node.js.yml/badge.svg)
3
+
4
+ # mvcc-api
5
+
6
+ Multiversion Concurrency Control (MVCC) API for TypeScript.
7
+
8
+ This library provides a robust framework for implementing Snapshot Isolation (SI) using MVCC. It supports both synchronous and asynchronous operations and is designed to be storage-agnostic via the Strategy pattern.
9
+
10
+ ## Features
11
+
12
+ - **MVCC (Multiversion Concurrency Control)**: Provides Snapshot Isolation, allowing readers to not block writers and vice versa.
13
+ - **Sync & Async Support**: Separate `SyncMVCCManager` and `AsyncMVCCManager` for different use cases.
14
+ - **Storage Agnostic**: Implement your own `Strategy` (e.g., File System, In-Memory, Key-Value Store) to handle actual data persistence.
15
+ - **Transaction Management**: Methods to `create`, `commit`, and `rollback` transactions easily.
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install mvcc-api
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ### 1. Implement a Strategy
26
+
27
+ First, you need to define how data is stored by extending `MVCCStrategy`. Here is a simple example using Node.js `fs/promises`.
28
+
29
+ ```typescript
30
+ import fs from 'node:fs/promises'
31
+ import { AsyncMVCCStrategy } from 'mvcc-api'
32
+
33
+ export class AsyncFileStrategy extends AsyncMVCCStrategy<string> {
34
+ async read(key: string): Promise<string> {
35
+ return fs.readFile(key, 'utf-8')
36
+ }
37
+ async write(key: string, value: string): Promise<void> {
38
+ await fs.writeFile(key, value, 'utf-8')
39
+ }
40
+ async delete(key: string): Promise<void> {
41
+ await fs.unlink(key)
42
+ }
43
+ async exists(key: string): Promise<boolean> {
44
+ try {
45
+ await fs.access(key)
46
+ return true
47
+ } catch {
48
+ return false
49
+ }
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### 2. Run Transactions
55
+
56
+ Initialize the Manager with your Strategy and start using transactions.
57
+
58
+ ```typescript
59
+ import { AsyncMVCCManager } from 'mvcc-api'
60
+ import { AsyncFileStrategy } from './AsyncFileStrategy' // Your strategy
61
+
62
+ async function main() {
63
+ const strategy = new AsyncFileStrategy()
64
+ const db = new AsyncMVCCManager(strategy)
65
+
66
+ // Start a transaction
67
+ const tx = db.createTransaction()
68
+
69
+ try {
70
+ // Write data (buffered in memory)
71
+ tx.write('user:1', JSON.stringify({ name: 'Alice', balance: 100 }))
72
+
73
+ // Read data (snapshot isolation)
74
+ const data = await tx.read('user:1')
75
+ console.log('Read within tx:', data)
76
+
77
+ // Commit changes to storage
78
+ await tx.commit()
79
+ console.log('Transaction committed!')
80
+ } catch (err) {
81
+ console.error('Transaction failed:', err)
82
+ tx.rollback()
83
+ }
84
+ }
85
+
86
+ main()
87
+ ```
88
+
89
+ ## Architecture
90
+
91
+ The follow diagram illustrates the flow of a transaction in `mvcc-api`.
92
+
93
+ ```mermaid
94
+ sequenceDiagram
95
+ participant App
96
+ participant Manager
97
+ participant Transaction
98
+ participant Strategy
99
+
100
+ Note over App, Manager: Initialization
101
+ App->>Manager: new Manager(Strategy)
102
+
103
+ Note over App, Transaction: Start Transaction
104
+ App->>Manager: createTransaction()
105
+ Manager-->>Transaction: new(snapshotVersion)
106
+ Manager-->>App: tx instance
107
+
108
+ Note over App, Transaction: Operations
109
+ App->>Transaction: read(key)
110
+ Transaction->>Manager: _diskRead(key, snapshotVersion)
111
+ Manager->>Manager: Check Version Index / Cache
112
+ alt Data in Cache/Index
113
+ Manager-->>Transaction: Return visible version
114
+ else Data in Strategy
115
+ Manager->>Strategy: read(key)
116
+ Strategy-->>Manager: data
117
+ Manager-->>Transaction: data
118
+ end
119
+
120
+ App->>Transaction: write(key, value)
121
+ Transaction-->>Transaction: Buffer write (In-Memory)
122
+
123
+ Note over App, Strategy: Commit Phase
124
+ App->>Transaction: commit()
125
+ Transaction->>Manager: _commit(tx)
126
+ Manager->>Manager: Check Conflicts (Optimistic Lock)
127
+ alt Conflict Detected
128
+ Manager-->>App: Throw Error
129
+ else No Config
130
+ Manager->>Strategy: write(key, value)
131
+ Strategy-->>Manager: success
132
+ Manager->>Manager: Update Version Index
133
+ Manager-->>App: Success
134
+ end
135
+ ```
136
+
137
+ ## API Reference
138
+
139
+ ### `MVCCStrategy<T>` (Abstract)
140
+ - `read(key: string): Deferred<T>`
141
+ - `write(key: string, value: T): Deferred<void>`
142
+ - `delete(key: string): Deferred<void>`
143
+ - `exists(key: string): Deferred<boolean>`
144
+
145
+ ### `MVCCManager<T, S>`
146
+ - `createTransaction(): Transaction`
147
+ - `version`: Current global version.
148
+
149
+ ### `MVCCTransaction<T>`
150
+ - `read(key: string): Deferred<T | null>`
151
+ - `write(key: string, value: T): this`
152
+ - `delete(key: string): this`
153
+ - `commit(): Deferred<this>`
154
+ - `rollback(): this`
155
+
156
+ ## License
157
+
158
+ MIT