dataply 0.0.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.
@@ -0,0 +1,75 @@
1
+ import type { DataplyOptions, DataplyMetadata } from '../types';
2
+ import { DataplyAPI } from './DataplyAPI';
3
+ import { Transaction } from './transaction/Transaction';
4
+ /**
5
+ * Class for managing Dataply files.
6
+ */
7
+ export declare class Dataply {
8
+ protected readonly api: DataplyAPI;
9
+ constructor(file: string, options?: DataplyOptions);
10
+ /**
11
+ * Gets the options used to open the dataply.
12
+ * @returns Options used to open the dataply.
13
+ */
14
+ get options(): Required<DataplyOptions>;
15
+ /**
16
+ * Creates a transaction.
17
+ * The created transaction object can be used to add or modify data.
18
+ * A transaction must be terminated by calling either `commit` or `rollback`.
19
+ * @returns Transaction object
20
+ */
21
+ createTransaction(): Transaction;
22
+ /**
23
+ * Initializes the dataply instance.
24
+ * Must be called before using the dataply instance.
25
+ * If not called, the dataply instance cannot be used.
26
+ */
27
+ init(): Promise<void>;
28
+ /**
29
+ * Retrieves metadata from the dataply.
30
+ * @returns Metadata of the dataply.
31
+ */
32
+ getMetadata(): Promise<DataplyMetadata>;
33
+ /**
34
+ * Inserts data. Returns the PK of the added row.
35
+ * @param data Data to add
36
+ * @param tx Transaction
37
+ * @returns PK of the added data
38
+ */
39
+ insert(data: string | Uint8Array, tx?: Transaction): Promise<number>;
40
+ /**
41
+ * Inserts multiple data in batch.
42
+ * If a transaction is not provided, it internally creates a single transaction to process.
43
+ * @param dataList Array of data to add
44
+ * @param tx Transaction
45
+ * @returns Array of PKs of the added data
46
+ */
47
+ insertBatch(dataList: (string | Uint8Array)[], tx?: Transaction): Promise<number[]>;
48
+ /**
49
+ * Updates data.
50
+ * @param pk PK of the data to update
51
+ * @param data Data to update
52
+ * @param tx Transaction
53
+ */
54
+ update(pk: number, data: string | Uint8Array, tx?: Transaction): Promise<void>;
55
+ /**
56
+ * Deletes data.
57
+ * @param pk PK of the data to delete
58
+ * @param tx Transaction
59
+ */
60
+ delete(pk: number, tx?: Transaction): Promise<void>;
61
+ /**
62
+ * Selects data.
63
+ * @param pk PK of the data to select
64
+ * @param asRaw Whether to return the selected data as raw
65
+ * @param tx Transaction
66
+ * @returns Selected data
67
+ */
68
+ select(pk: number, asRaw: true, tx?: Transaction): Promise<Uint8Array | null>;
69
+ select(pk: number, asRaw: false, tx?: Transaction): Promise<string | null>;
70
+ select(pk: number, asRaw?: boolean, tx?: Transaction): Promise<string | null>;
71
+ /**
72
+ * Closes the dataply file.
73
+ */
74
+ close(): Promise<void>;
75
+ }
@@ -0,0 +1,121 @@
1
+ import type { DataplyOptions, DataplyMetadata } from '../types';
2
+ import { PageFileSystem } from './PageFileSystem';
3
+ import { RowTableEngine } from './RowTableEngine';
4
+ import { TextCodec } from '../utils/TextCodec';
5
+ import { LockManager } from './transaction/LockManager';
6
+ import { Transaction } from './transaction/Transaction';
7
+ /**
8
+ * Class for managing Dataply files.
9
+ */
10
+ export declare class DataplyAPI {
11
+ protected file: string;
12
+ protected fileHandle: number;
13
+ readonly options: Required<DataplyOptions>;
14
+ protected readonly pfs: PageFileSystem;
15
+ protected readonly rowTableEngine: RowTableEngine;
16
+ protected readonly lockManager: LockManager;
17
+ protected readonly textCodec: TextCodec;
18
+ protected initialized: boolean;
19
+ private txIdCounter;
20
+ protected constructor(file: string, fileHandle: number, options: Required<DataplyOptions>);
21
+ /**
22
+ * Verifies if the page file is a valid Dataply file.
23
+ * The metadata page must be located at the beginning of the Dataply file.
24
+ * @param fileHandle File handle
25
+ * @returns Whether the page file is a valid Dataply file
26
+ */
27
+ static VerifyFormat(fileHandle: number): boolean;
28
+ /**
29
+ * Fills missing options with default values.
30
+ * @param options Options
31
+ * @returns Options filled without omissions
32
+ */
33
+ static VerboseOptions(options?: DataplyOptions): Required<DataplyOptions>;
34
+ /**
35
+ * Initializes the database file.
36
+ * The first page is initialized as the metadata page.
37
+ * The second page is initialized as the first data page.
38
+ * @param fileHandle File handle
39
+ */
40
+ static InitializeFile(fileHandle: number, options: Required<DataplyOptions>): void;
41
+ /**
42
+ * Opens the database file. If the file does not exist, it initializes it.
43
+ * @param file Database file path
44
+ * @param options Options
45
+ * @returns Dataply instance
46
+ */
47
+ static Use(file: string, options?: DataplyOptions): DataplyAPI;
48
+ /**
49
+ * Initializes the dataply instance.
50
+ * Must be called before using the dataply instance.
51
+ * If not called, the dataply instance cannot be used.
52
+ */
53
+ init(): Promise<void>;
54
+ /**
55
+ * Creates a transaction.
56
+ * The created transaction object can be used to add or modify data.
57
+ * A transaction must be terminated by calling either `commit` or `rollback`.
58
+ * @returns Transaction object
59
+ */
60
+ createTransaction(): Transaction;
61
+ /**
62
+ * Runs a callback function within a transaction context.
63
+ * If no transaction is provided, a new transaction is created.
64
+ * The transaction is committed if the callback completes successfully,
65
+ * or rolled back if an error occurs.
66
+ * @param callback The callback function to run within the transaction context.
67
+ * @param tx The transaction to use. If not provided, a new transaction is created.
68
+ * @returns The result of the callback function.
69
+ */
70
+ private runWithDefault;
71
+ /**
72
+ * Retrieves metadata from the dataply.
73
+ * @returns Metadata of the dataply.
74
+ */
75
+ getMetadata(): Promise<DataplyMetadata>;
76
+ /**
77
+ * Inserts data. Returns the PK of the added row.
78
+ * @param data Data to add
79
+ * @param incrementRowCount Whether to increment the row count to metadata
80
+ * @param tx Transaction
81
+ * @returns PK of the added data
82
+ */
83
+ insert(data: string | Uint8Array, incrementRowCount?: boolean, tx?: Transaction): Promise<number>;
84
+ /**
85
+ * Inserts multiple data in batch.
86
+ * If a transaction is not provided, it internally creates a single transaction to process.
87
+ * @param dataList Array of data to add
88
+ * @param incrementRowCount Whether to increment the row count to metadata
89
+ * @param tx Transaction
90
+ * @returns Array of PKs of the added data
91
+ */
92
+ insertBatch(dataList: (string | Uint8Array)[], incrementRowCount?: boolean, tx?: Transaction): Promise<number[]>;
93
+ /**
94
+ * Updates data.
95
+ * @param pk PK of the data to update
96
+ * @param data Data to update
97
+ * @param tx Transaction
98
+ */
99
+ update(pk: number, data: string | Uint8Array, tx?: Transaction): Promise<void>;
100
+ /**
101
+ * Deletes data.
102
+ * @param pk PK of the data to delete
103
+ * @param decrementRowCount Whether to decrement the row count to metadata
104
+ * @param tx Transaction
105
+ */
106
+ delete(pk: number, decrementRowCount?: boolean, tx?: Transaction): Promise<void>;
107
+ /**
108
+ * Selects data.
109
+ * @param pk PK of the data to select
110
+ * @param asRaw Whether to return the selected data as raw
111
+ * @param tx Transaction
112
+ * @returns Selected data
113
+ */
114
+ select(pk: number, asRaw: true, tx?: Transaction): Promise<Uint8Array | null>;
115
+ select(pk: number, asRaw: false, tx?: Transaction): Promise<string | null>;
116
+ select(pk: number, asRaw?: boolean, tx?: Transaction): Promise<string | null>;
117
+ /**
118
+ * Closes the dataply file.
119
+ */
120
+ close(): Promise<void>;
121
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Key Manager class.
3
+ */
4
+ export declare class KeyManager {
5
+ /**
6
+ * Returns a numeric key from the buffer.
7
+ * @param buffer Buffer
8
+ * @returns Numeric key
9
+ */
10
+ toNumericKey(buffer: Uint8Array): number;
11
+ /**
12
+ * Sets a numeric key in the buffer.
13
+ * @param key Numeric key
14
+ * @param buffer Buffer
15
+ * @returns Buffer
16
+ */
17
+ setBufferFromKey(key: number, buffer: Uint8Array): Uint8Array;
18
+ /**
19
+ * Returns the Page ID from the buffer.
20
+ * @param buffer Buffer
21
+ * @returns Page ID
22
+ */
23
+ getPageId(buffer: Uint8Array): number;
24
+ /**
25
+ * Sets the Page ID in the buffer.
26
+ * @param buffer Buffer
27
+ * @param pageId Page ID
28
+ */
29
+ setPageId(buffer: Uint8Array, pageId: number): void;
30
+ /**
31
+ * Returns the Slot Index from the buffer.
32
+ * @param buffer Buffer
33
+ * @returns Slot index
34
+ */
35
+ getSlotIndex(buffer: Uint8Array): number;
36
+ /**
37
+ * Sets the Slot Index in the buffer.
38
+ * @param buffer Buffer
39
+ * @param slotIndex Slot index
40
+ */
41
+ setSlotIndex(buffer: Uint8Array, slotIndex: number): void;
42
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Log Manager class.
3
+ * Records changes to a log file (WAL) and manages them to ensure atomicity of the database.
4
+ */
5
+ export declare class LogManager {
6
+ private fd;
7
+ private readonly walFilePath;
8
+ private readonly pageSize;
9
+ private readonly entrySize;
10
+ private buffer;
11
+ private view;
12
+ /**
13
+ * Constructor
14
+ * @param walFilePath WAL file path
15
+ * @param pageSize Page size
16
+ */
17
+ constructor(walFilePath: string, pageSize: number);
18
+ /**
19
+ * Opens the log file.
20
+ */
21
+ open(): void;
22
+ /**
23
+ * Appends changed pages to the log file.
24
+ * Records them sorted by page ID.
25
+ * @param pages Map of changed pages (Page ID -> Data)
26
+ */
27
+ append(pages: Map<number, Uint8Array>): Promise<void>;
28
+ /**
29
+ * Reads the log file to recover the page map.
30
+ * Runs synchronously as it is called by the VFS constructor.
31
+ * @returns Recovered page map
32
+ */
33
+ readAllSync(): Map<number, Uint8Array>;
34
+ /**
35
+ * Initializes (clears) the log file.
36
+ * Should be called after a checkpoint.
37
+ */
38
+ clear(): Promise<void>;
39
+ /**
40
+ * Cleans up resources.
41
+ */
42
+ close(): void;
43
+ }