mvcc-api 1.2.7 → 1.2.9

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
@@ -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,
@@ -300,6 +304,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
300
304
  this.originallyExisted.clear();
301
305
  this.keyVersions.clear();
302
306
  this.localVersion = 0;
307
+ this.snapshotVersion = this.version;
303
308
  }
304
309
  }
305
310
  return {
@@ -871,7 +876,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
871
876
  }
872
877
  }
873
878
  async commit(label) {
874
- const { created, updated, deleted } = this._getResultEntries();
879
+ const { created, updated, deleted } = this.getResultEntries();
875
880
  if (this.committed) {
876
881
  return {
877
882
  label,
@@ -929,6 +934,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
929
934
  this.originallyExisted.clear();
930
935
  this.keyVersions.clear();
931
936
  this.localVersion = 0;
937
+ this.snapshotVersion = this.version;
932
938
  }
933
939
  }
934
940
  return {
@@ -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,
@@ -269,6 +273,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
269
273
  this.originallyExisted.clear();
270
274
  this.keyVersions.clear();
271
275
  this.localVersion = 0;
276
+ this.snapshotVersion = this.version;
272
277
  }
273
278
  }
274
279
  return {
@@ -840,7 +845,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
840
845
  }
841
846
  }
842
847
  async commit(label) {
843
- const { created, updated, deleted } = this._getResultEntries();
848
+ const { created, updated, deleted } = this.getResultEntries();
844
849
  if (this.committed) {
845
850
  return {
846
851
  label,
@@ -898,6 +903,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
898
903
  this.originallyExisted.clear();
899
904
  this.keyVersions.clear();
900
905
  this.localVersion = 0;
906
+ this.snapshotVersion = this.version;
901
907
  }
902
908
  }
903
909
  return {
@@ -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.7",
3
+ "version": "1.2.9",
4
4
  "description": "Multiversion Concurrency Control (MVCC) API for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",