mvcc-api 1.2.8 → 1.2.10

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 CHANGED
@@ -102,6 +102,22 @@ const result = await tx.commit()
102
102
  await root.commit() // Persist to storage
103
103
  ```
104
104
 
105
+ > [!CAUTION]
106
+ > **Immutability and Reference Types**
107
+ >
108
+ > When using `write(key, value)` with reference types (objects, arrays), you MUST provide a **copy** of the value (Copy-on-Write).
109
+ > Since `mvcc-api` stores the value in an internal buffer, modifying the original object/array after calling `write()` but before `commit()` will affect the data in the transaction.
110
+ >
111
+ > ```typescript
112
+ > // ❌ Wrong: Modifying original object
113
+ > const data = { count: 1 }
114
+ > tx.write('key', data)
115
+ > data.count = 2 // Internal buffer also changes!
116
+ >
117
+ > // ✅ Correct: Pass a copy
118
+ > tx.write('key', { ...data })
119
+ > ```
120
+
105
121
  ## Visibility Rules
106
122
 
107
123
  ```mermaid
@@ -125,6 +141,24 @@ sequenceDiagram
125
141
  > - Children can only see **committed data** at the time of creation.
126
142
  > - Snapshots are maintained even if external commits occur after creation.
127
143
 
144
+ > [!WARNING]
145
+ > **Memory Management**
146
+ >
147
+ > Every transaction created via `createNested()` MUST be finished with either `commit()` or `rollback()`.
148
+ > Internally, the Root transaction tracks all active transactions to determine which old versions can be safely pruned from memory.
149
+ > Failing to close a transaction will prevent the internal Garbage Collection (Pruning) from reclaiming memory, eventually leading to a **Memory Leak**.
150
+ >
151
+ > ```typescript
152
+ > const tx = root.createNested()
153
+ > try {
154
+ > // ... work ...
155
+ > await tx.commit()
156
+ > } finally {
157
+ > // Ensure rollback is called if commit failed or an error occurred
158
+ > if (!tx.committed) tx.rollback()
159
+ > }
160
+ > ```
161
+
128
162
  ## Conflict Detection
129
163
 
130
164
  Conflicts occur upon commit if transactions have modified the same key.
@@ -121,7 +121,11 @@ var MVCCTransaction = class {
121
121
  this.createdKeys.delete(key);
122
122
  this.keyVersions.set(key, this.localVersion);
123
123
  }
124
- _getResultEntries() {
124
+ /**
125
+ * Returns the entries that will be created, updated, and deleted by this transaction.
126
+ * @returns An object containing arrays of created, updated, and deleted entries.
127
+ */
128
+ getResultEntries() {
125
129
  const created = [];
126
130
  const updated = [];
127
131
  for (const [key, data] of this.writeBuffer.entries()) {
@@ -147,7 +151,7 @@ var MVCCTransaction = class {
147
151
  * @returns The result object with success, created, updated, and deleted keys.
148
152
  */
149
153
  rollback() {
150
- const { created, updated, deleted } = this._getResultEntries();
154
+ const { created, updated, deleted } = this.getResultEntries();
151
155
  this.writeBuffer.clear();
152
156
  this.deleteBuffer.clear();
153
157
  this.createdKeys.clear();
@@ -242,7 +246,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
242
246
  }
243
247
  }
244
248
  commit(label) {
245
- const { created, updated, deleted } = this._getResultEntries();
249
+ const { created, updated, deleted } = this.getResultEntries();
246
250
  if (this.committed) {
247
251
  return {
248
252
  label,
@@ -872,7 +876,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
872
876
  }
873
877
  }
874
878
  async commit(label) {
875
- const { created, updated, deleted } = this._getResultEntries();
879
+ const { created, updated, deleted } = this.getResultEntries();
876
880
  if (this.committed) {
877
881
  return {
878
882
  label,
@@ -90,7 +90,11 @@ var MVCCTransaction = class {
90
90
  this.createdKeys.delete(key);
91
91
  this.keyVersions.set(key, this.localVersion);
92
92
  }
93
- _getResultEntries() {
93
+ /**
94
+ * Returns the entries that will be created, updated, and deleted by this transaction.
95
+ * @returns An object containing arrays of created, updated, and deleted entries.
96
+ */
97
+ getResultEntries() {
94
98
  const created = [];
95
99
  const updated = [];
96
100
  for (const [key, data] of this.writeBuffer.entries()) {
@@ -116,7 +120,7 @@ var MVCCTransaction = class {
116
120
  * @returns The result object with success, created, updated, and deleted keys.
117
121
  */
118
122
  rollback() {
119
- const { created, updated, deleted } = this._getResultEntries();
123
+ const { created, updated, deleted } = this.getResultEntries();
120
124
  this.writeBuffer.clear();
121
125
  this.deleteBuffer.clear();
122
126
  this.createdKeys.clear();
@@ -211,7 +215,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
211
215
  }
212
216
  }
213
217
  commit(label) {
214
- const { created, updated, deleted } = this._getResultEntries();
218
+ const { created, updated, deleted } = this.getResultEntries();
215
219
  if (this.committed) {
216
220
  return {
217
221
  label,
@@ -841,7 +845,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
841
845
  }
842
846
  }
843
847
  async commit(label) {
844
- const { created, updated, deleted } = this._getResultEntries();
848
+ const { created, updated, deleted } = this.getResultEntries();
845
849
  if (this.committed) {
846
850
  return {
847
851
  label,
@@ -66,7 +66,11 @@ export declare abstract class MVCCTransaction<S extends MVCCStrategy<K, T>, K, T
66
66
  protected _bufferCreate(key: K, value: T): void;
67
67
  protected _bufferWrite(key: K, value: T): void;
68
68
  protected _bufferDelete(key: K): void;
69
- protected _getResultEntries(): {
69
+ /**
70
+ * Returns the entries that will be created, updated, and deleted by this transaction.
71
+ * @returns An object containing arrays of created, updated, and deleted entries.
72
+ */
73
+ getResultEntries(): {
70
74
  created: TransactionEntry<K, T>[];
71
75
  updated: TransactionEntry<K, T>[];
72
76
  deleted: TransactionEntry<K, T>[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mvcc-api",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
4
4
  "description": "Multiversion Concurrency Control (MVCC) API for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",