dataply 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -4626,9 +4626,6 @@ var HookallSync = class _HookallSync {
4626
4626
  return value;
4627
4627
  }
4628
4628
  };
4629
- function useHookallSync(target = HookallSync.Global) {
4630
- return new HookallSync(target);
4631
- }
4632
4629
 
4633
4630
  // src/core/VirtualFileSystem.ts
4634
4631
  var import_node_fs2 = __toESM(require("node:fs"));
@@ -6218,11 +6215,9 @@ var Transaction = class {
6218
6215
  var DataplyAPI = class {
6219
6216
  constructor(file, options) {
6220
6217
  this.file = file;
6221
- this.hook = {
6222
- sync: useHookallSync(this),
6223
- async: useHookall(this)
6224
- };
6218
+ this.hook = useHookall(this);
6225
6219
  this.options = this.verboseOptions(options);
6220
+ this.isNewlyCreated = !import_node_fs3.default.existsSync(file);
6226
6221
  this.fileHandle = this.createOrOpen(file, this.options);
6227
6222
  this.pfs = new PageFileSystem(
6228
6223
  this.fileHandle,
@@ -6236,14 +6231,28 @@ var DataplyAPI = class {
6236
6231
  this.initialized = false;
6237
6232
  this.txIdCounter = 0;
6238
6233
  }
6234
+ /**
6235
+ * These are not the same options that were used when the database was created.
6236
+ * They are simply the options received when the instance was created.
6237
+ * If you want to retrieve the options used during database creation, use `getMetadata()` instead.
6238
+ */
6239
6239
  options;
6240
+ /** File handle. Database file descriptor */
6240
6241
  fileHandle;
6242
+ /** Page file system. Used for managing pages. If you know what it is, you can skip this. */
6241
6243
  pfs;
6244
+ /** Row table engine. Used for managing rows. If you know what it is, you can skip this. */
6242
6245
  rowTableEngine;
6246
+ /** Lock manager. Used for managing transactions */
6243
6247
  lockManager;
6248
+ /** Text codec. Used for encoding and decoding text data */
6244
6249
  textCodec;
6250
+ /** Hook */
6245
6251
  hook;
6252
+ /** Whether the database was initialized via `init()` */
6246
6253
  initialized;
6254
+ /** Whether the database was created this time. */
6255
+ isNewlyCreated;
6247
6256
  txIdCounter;
6248
6257
  /**
6249
6258
  * Verifies if the page file is a valid Dataply file.
@@ -6280,47 +6289,45 @@ var DataplyAPI = class {
6280
6289
  * @param fileHandle File handle
6281
6290
  */
6282
6291
  initializeFile(file, fileHandle, options) {
6283
- this.hook.sync.trigger("create", void 0, () => {
6284
- const metadataPageManager = new MetadataPageManager();
6285
- const bitmapPageManager = new BitmapPageManager();
6286
- const dataPageManager = new DataPageManager();
6287
- const metadataPage = new Uint8Array(options.pageSize);
6288
- const dataPage = new Uint8Array(options.pageSize);
6289
- metadataPageManager.initial(
6290
- metadataPage,
6291
- MetadataPageManager.CONSTANT.PAGE_TYPE_METADATA,
6292
- 0,
6293
- 0,
6294
- options.pageSize - MetadataPageManager.CONSTANT.SIZE_PAGE_HEADER
6295
- );
6296
- metadataPageManager.setMagicString(metadataPage);
6297
- metadataPageManager.setPageSize(metadataPage, options.pageSize);
6298
- metadataPageManager.setRootIndexPageId(metadataPage, -1);
6299
- metadataPageManager.setBitmapPageId(metadataPage, 1);
6300
- metadataPageManager.setLastInsertPageId(metadataPage, 2);
6301
- metadataPageManager.setPageCount(metadataPage, 3);
6302
- metadataPageManager.setFreePageId(metadataPage, -1);
6303
- const bitmapPage = new Uint8Array(options.pageSize);
6304
- bitmapPageManager.initial(
6305
- bitmapPage,
6306
- BitmapPageManager.CONSTANT.PAGE_TYPE_BITMAP,
6307
- 1,
6308
- -1,
6309
- options.pageSize - BitmapPageManager.CONSTANT.SIZE_PAGE_HEADER
6310
- );
6311
- dataPageManager.initial(
6312
- dataPage,
6313
- DataPageManager.CONSTANT.PAGE_TYPE_DATA,
6314
- 2,
6315
- -1,
6316
- options.pageSize - DataPageManager.CONSTANT.SIZE_PAGE_HEADER
6317
- );
6318
- import_node_fs3.default.appendFileSync(fileHandle, new Uint8Array([
6319
- ...metadataPage,
6320
- ...bitmapPage,
6321
- ...dataPage
6322
- ]));
6323
- }, file, fileHandle, options);
6292
+ const metadataPageManager = new MetadataPageManager();
6293
+ const bitmapPageManager = new BitmapPageManager();
6294
+ const dataPageManager = new DataPageManager();
6295
+ const metadataPage = new Uint8Array(options.pageSize);
6296
+ const dataPage = new Uint8Array(options.pageSize);
6297
+ metadataPageManager.initial(
6298
+ metadataPage,
6299
+ MetadataPageManager.CONSTANT.PAGE_TYPE_METADATA,
6300
+ 0,
6301
+ 0,
6302
+ options.pageSize - MetadataPageManager.CONSTANT.SIZE_PAGE_HEADER
6303
+ );
6304
+ metadataPageManager.setMagicString(metadataPage);
6305
+ metadataPageManager.setPageSize(metadataPage, options.pageSize);
6306
+ metadataPageManager.setRootIndexPageId(metadataPage, -1);
6307
+ metadataPageManager.setBitmapPageId(metadataPage, 1);
6308
+ metadataPageManager.setLastInsertPageId(metadataPage, 2);
6309
+ metadataPageManager.setPageCount(metadataPage, 3);
6310
+ metadataPageManager.setFreePageId(metadataPage, -1);
6311
+ const bitmapPage = new Uint8Array(options.pageSize);
6312
+ bitmapPageManager.initial(
6313
+ bitmapPage,
6314
+ BitmapPageManager.CONSTANT.PAGE_TYPE_BITMAP,
6315
+ 1,
6316
+ -1,
6317
+ options.pageSize - BitmapPageManager.CONSTANT.SIZE_PAGE_HEADER
6318
+ );
6319
+ dataPageManager.initial(
6320
+ dataPage,
6321
+ DataPageManager.CONSTANT.PAGE_TYPE_DATA,
6322
+ 2,
6323
+ -1,
6324
+ options.pageSize - DataPageManager.CONSTANT.SIZE_PAGE_HEADER
6325
+ );
6326
+ import_node_fs3.default.appendFileSync(fileHandle, new Uint8Array([
6327
+ ...metadataPage,
6328
+ ...bitmapPage,
6329
+ ...dataPage
6330
+ ]));
6324
6331
  }
6325
6332
  /**
6326
6333
  * Opens the database file. If the file does not exist, it initializes it.
@@ -6367,11 +6374,12 @@ var DataplyAPI = class {
6367
6374
  if (this.initialized) {
6368
6375
  return;
6369
6376
  }
6370
- await this.runWithDefault(() => {
6371
- return this.hook.async.trigger("init", void 0, async () => {
6377
+ await this.runWithDefault(async (tx) => {
6378
+ await this.hook.trigger("init", tx, async (tx2) => {
6372
6379
  await this.rowTableEngine.init();
6373
6380
  this.initialized = true;
6374
- });
6381
+ return tx2;
6382
+ }, this.isNewlyCreated);
6375
6383
  });
6376
6384
  }
6377
6385
  /**
@@ -6530,7 +6538,7 @@ var DataplyAPI = class {
6530
6538
  if (!this.initialized) {
6531
6539
  throw new Error("Dataply instance is not initialized");
6532
6540
  }
6533
- return this.hook.async.trigger("close", void 0, async () => {
6541
+ return this.hook.trigger("close", void 0, async () => {
6534
6542
  await this.pfs.close();
6535
6543
  import_node_fs3.default.closeSync(this.fileHandle);
6536
6544
  });
@@ -1,15 +1,12 @@
1
1
  import type { DataplyOptions, DataplyMetadata } from '../types';
2
- import { type IHookall, type IHookallSync } from 'hookall';
2
+ import { type IHookall } from 'hookall';
3
3
  import { PageFileSystem } from './PageFileSystem';
4
4
  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
- interface DataplyAPISyncHook {
9
- create: (_: void, file: string, fileHandle: number, options: Required<DataplyOptions>) => void;
10
- }
11
8
  interface DataplyAPIAsyncHook {
12
- init: () => Promise<void>;
9
+ init: (tx: Transaction, isNewlyCreated: boolean) => Promise<Transaction>;
13
10
  close: () => Promise<void>;
14
11
  }
15
12
  /**
@@ -17,17 +14,28 @@ interface DataplyAPIAsyncHook {
17
14
  */
18
15
  export declare class DataplyAPI {
19
16
  protected readonly file: string;
17
+ /**
18
+ * These are not the same options that were used when the database was created.
19
+ * They are simply the options received when the instance was created.
20
+ * If you want to retrieve the options used during database creation, use `getMetadata()` instead.
21
+ */
20
22
  readonly options: Required<DataplyOptions>;
23
+ /** File handle. Database file descriptor */
21
24
  protected readonly fileHandle: number;
25
+ /** Page file system. Used for managing pages. If you know what it is, you can skip this. */
22
26
  protected readonly pfs: PageFileSystem;
27
+ /** Row table engine. Used for managing rows. If you know what it is, you can skip this. */
23
28
  protected readonly rowTableEngine: RowTableEngine;
29
+ /** Lock manager. Used for managing transactions */
24
30
  protected readonly lockManager: LockManager;
31
+ /** Text codec. Used for encoding and decoding text data */
25
32
  protected readonly textCodec: TextCodec;
26
- protected readonly hook: {
27
- sync: IHookallSync<DataplyAPISyncHook>;
28
- async: IHookall<DataplyAPIAsyncHook>;
29
- };
33
+ /** Hook */
34
+ protected readonly hook: IHookall<DataplyAPIAsyncHook>;
35
+ /** Whether the database was initialized via `init()` */
30
36
  protected initialized: boolean;
37
+ /** Whether the database was created this time. */
38
+ private readonly isNewlyCreated;
31
39
  private txIdCounter;
32
40
  constructor(file: string, options: DataplyOptions);
33
41
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataply",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
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>",
package/readme.md CHANGED
@@ -178,6 +178,8 @@ Rolls back all added transactions.
178
178
 
179
179
  If you want to extend Dataply's functionality, use the `DataplyAPI` class. Unlike the standard `Dataply` class, `DataplyAPI` provides direct access to internal components like `PageFileSystem` or `RowTableEngine`, offering much more flexibility for custom implementations.
180
180
 
181
+ For a detailed guide and examples on how to extend Dataply using Hooks, see [Extending Dataply Guide](docs/extension.md).
182
+
181
183
  ### Using DataplyAPI
182
184
 
183
185
  ```typescript
@@ -222,7 +224,7 @@ graph TD
222
224
  - **Fixed-size Pages**: All data is managed in fixed-size units (default 8KB) called pages.
223
225
  - **VFS Cache**: Minimizes disk I/O by caching frequently accessed pages in memory.
224
226
  - **Dirty Page Tracking**: Tracks modified pages (Dirty) to synchronize them with disk efficiently only at the time of commit.
225
- - **Detailed Structure**: For technical details on the physical layout, see [structure.md](structure.md).
227
+ - **Detailed Structure**: For technical details on the physical layout, see [structure.md](docs/structure.md).
226
228
 
227
229
  #### Page & Row Layout
228
230
  Dataply uses a **Slotted Page** architecture to manage records efficiently: