dataply 0.0.9 → 0.0.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/dist/cjs/index.js CHANGED
@@ -5422,24 +5422,13 @@ var TextCodec = class _TextCodec {
5422
5422
  }
5423
5423
  };
5424
5424
 
5425
- // src/core/transaction/TxContext.ts
5426
- var import_node_async_hooks = require("node:async_hooks");
5427
- var storage = new import_node_async_hooks.AsyncLocalStorage();
5428
- var TxContext = {
5429
- run: (tx, callback) => {
5430
- return storage.run(tx, callback);
5431
- },
5432
- get: () => {
5433
- return storage.getStore();
5434
- }
5435
- };
5436
-
5437
5425
  // src/core/RowIndexStrategy.ts
5438
5426
  var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5439
- constructor(order, pfs) {
5427
+ constructor(order, pfs, txContext) {
5440
5428
  super(order);
5441
5429
  this.order = order;
5442
5430
  this.pfs = pfs;
5431
+ this.txContext = txContext;
5443
5432
  this.factory = new PageManagerFactory();
5444
5433
  this.indexPageManger = this.factory.getManagerFromType(PageManager.CONSTANT.PAGE_TYPE_INDEX);
5445
5434
  this.codec = new TextCodec();
@@ -5449,12 +5438,12 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5449
5438
  indexPageManger;
5450
5439
  codec;
5451
5440
  async id(isLeaf) {
5452
- const tx = TxContext.get();
5441
+ const tx = this.txContext.get();
5453
5442
  const pageId = await this.pfs.appendNewPage(PageManager.CONSTANT.PAGE_TYPE_INDEX, tx);
5454
5443
  return pageId.toString();
5455
5444
  }
5456
5445
  async read(id) {
5457
- const tx = TxContext.get();
5446
+ const tx = this.txContext.get();
5458
5447
  const pageId = +id;
5459
5448
  const page = await this.pfs.get(pageId, tx);
5460
5449
  const indexId = this.indexPageManger.getIndexId(page);
@@ -5486,7 +5475,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5486
5475
  return res;
5487
5476
  }
5488
5477
  async write(id, node) {
5489
- const tx = TxContext.get();
5478
+ const tx = this.txContext.get();
5490
5479
  const pageId = +id;
5491
5480
  const page = await this.pfs.get(pageId, tx);
5492
5481
  if (node.leaf) {
@@ -5521,7 +5510,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5521
5510
  await this.pfs.setPage(pageId, page, tx);
5522
5511
  }
5523
5512
  async delete(id) {
5524
- const tx = TxContext.get();
5513
+ const tx = this.txContext.get();
5525
5514
  const manager = this.factory.getManagerFromType(PageManager.CONSTANT.PAGE_TYPE_INDEX);
5526
5515
  let pageId = +id;
5527
5516
  while (true) {
@@ -5535,7 +5524,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5535
5524
  }
5536
5525
  }
5537
5526
  async readHead() {
5538
- const tx = TxContext.get();
5527
+ const tx = this.txContext.get();
5539
5528
  const metadataPage = await this.pfs.getMetadata(tx);
5540
5529
  const manager = this.factory.getManager(metadataPage);
5541
5530
  const rootIndexPageId = manager.getRootIndexPageId(metadataPage);
@@ -5549,7 +5538,7 @@ var RowIdentifierStrategy = class extends SerializeStrategyAsync {
5549
5538
  };
5550
5539
  }
5551
5540
  async writeHead(head) {
5552
- const tx = TxContext.get();
5541
+ const tx = this.txContext.get();
5553
5542
  const { root, order } = head;
5554
5543
  if (root === null) {
5555
5544
  throw new Error("");
@@ -5617,8 +5606,9 @@ var KeyManager = class {
5617
5606
 
5618
5607
  // src/core/RowTableEngine.ts
5619
5608
  var RowTableEngine = class {
5620
- constructor(pfs, options) {
5609
+ constructor(pfs, txContext, options) {
5621
5610
  this.pfs = pfs;
5611
+ this.txContext = txContext;
5622
5612
  this.options = options;
5623
5613
  this.factory = new PageManagerFactory();
5624
5614
  this.metadataPageManager = this.factory.getManagerFromType(MetadataPageManager.CONSTANT.PAGE_TYPE_METADATA);
@@ -5631,7 +5621,7 @@ var RowTableEngine = class {
5631
5621
  this.maxBodySize = this.pfs.pageSize - DataPageManager.CONSTANT.SIZE_PAGE_HEADER;
5632
5622
  this.order = this.getOptimalOrder(pfs.pageSize, IndexPageManager.CONSTANT.SIZE_KEY, IndexPageManager.CONSTANT.SIZE_VALUE);
5633
5623
  this.bptree = new BPTreeAsync(
5634
- new RowIdentifierStrategy(this.order, pfs),
5624
+ new RowIdentifierStrategy(this.order, pfs, txContext),
5635
5625
  new NumericComparator(),
5636
5626
  {
5637
5627
  capacity: this.options.pageCacheCapacity
@@ -6061,7 +6051,8 @@ var Transaction = class {
6061
6051
  * @param vfs VFS instance
6062
6052
  * @param lockManager LockManager instance
6063
6053
  */
6064
- constructor(id, vfs, lockManager) {
6054
+ constructor(id, context, vfs, lockManager) {
6055
+ this.context = context;
6065
6056
  this.vfs = vfs;
6066
6057
  this.lockManager = lockManager;
6067
6058
  this.id = id;
@@ -6170,7 +6161,7 @@ var Transaction = class {
6170
6161
  async commit() {
6171
6162
  await this.vfs.prepareCommit(this);
6172
6163
  await this.vfs.finalizeCommit(this);
6173
- await TxContext.run(this, async () => {
6164
+ await this.context.run(this, async () => {
6174
6165
  for (const hook of this.commitHooks) {
6175
6166
  await hook();
6176
6167
  }
@@ -6211,6 +6202,18 @@ var Transaction = class {
6211
6202
  }
6212
6203
  };
6213
6204
 
6205
+ // src/core/transaction/TxContext.ts
6206
+ var import_node_async_hooks = require("node:async_hooks");
6207
+ var TransactionContext = class {
6208
+ storage = new import_node_async_hooks.AsyncLocalStorage();
6209
+ run(tx, callback) {
6210
+ return this.storage.run(tx, callback);
6211
+ }
6212
+ get() {
6213
+ return this.storage.getStore();
6214
+ }
6215
+ };
6216
+
6214
6217
  // src/core/DataplyAPI.ts
6215
6218
  var DataplyAPI = class {
6216
6219
  constructor(file, options) {
@@ -6226,8 +6229,9 @@ var DataplyAPI = class {
6226
6229
  this.options.wal
6227
6230
  );
6228
6231
  this.textCodec = new TextCodec();
6232
+ this.txContext = new TransactionContext();
6229
6233
  this.lockManager = new LockManager();
6230
- this.rowTableEngine = new RowTableEngine(this.pfs, this.options);
6234
+ this.rowTableEngine = new RowTableEngine(this.pfs, this.txContext, this.options);
6231
6235
  this.initialized = false;
6232
6236
  this.txIdCounter = 0;
6233
6237
  }
@@ -6247,6 +6251,8 @@ var DataplyAPI = class {
6247
6251
  lockManager;
6248
6252
  /** Text codec. Used for encoding and decoding text data */
6249
6253
  textCodec;
6254
+ /** Transaction context */
6255
+ txContext;
6250
6256
  /** Hook */
6251
6257
  hook;
6252
6258
  /** Whether the database was initialized via `init()` */
@@ -6389,7 +6395,12 @@ var DataplyAPI = class {
6389
6395
  * @returns Transaction object
6390
6396
  */
6391
6397
  createTransaction() {
6392
- return new Transaction(++this.txIdCounter, this.pfs.vfsInstance, this.lockManager);
6398
+ return new Transaction(
6399
+ ++this.txIdCounter,
6400
+ this.txContext,
6401
+ this.pfs.vfsInstance,
6402
+ this.lockManager
6403
+ );
6393
6404
  }
6394
6405
  /**
6395
6406
  * Runs a callback function within a transaction context.
@@ -6405,7 +6416,7 @@ var DataplyAPI = class {
6405
6416
  if (!tx) {
6406
6417
  tx = this.createTransaction();
6407
6418
  }
6408
- const [error, result] = await catchPromise(TxContext.run(tx, () => callback(tx)));
6419
+ const [error, result] = await catchPromise(this.txContext.run(tx, () => callback(tx)));
6409
6420
  if (error) {
6410
6421
  if (isInternalTx) {
6411
6422
  await tx.rollback();
@@ -5,6 +5,7 @@ import { RowTableEngine } from './RowTableEngine';
5
5
  import { TextCodec } from '../utils/TextCodec';
6
6
  import { LockManager } from './transaction/LockManager';
7
7
  import { Transaction } from './transaction/Transaction';
8
+ import { TransactionContext } from './transaction/TxContext';
8
9
  interface DataplyAPIAsyncHook {
9
10
  init: (tx: Transaction, isNewlyCreated: boolean) => Promise<Transaction>;
10
11
  close: () => Promise<void>;
@@ -30,6 +31,8 @@ export declare class DataplyAPI {
30
31
  protected readonly lockManager: LockManager;
31
32
  /** Text codec. Used for encoding and decoding text data */
32
33
  protected readonly textCodec: TextCodec;
34
+ /** Transaction context */
35
+ protected readonly txContext: TransactionContext;
33
36
  /** Hook */
34
37
  protected readonly hook: IHookall<DataplyAPIAsyncHook>;
35
38
  /** Whether the database was initialized via `init()` */
@@ -3,14 +3,16 @@ import { SerializeStrategyAsync } from 'serializable-bptree';
3
3
  import { PageFileSystem } from './PageFileSystem';
4
4
  import { IndexPageManager, PageManagerFactory } from './Page';
5
5
  import { TextCodec } from '../utils/TextCodec';
6
+ import { TransactionContext } from './transaction/TxContext';
6
7
  export declare class RowIdentifierStrategy extends SerializeStrategyAsync<number, number> {
7
8
  readonly order: number;
8
9
  protected readonly pfs: PageFileSystem;
10
+ protected readonly txContext: TransactionContext;
9
11
  protected rootPageId: number;
10
12
  protected factory: PageManagerFactory;
11
13
  protected indexPageManger: IndexPageManager;
12
14
  protected codec: TextCodec;
13
- constructor(order: number, pfs: PageFileSystem);
15
+ constructor(order: number, pfs: PageFileSystem, txContext: TransactionContext);
14
16
  id(isLeaf: boolean): Promise<string>;
15
17
  read(id: string): Promise<BPTreeNode<number, number>>;
16
18
  write(id: string, node: BPTreeNode<number, number>): Promise<void>;
@@ -5,8 +5,10 @@ import { Row } from './Row';
5
5
  import { KeyManager } from './KeyManager';
6
6
  import { PageManagerFactory } from './Page';
7
7
  import { Transaction } from './transaction/Transaction';
8
+ import { TransactionContext } from './transaction/TxContext';
8
9
  export declare class RowTableEngine {
9
10
  protected readonly pfs: PageFileSystem;
11
+ protected readonly txContext: TransactionContext;
10
12
  protected readonly options: Required<DataplyOptions>;
11
13
  protected readonly bptree: BPTreeAsync<number, number>;
12
14
  protected readonly order: number;
@@ -20,7 +22,7 @@ export declare class RowTableEngine {
20
22
  private readonly ridBuffer;
21
23
  private readonly pageIdBuffer;
22
24
  private initialized;
23
- constructor(pfs: PageFileSystem, options: Required<DataplyOptions>);
25
+ constructor(pfs: PageFileSystem, txContext: TransactionContext, options: Required<DataplyOptions>);
24
26
  /**
25
27
  * Initializes the B+ Tree.
26
28
  */
@@ -1,12 +1,14 @@
1
1
  import { LockManager } from './LockManager';
2
2
  import { VirtualFileSystem } from '../VirtualFileSystem';
3
+ import { TransactionContext } from './TxContext';
3
4
  /**
4
5
  * Transaction class.
5
6
  * Manages the lifecycle and resources of a database transaction.
6
7
  */
7
8
  export declare class Transaction {
8
- private vfs;
9
- private lockManager;
9
+ readonly context: TransactionContext;
10
+ private readonly vfs;
11
+ private readonly lockManager;
10
12
  /** Transaction ID */
11
13
  readonly id: number;
12
14
  /** List of held lock IDs (LOCK_ID) */
@@ -26,7 +28,7 @@ export declare class Transaction {
26
28
  * @param vfs VFS instance
27
29
  * @param lockManager LockManager instance
28
30
  */
29
- constructor(id: number, vfs: VirtualFileSystem, lockManager: LockManager);
31
+ constructor(id: number, context: TransactionContext, vfs: VirtualFileSystem, lockManager: LockManager);
30
32
  /**
31
33
  * Registers a commit hook.
32
34
  * @param hook Function to execute
@@ -1,5 +1,6 @@
1
1
  import { Transaction } from './Transaction';
2
- export declare const TxContext: {
3
- run: <T>(tx: Transaction, callback: () => T) => T;
4
- get: () => Transaction | undefined;
5
- };
2
+ export declare class TransactionContext {
3
+ private readonly storage;
4
+ run<T>(tx: Transaction, callback: () => T): T;
5
+ get(): Transaction | undefined;
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
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>",