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.
- package/dist/cjs/index.js +5965 -0
- package/dist/types/core/Dataply.d.ts +75 -0
- package/dist/types/core/DataplyAPI.d.ts +121 -0
- package/dist/types/core/KeyManager.d.ts +42 -0
- package/dist/types/core/LogManager.d.ts +43 -0
- package/dist/types/core/Page.d.ts +690 -0
- package/dist/types/core/PageFileSystem.d.ts +98 -0
- package/dist/types/core/Row.d.ts +77 -0
- package/dist/types/core/RowIndexStategy.d.ts +19 -0
- package/dist/types/core/RowIndexStrategy.d.ts +20 -0
- package/dist/types/core/RowTableEngine.d.ts +121 -0
- package/dist/types/core/Shard.d.ts +75 -0
- package/dist/types/core/ShareAPI.d.ts +121 -0
- package/dist/types/core/VirtualFileSystem.d.ts +96 -0
- package/dist/types/core/transaction/LockManager.d.ts +28 -0
- package/dist/types/core/transaction/Transaction.d.ts +112 -0
- package/dist/types/core/transaction/TxContext.d.ts +5 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/types/index.d.ts +49 -0
- package/dist/types/utils/TextCodec.d.ts +10 -0
- package/dist/types/utils/bitwise.d.ts +17 -0
- package/dist/types/utils/bytesToNumber.d.ts +1 -0
- package/dist/types/utils/catchPromise.d.ts +12 -0
- package/dist/types/utils/crc32.d.ts +6 -0
- package/dist/types/utils/index.d.ts +4 -0
- package/dist/types/utils/numberToBytes.d.ts +1 -0
- package/package.json +51 -0
- package/readme.md +248 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { IndexPage, type MetadataPage } from '../types';
|
|
2
|
+
import { PageManagerFactory } from './Page';
|
|
3
|
+
import { VirtualFileSystem } from './VirtualFileSystem';
|
|
4
|
+
import type { Transaction } from './transaction/Transaction';
|
|
5
|
+
/**
|
|
6
|
+
* Page File System class.
|
|
7
|
+
* Manages pages using VFS and page factory.
|
|
8
|
+
*/
|
|
9
|
+
export declare class PageFileSystem {
|
|
10
|
+
readonly fileHandle: number;
|
|
11
|
+
readonly pageSize: number;
|
|
12
|
+
readonly pageCacheCapacity: number;
|
|
13
|
+
readonly walPath?: string | undefined | null;
|
|
14
|
+
protected readonly pageFactory: PageManagerFactory;
|
|
15
|
+
protected readonly vfs: VirtualFileSystem;
|
|
16
|
+
protected readonly pageManagerFactory: PageManagerFactory;
|
|
17
|
+
/**
|
|
18
|
+
* @param fileHandle 파일 핸들 (fs.open으로 얻은 핸들)
|
|
19
|
+
* @param pageSize 페이지 크기
|
|
20
|
+
* @param pageCacheCapacity 페이지 캐시 크기
|
|
21
|
+
* @param walPath WAL 파일 경로 (기본값: null)
|
|
22
|
+
*/
|
|
23
|
+
constructor(fileHandle: number, pageSize: number, pageCacheCapacity: number, walPath?: string | undefined | null);
|
|
24
|
+
/**
|
|
25
|
+
* VFS 인스턴스를 반환합니다.
|
|
26
|
+
* Transaction 생성 시 사용됩니다.
|
|
27
|
+
*/
|
|
28
|
+
get vfsInstance(): VirtualFileSystem;
|
|
29
|
+
/**
|
|
30
|
+
* @param pageIndex 페이지 인덱스
|
|
31
|
+
* @param tx 트랜잭션
|
|
32
|
+
* @returns 페이지 버퍼
|
|
33
|
+
*/
|
|
34
|
+
get(pageIndex: number, tx: Transaction): Promise<Uint8Array>;
|
|
35
|
+
/**
|
|
36
|
+
* Reads the page header.
|
|
37
|
+
* @param pageIndex Page index
|
|
38
|
+
* @param tx Transaction
|
|
39
|
+
* @returns Page header buffer
|
|
40
|
+
*/
|
|
41
|
+
getHeader(pageIndex: number, tx: Transaction): Promise<Uint8Array>;
|
|
42
|
+
/**
|
|
43
|
+
* Reads the page body.
|
|
44
|
+
* @param pageIndex Page index
|
|
45
|
+
* @param recursive Whether to read pages recursively
|
|
46
|
+
* @param tx Transaction
|
|
47
|
+
* @returns Page body buffer
|
|
48
|
+
*/
|
|
49
|
+
getBody(pageIndex: number, recursive: boolean | undefined, tx: Transaction): Promise<Uint8Array>;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the metadata page.
|
|
52
|
+
* @param tx Transaction
|
|
53
|
+
* @returns Metadata page
|
|
54
|
+
*/
|
|
55
|
+
getMetadata(tx: Transaction): Promise<MetadataPage>;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the number of pages stored in the database.
|
|
58
|
+
* @param tx Transaction
|
|
59
|
+
* @returns Number of pages
|
|
60
|
+
*/
|
|
61
|
+
getPageCount(tx: Transaction): Promise<number>;
|
|
62
|
+
/**
|
|
63
|
+
* Returns the root index page.
|
|
64
|
+
* @param tx Transaction
|
|
65
|
+
* @returns Root index page
|
|
66
|
+
*/
|
|
67
|
+
getRootIndex(tx: Transaction): Promise<IndexPage>;
|
|
68
|
+
/**
|
|
69
|
+
* Sets the metadata page.
|
|
70
|
+
* @param metadataPage Metadata page
|
|
71
|
+
* @param tx Transaction
|
|
72
|
+
*/
|
|
73
|
+
setMetadata(metadataPage: MetadataPage, tx: Transaction): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Sets the page.
|
|
76
|
+
* @param pageIndex Page index
|
|
77
|
+
* @param page Page data
|
|
78
|
+
* @param tx Transaction
|
|
79
|
+
*/
|
|
80
|
+
setPage(pageIndex: number, page: Uint8Array, tx: Transaction): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Appends and inserts a new page.
|
|
83
|
+
* @returns Created page ID
|
|
84
|
+
*/
|
|
85
|
+
appendNewPage(pageType: number | undefined, tx: Transaction): Promise<number>;
|
|
86
|
+
/**
|
|
87
|
+
* Writes data to a page. If it overflows, creates the next page and continues writing.
|
|
88
|
+
* @param pageId Page ID
|
|
89
|
+
* @param data Data to write
|
|
90
|
+
* @param offset Position to write (default: 0)
|
|
91
|
+
* @param tx Transaction
|
|
92
|
+
*/
|
|
93
|
+
writePageContent(pageId: number, data: Uint8Array, offset: number | undefined, tx: Transaction): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Closes the page file system.
|
|
96
|
+
*/
|
|
97
|
+
close(): Promise<void>;
|
|
98
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A row consists of a header and a body. The header is 3 bytes, and the body can be up to 65535 bytes.
|
|
3
|
+
*/
|
|
4
|
+
export declare class Row {
|
|
5
|
+
static readonly CONSTANT: {
|
|
6
|
+
readonly FLAG_DELETED: 0;
|
|
7
|
+
readonly FLAG_OVERFLOW: 2;
|
|
8
|
+
readonly SIZE_FLAG: 1;
|
|
9
|
+
readonly SIZE_BODY: 2;
|
|
10
|
+
readonly SIZE_PK: 6;
|
|
11
|
+
readonly SIZE_RID: 6;
|
|
12
|
+
readonly SIZE_HEADER: 9;
|
|
13
|
+
readonly OFFSET_FLAG: 0;
|
|
14
|
+
readonly OFFSET_BODY_SIZE: 1;
|
|
15
|
+
readonly OFFSET_PK: 3;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Returns whether the row is deleted.
|
|
19
|
+
* @param row Row data
|
|
20
|
+
* @returns Whether deleted
|
|
21
|
+
*/
|
|
22
|
+
getDeletedFlag(row: Uint8Array): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Returns whether the row is overflowed.
|
|
25
|
+
* @param row Row data
|
|
26
|
+
* @returns Whether overflowed
|
|
27
|
+
*/
|
|
28
|
+
getOverflowFlag(row: Uint8Array): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the size of the row body. This represents purely the data size of the row.
|
|
31
|
+
* @param row Row data
|
|
32
|
+
* @returns Body size
|
|
33
|
+
*/
|
|
34
|
+
getBodySize(row: Uint8Array): number;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the primary key (PK) of the row.
|
|
37
|
+
* @param row Row data
|
|
38
|
+
* @returns Primary key (PK)
|
|
39
|
+
*/
|
|
40
|
+
getPK(row: Uint8Array): number;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the row body.
|
|
43
|
+
* @param row Row data
|
|
44
|
+
* @returns Row body
|
|
45
|
+
*/
|
|
46
|
+
getBody(row: Uint8Array): Uint8Array;
|
|
47
|
+
/**
|
|
48
|
+
* Sets whether the row is deleted.
|
|
49
|
+
* @param row Row data
|
|
50
|
+
* @param deleted Whether deleted
|
|
51
|
+
*/
|
|
52
|
+
setDeletedFlag(row: Uint8Array, deleted: boolean): void;
|
|
53
|
+
/**
|
|
54
|
+
* Sets whether the row is overflowed.
|
|
55
|
+
* @param row Row data
|
|
56
|
+
* @param overflow Whether overflowed
|
|
57
|
+
*/
|
|
58
|
+
setOverflowFlag(row: Uint8Array, overflow: boolean): void;
|
|
59
|
+
/**
|
|
60
|
+
* Sets the size of the row body.
|
|
61
|
+
* @param row Row data
|
|
62
|
+
* @param rowSize Body size
|
|
63
|
+
*/
|
|
64
|
+
setBodySize(row: Uint8Array, rowSize: number): void;
|
|
65
|
+
/**
|
|
66
|
+
* Sets the primary key (PK) of the row.
|
|
67
|
+
* @param row Row data
|
|
68
|
+
* @param pk Primary key (PK)
|
|
69
|
+
*/
|
|
70
|
+
setPK(row: Uint8Array, pk: number): void;
|
|
71
|
+
/**
|
|
72
|
+
* Sets the row body.
|
|
73
|
+
* @param row Row data
|
|
74
|
+
* @param body Row body
|
|
75
|
+
*/
|
|
76
|
+
setBody(row: Uint8Array, body: Uint8Array): void;
|
|
77
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BPTreeNode, SerializeStrategyHead } from 'serializable-bptree';
|
|
2
|
+
import { SerializeStrategyAsync } from 'serializable-bptree';
|
|
3
|
+
import { PageFileSystem } from './PageFileSystem';
|
|
4
|
+
import { IndexPageManager, PageManagerFactory } from './Page';
|
|
5
|
+
import { TextCodec } from '../utils/TextCodec';
|
|
6
|
+
export declare class RowIdentifierStrategy extends SerializeStrategyAsync<number, number> {
|
|
7
|
+
readonly order: number;
|
|
8
|
+
protected readonly pfs: PageFileSystem;
|
|
9
|
+
protected rootPageId: number;
|
|
10
|
+
protected factory: PageManagerFactory;
|
|
11
|
+
protected indexPageManger: IndexPageManager;
|
|
12
|
+
protected codec: TextCodec;
|
|
13
|
+
constructor(order: number, pfs: PageFileSystem);
|
|
14
|
+
id(isLeaf: boolean): Promise<string>;
|
|
15
|
+
read(id: string): Promise<BPTreeNode<number, number>>;
|
|
16
|
+
write(id: string, node: BPTreeNode<number, number>): Promise<void>;
|
|
17
|
+
delete(id: string): Promise<void>;
|
|
18
|
+
readHead(): Promise<SerializeStrategyHead | null>;
|
|
19
|
+
writeHead(head: SerializeStrategyHead): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { DataplyMetadata, DataplyOptions } from '../types';
|
|
2
|
+
import { BPTreeAsync } from 'serializable-bptree';
|
|
3
|
+
import { PageFileSystem } from './PageFileSystem';
|
|
4
|
+
import { Row } from './Row';
|
|
5
|
+
import { KeyManager } from './KeyManager';
|
|
6
|
+
import { PageManagerFactory } from './Page';
|
|
7
|
+
import { Transaction } from './transaction/Transaction';
|
|
8
|
+
export declare class RowTableEngine {
|
|
9
|
+
protected readonly pfs: PageFileSystem;
|
|
10
|
+
protected readonly options: Required<DataplyOptions>;
|
|
11
|
+
protected readonly bptree: BPTreeAsync<number, number>;
|
|
12
|
+
protected readonly order: number;
|
|
13
|
+
protected readonly factory: PageManagerFactory;
|
|
14
|
+
private readonly metadataPageManager;
|
|
15
|
+
private readonly dataPageManager;
|
|
16
|
+
private readonly overflowPageManager;
|
|
17
|
+
protected readonly keyManager: KeyManager;
|
|
18
|
+
protected readonly rowManager: Row;
|
|
19
|
+
protected readonly maxBodySize: number;
|
|
20
|
+
private readonly ridBuffer;
|
|
21
|
+
private readonly pageIdBuffer;
|
|
22
|
+
private initialized;
|
|
23
|
+
constructor(pfs: PageFileSystem, options: Required<DataplyOptions>);
|
|
24
|
+
/**
|
|
25
|
+
* Initializes the B+ Tree.
|
|
26
|
+
*/
|
|
27
|
+
init(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Calculates the optimized order.
|
|
30
|
+
* @param pageSize Page size
|
|
31
|
+
* @param keySize Key size
|
|
32
|
+
* @param pointerSize Pointer size
|
|
33
|
+
* @returns Optimal order
|
|
34
|
+
*/
|
|
35
|
+
protected getOptimalOrder(pageSize: number, keySize: number, pointerSize: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the actual row size generated from the data.
|
|
38
|
+
* @param rowBody Data
|
|
39
|
+
* @returns Actual row size generated
|
|
40
|
+
*/
|
|
41
|
+
protected getRequiredRowSize(rowBody: Uint8Array): number;
|
|
42
|
+
/**
|
|
43
|
+
* Sets the RID in the buffer.
|
|
44
|
+
* @param pageId Page ID
|
|
45
|
+
* @param slotIndex Slot index
|
|
46
|
+
* @returns Buffer
|
|
47
|
+
*/
|
|
48
|
+
private setRID;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the RID from the buffer.
|
|
51
|
+
* @returns RID
|
|
52
|
+
*/
|
|
53
|
+
private getRID;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the metadata of the dataply.
|
|
56
|
+
* @param tx Transaction
|
|
57
|
+
* @returns Metadata
|
|
58
|
+
*/
|
|
59
|
+
getMetadata(tx: Transaction): Promise<DataplyMetadata>;
|
|
60
|
+
/**
|
|
61
|
+
* Inserts data.
|
|
62
|
+
* @param data Data
|
|
63
|
+
* @param incrementRowCount Whether to increment the row count to metadata
|
|
64
|
+
* @param tx Transaction
|
|
65
|
+
* @returns PK of the inserted data
|
|
66
|
+
*/
|
|
67
|
+
insert(data: Uint8Array, incrementRowCount: boolean, tx: Transaction): Promise<number>;
|
|
68
|
+
/**
|
|
69
|
+
* Looks up the RID by PK.
|
|
70
|
+
* Checks Pending Updates first if a transaction exists.
|
|
71
|
+
* @param pk PK
|
|
72
|
+
* @param tx Transaction
|
|
73
|
+
* @returns RID or null (if not found)
|
|
74
|
+
*/
|
|
75
|
+
private getRidByPK;
|
|
76
|
+
/**
|
|
77
|
+
* Looks up the RID corresponding to the PK in the B+ Tree and returns the actual row.
|
|
78
|
+
* @param pk Primary key of the row
|
|
79
|
+
* @param tx Transaction
|
|
80
|
+
* @returns Raw data of the row
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* Updates data.
|
|
84
|
+
* @param pk Primary key of the row
|
|
85
|
+
* @param data Data to update
|
|
86
|
+
* @param tx Transaction
|
|
87
|
+
*/
|
|
88
|
+
update(pk: number, data: Uint8Array, tx: Transaction): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Updates a normal row.
|
|
91
|
+
* @param page Page data
|
|
92
|
+
* @param pageId Page ID
|
|
93
|
+
* @param slotIndex Slot index
|
|
94
|
+
* @param row Row data
|
|
95
|
+
* @param pk Primary key of the row
|
|
96
|
+
* @param data Data to update
|
|
97
|
+
* @param tx Transaction
|
|
98
|
+
*/
|
|
99
|
+
private updateNormalRow;
|
|
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
|
+
* Looks up the RID corresponding to the PK in the B+ Tree and returns the actual row.
|
|
109
|
+
* @param pk Primary key of the row
|
|
110
|
+
* @param tx Transaction
|
|
111
|
+
* @returns Raw data of the row
|
|
112
|
+
*/
|
|
113
|
+
selectByPK(pk: number, tx: Transaction): Promise<Uint8Array | null>;
|
|
114
|
+
private fetchRowByRid;
|
|
115
|
+
/**
|
|
116
|
+
* Returns the count of rows.
|
|
117
|
+
* @param tx Transaction
|
|
118
|
+
* @returns Row count
|
|
119
|
+
*/
|
|
120
|
+
getRowCount(tx: Transaction): Promise<number>;
|
|
121
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { LRUMap } from 'cache-entanglement';
|
|
2
|
+
import { LogManager } from './LogManager';
|
|
3
|
+
import type { Transaction } from './transaction/Transaction';
|
|
4
|
+
/**
|
|
5
|
+
* Virtual File System class that manages and caches files in page units.
|
|
6
|
+
*/
|
|
7
|
+
export declare class VirtualFileSystem {
|
|
8
|
+
protected fileHandle: number;
|
|
9
|
+
protected pageSize: number;
|
|
10
|
+
protected pageCacheCapacity: number;
|
|
11
|
+
/** Cache list (Page ID -> Data Buffer) */
|
|
12
|
+
protected cache: LRUMap<number, Uint8Array>;
|
|
13
|
+
/** Page IDs that have changes and need disk synchronization */
|
|
14
|
+
protected dirtyPages: Set<number>;
|
|
15
|
+
/** Track logical file size */
|
|
16
|
+
protected fileSize: number;
|
|
17
|
+
/** Bit shift value for page size */
|
|
18
|
+
protected pageShift: number;
|
|
19
|
+
/** Bit mask value for page size */
|
|
20
|
+
protected pageMask: number;
|
|
21
|
+
protected logManager?: LogManager;
|
|
22
|
+
protected dirtyPageOwners: Map<number, Transaction>;
|
|
23
|
+
protected activeTransactions: Map<number, Transaction>;
|
|
24
|
+
constructor(fileHandle: number, pageSize: number, pageCacheCapacity: number, walPath?: string | undefined | null);
|
|
25
|
+
/**
|
|
26
|
+
* Performs recovery (Redo) using WAL logs.
|
|
27
|
+
* Called in constructor, so it's a synchronous process and data is only reflected in cache.
|
|
28
|
+
* Actual disk sync and log clearing are performed during future transactions or closure.
|
|
29
|
+
*/
|
|
30
|
+
private recover;
|
|
31
|
+
/**
|
|
32
|
+
* Commits the transaction.
|
|
33
|
+
* @param tx Transaction
|
|
34
|
+
*/
|
|
35
|
+
commit(tx: Transaction): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Rolls back the transaction.
|
|
38
|
+
* @param tx Transaction
|
|
39
|
+
*/
|
|
40
|
+
rollback(tx: Transaction): Promise<void>;
|
|
41
|
+
private cleanupTransaction;
|
|
42
|
+
/**
|
|
43
|
+
* Reads data from a specific position in the file.
|
|
44
|
+
* @param handle File handle
|
|
45
|
+
* @param buffer Data buffer to read into
|
|
46
|
+
* @param offset Start position in buffer
|
|
47
|
+
* @param length Length of data to read
|
|
48
|
+
* @param position Start position in file
|
|
49
|
+
* @returns Length of data read
|
|
50
|
+
*/
|
|
51
|
+
protected _readAsync(handle: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<number>;
|
|
52
|
+
/**
|
|
53
|
+
* Writes data to a specific position in the file.
|
|
54
|
+
* @param handle File handle
|
|
55
|
+
* @param buffer Data buffer to write
|
|
56
|
+
* @param offset Start position in buffer
|
|
57
|
+
* @param length Length of data to write
|
|
58
|
+
* @param position Start position in file
|
|
59
|
+
*/
|
|
60
|
+
protected _writeAsync(handle: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Appends data to the end of the file.
|
|
63
|
+
* @param buffer Data buffer to append
|
|
64
|
+
*/
|
|
65
|
+
protected _appendAsync(handle: number, buffer: Uint8Array): Promise<void>;
|
|
66
|
+
protected _readPage(pageIndex: number, tx: Transaction): Promise<Uint8Array>;
|
|
67
|
+
/**
|
|
68
|
+
* Reads data from a specific position in the file.
|
|
69
|
+
* @param offset Start position
|
|
70
|
+
* @param length Length of data to read
|
|
71
|
+
* @param tx Transaction
|
|
72
|
+
* @returns Read data buffer
|
|
73
|
+
*/
|
|
74
|
+
read(offset: number, length: number, tx: Transaction): Promise<Uint8Array>;
|
|
75
|
+
/**
|
|
76
|
+
* Appends data to the end of the file.
|
|
77
|
+
* @param buffer Data buffer to append
|
|
78
|
+
* @returns Length of appended data
|
|
79
|
+
*/
|
|
80
|
+
append(buffer: Uint8Array, tx: Transaction): Promise<number>;
|
|
81
|
+
/**
|
|
82
|
+
* Writes data to a specific position in the file.
|
|
83
|
+
* @param offset Start position
|
|
84
|
+
* @param buffer Data buffer to write
|
|
85
|
+
* @returns Length of data written
|
|
86
|
+
*/
|
|
87
|
+
write(offset: number, buffer: Uint8Array, tx: Transaction): Promise<number>;
|
|
88
|
+
/**
|
|
89
|
+
* Synchronizes dirty pages to disk.
|
|
90
|
+
*/
|
|
91
|
+
sync(): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Closes the file.
|
|
94
|
+
*/
|
|
95
|
+
close(): Promise<void>;
|
|
96
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lock Manager class.
|
|
3
|
+
* Controls concurrency for page access.
|
|
4
|
+
* Implemented using the Ryoiki library.
|
|
5
|
+
*/
|
|
6
|
+
export declare class LockManager {
|
|
7
|
+
private lock;
|
|
8
|
+
private unlockMap;
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
* Requests a read (Shared) lock for a page.
|
|
12
|
+
* Ryoiki maintains the lock until explicitly released, even if the callback ends.
|
|
13
|
+
* @param pageId Page ID
|
|
14
|
+
* @returns Lock ID to be used for releasing the lock
|
|
15
|
+
*/
|
|
16
|
+
acquireRead(pageId: number): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Requests a write (Exclusive) lock for a page.
|
|
19
|
+
* @param pageId Page ID
|
|
20
|
+
* @returns Lock ID to be used for releasing the lock
|
|
21
|
+
*/
|
|
22
|
+
acquireWrite(pageId: number): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Releases a lock.
|
|
25
|
+
* @param lockId Lock ID to release
|
|
26
|
+
*/
|
|
27
|
+
release(lockId: string): void;
|
|
28
|
+
}
|