dataply 0.0.22-alpha.2 → 0.0.22-alpha.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.
Files changed (2) hide show
  1. package/dist/cjs/index.js +73 -22
  2. package/package.json +4 -4
package/dist/cjs/index.js CHANGED
@@ -375,6 +375,30 @@ var MVCCTransaction = class {
375
375
  deleted
376
376
  };
377
377
  }
378
+ /**
379
+ * Checks for conflicts among multiple transactions.
380
+ * A conflict occurs if two or more transactions modify (write or delete) the same key.
381
+ * @param transactions Array of transactions to check.
382
+ * @returns An array of keys that are in conflict.
383
+ */
384
+ static CheckConflicts(transactions) {
385
+ const modifiedKeys = /* @__PURE__ */ new Map();
386
+ const conflicts = /* @__PURE__ */ new Set();
387
+ for (const tx of transactions) {
388
+ const txModified = /* @__PURE__ */ new Set([
389
+ ...tx.writeBuffer.keys(),
390
+ ...tx.deleteBuffer
391
+ ]);
392
+ for (const key of txModified) {
393
+ const count = modifiedKeys.get(key) ?? 0;
394
+ if (count > 0) {
395
+ conflicts.add(key);
396
+ }
397
+ modifiedKeys.set(key, count + 1);
398
+ }
399
+ }
400
+ return Array.from(conflicts);
401
+ }
378
402
  /**
379
403
  * Cleans up both deletedCache and versionIndex based on minActiveVersion.
380
404
  * Root transactions call this after commit to reclaim memory.
@@ -1662,6 +1686,15 @@ var BPTreeTransaction = class _BPTreeTransaction {
1662
1686
  }
1663
1687
  return best;
1664
1688
  }
1689
+ /**
1690
+ * Checks for conflicts between multiple transactions.
1691
+ *
1692
+ * @param transactions Array of BPTreeTransaction instances to check
1693
+ * @returns An array of keys that are in conflict. Empty array if no conflicts.
1694
+ */
1695
+ static CheckConflicts(transactions) {
1696
+ return MVCCTransaction.CheckConflicts(transactions.map((tx) => tx.mvcc));
1697
+ }
1665
1698
  /**
1666
1699
  * Returns the ID of the root node.
1667
1700
  * @returns The root node ID.
@@ -2112,11 +2145,7 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2112
2145
  const { root, order } = head;
2113
2146
  this.strategy.head = head;
2114
2147
  this.order = order;
2115
- this._writeHead({
2116
- root,
2117
- order: this.order,
2118
- data: this.strategy.head.data
2119
- });
2148
+ this.rootId = root;
2120
2149
  }
2121
2150
  if (this.order < 3) {
2122
2151
  throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
@@ -2442,6 +2471,12 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2442
2471
  return node;
2443
2472
  }
2444
2473
  delete(key, value) {
2474
+ if (value === void 0) {
2475
+ value = this.get(key);
2476
+ }
2477
+ if (value === void 0) {
2478
+ return;
2479
+ }
2445
2480
  let node = this.insertableNodeByPrimary(value);
2446
2481
  let found = false;
2447
2482
  while (true) {
@@ -2495,15 +2530,13 @@ var BPTreeSyncTransaction = class extends BPTreeTransaction {
2495
2530
  commit(label) {
2496
2531
  let result = this.mvcc.commit(label);
2497
2532
  if (result.success) {
2498
- const isRootTx = this.rootTx !== this;
2499
- if (isRootTx) {
2533
+ const isRootTx = this.rootTx === this;
2534
+ if (!isRootTx) {
2500
2535
  result = this.rootTx.commit(label);
2501
2536
  if (result.success) {
2502
2537
  this.rootTx.rootId = this.rootId;
2503
2538
  }
2504
2539
  }
2505
- if (result.success) {
2506
- }
2507
2540
  }
2508
2541
  return result;
2509
2542
  }
@@ -2887,14 +2920,6 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
2887
2920
  next,
2888
2921
  prev
2889
2922
  };
2890
- const head = await this._readHead();
2891
- if (head) {
2892
- await this._writeHead({
2893
- root: head.root,
2894
- order: head.order,
2895
- data: this.strategy.head.data
2896
- });
2897
- }
2898
2923
  await this.mvcc.create(id, node);
2899
2924
  return node;
2900
2925
  }
@@ -3187,11 +3212,7 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3187
3212
  const { root, order } = head;
3188
3213
  this.strategy.head = head;
3189
3214
  this.order = order;
3190
- await this._writeHead({
3191
- root,
3192
- order: this.order,
3193
- data: this.strategy.head.data
3194
- });
3215
+ this.rootId = root;
3195
3216
  }
3196
3217
  if (this.order < 3) {
3197
3218
  throw new Error(`The 'order' parameter must be greater than 2. but got a '${this.order}'.`);
@@ -3520,6 +3541,12 @@ var BPTreeAsyncTransaction = class extends BPTreeTransaction {
3520
3541
  }
3521
3542
  async delete(key, value) {
3522
3543
  return this.writeLock(0, async () => {
3544
+ if (value === void 0) {
3545
+ value = await this.get(key);
3546
+ }
3547
+ if (value === void 0) {
3548
+ return;
3549
+ }
3523
3550
  let node = await this.insertableNodeByPrimary(value);
3524
3551
  let found = false;
3525
3552
  while (true) {
@@ -4761,6 +4788,30 @@ var MVCCTransaction2 = class {
4761
4788
  deleted
4762
4789
  };
4763
4790
  }
4791
+ /**
4792
+ * Checks for conflicts among multiple transactions.
4793
+ * A conflict occurs if two or more transactions modify (write or delete) the same key.
4794
+ * @param transactions Array of transactions to check.
4795
+ * @returns An array of keys that are in conflict.
4796
+ */
4797
+ static CheckConflicts(transactions) {
4798
+ const modifiedKeys = /* @__PURE__ */ new Map();
4799
+ const conflicts = /* @__PURE__ */ new Set();
4800
+ for (const tx of transactions) {
4801
+ const txModified = /* @__PURE__ */ new Set([
4802
+ ...tx.writeBuffer.keys(),
4803
+ ...tx.deleteBuffer
4804
+ ]);
4805
+ for (const key of txModified) {
4806
+ const count = modifiedKeys.get(key) ?? 0;
4807
+ if (count > 0) {
4808
+ conflicts.add(key);
4809
+ }
4810
+ modifiedKeys.set(key, count + 1);
4811
+ }
4812
+ }
4813
+ return Array.from(conflicts);
4814
+ }
4764
4815
  /**
4765
4816
  * Cleans up both deletedCache and versionIndex based on minActiveVersion.
4766
4817
  * Root transactions call this after commit to reclaim memory.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.22-alpha.2",
3
+ "version": "0.0.22-alpha.4",
4
4
  "description": "A lightweight storage engine for Node.js with support for MVCC, WAL.",
5
5
  "license": "MIT",
6
6
  "author": "izure <admin@izure.org>",
@@ -47,8 +47,8 @@
47
47
  "dependencies": {
48
48
  "cache-entanglement": "^1.7.1",
49
49
  "hookall": "^2.2.0",
50
- "mvcc-api": "^1.3.3",
50
+ "mvcc-api": "^1.3.4",
51
51
  "ryoiki": "^1.2.0",
52
- "serializable-bptree": "^8.1.5"
52
+ "serializable-bptree": "^8.1.7"
53
53
  }
54
- }
54
+ }