dataply 0.0.1 → 0.0.3
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 +712 -67
- package/dist/types/core/DataplyAPI.d.ts +22 -8
- package/dist/types/core/LogManager.d.ts +6 -0
- package/dist/types/core/Page.d.ts +43 -0
- package/dist/types/core/PageFileSystem.d.ts +17 -1
- package/dist/types/core/VirtualFileSystem.d.ts +14 -1
- package/dist/types/core/transaction/GlobalTransaction.d.ts +25 -0
- package/dist/types/core/transaction/Transaction.d.ts +5 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +4 -3
- package/readme.md +51 -2
- package/dist/types/core/RowIndexStategy.d.ts +0 -19
- package/dist/types/core/Shard.d.ts +0 -75
- package/dist/types/core/ShareAPI.d.ts +0 -121
|
@@ -1,50 +1,63 @@
|
|
|
1
1
|
import type { DataplyOptions, DataplyMetadata } from '../types';
|
|
2
|
+
import { type IHookall, type IHookallSync } from 'hookall';
|
|
2
3
|
import { PageFileSystem } from './PageFileSystem';
|
|
3
4
|
import { RowTableEngine } from './RowTableEngine';
|
|
4
5
|
import { TextCodec } from '../utils/TextCodec';
|
|
5
6
|
import { LockManager } from './transaction/LockManager';
|
|
6
7
|
import { Transaction } from './transaction/Transaction';
|
|
8
|
+
interface DataplyAPISyncHook {
|
|
9
|
+
create: (fileData: Uint8Array, file: string, fileHandle: number, options: Required<DataplyOptions>) => Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
interface DataplyAPIAsyncHook {
|
|
12
|
+
init: () => Promise<void>;
|
|
13
|
+
close: () => Promise<void>;
|
|
14
|
+
}
|
|
7
15
|
/**
|
|
8
16
|
* Class for managing Dataply files.
|
|
9
17
|
*/
|
|
10
18
|
export declare class DataplyAPI {
|
|
11
|
-
protected file: string;
|
|
12
|
-
protected fileHandle: number;
|
|
19
|
+
protected readonly file: string;
|
|
13
20
|
readonly options: Required<DataplyOptions>;
|
|
21
|
+
protected readonly fileHandle: number;
|
|
14
22
|
protected readonly pfs: PageFileSystem;
|
|
15
23
|
protected readonly rowTableEngine: RowTableEngine;
|
|
16
24
|
protected readonly lockManager: LockManager;
|
|
17
25
|
protected readonly textCodec: TextCodec;
|
|
26
|
+
protected readonly hook: {
|
|
27
|
+
sync: IHookallSync<DataplyAPISyncHook>;
|
|
28
|
+
async: IHookall<DataplyAPIAsyncHook>;
|
|
29
|
+
};
|
|
18
30
|
protected initialized: boolean;
|
|
19
31
|
private txIdCounter;
|
|
20
|
-
|
|
32
|
+
constructor(file: string, options: DataplyOptions);
|
|
21
33
|
/**
|
|
22
34
|
* Verifies if the page file is a valid Dataply file.
|
|
23
35
|
* The metadata page must be located at the beginning of the Dataply file.
|
|
24
36
|
* @param fileHandle File handle
|
|
25
37
|
* @returns Whether the page file is a valid Dataply file
|
|
26
38
|
*/
|
|
27
|
-
|
|
39
|
+
private verifyFormat;
|
|
28
40
|
/**
|
|
29
41
|
* Fills missing options with default values.
|
|
30
42
|
* @param options Options
|
|
31
43
|
* @returns Options filled without omissions
|
|
32
44
|
*/
|
|
33
|
-
|
|
45
|
+
private verboseOptions;
|
|
34
46
|
/**
|
|
35
47
|
* Initializes the database file.
|
|
36
48
|
* The first page is initialized as the metadata page.
|
|
37
49
|
* The second page is initialized as the first data page.
|
|
50
|
+
* @param file Database file path
|
|
38
51
|
* @param fileHandle File handle
|
|
39
52
|
*/
|
|
40
|
-
|
|
53
|
+
private initializeFile;
|
|
41
54
|
/**
|
|
42
55
|
* Opens the database file. If the file does not exist, it initializes it.
|
|
43
56
|
* @param file Database file path
|
|
44
57
|
* @param options Options
|
|
45
|
-
* @returns
|
|
58
|
+
* @returns File handle
|
|
46
59
|
*/
|
|
47
|
-
|
|
60
|
+
private createOrOpen;
|
|
48
61
|
/**
|
|
49
62
|
* Initializes the dataply instance.
|
|
50
63
|
* Must be called before using the dataply instance.
|
|
@@ -119,3 +132,4 @@ export declare class DataplyAPI {
|
|
|
119
132
|
*/
|
|
120
133
|
close(): Promise<void>;
|
|
121
134
|
}
|
|
135
|
+
export {};
|
|
@@ -25,9 +25,15 @@ export declare class LogManager {
|
|
|
25
25
|
* @param pages Map of changed pages (Page ID -> Data)
|
|
26
26
|
*/
|
|
27
27
|
append(pages: Map<number, Uint8Array>): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Writes a commit marker to the log file.
|
|
30
|
+
* This indicates that the preceding logs are part of a committed transaction.
|
|
31
|
+
*/
|
|
32
|
+
writeCommitMarker(): Promise<void>;
|
|
28
33
|
/**
|
|
29
34
|
* Reads the log file to recover the page map.
|
|
30
35
|
* Runs synchronously as it is called by the VFS constructor.
|
|
36
|
+
* Only returns pages from committed transactions (ended with a commit marker).
|
|
31
37
|
* @returns Recovered page map
|
|
32
38
|
*/
|
|
33
39
|
readAllSync(): Map<number, Uint8Array>;
|
|
@@ -421,11 +421,16 @@ export declare class MetadataPageManager extends PageManager {
|
|
|
421
421
|
readonly OFFSET_ROOT_INDEX_ORDER: 126;
|
|
422
422
|
readonly OFFSET_LAST_INSERT_PAGE_ID: 130;
|
|
423
423
|
readonly OFFSET_LAST_ROW_PK: 134;
|
|
424
|
+
readonly OFFSET_BITMAP_PAGE_ID: 140;
|
|
425
|
+
readonly OFFSET_FREE_PAGE_ID: 144;
|
|
424
426
|
readonly SIZE_PAGE_COUNT: 4;
|
|
425
427
|
readonly SIZE_PAGE_SIZE: 4;
|
|
426
428
|
readonly SIZE_ROOT_INDEX_PAGE_ID: 4;
|
|
427
429
|
readonly SIZE_ROOT_INDEX_ORDER: 4;
|
|
428
430
|
readonly SIZE_LAST_INSERT_PAGE_ID: 4;
|
|
431
|
+
readonly SIZE_ROW_PK: 6;
|
|
432
|
+
readonly SIZE_BITMAP_PAGE_ID: 4;
|
|
433
|
+
readonly SIZE_FREE_PAGE_ID: 4;
|
|
429
434
|
readonly PAGE_TYPE_UNKNOWN: 0;
|
|
430
435
|
readonly PAGE_TYPE_EMPTY: 1;
|
|
431
436
|
readonly PAGE_TYPE_METADATA: 2;
|
|
@@ -508,6 +513,18 @@ export declare class MetadataPageManager extends PageManager {
|
|
|
508
513
|
* @returns Number of rows
|
|
509
514
|
*/
|
|
510
515
|
getRowCount(page: MetadataPage): number;
|
|
516
|
+
/**
|
|
517
|
+
* Returns the ID of the bitmap page.
|
|
518
|
+
* @param page Page data
|
|
519
|
+
* @returns Bitmap page ID
|
|
520
|
+
*/
|
|
521
|
+
getBitmapPageId(page: MetadataPage): number;
|
|
522
|
+
/**
|
|
523
|
+
* Returns the ID of the free page.
|
|
524
|
+
* @param page Page data
|
|
525
|
+
* @returns Free page ID
|
|
526
|
+
*/
|
|
527
|
+
getFreePageId(page: MetadataPage): number;
|
|
511
528
|
/**
|
|
512
529
|
* Sets the number of pages stored in the database.
|
|
513
530
|
* @param page Page data
|
|
@@ -555,6 +572,18 @@ export declare class MetadataPageManager extends PageManager {
|
|
|
555
572
|
* @param rowCount Number of rows
|
|
556
573
|
*/
|
|
557
574
|
setRowCount(page: MetadataPage, rowCount: number): void;
|
|
575
|
+
/**
|
|
576
|
+
* Sets the ID of the bitmap page.
|
|
577
|
+
* @param page Page data
|
|
578
|
+
* @param bitmapPageId Bitmap page ID
|
|
579
|
+
*/
|
|
580
|
+
setBitmapPageId(page: MetadataPage, bitmapPageId: number): void;
|
|
581
|
+
/**
|
|
582
|
+
* Sets the ID of the free page.
|
|
583
|
+
* @param page Page data
|
|
584
|
+
* @param pageId Free page ID
|
|
585
|
+
*/
|
|
586
|
+
setFreePageId(page: MetadataPage, pageId: number): void;
|
|
558
587
|
}
|
|
559
588
|
/**
|
|
560
589
|
* Represents a bitmap page.
|
|
@@ -581,6 +610,20 @@ export declare class BitmapPageManager extends PageManager {
|
|
|
581
610
|
* @returns boolean indicating if the page is empty
|
|
582
611
|
*/
|
|
583
612
|
isEmptyPage(page: BitmapPage, index: number): boolean;
|
|
613
|
+
/**
|
|
614
|
+
* Gets a bit from the bitmap page.
|
|
615
|
+
* @param page Page data
|
|
616
|
+
* @param index Bit index
|
|
617
|
+
* @returns boolean indicating if the bit is set
|
|
618
|
+
*/
|
|
619
|
+
getBit(page: BitmapPage, index: number): boolean;
|
|
620
|
+
/**
|
|
621
|
+
* Sets a bit in the bitmap page.
|
|
622
|
+
* @param page Page data
|
|
623
|
+
* @param index Bit index
|
|
624
|
+
* @param flag boolean indicating if the bit is set
|
|
625
|
+
*/
|
|
626
|
+
setBit(page: BitmapPage, index: number, flag: boolean): void;
|
|
584
627
|
}
|
|
585
628
|
/**
|
|
586
629
|
* Represents an overflow page.
|
|
@@ -21,6 +21,13 @@ export declare class PageFileSystem {
|
|
|
21
21
|
* @param walPath WAL 파일 경로 (기본값: null)
|
|
22
22
|
*/
|
|
23
23
|
constructor(fileHandle: number, pageSize: number, pageCacheCapacity: number, walPath?: string | undefined | null);
|
|
24
|
+
/**
|
|
25
|
+
* Updates the bitmap status for a specific page.
|
|
26
|
+
* @param pageId The ID of the page to update
|
|
27
|
+
* @param isFree True to mark as free, false to mark as used
|
|
28
|
+
* @param tx Transaction
|
|
29
|
+
*/
|
|
30
|
+
private updateBitmap;
|
|
24
31
|
/**
|
|
25
32
|
* VFS 인스턴스를 반환합니다.
|
|
26
33
|
* Transaction 생성 시 사용됩니다.
|
|
@@ -80,7 +87,9 @@ export declare class PageFileSystem {
|
|
|
80
87
|
setPage(pageIndex: number, page: Uint8Array, tx: Transaction): Promise<void>;
|
|
81
88
|
/**
|
|
82
89
|
* Appends and inserts a new page.
|
|
83
|
-
*
|
|
90
|
+
* If a free page is available in the free list, it reuses it.
|
|
91
|
+
* Otherwise, it appends a new page to the end of the file.
|
|
92
|
+
* @returns Created or reused page ID
|
|
84
93
|
*/
|
|
85
94
|
appendNewPage(pageType: number | undefined, tx: Transaction): Promise<number>;
|
|
86
95
|
/**
|
|
@@ -91,6 +100,13 @@ export declare class PageFileSystem {
|
|
|
91
100
|
* @param tx Transaction
|
|
92
101
|
*/
|
|
93
102
|
writePageContent(pageId: number, data: Uint8Array, offset: number | undefined, tx: Transaction): Promise<void>;
|
|
103
|
+
/**
|
|
104
|
+
* Frees the page and marks it as available in the bitmap.
|
|
105
|
+
* It also adds the page to the linked list of free pages in metadata.
|
|
106
|
+
* @param pageId Page ID
|
|
107
|
+
* @param tx Transaction
|
|
108
|
+
*/
|
|
109
|
+
setFreePage(pageId: number, tx: Transaction): Promise<void>;
|
|
94
110
|
/**
|
|
95
111
|
* Closes the page file system.
|
|
96
112
|
*/
|
|
@@ -29,7 +29,20 @@ export declare class VirtualFileSystem {
|
|
|
29
29
|
*/
|
|
30
30
|
private recover;
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* Prepares the transaction for commit (Phase 1).
|
|
33
|
+
* Writes dirty pages to WAL but does not update the main database file.
|
|
34
|
+
* @param tx Transaction
|
|
35
|
+
*/
|
|
36
|
+
prepareCommit(tx: Transaction): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Finalizes the transaction commit (Phase 2).
|
|
39
|
+
* Writes commit marker to WAL and updates the main database file (Checkpoint).
|
|
40
|
+
* @param tx Transaction
|
|
41
|
+
*/
|
|
42
|
+
finalizeCommit(tx: Transaction): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Commits the transaction (Single Phase).
|
|
45
|
+
* Wrapper for prepare + finalize for backward compatibility.
|
|
33
46
|
* @param tx Transaction
|
|
34
47
|
*/
|
|
35
48
|
commit(tx: Transaction): Promise<void>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Transaction } from './Transaction';
|
|
2
|
+
/**
|
|
3
|
+
* Global Transaction Manager.
|
|
4
|
+
* Coordinates transactions across multiple instances (shards) using 2-Phase Commit (2PC).
|
|
5
|
+
*/
|
|
6
|
+
export declare class GlobalTransaction {
|
|
7
|
+
private transactions;
|
|
8
|
+
private isCommitted;
|
|
9
|
+
private isRolledBack;
|
|
10
|
+
/**
|
|
11
|
+
* Adds a transaction to the global transaction.
|
|
12
|
+
* @param tx Transaction to add
|
|
13
|
+
*/
|
|
14
|
+
add(tx: Transaction): void;
|
|
15
|
+
/**
|
|
16
|
+
* Commits all transactions atomically.
|
|
17
|
+
* Phase 1: Prepare (Write WAL)
|
|
18
|
+
* Phase 2: Commit (Write Commit Marker & Checkpoint)
|
|
19
|
+
*/
|
|
20
|
+
commit(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Rolls back all transactions.
|
|
23
|
+
*/
|
|
24
|
+
rollback(): Promise<void>;
|
|
25
|
+
}
|
|
@@ -86,6 +86,11 @@ export declare class Transaction {
|
|
|
86
86
|
* @param pageId Page ID
|
|
87
87
|
*/
|
|
88
88
|
__acquireWriteLock(pageId: number): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Prepares the transaction for commit (Phase 1 of 2PC).
|
|
91
|
+
* Writes dirty pages to WAL but does not update the database file yet.
|
|
92
|
+
*/
|
|
93
|
+
prepare(): Promise<void>;
|
|
89
94
|
/**
|
|
90
95
|
* Commits the transaction.
|
|
91
96
|
*/
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dataply",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
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>",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"dist/**/*"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"test": "jest
|
|
20
|
+
"test": "jest -i",
|
|
21
21
|
"benchmark": "npx tsx benchmark/benchmark.ts",
|
|
22
22
|
"build": "node build/index.js && tsc"
|
|
23
23
|
},
|
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"cache-entanglement": "^1.7.1",
|
|
48
|
+
"hookall": "^2.2.0",
|
|
48
49
|
"ryoiki": "^1.2.0",
|
|
49
50
|
"serializable-bptree": "^5.2.1"
|
|
50
51
|
}
|
|
51
|
-
}
|
|
52
|
+
}
|
package/readme.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# Dataply
|
|
4
4
|
|
|
5
5
|
> [!WARNING]
|
|
6
|
-
> **Dataply is currently in Alpha version.** It is experimental and not yet suitable for production use.
|
|
6
|
+
> **Dataply is currently in Alpha version.** It is experimental and not yet suitable for production use. Internal data structures and file formats are subject to change at any time.
|
|
7
7
|
|
|
8
8
|
**Dataply** is a lightweight, high-performance **Record Store** designed for Node.js. It focuses on storing arbitrary data and providing an auto-generated Primary Key (PK) for ultra-fast retrieval, while supporting core enterprise features like MVCC, WAL, and atomic transactions.
|
|
9
9
|
|
|
@@ -82,6 +82,36 @@ try {
|
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
### Global Transactions
|
|
86
|
+
You can perform atomic operations across multiple `Dataply` instances using the `GlobalTransaction` class. This uses a **2-Phase Commit (2PC)** mechanism to ensure that either all instances commit successfully or all are rolled back.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import { Dataply, GlobalTransaction } from 'dataply'
|
|
90
|
+
|
|
91
|
+
const db1 = new Dataply('./db1.db', { wal: './db1.wal' })
|
|
92
|
+
const db2 = new Dataply('./db2.db', { wal: './db2.wal' })
|
|
93
|
+
|
|
94
|
+
await db1.init()
|
|
95
|
+
await db2.init()
|
|
96
|
+
|
|
97
|
+
const tx1 = db1.createTransaction()
|
|
98
|
+
const tx2 = db2.createTransaction()
|
|
99
|
+
|
|
100
|
+
const globalTx = new GlobalTransaction()
|
|
101
|
+
globalTx.add(tx1)
|
|
102
|
+
globalTx.add(tx2)
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
await db1.insert('Data for DB1', tx1)
|
|
106
|
+
await db2.insert('Data for DB2', tx2)
|
|
107
|
+
|
|
108
|
+
// Phase 1: Prepare (WAL write) -> Phase 2: Commit (Marker write)
|
|
109
|
+
await globalTx.commit()
|
|
110
|
+
} catch (error) {
|
|
111
|
+
await globalTx.rollback()
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
85
115
|
### Auto-Transaction
|
|
86
116
|
If you omit the `tx` argument when calling methods like `insert`, `update`, or `delete`, Dataply internally **creates an individual transaction automatically**.
|
|
87
117
|
|
|
@@ -133,6 +163,17 @@ Permanently reflects all changes made during the transaction to disk and release
|
|
|
133
163
|
#### `async rollback(): Promise<void>`
|
|
134
164
|
Cancels all changes made during the transaction and restores the original state.
|
|
135
165
|
|
|
166
|
+
### GlobalTransaction Class
|
|
167
|
+
|
|
168
|
+
#### `add(tx: Transaction): void`
|
|
169
|
+
Adds an individual transaction from a `Dataply` instance to the global transaction.
|
|
170
|
+
|
|
171
|
+
#### `async commit(): Promise<void>`
|
|
172
|
+
Atomically commits all added transactions using a 2-Phase Commit (2PC) process.
|
|
173
|
+
|
|
174
|
+
#### `async rollback(): Promise<void>`
|
|
175
|
+
Rolls back all added transactions.
|
|
176
|
+
|
|
136
177
|
## Extending Dataply
|
|
137
178
|
|
|
138
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.
|
|
@@ -152,7 +193,7 @@ class CustomDataply extends DataplyAPI {
|
|
|
152
193
|
}
|
|
153
194
|
}
|
|
154
195
|
|
|
155
|
-
const custom = CustomDataply
|
|
196
|
+
const custom = new CustomDataply('./data.db')
|
|
156
197
|
await custom.init()
|
|
157
198
|
|
|
158
199
|
const stats = await custom.getInternalStats()
|
|
@@ -177,9 +218,17 @@ graph TD
|
|
|
177
218
|
```
|
|
178
219
|
|
|
179
220
|
### 2. Page-Based Storage and VFS Caching
|
|
221
|
+
|
|
180
222
|
- **Fixed-size Pages**: All data is managed in fixed-size units (default 8KB) called pages.
|
|
181
223
|
- **VFS Cache**: Minimizes disk I/O by caching frequently accessed pages in memory.
|
|
182
224
|
- **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).
|
|
226
|
+
|
|
227
|
+
#### Page & Row Layout
|
|
228
|
+
Dataply uses a **Slotted Page** architecture to manage records efficiently:
|
|
229
|
+
- **Pages**: Consists of a 100-byte header (containing `type`, `id`, `checksum`, etc.) and a body where rows are stored. Slot offsets are stored at the end of the page to track row positions.
|
|
230
|
+
- **Rows**: Each row has a 9-byte header (`flags`, `size`, `PK`) followed by the actual data. Large records automatically trigger **Overflow Pages** to handle data exceeding page capacity.
|
|
231
|
+
- **Keys & Identifiers**: Uses a 6-byte **Primary Key (PK)** for logical mapping and a 6-byte **Record Identifier (RID)** (Slot + Page ID) for direct physical addressing.
|
|
183
232
|
|
|
184
233
|
### 3. MVCC and Snapshot Isolation
|
|
185
234
|
- **Non-blocking Reads**: Read operations are not blocked by write operations.
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type BPTreeNode, type SerializeStrategyHead, SerializeStrategyAsync } from 'serializable-bptree';
|
|
2
|
-
import { PageFileSystem } from './PageFileSystem';
|
|
3
|
-
import { IndexPageManager, PageManagerFactory } from './Page';
|
|
4
|
-
import { TextCodec } from '../utils/TextCodec';
|
|
5
|
-
export declare class RowIdentifierStrategy extends SerializeStrategyAsync<number, number> {
|
|
6
|
-
readonly order: number;
|
|
7
|
-
protected readonly pfs: PageFileSystem;
|
|
8
|
-
protected rootPageId: number;
|
|
9
|
-
protected factory: PageManagerFactory;
|
|
10
|
-
protected indexPageManger: IndexPageManager;
|
|
11
|
-
protected codec: TextCodec;
|
|
12
|
-
constructor(order: number, pfs: PageFileSystem);
|
|
13
|
-
id(isLeaf: boolean): Promise<string>;
|
|
14
|
-
read(id: string): Promise<BPTreeNode<number, number>>;
|
|
15
|
-
write(id: string, node: BPTreeNode<number, number>): Promise<void>;
|
|
16
|
-
delete(id: string): Promise<void>;
|
|
17
|
-
readHead(): Promise<SerializeStrategyHead | null>;
|
|
18
|
-
writeHead(head: SerializeStrategyHead): Promise<void>;
|
|
19
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { ShardOptions, ShardMetadata } from '../types';
|
|
2
|
-
import { ShardAPI } from './ShareAPI';
|
|
3
|
-
import { Transaction } from './transaction/Transaction';
|
|
4
|
-
/**
|
|
5
|
-
* Class for managing Shard files.
|
|
6
|
-
*/
|
|
7
|
-
export declare class Shard {
|
|
8
|
-
protected readonly api: ShardAPI;
|
|
9
|
-
constructor(file: string, options?: ShardOptions);
|
|
10
|
-
/**
|
|
11
|
-
* Gets the options used to open the shard.
|
|
12
|
-
* @returns Options used to open the shard.
|
|
13
|
-
*/
|
|
14
|
-
get options(): Required<ShardOptions>;
|
|
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 shard instance.
|
|
24
|
-
* Must be called before using the shard instance.
|
|
25
|
-
* If not called, the shard instance cannot be used.
|
|
26
|
-
*/
|
|
27
|
-
init(): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Retrieves metadata from the shard.
|
|
30
|
-
* @returns Metadata of the shard.
|
|
31
|
-
*/
|
|
32
|
-
getMetadata(): Promise<ShardMetadata>;
|
|
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 shard file.
|
|
73
|
-
*/
|
|
74
|
-
close(): Promise<void>;
|
|
75
|
-
}
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import type { ShardOptions, ShardMetadata } 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 Shard files.
|
|
9
|
-
*/
|
|
10
|
-
export declare class ShardAPI {
|
|
11
|
-
protected file: string;
|
|
12
|
-
protected fileHandle: number;
|
|
13
|
-
readonly options: Required<ShardOptions>;
|
|
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<ShardOptions>);
|
|
21
|
-
/**
|
|
22
|
-
* Verifies if the page file is a valid Shard file.
|
|
23
|
-
* The metadata page must be located at the beginning of the Shard file.
|
|
24
|
-
* @param fileHandle File handle
|
|
25
|
-
* @returns Whether the page file is a valid Shard 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?: ShardOptions): Required<ShardOptions>;
|
|
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<ShardOptions>): 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 Shard instance
|
|
46
|
-
*/
|
|
47
|
-
static Use(file: string, options?: ShardOptions): ShardAPI;
|
|
48
|
-
/**
|
|
49
|
-
* Initializes the shard instance.
|
|
50
|
-
* Must be called before using the shard instance.
|
|
51
|
-
* If not called, the shard 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 shard.
|
|
73
|
-
* @returns Metadata of the shard.
|
|
74
|
-
*/
|
|
75
|
-
getMetadata(): Promise<ShardMetadata>;
|
|
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 shard file.
|
|
119
|
-
*/
|
|
120
|
-
close(): Promise<void>;
|
|
121
|
-
}
|