serializable-bptree 8.1.3 → 8.1.4

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.
@@ -93,6 +93,42 @@ var StringComparator = class extends ValueComparator {
93
93
  // node_modules/mvcc-api/dist/esm/index.mjs
94
94
  var MVCCStrategy = class {
95
95
  };
96
+ var LRUMap = class {
97
+ cache = /* @__PURE__ */ new Map();
98
+ capacity;
99
+ constructor(capacity) {
100
+ this.capacity = capacity;
101
+ }
102
+ get(key) {
103
+ if (!this.cache.has(key)) return void 0;
104
+ const value = this.cache.get(key);
105
+ this.cache.delete(key);
106
+ this.cache.set(key, value);
107
+ return value;
108
+ }
109
+ set(key, value) {
110
+ if (this.cache.has(key)) {
111
+ this.cache.delete(key);
112
+ } else if (this.cache.size >= this.capacity) {
113
+ const oldestKey = this.cache.keys().next().value;
114
+ if (oldestKey !== void 0) this.cache.delete(oldestKey);
115
+ }
116
+ this.cache.set(key, value);
117
+ return this;
118
+ }
119
+ has(key) {
120
+ return this.cache.has(key);
121
+ }
122
+ delete(key) {
123
+ return this.cache.delete(key);
124
+ }
125
+ clear() {
126
+ this.cache.clear();
127
+ }
128
+ get size() {
129
+ return this.cache.size;
130
+ }
131
+ };
96
132
  var MVCCTransaction = class {
97
133
  committed;
98
134
  snapshotVersion;
@@ -100,7 +136,6 @@ var MVCCTransaction = class {
100
136
  writeBuffer;
101
137
  deleteBuffer;
102
138
  createdKeys;
103
- // create()로 생성된 키 추적
104
139
  deletedValues;
105
140
  // delete 시 삭제 전 값 저장
106
141
  originallyExisted;
@@ -119,7 +154,8 @@ var MVCCTransaction = class {
119
154
  versionIndex = /* @__PURE__ */ new Map();
120
155
  deletedCache = /* @__PURE__ */ new Map();
121
156
  activeTransactions = /* @__PURE__ */ new Set();
122
- constructor(strategy, parent, snapshotVersion) {
157
+ diskCache;
158
+ constructor(strategy, options, parent, snapshotVersion) {
123
159
  this.snapshotVersion = snapshotVersion ?? 0;
124
160
  this.writeBuffer = /* @__PURE__ */ new Map();
125
161
  this.deleteBuffer = /* @__PURE__ */ new Set();
@@ -134,6 +170,7 @@ var MVCCTransaction = class {
134
170
  this.snapshotLocalVersion = parent.localVersion;
135
171
  this.strategy = void 0;
136
172
  this.root = parent.root;
173
+ this.diskCache = parent.diskCache;
137
174
  } else {
138
175
  if (!strategy) throw new Error("Root Transaction must get Strategy");
139
176
  this.strategy = strategy;
@@ -141,8 +178,13 @@ var MVCCTransaction = class {
141
178
  this.localVersion = 0;
142
179
  this.snapshotLocalVersion = 0;
143
180
  this.root = this;
181
+ this.diskCache = new LRUMap(options?.cacheCapacity ?? 1e3);
144
182
  }
145
183
  }
184
+ /**
185
+ * Checks if the transaction is a root transaction.
186
+ * @returns True if the transaction is a root transaction, false otherwise.
187
+ */
146
188
  isRoot() {
147
189
  return !this.parent;
148
190
  }
@@ -159,7 +201,22 @@ var MVCCTransaction = class {
159
201
  }
160
202
  return false;
161
203
  }
162
- // --- Internal buffer manipulation helpers ---
204
+ /**
205
+ * Checks if a key was written in this transaction.
206
+ * @param key The key to check.
207
+ * @returns True if the key was written in this transaction, false otherwise.
208
+ */
209
+ isWrote(key) {
210
+ return this.writeBuffer.has(key);
211
+ }
212
+ /**
213
+ * Checks if a key was deleted in this transaction.
214
+ * @param key The key to check.
215
+ * @returns True if the key was deleted in this transaction, false otherwise.
216
+ */
217
+ isDeleted(key) {
218
+ return this.deleteBuffer.has(key);
219
+ }
163
220
  _recordHistory(key) {
164
221
  const existsInWriteBuffer = this.writeBuffer.has(key);
165
222
  const existsInDeleteBuffer = this.deleteBuffer.has(key);
@@ -222,7 +279,11 @@ var MVCCTransaction = class {
222
279
  deleted.push({ key, data });
223
280
  }
224
281
  }
225
- return { created, updated, deleted };
282
+ return {
283
+ created,
284
+ updated,
285
+ deleted
286
+ };
226
287
  }
227
288
  /**
228
289
  * Rolls back the transaction.
@@ -240,7 +301,12 @@ var MVCCTransaction = class {
240
301
  if (this.root !== this) {
241
302
  this.root.activeTransactions.delete(this);
242
303
  }
243
- return { success: true, created, updated, deleted };
304
+ return {
305
+ success: true,
306
+ created,
307
+ updated,
308
+ deleted
309
+ };
244
310
  }
245
311
  /**
246
312
  * Cleans up both deletedCache and versionIndex based on minActiveVersion.
@@ -326,7 +392,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
326
392
  createNested() {
327
393
  if (this.committed) throw new Error("Transaction already committed");
328
394
  const childVersion = this.isRoot() ? this.version : this.snapshotVersion;
329
- const child = new _SyncMVCCTransaction(void 0, this, childVersion);
395
+ const child = new _SyncMVCCTransaction(void 0, void 0, this, childVersion);
330
396
  this.root.activeTransactions.add(child);
331
397
  return child;
332
398
  }
@@ -405,22 +471,54 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
405
471
  commit(label) {
406
472
  const { created, updated, deleted } = this.getResultEntries();
407
473
  if (this.committed) {
408
- return { label, success: false, error: "Transaction already committed", conflict: void 0, created, updated, deleted };
474
+ return {
475
+ label,
476
+ success: false,
477
+ error: "Transaction already committed",
478
+ conflict: void 0,
479
+ created,
480
+ updated,
481
+ deleted
482
+ };
409
483
  }
410
484
  if (this.hasCommittedAncestor()) {
411
- return { label, success: false, error: "Ancestor transaction already committed", conflict: void 0, created, updated, deleted };
485
+ return {
486
+ label,
487
+ success: false,
488
+ error: "Ancestor transaction already committed",
489
+ conflict: void 0,
490
+ created,
491
+ updated,
492
+ deleted
493
+ };
412
494
  }
413
495
  if (this.parent) {
414
496
  const failure = this.parent._merge(this);
415
497
  if (failure) {
416
- return { label, success: false, error: failure.error, conflict: failure.conflict, created, updated, deleted };
498
+ return {
499
+ label,
500
+ success: false,
501
+ error: failure.error,
502
+ conflict: failure.conflict,
503
+ created,
504
+ updated,
505
+ deleted
506
+ };
417
507
  }
418
508
  this.committed = true;
419
509
  } else {
420
510
  if (this.writeBuffer.size > 0 || this.deleteBuffer.size > 0) {
421
511
  const failure = this._merge(this);
422
512
  if (failure) {
423
- return { label, success: false, error: failure.error, conflict: failure.conflict, created: [], updated: [], deleted: [] };
513
+ return {
514
+ label,
515
+ success: false,
516
+ error: failure.error,
517
+ conflict: failure.conflict,
518
+ created: [],
519
+ updated: [],
520
+ deleted: []
521
+ };
424
522
  }
425
523
  this.writeBuffer.clear();
426
524
  this.deleteBuffer.clear();
@@ -433,7 +531,13 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
433
531
  this.snapshotVersion = this.version;
434
532
  }
435
533
  }
436
- return { label, success: true, created, updated, deleted };
534
+ return {
535
+ label,
536
+ success: true,
537
+ created,
538
+ updated,
539
+ deleted
540
+ };
437
541
  }
438
542
  _merge(child) {
439
543
  if (this.parent) {
@@ -442,7 +546,11 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
442
546
  if (lastModLocalVer !== void 0 && lastModLocalVer > child.snapshotLocalVersion) {
443
547
  return {
444
548
  error: `Commit conflict: Key '${key}' was modified by a newer transaction (Local v${lastModLocalVer})`,
445
- conflict: { key, parent: this.read(key), child: child.read(key) }
549
+ conflict: {
550
+ key,
551
+ parent: this.read(key),
552
+ child: child.read(key)
553
+ }
446
554
  };
447
555
  }
448
556
  }
@@ -451,7 +559,11 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
451
559
  if (lastModLocalVer !== void 0 && lastModLocalVer > child.snapshotLocalVersion) {
452
560
  return {
453
561
  error: `Commit conflict: Key '${key}' was modified by a newer transaction (Local v${lastModLocalVer})`,
454
- conflict: { key, parent: this.read(key), child: child.read(key) }
562
+ conflict: {
563
+ key,
564
+ parent: this.read(key),
565
+ child: child.read(key)
566
+ }
455
567
  };
456
568
  }
457
569
  }
@@ -480,7 +592,11 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
480
592
  if (lastVer > child.snapshotVersion) {
481
593
  return {
482
594
  error: `Commit conflict: Key '${key}' was modified by a newer transaction (v${lastVer})`,
483
- conflict: { key, parent: this.read(key), child: child.read(key) }
595
+ conflict: {
596
+ key,
597
+ parent: this.read(key),
598
+ child: child.read(key)
599
+ }
484
600
  };
485
601
  }
486
602
  }
@@ -488,7 +604,11 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
488
604
  if (lastModLocalVer !== void 0 && lastModLocalVer > child.snapshotLocalVersion) {
489
605
  return {
490
606
  error: `Commit conflict: Key '${key}' was modified by a newer transaction in the same session (Local v${lastModLocalVer})`,
491
- conflict: { key, parent: this.read(key), child: child.read(key) }
607
+ conflict: {
608
+ key,
609
+ parent: this.read(key),
610
+ child: child.read(key)
611
+ }
492
612
  };
493
613
  }
494
614
  }
@@ -523,10 +643,14 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
523
643
  _diskWrite(key, value, version) {
524
644
  const strategy = this.strategy;
525
645
  if (!strategy) throw new Error("Root Transaction missing strategy");
526
- if (strategy.exists(key)) {
527
- const currentVal = strategy.read(key);
646
+ const rootAsAny = this.root;
647
+ if (this._diskExists(key, version)) {
648
+ const currentVal = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : strategy.read(key);
528
649
  if (!this.deletedCache.has(key)) this.deletedCache.set(key, []);
529
650
  this.deletedCache.get(key).push({ value: currentVal, deletedAtVersion: version });
651
+ rootAsAny.diskCache.set(key, value);
652
+ } else {
653
+ rootAsAny.diskCache.set(key, value);
530
654
  }
531
655
  strategy.write(key, value);
532
656
  if (!this.versionIndex.has(key)) this.versionIndex.set(key, []);
@@ -537,7 +661,13 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
537
661
  if (!strategy) throw new Error("Root Transaction missing strategy");
538
662
  const versions = this.versionIndex.get(key);
539
663
  if (!versions) {
540
- return strategy.exists(key) ? strategy.read(key) : null;
664
+ const rootAsAny = this.root;
665
+ if (this._diskExists(key, snapshotVersion)) {
666
+ const val = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : strategy.read(key);
667
+ rootAsAny.diskCache.set(key, val);
668
+ return val;
669
+ }
670
+ return null;
541
671
  }
542
672
  let targetVerObj = null;
543
673
  let nextVerObj = null;
@@ -561,7 +691,13 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
561
691
  if (!targetVerObj.exists) return null;
562
692
  if (!nextVerObj) {
563
693
  if (this.writeBuffer.has(key)) return this.writeBuffer.get(key);
564
- return strategy.read(key);
694
+ if (this._diskExists(key, snapshotVersion)) {
695
+ const rootAsAny = this.root;
696
+ const val = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : strategy.read(key);
697
+ rootAsAny.diskCache.set(key, val);
698
+ return val;
699
+ }
700
+ return null;
565
701
  }
566
702
  const cached = this.deletedCache.get(key);
567
703
  if (cached) {
@@ -575,7 +711,11 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
575
711
  if (!strategy) throw new Error("Root Transaction missing strategy");
576
712
  const versions = this.versionIndex.get(key);
577
713
  if (!versions) {
578
- return strategy.exists(key);
714
+ const rootAsAny = this.root;
715
+ if (rootAsAny.diskCache.has(key)) return rootAsAny.diskCache.get(key) !== null;
716
+ const exists = strategy.exists(key);
717
+ if (!exists) rootAsAny.diskCache.set(key, null);
718
+ return exists;
579
719
  }
580
720
  let targetVerObj = null;
581
721
  let nextVerObj = null;
@@ -601,11 +741,13 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
601
741
  _diskDelete(key, snapshotVersion) {
602
742
  const strategy = this.strategy;
603
743
  if (!strategy) throw new Error("Root Transaction missing strategy");
604
- if (strategy.exists(key)) {
605
- const currentVal = strategy.read(key);
744
+ const rootAsAny = this.root;
745
+ if (this._diskExists(key, snapshotVersion)) {
746
+ const currentVal = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : strategy.read(key);
606
747
  if (!this.deletedCache.has(key)) this.deletedCache.set(key, []);
607
748
  this.deletedCache.get(key).push({ value: currentVal, deletedAtVersion: snapshotVersion });
608
749
  strategy.delete(key);
750
+ rootAsAny.diskCache.delete(key);
609
751
  }
610
752
  if (!this.versionIndex.has(key)) this.versionIndex.set(key, []);
611
753
  this.versionIndex.get(key).push({ version: snapshotVersion, exists: false });
@@ -919,7 +1061,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
919
1061
  createNested() {
920
1062
  if (this.committed) throw new Error("Transaction already committed");
921
1063
  const childVersion = this.isRoot() ? this.version : this.snapshotVersion;
922
- const child = new _AsyncMVCCTransaction(void 0, this, childVersion);
1064
+ const child = new _AsyncMVCCTransaction(void 0, void 0, this, childVersion);
923
1065
  this.root.activeTransactions.add(child);
924
1066
  return child;
925
1067
  }
@@ -998,22 +1140,54 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
998
1140
  async commit(label) {
999
1141
  const { created, updated, deleted } = this.getResultEntries();
1000
1142
  if (this.committed) {
1001
- return { label, success: false, error: "Transaction already committed", conflict: void 0, created, updated, deleted };
1143
+ return {
1144
+ label,
1145
+ success: false,
1146
+ error: "Transaction already committed",
1147
+ conflict: void 0,
1148
+ created,
1149
+ updated,
1150
+ deleted
1151
+ };
1002
1152
  }
1003
1153
  if (this.hasCommittedAncestor()) {
1004
- return { label, success: false, error: "Ancestor transaction already committed", conflict: void 0, created, updated, deleted };
1154
+ return {
1155
+ label,
1156
+ success: false,
1157
+ error: "Ancestor transaction already committed",
1158
+ conflict: void 0,
1159
+ created,
1160
+ updated,
1161
+ deleted
1162
+ };
1005
1163
  }
1006
1164
  if (this.parent) {
1007
1165
  const failure = await this.parent._merge(this);
1008
1166
  if (failure) {
1009
- return { label, success: false, error: failure.error, conflict: failure.conflict, created, updated, deleted };
1167
+ return {
1168
+ label,
1169
+ success: false,
1170
+ error: failure.error,
1171
+ conflict: failure.conflict,
1172
+ created,
1173
+ updated,
1174
+ deleted
1175
+ };
1010
1176
  }
1011
1177
  this.committed = true;
1012
1178
  } else {
1013
1179
  if (this.writeBuffer.size > 0 || this.deleteBuffer.size > 0) {
1014
1180
  const failure = await this._merge(this);
1015
1181
  if (failure) {
1016
- return { label, success: false, error: failure.error, conflict: failure.conflict, created: [], updated: [], deleted: [] };
1182
+ return {
1183
+ label,
1184
+ success: false,
1185
+ error: failure.error,
1186
+ conflict: failure.conflict,
1187
+ created: [],
1188
+ updated: [],
1189
+ deleted: []
1190
+ };
1017
1191
  }
1018
1192
  this.writeBuffer.clear();
1019
1193
  this.deleteBuffer.clear();
@@ -1026,7 +1200,13 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1026
1200
  this.snapshotVersion = this.version;
1027
1201
  }
1028
1202
  }
1029
- return { label, success: true, created, updated, deleted };
1203
+ return {
1204
+ label,
1205
+ success: true,
1206
+ created,
1207
+ updated,
1208
+ deleted
1209
+ };
1030
1210
  }
1031
1211
  async _merge(child) {
1032
1212
  return this.writeLock(async () => {
@@ -1036,7 +1216,11 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1036
1216
  if (lastModLocalVer !== void 0 && lastModLocalVer > child.snapshotLocalVersion) {
1037
1217
  return {
1038
1218
  error: `Commit conflict: Key '${key}' was modified by a newer transaction (Local v${lastModLocalVer})`,
1039
- conflict: { key, parent: await this.read(key), child: await child.read(key) }
1219
+ conflict: {
1220
+ key,
1221
+ parent: await this.read(key),
1222
+ child: await child.read(key)
1223
+ }
1040
1224
  };
1041
1225
  }
1042
1226
  }
@@ -1045,7 +1229,11 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1045
1229
  if (lastModLocalVer !== void 0 && lastModLocalVer > child.snapshotLocalVersion) {
1046
1230
  return {
1047
1231
  error: `Commit conflict: Key '${key}' was modified by a newer transaction (Local v${lastModLocalVer})`,
1048
- conflict: { key, parent: await this.read(key), child: await child.read(key) }
1232
+ conflict: {
1233
+ key,
1234
+ parent: await this.read(key),
1235
+ child: await child.read(key)
1236
+ }
1049
1237
  };
1050
1238
  }
1051
1239
  }
@@ -1075,7 +1263,11 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1075
1263
  if (lastVer > child.snapshotVersion) {
1076
1264
  return {
1077
1265
  error: `Commit conflict: Key '${key}' was modified by a newer transaction (v${lastVer})`,
1078
- conflict: { key, parent: await this.read(key), child: await child.read(key) }
1266
+ conflict: {
1267
+ key,
1268
+ parent: await this.read(key),
1269
+ child: await child.read(key)
1270
+ }
1079
1271
  };
1080
1272
  }
1081
1273
  }
@@ -1083,7 +1275,11 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1083
1275
  if (lastModLocalVer !== void 0 && lastModLocalVer > child.snapshotLocalVersion) {
1084
1276
  return {
1085
1277
  error: `Commit conflict: Key '${key}' was modified by a newer transaction in the same session (Local v${lastModLocalVer})`,
1086
- conflict: { key, parent: await this.read(key), child: await child.read(key) }
1278
+ conflict: {
1279
+ key,
1280
+ parent: await this.read(key),
1281
+ child: await child.read(key)
1282
+ }
1087
1283
  };
1088
1284
  }
1089
1285
  }
@@ -1119,10 +1315,14 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1119
1315
  async _diskWrite(key, value, version) {
1120
1316
  const strategy = this.strategy;
1121
1317
  if (!strategy) throw new Error("Root Transaction missing strategy");
1122
- if (await strategy.exists(key)) {
1123
- const currentVal = await strategy.read(key);
1318
+ const rootAsAny = this.root;
1319
+ if (await this._diskExists(key, version)) {
1320
+ const currentVal = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : await strategy.read(key);
1124
1321
  if (!this.deletedCache.has(key)) this.deletedCache.set(key, []);
1125
1322
  this.deletedCache.get(key).push({ value: currentVal, deletedAtVersion: version });
1323
+ rootAsAny.diskCache.set(key, value);
1324
+ } else {
1325
+ rootAsAny.diskCache.set(key, value);
1126
1326
  }
1127
1327
  await strategy.write(key, value);
1128
1328
  if (!this.versionIndex.has(key)) this.versionIndex.set(key, []);
@@ -1133,7 +1333,13 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1133
1333
  if (!strategy) throw new Error("Root Transaction missing strategy");
1134
1334
  const versions = this.versionIndex.get(key);
1135
1335
  if (!versions) {
1136
- return await strategy.exists(key) ? await strategy.read(key) : null;
1336
+ const rootAsAny = this.root;
1337
+ if (await this._diskExists(key, snapshotVersion)) {
1338
+ const val = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : await strategy.read(key);
1339
+ rootAsAny.diskCache.set(key, val);
1340
+ return val;
1341
+ }
1342
+ return null;
1137
1343
  }
1138
1344
  let targetVerObj = null;
1139
1345
  let nextVerObj = null;
@@ -1157,7 +1363,13 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1157
1363
  if (!targetVerObj.exists) return null;
1158
1364
  if (!nextVerObj) {
1159
1365
  if (this.writeBuffer.has(key)) return this.writeBuffer.get(key);
1160
- return strategy.read(key);
1366
+ if (await this._diskExists(key, snapshotVersion)) {
1367
+ const rootAsAny = this.root;
1368
+ const val = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : await strategy.read(key);
1369
+ rootAsAny.diskCache.set(key, val);
1370
+ return val;
1371
+ }
1372
+ return null;
1161
1373
  }
1162
1374
  const cached = this.deletedCache.get(key);
1163
1375
  if (cached) {
@@ -1171,7 +1383,11 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1171
1383
  if (!strategy) throw new Error("Root Transaction missing strategy");
1172
1384
  const versions = this.versionIndex.get(key);
1173
1385
  if (!versions) {
1174
- return await strategy.exists(key);
1386
+ const rootAsAny = this.root;
1387
+ if (rootAsAny.diskCache.has(key)) return rootAsAny.diskCache.get(key) !== null;
1388
+ const exists = await strategy.exists(key);
1389
+ if (!exists) rootAsAny.diskCache.set(key, null);
1390
+ return exists;
1175
1391
  }
1176
1392
  let targetVerObj = null;
1177
1393
  let nextVerObj = null;
@@ -1197,344 +1413,23 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1197
1413
  async _diskDelete(key, snapshotVersion) {
1198
1414
  const strategy = this.strategy;
1199
1415
  if (!strategy) throw new Error("Root Transaction missing strategy");
1200
- if (await strategy.exists(key)) {
1201
- const currentVal = await strategy.read(key);
1416
+ const rootAsAny = this.root;
1417
+ if (await this._diskExists(key, snapshotVersion)) {
1418
+ const currentVal = rootAsAny.diskCache.has(key) ? rootAsAny.diskCache.get(key) : await strategy.read(key);
1202
1419
  if (!this.deletedCache.has(key)) this.deletedCache.set(key, []);
1203
1420
  this.deletedCache.get(key).push({ value: currentVal, deletedAtVersion: snapshotVersion });
1204
1421
  await strategy.delete(key);
1422
+ rootAsAny.diskCache.delete(key);
1205
1423
  }
1206
1424
  if (!this.versionIndex.has(key)) this.versionIndex.set(key, []);
1207
1425
  this.versionIndex.get(key).push({ version: snapshotVersion, exists: false });
1208
1426
  }
1209
1427
  };
1210
1428
 
1211
- // node_modules/cache-entanglement/dist/esm/index.mjs
1212
- var LRUMap = class {
1213
- capacity;
1214
- map;
1215
- head = null;
1216
- tail = null;
1217
- /**
1218
- * Creates an instance of LRUMap.
1219
- * @param capacity The maximum number of items the cache can hold.
1220
- */
1221
- constructor(capacity) {
1222
- this.capacity = capacity;
1223
- this.map = /* @__PURE__ */ new Map();
1224
- }
1225
- /**
1226
- * Promotes a node to the head of the linked list (marks as most recently used).
1227
- * @param node The node to promote.
1228
- */
1229
- promote(node) {
1230
- this.extract(node);
1231
- this.prepend(node);
1232
- }
1233
- /**
1234
- * Disconnects a node from the doubly linked list.
1235
- * @param node The node to extract.
1236
- */
1237
- extract(node) {
1238
- if (node.prev) node.prev.next = node.next;
1239
- else this.head = node.next;
1240
- if (node.next) node.next.prev = node.prev;
1241
- else this.tail = node.prev;
1242
- node.prev = null;
1243
- node.next = null;
1244
- }
1245
- /**
1246
- * Inserts a node at the head of the doubly linked list.
1247
- * @param node The node to prepend.
1248
- */
1249
- prepend(node) {
1250
- node.next = this.head;
1251
- if (this.head) this.head.prev = node;
1252
- this.head = node;
1253
- if (!this.tail) this.tail = node;
1254
- }
1255
- /**
1256
- * Stores or updates a value by key.
1257
- * If the capacity is exceeded, the least recently used item (tail) is removed.
1258
- * @param key The key to store.
1259
- * @param value The value to store.
1260
- */
1261
- set(key, value) {
1262
- const existing = this.map.get(key);
1263
- if (existing) {
1264
- existing.value = value;
1265
- this.promote(existing);
1266
- return;
1267
- }
1268
- const newNode = { key, value, prev: null, next: null };
1269
- this.map.set(key, newNode);
1270
- this.prepend(newNode);
1271
- if (this.map.size > this.capacity && this.tail) {
1272
- this.map.delete(this.tail.key);
1273
- this.extract(this.tail);
1274
- }
1275
- }
1276
- /**
1277
- * Retrieves a value by key.
1278
- * Accessing the item moves it to the "most recently used" position.
1279
- * @param key The key to look for.
1280
- * @returns The value associated with the key, or undefined if not found.
1281
- */
1282
- get(key) {
1283
- const node = this.map.get(key);
1284
- if (!node) return void 0;
1285
- this.promote(node);
1286
- return node.value;
1287
- }
1288
- /**
1289
- * Checks if a key exists in the cache without changing its access order.
1290
- * @param key The key to check.
1291
- * @returns True if the key exists, false otherwise.
1292
- */
1293
- has(key) {
1294
- return this.map.has(key);
1295
- }
1296
- /**
1297
- * Removes a key and its associated value from the cache.
1298
- * @param key The key to remove.
1299
- * @returns True if the key was found and removed, false otherwise.
1300
- */
1301
- delete(key) {
1302
- const node = this.map.get(key);
1303
- if (!node) return false;
1304
- this.extract(node);
1305
- this.map.delete(key);
1306
- return true;
1307
- }
1308
- /**
1309
- * Returns an iterator of keys in the order of most recently used to least recently used.
1310
- * @returns An iterable iterator of keys.
1311
- */
1312
- *keys() {
1313
- let current = this.head;
1314
- while (current) {
1315
- yield current.key;
1316
- current = current.next;
1317
- }
1318
- }
1319
- /**
1320
- * Returns the current number of items in the cache.
1321
- */
1322
- get size() {
1323
- return this.map.size;
1324
- }
1325
- /**
1326
- * Clears all items from the cache.
1327
- */
1328
- clear() {
1329
- this.map.clear();
1330
- this.head = null;
1331
- this.tail = null;
1332
- }
1333
- };
1334
- var CacheEntanglement = class {
1335
- creation;
1336
- beforeUpdateHook;
1337
- capacity;
1338
- dependencies;
1339
- caches;
1340
- parameters;
1341
- assignments;
1342
- dependencyProperties;
1343
- updateRequirements;
1344
- constructor(creation, option) {
1345
- option = option ?? {};
1346
- const {
1347
- dependencies,
1348
- capacity,
1349
- beforeUpdateHook
1350
- } = option;
1351
- this.creation = creation;
1352
- this.beforeUpdateHook = beforeUpdateHook ?? (() => {
1353
- });
1354
- this.capacity = capacity ?? 100;
1355
- this.assignments = [];
1356
- this.caches = new LRUMap(this.capacity);
1357
- this.parameters = /* @__PURE__ */ new Map();
1358
- this.dependencies = dependencies ?? {};
1359
- this.dependencyProperties = Object.keys(this.dependencies);
1360
- this.updateRequirements = /* @__PURE__ */ new Set();
1361
- for (const name in this.dependencies) {
1362
- const dependency = this.dependencies[name];
1363
- if (!dependency.assignments.includes(this)) {
1364
- dependency.assignments.push(this);
1365
- }
1366
- }
1367
- }
1368
- bubbleUpdateSignal(key) {
1369
- this.updateRequirements.add(key);
1370
- for (let i = 0, len = this.assignments.length; i < len; i++) {
1371
- const t = this.assignments[i];
1372
- const instance = t;
1373
- for (const cacheKey of instance.caches.keys()) {
1374
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
1375
- instance.bubbleUpdateSignal(cacheKey);
1376
- }
1377
- }
1378
- }
1379
- }
1380
- dependencyKey(key) {
1381
- const i = key.lastIndexOf("/");
1382
- if (i === -1) {
1383
- return key;
1384
- }
1385
- return key.substring(0, i);
1386
- }
1387
- /**
1388
- * Returns all keys stored in the instance.
1389
- */
1390
- keys() {
1391
- return this.parameters.keys();
1392
- }
1393
- /**
1394
- * Deletes all cache values stored in the instance.
1395
- */
1396
- clear() {
1397
- for (const key of this.keys()) {
1398
- this.delete(key);
1399
- }
1400
- }
1401
- /**
1402
- * Checks if there is a cache value stored in the key within the instance.
1403
- * @param key The key to search.
1404
- */
1405
- exists(key) {
1406
- return this.parameters.has(key);
1407
- }
1408
- /**
1409
- * Checks if there is a cache value stored in the key within the instance.
1410
- * This method is an alias for `exists`.
1411
- * @param key The key to search.
1412
- */
1413
- has(key) {
1414
- return this.exists(key);
1415
- }
1416
- /**
1417
- * Deletes the cache value stored in the key within the instance.
1418
- * @param key The key to delete.
1419
- */
1420
- delete(key) {
1421
- this.caches.delete(key);
1422
- this.parameters.delete(key);
1423
- this.updateRequirements.delete(key);
1424
- for (let i = 0, len = this.assignments.length; i < len; i++) {
1425
- const t = this.assignments[i];
1426
- const instance = t;
1427
- for (const cacheKey of instance.keys()) {
1428
- if (cacheKey === key || cacheKey.startsWith(`${key}/`)) {
1429
- instance.delete(cacheKey);
1430
- }
1431
- }
1432
- }
1433
- }
1434
- };
1435
- var CacheData = class _CacheData {
1436
- static StructuredClone = globalThis.structuredClone.bind(globalThis);
1437
- _value;
1438
- constructor(value) {
1439
- this._value = value;
1440
- }
1441
- /**
1442
- * This is cached data.
1443
- * It was generated at the time of caching, so there is a risk of modification if it's an object due to shallow copying.
1444
- * Therefore, if it's not a primitive type, please avoid using this value directly and use the `clone` method to use a copied version of the data.
1445
- */
1446
- get raw() {
1447
- return this._value;
1448
- }
1449
- /**
1450
- * The method returns a copied value of the cached data.
1451
- * You can pass a function as a parameter to copy the value. This parameter function should return the copied value.
1452
- *
1453
- * If no parameter is passed, it defaults to using `structuredClone` function to copy the value.
1454
- * If you prefer shallow copying instead of deep copying,
1455
- * you can use the default options `array-shallow-copy`, `object-shallow-copy` and `deep-copy`,
1456
- * which are replaced with functions to shallow copy arrays and objects, respectively. This is a syntactic sugar.
1457
- * @param strategy The function that returns the copied value.
1458
- * If you want to perform a shallow copy, simply pass the strings `array-shallow-copy` or `object-shallow-copy` for easy use.
1459
- * The `array-shallow-copy` strategy performs a shallow copy of an array.
1460
- * The `object-shallow-copy` strategy performs a shallow copy of an object.
1461
- * The `deep-copy` strategy performs a deep copy of the value using `structuredClone`.
1462
- * The default is `deep-copy`.
1463
- */
1464
- clone(strategy = "deep-copy") {
1465
- if (strategy && typeof strategy !== "string") {
1466
- return strategy(this.raw);
1467
- }
1468
- switch (strategy) {
1469
- case "array-shallow-copy":
1470
- return [].concat(this.raw);
1471
- case "object-shallow-copy":
1472
- return Object.assign({}, this.raw);
1473
- case "deep-copy":
1474
- default:
1475
- return _CacheData.StructuredClone(this.raw);
1476
- }
1477
- }
1478
- };
1479
- var CacheEntanglementSync = class extends CacheEntanglement {
1480
- constructor(creation, option) {
1481
- super(creation, option);
1482
- }
1483
- recache(key) {
1484
- if (!this.parameters.has(key)) {
1485
- return;
1486
- }
1487
- if (!this.caches.has(key) || this.updateRequirements.has(key)) {
1488
- this.resolve(key, ...this.parameters.get(key));
1489
- }
1490
- return this.caches.get(key);
1491
- }
1492
- resolve(key, ...parameter) {
1493
- const resolved = {};
1494
- const dependencyKey = this.dependencyKey(key);
1495
- this.beforeUpdateHook(key, dependencyKey, ...parameter);
1496
- for (let i = 0, len = this.dependencyProperties.length; i < len; i++) {
1497
- const name = this.dependencyProperties[i];
1498
- const dependency = this.dependencies[name];
1499
- if (!dependency.exists(key) && !dependency.exists(dependencyKey)) {
1500
- throw new Error(`The key '${key}' or '${dependencyKey}' has not been assigned yet in dependency '${name.toString()}'.`, {
1501
- cause: {
1502
- from: this
1503
- }
1504
- });
1505
- }
1506
- const dependencyValue = dependency.recache(key) ?? dependency.recache(dependencyKey);
1507
- resolved[name] = dependencyValue;
1508
- }
1509
- const value = new CacheData(this.creation(key, resolved, ...parameter));
1510
- this.updateRequirements.delete(key);
1511
- this.parameters.set(key, parameter);
1512
- this.caches.set(key, value);
1513
- return value;
1514
- }
1515
- get(key) {
1516
- if (!this.parameters.has(key)) {
1517
- throw new Error(`Cache value not found: ${key}`);
1518
- }
1519
- return this.cache(key, ...this.parameters.get(key));
1520
- }
1521
- cache(key, ...parameter) {
1522
- if (!this.caches.has(key) || this.updateRequirements.has(key)) {
1523
- this.resolve(key, ...parameter);
1524
- }
1525
- return this.caches.get(key);
1526
- }
1527
- update(key, ...parameter) {
1528
- this.bubbleUpdateSignal(key);
1529
- this.resolve(key, ...parameter);
1530
- return this.caches.get(key);
1531
- }
1532
- };
1533
-
1534
1429
  // src/base/BPTreeTransaction.ts
1535
1430
  var BPTreeTransaction = class _BPTreeTransaction {
1536
- _cachedRegexp;
1537
- nodes;
1431
+ _cachedRegexp = /* @__PURE__ */ new Map();
1432
+ nodes = /* @__PURE__ */ new Map();
1538
1433
  deletedNodeBuffer = /* @__PURE__ */ new Map();
1539
1434
  rootTx;
1540
1435
  mvccRoot;
@@ -1564,8 +1459,12 @@ var BPTreeTransaction = class _BPTreeTransaction {
1564
1459
  like: (nv, v) => {
1565
1460
  const nodeValue = this.comparator.match(nv);
1566
1461
  const value = v;
1567
- const cache = this._cachedRegexp.cache(value);
1568
- const regexp = cache.raw;
1462
+ if (!this._cachedRegexp.has(value)) {
1463
+ const pattern = value.replace(/%/g, ".*").replace(/_/g, ".");
1464
+ const regexp2 = new RegExp(`^${pattern}$`, "i");
1465
+ this._cachedRegexp.set(value, regexp2);
1466
+ }
1467
+ const regexp = this._cachedRegexp.get(value);
1569
1468
  return regexp.test(nodeValue);
1570
1469
  }
1571
1470
  };
@@ -1737,6 +1636,9 @@ var BPTreeTransaction = class _BPTreeTransaction {
1737
1636
  }
1738
1637
  return true;
1739
1638
  }
1639
+ _cloneNode(node) {
1640
+ return JSON.parse(JSON.stringify(node));
1641
+ }
1740
1642
  /**
1741
1643
  * Selects the best driver key from a condition object.
1742
1644
  * The driver key determines the starting point and traversal direction for queries.
@@ -1769,17 +1671,6 @@ var BPTreeTransaction = class _BPTreeTransaction {
1769
1671
  this.strategy = strategy;
1770
1672
  this.comparator = comparator;
1771
1673
  this.option = option ?? {};
1772
- this.nodes = new LRUMap(this.option.capacity ?? 1e3);
1773
- this._cachedRegexp = this._createCachedRegexp();
1774
- }
1775
- _createCachedRegexp() {
1776
- return new CacheEntanglementSync((key) => {
1777
- const pattern = key.replace(/%/g, ".*").replace(/_/g, ".");
1778
- const regexp = new RegExp(`^${pattern}$`, "i");
1779
- return regexp;
1780
- }, {
1781
- capacity: this.option.capacity ?? 1e3
1782
- });
1783
1674
  }
1784
1675
  ensureValues(v) {
1785
1676
  if (!Array.isArray(v)) {
@@ -1812,7 +1703,6 @@ var BPTreeTransaction = class _BPTreeTransaction {
1812
1703
  }
1813
1704
  _clearCache() {
1814
1705
  this._cachedRegexp.clear();
1815
- this.nodes.clear();
1816
1706
  }
1817
1707
  /**
1818
1708
  * Clears all cached nodes.
@@ -1865,9 +1755,6 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1865
1755
  );
1866
1756
  }
1867
1757
  getNode(id) {
1868
- if (this.nodes.has(id)) {
1869
- return this.nodes.get(id);
1870
- }
1871
1758
  return this.mvcc.read(id);
1872
1759
  }
1873
1760
  /**
@@ -1885,23 +1772,22 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1885
1772
  prev
1886
1773
  };
1887
1774
  this.mvcc.create(id, node);
1888
- this.nodes.set(id, node);
1889
1775
  return node;
1890
1776
  }
1891
1777
  _updateNode(node) {
1778
+ if (this.mvcc.isDeleted(node.id)) {
1779
+ return;
1780
+ }
1892
1781
  this.mvcc.write(node.id, node);
1893
- this.nodes.set(node.id, node);
1894
1782
  }
1895
1783
  _deleteNode(node) {
1784
+ if (this.mvcc.isDeleted(node.id)) {
1785
+ return;
1786
+ }
1896
1787
  this.mvcc.delete(node.id);
1897
- this.nodes.delete(node.id);
1898
1788
  }
1899
1789
  _readHead() {
1900
- if (this.nodes.has("__HEAD__")) {
1901
- return this.nodes.get("__HEAD__") ?? null;
1902
- }
1903
- const head = this.mvcc.read("__HEAD__");
1904
- return head ?? null;
1790
+ return this.mvcc.read("__HEAD__");
1905
1791
  }
1906
1792
  _writeHead(head) {
1907
1793
  if (!this.mvcc.exists("__HEAD__")) {
@@ -1909,41 +1795,45 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1909
1795
  } else {
1910
1796
  this.mvcc.write("__HEAD__", head);
1911
1797
  }
1912
- this.nodes.set("__HEAD__", head);
1913
1798
  this.rootId = head.root;
1914
1799
  }
1915
1800
  _insertAtLeaf(node, key, value) {
1916
- if (node.values.length) {
1917
- for (let i = 0, len = node.values.length; i < len; i++) {
1918
- const nValue = node.values[i];
1801
+ let leaf = node;
1802
+ leaf = this._cloneNode(leaf);
1803
+ if (leaf.values.length) {
1804
+ for (let i = 0, len = leaf.values.length; i < len; i++) {
1805
+ const nValue = leaf.values[i];
1919
1806
  if (this.comparator.isSame(value, nValue)) {
1920
- const keys = node.keys[i];
1807
+ const keys = leaf.keys[i];
1921
1808
  if (keys.includes(key)) {
1922
1809
  break;
1923
1810
  }
1924
1811
  keys.push(key);
1925
- this._updateNode(node);
1926
- return;
1812
+ this._updateNode(leaf);
1813
+ return leaf;
1927
1814
  } else if (this.comparator.isLower(value, nValue)) {
1928
- node.values.splice(i, 0, value);
1929
- node.keys.splice(i, 0, [key]);
1930
- this._updateNode(node);
1931
- return;
1932
- } else if (i + 1 === node.values.length) {
1933
- node.values.push(value);
1934
- node.keys.push([key]);
1935
- this._updateNode(node);
1936
- return;
1815
+ leaf.values.splice(i, 0, value);
1816
+ leaf.keys.splice(i, 0, [key]);
1817
+ this._updateNode(leaf);
1818
+ return leaf;
1819
+ } else if (i + 1 === leaf.values.length) {
1820
+ leaf.values.push(value);
1821
+ leaf.keys.push([key]);
1822
+ this._updateNode(leaf);
1823
+ return leaf;
1937
1824
  }
1938
1825
  }
1939
1826
  } else {
1940
- node.values = [value];
1941
- node.keys = [[key]];
1942
- this._updateNode(node);
1943
- return;
1827
+ leaf.values = [value];
1828
+ leaf.keys = [[key]];
1829
+ this._updateNode(leaf);
1830
+ return leaf;
1944
1831
  }
1832
+ return leaf;
1945
1833
  }
1946
1834
  _insertInParent(node, value, pointer) {
1835
+ node = this._cloneNode(node);
1836
+ pointer = this._cloneNode(pointer);
1947
1837
  if (this.rootId === node.id) {
1948
1838
  const root = this._createNode(false, [node.id, pointer.id], [value]);
1949
1839
  this.rootId = root.id;
@@ -1962,7 +1852,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1962
1852
  this._updateNode(pointer);
1963
1853
  return;
1964
1854
  }
1965
- const parentNode = this.getNode(node.parent);
1855
+ const parentNode = this._cloneNode(this.getNode(node.parent));
1966
1856
  const nodeIndex = parentNode.keys.indexOf(node.id);
1967
1857
  if (nodeIndex === -1) {
1968
1858
  throw new Error(`Node ${node.id} not found in parent ${parentNode.id}`);
@@ -1978,7 +1868,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1978
1868
  leftSibling.next = pointer.id;
1979
1869
  this._updateNode(leftSibling);
1980
1870
  if (oldNextId) {
1981
- const oldNext = this.getNode(oldNextId);
1871
+ const oldNext = this._cloneNode(this.getNode(oldNextId));
1982
1872
  oldNext.prev = pointer.id;
1983
1873
  this._updateNode(oldNext);
1984
1874
  }
@@ -1995,12 +1885,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
1995
1885
  parentNode.values = parentNode.values.slice(0, mid);
1996
1886
  parentNode.keys = parentNode.keys.slice(0, mid + 1);
1997
1887
  for (const k of parentNode.keys) {
1998
- const n = this.getNode(k);
1888
+ const n = this._cloneNode(this.getNode(k));
1999
1889
  n.parent = parentNode.id;
2000
1890
  this._updateNode(n);
2001
1891
  }
2002
1892
  for (const k of parentPointer.keys) {
2003
- const n = this.getNode(k);
1893
+ const n = this._cloneNode(this.getNode(k));
2004
1894
  n.parent = parentPointer.id;
2005
1895
  this._updateNode(n);
2006
1896
  }
@@ -2188,21 +2078,6 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2188
2078
  }
2189
2079
  return false;
2190
2080
  }
2191
- forceUpdate(id) {
2192
- if (id) {
2193
- this.nodes.delete(id);
2194
- this.getNode(id);
2195
- return 1;
2196
- }
2197
- const keys = Array.from(this.nodes.keys());
2198
- for (const key of keys) {
2199
- this.nodes.delete(key);
2200
- }
2201
- for (const key of keys) {
2202
- this.getNode(key);
2203
- }
2204
- return keys.length;
2205
- }
2206
2081
  get(key) {
2207
2082
  let node = this.leftestNode();
2208
2083
  while (true) {
@@ -2288,10 +2163,10 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2288
2163
  return map;
2289
2164
  }
2290
2165
  insert(key, value) {
2291
- const before = this.insertableNode(value);
2292
- this._insertAtLeaf(before, key, value);
2166
+ let before = this.insertableNode(value);
2167
+ before = this._insertAtLeaf(before, key, value);
2293
2168
  if (before.values.length === this.order) {
2294
- const after = this._createNode(
2169
+ let after = this._createNode(
2295
2170
  true,
2296
2171
  [],
2297
2172
  [],
@@ -2300,14 +2175,18 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2300
2175
  null
2301
2176
  );
2302
2177
  const mid = Math.ceil(this.order / 2) - 1;
2178
+ after = this._cloneNode(after);
2303
2179
  after.values = before.values.slice(mid + 1);
2304
2180
  after.keys = before.keys.slice(mid + 1);
2305
2181
  before.values = before.values.slice(0, mid + 1);
2306
2182
  before.keys = before.keys.slice(0, mid + 1);
2183
+ this._updateNode(before);
2184
+ this._updateNode(after);
2307
2185
  this._insertInParent(before, after.values[0], after);
2308
2186
  }
2309
2187
  }
2310
2188
  _deleteEntry(node, key) {
2189
+ node = this._cloneNode(node);
2311
2190
  if (!node.leaf) {
2312
2191
  let keyIndex = -1;
2313
2192
  for (let i = 0, len = node.keys.length; i < len; i++) {
@@ -2326,7 +2205,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2326
2205
  if (this.rootId === node.id && node.keys.length === 1 && !node.leaf) {
2327
2206
  const keys = node.keys;
2328
2207
  this._deleteNode(node);
2329
- const newRoot = this.getNode(keys[0]);
2208
+ const newRoot = this._cloneNode(this.getNode(keys[0]));
2330
2209
  newRoot.parent = null;
2331
2210
  this._updateNode(newRoot);
2332
2211
  this._writeHead({
@@ -2334,17 +2213,17 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2334
2213
  order: this.order,
2335
2214
  data: this.strategy.head.data
2336
2215
  });
2337
- return;
2216
+ return node;
2338
2217
  } else if (this.rootId === node.id) {
2339
2218
  this._writeHead({
2340
2219
  root: node.id,
2341
2220
  order: this.order,
2342
2221
  data: this.strategy.head.data
2343
2222
  });
2344
- return;
2223
+ return node;
2345
2224
  } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
2346
2225
  if (node.parent === null) {
2347
- return;
2226
+ return node;
2348
2227
  }
2349
2228
  let isPredecessor = false;
2350
2229
  let parentNode = this.getNode(node.parent);
@@ -2356,11 +2235,11 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2356
2235
  const nKey = parentNode.keys[i];
2357
2236
  if (nKey === node.id) {
2358
2237
  if (i > 0) {
2359
- prevNode = this.getNode(parentNode.keys[i - 1]);
2238
+ prevNode = this._cloneNode(this.getNode(parentNode.keys[i - 1]));
2360
2239
  prevValue = parentNode.values[i - 1];
2361
2240
  }
2362
2241
  if (i < parentNode.keys.length - 1) {
2363
- nextNode = this.getNode(parentNode.keys[i + 1]);
2242
+ nextNode = this._cloneNode(this.getNode(parentNode.keys[i + 1]));
2364
2243
  postValue = parentNode.values[i];
2365
2244
  }
2366
2245
  }
@@ -2385,7 +2264,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2385
2264
  }
2386
2265
  }
2387
2266
  if (!pointer) {
2388
- return;
2267
+ return node;
2389
2268
  }
2390
2269
  if (node.values.length + pointer.values.length < this.order) {
2391
2270
  if (!isPredecessor) {
@@ -2399,7 +2278,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2399
2278
  } else {
2400
2279
  pointer.next = node.next;
2401
2280
  if (pointer.next) {
2402
- const n = this.getNode(pointer.next);
2281
+ const n = this._cloneNode(this.getNode(pointer.next));
2403
2282
  n.prev = pointer.id;
2404
2283
  this._updateNode(n);
2405
2284
  }
@@ -2408,14 +2287,14 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2408
2287
  if (!pointer.leaf) {
2409
2288
  const keys = pointer.keys;
2410
2289
  for (const key2 of keys) {
2411
- const node2 = this.getNode(key2);
2290
+ const node2 = this._cloneNode(this.getNode(key2));
2412
2291
  node2.parent = pointer.id;
2413
2292
  this._updateNode(node2);
2414
2293
  }
2415
2294
  }
2416
2295
  this._deleteNode(node);
2417
2296
  this._updateNode(pointer);
2418
- this._deleteEntry(this.getNode(node.parent), node.id);
2297
+ this._deleteEntry(this._cloneNode(this.getNode(node.parent)), node.id);
2419
2298
  } else {
2420
2299
  if (isPredecessor) {
2421
2300
  let pointerPm;
@@ -2425,7 +2304,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2425
2304
  pointerKm = pointer.values.splice(-1)[0];
2426
2305
  node.keys = [pointerPm, ...node.keys];
2427
2306
  node.values = [guess, ...node.values];
2428
- parentNode = this.getNode(node.parent);
2307
+ parentNode = this._cloneNode(this.getNode(node.parent));
2429
2308
  const nodeIndex = parentNode.keys.indexOf(node.id);
2430
2309
  if (nodeIndex > 0) {
2431
2310
  parentNode.values[nodeIndex - 1] = pointerKm;
@@ -2436,7 +2315,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2436
2315
  pointerKm = pointer.values.splice(-1)[0];
2437
2316
  node.keys = [pointerPm, ...node.keys];
2438
2317
  node.values = [pointerKm, ...node.values];
2439
- parentNode = this.getNode(node.parent);
2318
+ parentNode = this._cloneNode(this.getNode(node.parent));
2440
2319
  const nodeIndex = parentNode.keys.indexOf(node.id);
2441
2320
  if (nodeIndex > 0) {
2442
2321
  parentNode.values[nodeIndex - 1] = pointerKm;
@@ -2453,7 +2332,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2453
2332
  pointerK0 = pointer.values.splice(0, 1)[0];
2454
2333
  node.keys = [...node.keys, pointerP0];
2455
2334
  node.values = [...node.values, guess];
2456
- parentNode = this.getNode(node.parent);
2335
+ parentNode = this._cloneNode(this.getNode(node.parent));
2457
2336
  const pointerIndex = parentNode.keys.indexOf(pointer.id);
2458
2337
  if (pointerIndex > 0) {
2459
2338
  parentNode.values[pointerIndex - 1] = pointerK0;
@@ -2464,7 +2343,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2464
2343
  pointerK0 = pointer.values.splice(0, 1)[0];
2465
2344
  node.keys = [...node.keys, pointerP0];
2466
2345
  node.values = [...node.values, pointerK0];
2467
- parentNode = this.getNode(node.parent);
2346
+ parentNode = this._cloneNode(this.getNode(node.parent));
2468
2347
  const pointerIndex = parentNode.keys.indexOf(pointer.id);
2469
2348
  if (pointerIndex > 0) {
2470
2349
  parentNode.values[pointerIndex - 1] = pointer.values[0];
@@ -2476,21 +2355,21 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2476
2355
  }
2477
2356
  if (!pointer.leaf) {
2478
2357
  for (const key2 of pointer.keys) {
2479
- const n = this.getNode(key2);
2358
+ const n = this._cloneNode(this.getNode(key2));
2480
2359
  n.parent = pointer.id;
2481
2360
  this._updateNode(n);
2482
2361
  }
2483
2362
  }
2484
2363
  if (!node.leaf) {
2485
2364
  for (const key2 of node.keys) {
2486
- const n = this.getNode(key2);
2365
+ const n = this._cloneNode(this.getNode(key2));
2487
2366
  n.parent = node.id;
2488
2367
  this._updateNode(n);
2489
2368
  }
2490
2369
  }
2491
2370
  if (!parentNode.leaf) {
2492
2371
  for (const key2 of parentNode.keys) {
2493
- const n = this.getNode(key2);
2372
+ const n = this._cloneNode(this.getNode(key2));
2494
2373
  n.parent = parentNode.id;
2495
2374
  this._updateNode(n);
2496
2375
  }
@@ -2499,10 +2378,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2499
2378
  } else {
2500
2379
  this._updateNode(node);
2501
2380
  }
2381
+ return node;
2502
2382
  }
2503
2383
  delete(key, value) {
2504
2384
  let node = this.insertableNodeByPrimary(value);
2505
2385
  let found = false;
2386
+ node = this._cloneNode(node);
2506
2387
  while (true) {
2507
2388
  let i = node.values.length;
2508
2389
  while (i--) {
@@ -2517,7 +2398,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2517
2398
  node.values.splice(i, 1);
2518
2399
  }
2519
2400
  this._updateNode(node);
2520
- this._deleteEntry(node, key);
2401
+ node = this._deleteEntry(node, key);
2521
2402
  found = true;
2522
2403
  break;
2523
2404
  }
@@ -2560,15 +2441,6 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2560
2441
  }
2561
2442
  }
2562
2443
  if (result.success) {
2563
- for (const r of result.created) {
2564
- this.nodes.set(r.key, r.data);
2565
- }
2566
- for (const r of result.updated) {
2567
- this.nodes.set(r.key, r.data);
2568
- }
2569
- for (const r of result.deleted) {
2570
- this.nodes.delete(r.key);
2571
- }
2572
2444
  }
2573
2445
  }
2574
2446
  return result;
@@ -2616,7 +2488,9 @@ var BPTreeMVCCStrategySync = class extends SyncMVCCStrategy {
2616
2488
  // src/BPTreeSync.ts
2617
2489
  var BPTreeSync = class extends BPTreeSyncTransaction {
2618
2490
  constructor(strategy, comparator, option) {
2619
- const mvccRoot = new SyncMVCCTransaction(new BPTreeMVCCStrategySync(strategy));
2491
+ const mvccRoot = new SyncMVCCTransaction(new BPTreeMVCCStrategySync(strategy), {
2492
+ cacheCapacity: option?.capacity ?? void 0
2493
+ });
2620
2494
  super(
2621
2495
  null,
2622
2496
  mvccRoot,
@@ -2943,9 +2817,6 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2943
2817
  });
2944
2818
  }
2945
2819
  async getNode(id) {
2946
- if (this.nodes.has(id)) {
2947
- return this.nodes.get(id);
2948
- }
2949
2820
  return await this.mvcc.read(id);
2950
2821
  }
2951
2822
  /**
@@ -2962,66 +2833,77 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2962
2833
  next,
2963
2834
  prev
2964
2835
  };
2836
+ const head = await this._readHead();
2837
+ if (head) {
2838
+ await this._writeHead({
2839
+ root: head.root,
2840
+ order: head.order,
2841
+ data: this.strategy.head.data
2842
+ });
2843
+ }
2965
2844
  await this.mvcc.create(id, node);
2966
- this.nodes.set(id, node);
2967
2845
  return node;
2968
2846
  }
2969
2847
  async _updateNode(node) {
2970
- await this.mvcc.write(node.id, node);
2971
- this.nodes.set(node.id, node);
2848
+ if (this.mvcc.isDeleted(node.id)) {
2849
+ return;
2850
+ }
2851
+ await this.mvcc.write(node.id, this._cloneNode(node));
2972
2852
  }
2973
2853
  async _deleteNode(node) {
2854
+ if (this.mvcc.isDeleted(node.id)) {
2855
+ return;
2856
+ }
2974
2857
  await this.mvcc.delete(node.id);
2975
- this.nodes.delete(node.id);
2976
2858
  }
2977
2859
  async _readHead() {
2978
- if (this.nodes.has("__HEAD__")) {
2979
- return this.nodes.get("__HEAD__") ?? null;
2980
- }
2981
- const head = await this.mvcc.read("__HEAD__");
2982
- return head ?? null;
2860
+ return await this.mvcc.read("__HEAD__");
2983
2861
  }
2984
2862
  async _writeHead(head) {
2985
2863
  if (!await this.mvcc.exists("__HEAD__")) {
2986
- await this.mvcc.create("__HEAD__", head);
2864
+ await this.mvcc.create("__HEAD__", this._cloneNode(head));
2987
2865
  } else {
2988
- await this.mvcc.write("__HEAD__", head);
2866
+ await this.mvcc.write("__HEAD__", this._cloneNode(head));
2989
2867
  }
2990
- this.nodes.set("__HEAD__", head);
2991
2868
  this.rootId = head.root;
2992
2869
  }
2993
2870
  async _insertAtLeaf(node, key, value) {
2994
- if (node.values.length) {
2995
- for (let i = 0, len = node.values.length; i < len; i++) {
2996
- const nValue = node.values[i];
2871
+ let leaf = node;
2872
+ leaf = this._cloneNode(leaf);
2873
+ if (leaf.values.length) {
2874
+ for (let i = 0, len = leaf.values.length; i < len; i++) {
2875
+ const nValue = leaf.values[i];
2997
2876
  if (this.comparator.isSame(value, nValue)) {
2998
- const keys = node.keys[i];
2877
+ const keys = leaf.keys[i];
2999
2878
  if (keys.includes(key)) {
3000
2879
  break;
3001
2880
  }
3002
2881
  keys.push(key);
3003
- await this._updateNode(node);
3004
- return;
2882
+ await this._updateNode(leaf);
2883
+ return leaf;
3005
2884
  } else if (this.comparator.isLower(value, nValue)) {
3006
- node.values.splice(i, 0, value);
3007
- node.keys.splice(i, 0, [key]);
3008
- await this._updateNode(node);
3009
- return;
3010
- } else if (i + 1 === node.values.length) {
3011
- node.values.push(value);
3012
- node.keys.push([key]);
3013
- await this._updateNode(node);
3014
- return;
2885
+ leaf.values.splice(i, 0, value);
2886
+ leaf.keys.splice(i, 0, [key]);
2887
+ await this._updateNode(leaf);
2888
+ return leaf;
2889
+ } else if (i + 1 === leaf.values.length) {
2890
+ leaf.values.push(value);
2891
+ leaf.keys.push([key]);
2892
+ await this._updateNode(leaf);
2893
+ return leaf;
3015
2894
  }
3016
2895
  }
3017
2896
  } else {
3018
- node.values = [value];
3019
- node.keys = [[key]];
3020
- await this._updateNode(node);
3021
- return;
2897
+ leaf.values = [value];
2898
+ leaf.keys = [[key]];
2899
+ await this._updateNode(leaf);
2900
+ return leaf;
3022
2901
  }
2902
+ return leaf;
3023
2903
  }
3024
2904
  async _insertInParent(node, value, pointer) {
2905
+ node = this._cloneNode(node);
2906
+ pointer = this._cloneNode(pointer);
3025
2907
  if (this.rootId === node.id) {
3026
2908
  const root = await this._createNode(false, [node.id, pointer.id], [value]);
3027
2909
  this.rootId = root.id;
@@ -3040,7 +2922,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3040
2922
  await this._updateNode(pointer);
3041
2923
  return;
3042
2924
  }
3043
- const parentNode = await this.getNode(node.parent);
2925
+ const parentNode = this._cloneNode(await this.getNode(node.parent));
3044
2926
  const nodeIndex = parentNode.keys.indexOf(node.id);
3045
2927
  if (nodeIndex === -1) {
3046
2928
  throw new Error(`Node ${node.id} not found in parent ${parentNode.id}`);
@@ -3056,7 +2938,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3056
2938
  leftSibling.next = pointer.id;
3057
2939
  await this._updateNode(leftSibling);
3058
2940
  if (oldNextId) {
3059
- const oldNext = await this.getNode(oldNextId);
2941
+ const oldNext = this._cloneNode(await this.getNode(oldNextId));
3060
2942
  oldNext.prev = pointer.id;
3061
2943
  await this._updateNode(oldNext);
3062
2944
  }
@@ -3073,12 +2955,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3073
2955
  parentNode.values = parentNode.values.slice(0, mid);
3074
2956
  parentNode.keys = parentNode.keys.slice(0, mid + 1);
3075
2957
  for (const k of parentNode.keys) {
3076
- const n = await this.getNode(k);
2958
+ const n = this._cloneNode(await this.getNode(k));
3077
2959
  n.parent = parentNode.id;
3078
2960
  await this._updateNode(n);
3079
2961
  }
3080
2962
  for (const k of parentPointer.keys) {
3081
- const n = await this.getNode(k);
2963
+ const n = this._cloneNode(await this.getNode(k));
3082
2964
  n.parent = parentPointer.id;
3083
2965
  await this._updateNode(n);
3084
2966
  }
@@ -3272,21 +3154,6 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3272
3154
  }
3273
3155
  return false;
3274
3156
  }
3275
- async forceUpdate(id) {
3276
- if (id) {
3277
- this.nodes.delete(id);
3278
- await this.getNode(id);
3279
- return 1;
3280
- }
3281
- const keys = Array.from(this.nodes.keys());
3282
- for (const key of keys) {
3283
- this.nodes.delete(key);
3284
- }
3285
- for (const key of keys) {
3286
- await this.getNode(key);
3287
- }
3288
- return keys.length;
3289
- }
3290
3157
  async get(key) {
3291
3158
  let node = await this.leftestNode();
3292
3159
  while (true) {
@@ -3373,10 +3240,10 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3373
3240
  }
3374
3241
  async insert(key, value) {
3375
3242
  return this.writeLock(0, async () => {
3376
- const before = await this.insertableNode(value);
3377
- await this._insertAtLeaf(before, key, value);
3243
+ let before = await this.insertableNode(value);
3244
+ before = await this._insertAtLeaf(before, key, value);
3378
3245
  if (before.values.length === this.order) {
3379
- const after = await this._createNode(
3246
+ let after = await this._createNode(
3380
3247
  true,
3381
3248
  [],
3382
3249
  [],
@@ -3385,15 +3252,19 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3385
3252
  null
3386
3253
  );
3387
3254
  const mid = Math.ceil(this.order / 2) - 1;
3255
+ after = this._cloneNode(after);
3388
3256
  after.values = before.values.slice(mid + 1);
3389
3257
  after.keys = before.keys.slice(mid + 1);
3390
3258
  before.values = before.values.slice(0, mid + 1);
3391
3259
  before.keys = before.keys.slice(0, mid + 1);
3260
+ await this._updateNode(before);
3261
+ await this._updateNode(after);
3392
3262
  await this._insertInParent(before, after.values[0], after);
3393
3263
  }
3394
3264
  });
3395
3265
  }
3396
3266
  async _deleteEntry(node, key) {
3267
+ node = this._cloneNode(node);
3397
3268
  if (!node.leaf) {
3398
3269
  let keyIndex = -1;
3399
3270
  for (let i = 0, len = node.keys.length; i < len; i++) {
@@ -3412,7 +3283,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3412
3283
  if (this.rootId === node.id && node.keys.length === 1 && !node.leaf) {
3413
3284
  const keys = node.keys;
3414
3285
  this._deleteNode(node);
3415
- const newRoot = await this.getNode(keys[0]);
3286
+ const newRoot = this._cloneNode(await this.getNode(keys[0]));
3416
3287
  newRoot.parent = null;
3417
3288
  await this._updateNode(newRoot);
3418
3289
  await this._writeHead({
@@ -3420,14 +3291,17 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3420
3291
  order: this.order,
3421
3292
  data: this.strategy.head.data
3422
3293
  });
3423
- return;
3294
+ return node;
3424
3295
  } else if (this.rootId === node.id) {
3425
- const root = await this.getNode(this.rootId);
3426
- await this._updateNode(root);
3427
- return;
3296
+ await this._writeHead({
3297
+ root: node.id,
3298
+ order: this.order,
3299
+ data: this.strategy.head.data
3300
+ });
3301
+ return node;
3428
3302
  } else if (node.keys.length < Math.ceil(this.order / 2) && !node.leaf || node.values.length < Math.ceil((this.order - 1) / 2) && node.leaf) {
3429
3303
  if (node.parent === null) {
3430
- return;
3304
+ return node;
3431
3305
  }
3432
3306
  let isPredecessor = false;
3433
3307
  let parentNode = await this.getNode(node.parent);
@@ -3439,11 +3313,11 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3439
3313
  const nKey = parentNode.keys[i];
3440
3314
  if (nKey === node.id) {
3441
3315
  if (i > 0) {
3442
- prevNode = await this.getNode(parentNode.keys[i - 1]);
3316
+ prevNode = this._cloneNode(await this.getNode(parentNode.keys[i - 1]));
3443
3317
  prevValue = parentNode.values[i - 1];
3444
3318
  }
3445
3319
  if (i < parentNode.keys.length - 1) {
3446
- nextNode = await this.getNode(parentNode.keys[i + 1]);
3320
+ nextNode = this._cloneNode(await this.getNode(parentNode.keys[i + 1]));
3447
3321
  postValue = parentNode.values[i];
3448
3322
  }
3449
3323
  }
@@ -3468,7 +3342,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3468
3342
  }
3469
3343
  }
3470
3344
  if (!pointer) {
3471
- return;
3345
+ return node;
3472
3346
  }
3473
3347
  if (node.values.length + pointer.values.length < this.order) {
3474
3348
  if (!isPredecessor) {
@@ -3482,7 +3356,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3482
3356
  } else {
3483
3357
  pointer.next = node.next;
3484
3358
  if (pointer.next) {
3485
- const n = await this.getNode(pointer.next);
3359
+ const n = this._cloneNode(await this.getNode(pointer.next));
3486
3360
  n.prev = pointer.id;
3487
3361
  await this._updateNode(n);
3488
3362
  }
@@ -3491,14 +3365,14 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3491
3365
  if (!pointer.leaf) {
3492
3366
  const keys = pointer.keys;
3493
3367
  for (const key2 of keys) {
3494
- const node2 = await this.getNode(key2);
3368
+ const node2 = this._cloneNode(await this.getNode(key2));
3495
3369
  node2.parent = pointer.id;
3496
3370
  await this._updateNode(node2);
3497
3371
  }
3498
3372
  }
3499
3373
  this._deleteNode(node);
3500
3374
  await this._updateNode(pointer);
3501
- await this._deleteEntry(await this.getNode(node.parent), node.id);
3375
+ await this._deleteEntry(this._cloneNode(await this.getNode(node.parent)), node.id);
3502
3376
  } else {
3503
3377
  if (isPredecessor) {
3504
3378
  let pointerPm;
@@ -3508,7 +3382,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3508
3382
  pointerKm = pointer.values.splice(-1)[0];
3509
3383
  node.keys = [pointerPm, ...node.keys];
3510
3384
  node.values = [guess, ...node.values];
3511
- parentNode = await this.getNode(node.parent);
3385
+ parentNode = this._cloneNode(await this.getNode(node.parent));
3512
3386
  const nodeIndex = parentNode.keys.indexOf(node.id);
3513
3387
  if (nodeIndex > 0) {
3514
3388
  parentNode.values[nodeIndex - 1] = pointerKm;
@@ -3519,7 +3393,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3519
3393
  pointerKm = pointer.values.splice(-1)[0];
3520
3394
  node.keys = [pointerPm, ...node.keys];
3521
3395
  node.values = [pointerKm, ...node.values];
3522
- parentNode = await this.getNode(node.parent);
3396
+ parentNode = this._cloneNode(await this.getNode(node.parent));
3523
3397
  const nodeIndex = parentNode.keys.indexOf(node.id);
3524
3398
  if (nodeIndex > 0) {
3525
3399
  parentNode.values[nodeIndex - 1] = pointerKm;
@@ -3536,7 +3410,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3536
3410
  pointerK0 = pointer.values.splice(0, 1)[0];
3537
3411
  node.keys = [...node.keys, pointerP0];
3538
3412
  node.values = [...node.values, guess];
3539
- parentNode = await this.getNode(node.parent);
3413
+ parentNode = this._cloneNode(await this.getNode(node.parent));
3540
3414
  const pointerIndex = parentNode.keys.indexOf(pointer.id);
3541
3415
  if (pointerIndex > 0) {
3542
3416
  parentNode.values[pointerIndex - 1] = pointerK0;
@@ -3547,7 +3421,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3547
3421
  pointerK0 = pointer.values.splice(0, 1)[0];
3548
3422
  node.keys = [...node.keys, pointerP0];
3549
3423
  node.values = [...node.values, pointerK0];
3550
- parentNode = await this.getNode(node.parent);
3424
+ parentNode = this._cloneNode(await this.getNode(node.parent));
3551
3425
  const pointerIndex = parentNode.keys.indexOf(pointer.id);
3552
3426
  if (pointerIndex > 0) {
3553
3427
  parentNode.values[pointerIndex - 1] = pointer.values[0];
@@ -3559,21 +3433,21 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3559
3433
  }
3560
3434
  if (!pointer.leaf) {
3561
3435
  for (const key2 of pointer.keys) {
3562
- const n = await this.getNode(key2);
3436
+ const n = this._cloneNode(await this.getNode(key2));
3563
3437
  n.parent = pointer.id;
3564
3438
  await this._updateNode(n);
3565
3439
  }
3566
3440
  }
3567
3441
  if (!node.leaf) {
3568
3442
  for (const key2 of node.keys) {
3569
- const n = await this.getNode(key2);
3443
+ const n = this._cloneNode(await this.getNode(key2));
3570
3444
  n.parent = node.id;
3571
3445
  await this._updateNode(n);
3572
3446
  }
3573
3447
  }
3574
3448
  if (!parentNode.leaf) {
3575
3449
  for (const key2 of parentNode.keys) {
3576
- const n = await this.getNode(key2);
3450
+ const n = this._cloneNode(await this.getNode(key2));
3577
3451
  n.parent = parentNode.id;
3578
3452
  await this._updateNode(n);
3579
3453
  }
@@ -3582,11 +3456,14 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3582
3456
  } else {
3583
3457
  await this._updateNode(node);
3584
3458
  }
3459
+ await this._updateNode(node);
3460
+ return node;
3585
3461
  }
3586
3462
  async delete(key, value) {
3587
3463
  return this.writeLock(0, async () => {
3588
3464
  let node = await this.insertableNodeByPrimary(value);
3589
3465
  let found = false;
3466
+ node = this._cloneNode(node);
3590
3467
  while (true) {
3591
3468
  let i = node.values.length;
3592
3469
  while (i--) {
@@ -3601,7 +3478,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3601
3478
  node.values.splice(i, 1);
3602
3479
  }
3603
3480
  await this._updateNode(node);
3604
- await this._deleteEntry(node, key);
3481
+ node = await this._deleteEntry(node, key);
3605
3482
  found = true;
3606
3483
  break;
3607
3484
  }
@@ -3644,21 +3521,10 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3644
3521
  this.rootTx.rootId = this.rootId;
3645
3522
  }
3646
3523
  }
3647
- if (result.success) {
3648
- for (const r of result.created) {
3649
- this.nodes.set(r.key, r.data);
3650
- }
3651
- for (const r of result.updated) {
3652
- this.nodes.set(r.key, r.data);
3653
- }
3654
- for (const r of result.deleted) {
3655
- this.nodes.delete(r.key);
3656
- }
3657
- }
3658
3524
  }
3659
3525
  return result;
3660
3526
  }
3661
- rollback() {
3527
+ async rollback() {
3662
3528
  return this.mvcc.rollback();
3663
3529
  }
3664
3530
  };
@@ -3701,7 +3567,9 @@ var BPTreeMVCCStrategyAsync = class extends AsyncMVCCStrategy {
3701
3567
  // src/BPTreeAsync.ts
3702
3568
  var BPTreeAsync = class extends BPTreeAsyncTransaction {
3703
3569
  constructor(strategy, comparator, option) {
3704
- const mvccRoot = new AsyncMVCCTransaction(new BPTreeMVCCStrategyAsync(strategy));
3570
+ const mvccRoot = new AsyncMVCCTransaction(new BPTreeMVCCStrategyAsync(strategy), {
3571
+ cacheCapacity: option?.capacity ?? void 0
3572
+ });
3705
3573
  super(
3706
3574
  null,
3707
3575
  mvccRoot,