serializable-bptree 8.4.0 → 8.4.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/README.md CHANGED
@@ -84,6 +84,7 @@ Additionally, this library supports asynchronous operations and rule-based query
84
84
  - **Async/Sync Support**: Provides both synchronous and asynchronous APIs.
85
85
  - **Query Optimization**: Rule-based optimizer to choose the best index for complex queries.
86
86
  - **TypeScript**: Fully typed for a better developer experience.
87
+ - **Auto Rollback**: Automatically cleans up internal memory buffers on commit failures, preventing memory leaks without manual intervention.
87
88
 
88
89
  ## How to use
89
90
 
@@ -413,11 +413,7 @@ var MVCCTransaction = class {
413
413
  */
414
414
  rollback() {
415
415
  const { created, updated, deleted } = this.getResultEntries();
416
- this.writeBuffer.clear();
417
- this.deleteBuffer.clear();
418
- this.createdKeys.clear();
419
- this.deletedValues.clear();
420
- this.originallyExisted.clear();
416
+ this._cleanupAll();
421
417
  this.committed = true;
422
418
  if (this.root !== this) {
423
419
  this.root.activeTransactions.delete(this);
@@ -453,6 +449,19 @@ var MVCCTransaction = class {
453
449
  }
454
450
  return Array.from(conflicts);
455
451
  }
452
+ /**
453
+ * Cleans up all buffers and history.
454
+ * This method is called by the commit method.
455
+ */
456
+ _cleanupAll() {
457
+ this.writeBuffer.clear();
458
+ this.deleteBuffer.clear();
459
+ this.createdKeys.clear();
460
+ this.deletedValues.clear();
461
+ this.originallyExisted.clear();
462
+ this.keyVersions.clear();
463
+ this.bufferHistory.clear();
464
+ }
456
465
  /**
457
466
  * Cleans up both deletedCache and versionIndex based on minActiveVersion.
458
467
  * Root transactions call this after commit to reclaim memory.
@@ -644,6 +653,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
644
653
  if (this.parent) {
645
654
  const failure = this.parent._merge(this);
646
655
  if (failure) {
656
+ this.rollback();
647
657
  return {
648
658
  label,
649
659
  success: false,
@@ -654,11 +664,13 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
654
664
  deleted
655
665
  };
656
666
  }
667
+ this._cleanupAll();
657
668
  this.committed = true;
658
669
  } else {
659
670
  if (this.writeBuffer.size > 0 || this.deleteBuffer.size > 0) {
660
671
  const failure = this._merge(this);
661
672
  if (failure) {
673
+ this.rollback();
662
674
  return {
663
675
  label,
664
676
  success: false,
@@ -669,13 +681,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
669
681
  deleted: []
670
682
  };
671
683
  }
672
- this.writeBuffer.clear();
673
- this.deleteBuffer.clear();
674
- this.createdKeys.clear();
675
- this.deletedValues.clear();
676
- this.originallyExisted.clear();
677
- this.keyVersions.clear();
678
- this.bufferHistory.clear();
684
+ this._cleanupAll();
679
685
  this.localVersion = 0;
680
686
  this.snapshotVersion = this.version;
681
687
  }
@@ -1309,6 +1315,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1309
1315
  if (this.parent) {
1310
1316
  const failure = await this.parent._merge(this);
1311
1317
  if (failure) {
1318
+ this.rollback();
1312
1319
  return {
1313
1320
  label,
1314
1321
  success: false,
@@ -1319,11 +1326,13 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1319
1326
  deleted
1320
1327
  };
1321
1328
  }
1329
+ this._cleanupAll();
1322
1330
  this.committed = true;
1323
1331
  } else {
1324
1332
  if (this.writeBuffer.size > 0 || this.deleteBuffer.size > 0) {
1325
1333
  const failure = await this._merge(this);
1326
1334
  if (failure) {
1335
+ this.rollback();
1327
1336
  return {
1328
1337
  label,
1329
1338
  success: false,
@@ -1334,13 +1343,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1334
1343
  deleted: []
1335
1344
  };
1336
1345
  }
1337
- this.writeBuffer.clear();
1338
- this.deleteBuffer.clear();
1339
- this.createdKeys.clear();
1340
- this.deletedValues.clear();
1341
- this.originallyExisted.clear();
1342
- this.keyVersions.clear();
1343
- this.bufferHistory.clear();
1346
+ this._cleanupAll();
1344
1347
  this.localVersion = 0;
1345
1348
  this.snapshotVersion = this.version;
1346
1349
  }
@@ -2899,8 +2902,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2899
2902
  result = this.rootTx.commit(label);
2900
2903
  if (result.success) {
2901
2904
  this.rootTx.rootId = this.rootId;
2905
+ } else {
2906
+ this.mvcc.rollback();
2902
2907
  }
2903
2908
  }
2909
+ } else {
2910
+ this.mvcc.rollback();
2904
2911
  }
2905
2912
  return result;
2906
2913
  }
@@ -4109,8 +4116,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
4109
4116
  result = await this.rootTx.commit(label);
4110
4117
  if (result.success) {
4111
4118
  this.rootTx.rootId = this.rootId;
4119
+ } else {
4120
+ this.mvcc.rollback();
4112
4121
  }
4113
4122
  }
4123
+ } else {
4124
+ this.mvcc.rollback();
4114
4125
  }
4115
4126
  return result;
4116
4127
  }
@@ -377,11 +377,7 @@ var MVCCTransaction = class {
377
377
  */
378
378
  rollback() {
379
379
  const { created, updated, deleted } = this.getResultEntries();
380
- this.writeBuffer.clear();
381
- this.deleteBuffer.clear();
382
- this.createdKeys.clear();
383
- this.deletedValues.clear();
384
- this.originallyExisted.clear();
380
+ this._cleanupAll();
385
381
  this.committed = true;
386
382
  if (this.root !== this) {
387
383
  this.root.activeTransactions.delete(this);
@@ -417,6 +413,19 @@ var MVCCTransaction = class {
417
413
  }
418
414
  return Array.from(conflicts);
419
415
  }
416
+ /**
417
+ * Cleans up all buffers and history.
418
+ * This method is called by the commit method.
419
+ */
420
+ _cleanupAll() {
421
+ this.writeBuffer.clear();
422
+ this.deleteBuffer.clear();
423
+ this.createdKeys.clear();
424
+ this.deletedValues.clear();
425
+ this.originallyExisted.clear();
426
+ this.keyVersions.clear();
427
+ this.bufferHistory.clear();
428
+ }
420
429
  /**
421
430
  * Cleans up both deletedCache and versionIndex based on minActiveVersion.
422
431
  * Root transactions call this after commit to reclaim memory.
@@ -608,6 +617,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
608
617
  if (this.parent) {
609
618
  const failure = this.parent._merge(this);
610
619
  if (failure) {
620
+ this.rollback();
611
621
  return {
612
622
  label,
613
623
  success: false,
@@ -618,11 +628,13 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
618
628
  deleted
619
629
  };
620
630
  }
631
+ this._cleanupAll();
621
632
  this.committed = true;
622
633
  } else {
623
634
  if (this.writeBuffer.size > 0 || this.deleteBuffer.size > 0) {
624
635
  const failure = this._merge(this);
625
636
  if (failure) {
637
+ this.rollback();
626
638
  return {
627
639
  label,
628
640
  success: false,
@@ -633,13 +645,7 @@ var SyncMVCCTransaction = class _SyncMVCCTransaction extends MVCCTransaction {
633
645
  deleted: []
634
646
  };
635
647
  }
636
- this.writeBuffer.clear();
637
- this.deleteBuffer.clear();
638
- this.createdKeys.clear();
639
- this.deletedValues.clear();
640
- this.originallyExisted.clear();
641
- this.keyVersions.clear();
642
- this.bufferHistory.clear();
648
+ this._cleanupAll();
643
649
  this.localVersion = 0;
644
650
  this.snapshotVersion = this.version;
645
651
  }
@@ -1273,6 +1279,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1273
1279
  if (this.parent) {
1274
1280
  const failure = await this.parent._merge(this);
1275
1281
  if (failure) {
1282
+ this.rollback();
1276
1283
  return {
1277
1284
  label,
1278
1285
  success: false,
@@ -1283,11 +1290,13 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1283
1290
  deleted
1284
1291
  };
1285
1292
  }
1293
+ this._cleanupAll();
1286
1294
  this.committed = true;
1287
1295
  } else {
1288
1296
  if (this.writeBuffer.size > 0 || this.deleteBuffer.size > 0) {
1289
1297
  const failure = await this._merge(this);
1290
1298
  if (failure) {
1299
+ this.rollback();
1291
1300
  return {
1292
1301
  label,
1293
1302
  success: false,
@@ -1298,13 +1307,7 @@ var AsyncMVCCTransaction = class _AsyncMVCCTransaction extends MVCCTransaction {
1298
1307
  deleted: []
1299
1308
  };
1300
1309
  }
1301
- this.writeBuffer.clear();
1302
- this.deleteBuffer.clear();
1303
- this.createdKeys.clear();
1304
- this.deletedValues.clear();
1305
- this.originallyExisted.clear();
1306
- this.keyVersions.clear();
1307
- this.bufferHistory.clear();
1310
+ this._cleanupAll();
1308
1311
  this.localVersion = 0;
1309
1312
  this.snapshotVersion = this.version;
1310
1313
  }
@@ -2863,8 +2866,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2863
2866
  result = this.rootTx.commit(label);
2864
2867
  if (result.success) {
2865
2868
  this.rootTx.rootId = this.rootId;
2869
+ } else {
2870
+ this.mvcc.rollback();
2866
2871
  }
2867
2872
  }
2873
+ } else {
2874
+ this.mvcc.rollback();
2868
2875
  }
2869
2876
  return result;
2870
2877
  }
@@ -4073,8 +4080,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
4073
4080
  result = await this.rootTx.commit(label);
4074
4081
  if (result.success) {
4075
4082
  this.rootTx.rootId = this.rootId;
4083
+ } else {
4084
+ this.mvcc.rollback();
4076
4085
  }
4077
4086
  }
4087
+ } else {
4088
+ this.mvcc.rollback();
4078
4089
  }
4079
4090
  return result;
4080
4091
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serializable-bptree",
3
- "version": "8.4.0",
3
+ "version": "8.4.1",
4
4
  "description": "Store the B+tree flexibly, not only in-memory.",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "main": "./dist/cjs/index.cjs",
@@ -44,7 +44,7 @@
44
44
  "typescript": "^5.9.3"
45
45
  },
46
46
  "dependencies": {
47
- "mvcc-api": "^1.3.5",
47
+ "mvcc-api": "^1.3.6",
48
48
  "ryoiki": "^1.2.0"
49
49
  }
50
50
  }