@the-w1zard/singularity 1.0.12 → 1.0.14
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/README.html +450 -0
- package/README.md +327 -2
- package/dist/bus_db_rpc/RULE_OF_2_same_node.d.ts +139 -0
- package/dist/bus_db_rpc/RULE_OF_2_same_node.d.ts.map +1 -0
- package/dist/bus_db_rpc/RULE_OF_2_same_node.js +502 -0
- package/dist/class.d.ts +161 -0
- package/dist/class.d.ts.map +1 -0
- package/dist/class.js +804 -0
- package/dist/comps/DISRUPTOR_CRYPTO.d.ts +47 -0
- package/dist/comps/DISRUPTOR_CRYPTO.d.ts.map +1 -0
- package/dist/comps/RULE_OF_2_same_app.d.ts +175 -0
- package/dist/comps/RULE_OF_2_same_app.d.ts.map +1 -0
- package/dist/comps/RULE_OF_2_same_app.js +482 -0
- package/dist/comps/binary.d.ts +71 -0
- package/dist/comps/binary.d.ts.map +1 -0
- package/dist/comps/byte_id.d.ts +12 -0
- package/dist/comps/byte_id.d.ts.map +1 -0
- package/dist/comps/cell-worker.d.ts +9 -0
- package/dist/comps/cell-worker.d.ts.map +1 -0
- package/dist/comps/cell.d.ts +349 -0
- package/dist/comps/cell.d.ts.map +1 -0
- package/dist/comps/create_live_self.d.ts +22 -0
- package/dist/comps/create_live_self.d.ts.map +1 -0
- package/dist/comps/queue.d.ts +103 -0
- package/dist/comps/queue.d.ts.map +1 -0
- package/dist/comps/serializer.d.ts +30 -0
- package/dist/comps/serializer.d.ts.map +1 -0
- package/dist/comps/serializer.js +131 -0
- package/dist/comps/vm-types.d.ts +20 -0
- package/dist/comps/vm-types.d.ts.map +1 -0
- package/dist/db/binary.d.ts +76 -0
- package/dist/db/binary.d.ts.map +1 -0
- package/dist/db/binary.js +441 -0
- package/dist/db/client.d.ts +28 -0
- package/dist/db/client.d.ts.map +1 -0
- package/dist/db/client.js +82 -0
- package/dist/db/db.d.ts +141 -0
- package/dist/db/db.d.ts.map +1 -0
- package/dist/db/db.js +898 -0
- package/dist/db/op.d.ts +13 -0
- package/dist/db/op.d.ts.map +1 -0
- package/dist/db/op.js +11 -0
- package/dist/db/storage-worker.d.ts +2 -0
- package/dist/db/storage-worker.d.ts.map +1 -0
- package/dist/db/storage-worker.js +152 -0
- package/dist/db/types.d.ts +134 -0
- package/dist/db/types.d.ts.map +1 -0
- package/dist/db/types.js +14 -0
- package/dist/db/wal.d.ts +30 -0
- package/dist/db/wal.d.ts.map +1 -0
- package/dist/db/wal.js +217 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/signals.js +10740 -5498
- package/dist/slab.d.ts +95 -0
- package/dist/slab.d.ts.map +1 -0
- package/dist/slab.js +575 -0
- package/dist/slab.types.d.ts +5 -0
- package/dist/slab.types.d.ts.map +1 -0
- package/dist/slab.types.js +1 -0
- package/dist/test.d.ts +152 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +1183 -0
- package/dist/testt.d.ts +61 -0
- package/dist/testt.d.ts.map +1 -0
- package/dist/testt.js +143 -0
- package/dist/y.d.ts +152 -0
- package/dist/y.d.ts.map +1 -0
- package/dist/y.js +36 -65
- package/package.json +22 -19
package/dist/db/op.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const KV_RPC_OP: {
|
|
2
|
+
readonly GET: 1;
|
|
3
|
+
readonly SET: 2;
|
|
4
|
+
readonly DELETE: 3;
|
|
5
|
+
readonly UPDATE: 4;
|
|
6
|
+
readonly SUBSCRIBE: 5;
|
|
7
|
+
readonly UNSUBSCRIBE: 6;
|
|
8
|
+
readonly SNAPSHOT: 7;
|
|
9
|
+
readonly CHANGE: 8;
|
|
10
|
+
readonly ERROR: 9;
|
|
11
|
+
};
|
|
12
|
+
export type KV_RPC_OP_VALUE = typeof KV_RPC_OP[keyof typeof KV_RPC_OP];
|
|
13
|
+
//# sourceMappingURL=op.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"op.d.ts","sourceRoot":"","sources":["../../src/db/op.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;;;;;;;;;;CAUV,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,OAAO,SAAS,CAAC,MAAM,OAAO,SAAS,CAAC,CAAC"}
|
package/dist/db/op.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-worker.d.ts","sourceRoot":"","sources":["../../src/db/storage-worker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { parentPort } from "node:worker_threads";
|
|
2
|
+
import { open, rename, truncate } from "node:fs/promises";
|
|
3
|
+
import { PAGE_SIZE } from "./types";
|
|
4
|
+
let dataFile;
|
|
5
|
+
let walFile;
|
|
6
|
+
let compactFile;
|
|
7
|
+
let walSize = 0;
|
|
8
|
+
function send(response) {
|
|
9
|
+
parentPort.postMessage(response);
|
|
10
|
+
}
|
|
11
|
+
function fail(id, error) {
|
|
12
|
+
send({
|
|
13
|
+
kind: "error",
|
|
14
|
+
id,
|
|
15
|
+
error: error instanceof Error ? error.message : String(error)
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async function readPage(pageNumber, dataFile) {
|
|
19
|
+
const page = new Uint8Array(PAGE_SIZE);
|
|
20
|
+
const buffer = Buffer.from(page.buffer, page.byteOffset, page.byteLength);
|
|
21
|
+
const result = await dataFile.read(buffer, 0, PAGE_SIZE, pageNumber * PAGE_SIZE);
|
|
22
|
+
if (result.bytesRead !== PAGE_SIZE) {
|
|
23
|
+
throw new Error(`Short read: page ${pageNumber}, read ${result.bytesRead} bytes`);
|
|
24
|
+
}
|
|
25
|
+
return page;
|
|
26
|
+
}
|
|
27
|
+
parentPort.on("message", async (request) => {
|
|
28
|
+
try {
|
|
29
|
+
switch (request.kind) {
|
|
30
|
+
case "init": {
|
|
31
|
+
dataFile = await open(request.dataPath, "r+");
|
|
32
|
+
walFile = await open(request.walPath, "r+");
|
|
33
|
+
const walStat = await walFile.stat();
|
|
34
|
+
walSize = Number(walStat.size);
|
|
35
|
+
send({
|
|
36
|
+
kind: "initOk",
|
|
37
|
+
id: request.id
|
|
38
|
+
});
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case "readPages": {
|
|
42
|
+
if (!dataFile) {
|
|
43
|
+
throw new Error("Data file is not open");
|
|
44
|
+
}
|
|
45
|
+
const pages = [];
|
|
46
|
+
for (const pageNumber of request.pageNumbers) {
|
|
47
|
+
pages.push(await readPage(pageNumber, dataFile));
|
|
48
|
+
}
|
|
49
|
+
send({
|
|
50
|
+
kind: "readPagesOk",
|
|
51
|
+
id: request.id,
|
|
52
|
+
pages
|
|
53
|
+
});
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case "appendWal": {
|
|
57
|
+
if (!walFile) {
|
|
58
|
+
throw new Error("WAL file is not open");
|
|
59
|
+
}
|
|
60
|
+
await walFile.write(request.bytes, 0, request.bytes.byteLength, walSize);
|
|
61
|
+
await walFile.sync();
|
|
62
|
+
walSize += request.bytes.byteLength;
|
|
63
|
+
send({
|
|
64
|
+
kind: "walOk",
|
|
65
|
+
id: request.id,
|
|
66
|
+
batchId: request.batchId,
|
|
67
|
+
walSize
|
|
68
|
+
});
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
case "checkpoint": {
|
|
72
|
+
if (!dataFile) {
|
|
73
|
+
throw new Error("Data file is not open");
|
|
74
|
+
}
|
|
75
|
+
for (const page of request.pages) {
|
|
76
|
+
await dataFile.write(page.buffer, 0, page.buffer.byteLength, page.pageNumber * PAGE_SIZE);
|
|
77
|
+
}
|
|
78
|
+
await dataFile.sync();
|
|
79
|
+
send({
|
|
80
|
+
kind: "checkpointOk",
|
|
81
|
+
id: request.id
|
|
82
|
+
});
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
case "compactBegin": {
|
|
86
|
+
compactFile = await open(request.compactPath, "w+");
|
|
87
|
+
send({
|
|
88
|
+
kind: "compactOk",
|
|
89
|
+
id: request.id
|
|
90
|
+
});
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "compactWrite": {
|
|
94
|
+
if (!compactFile) {
|
|
95
|
+
throw new Error("Compact file is not open");
|
|
96
|
+
}
|
|
97
|
+
for (const page of request.pages) {
|
|
98
|
+
await compactFile.write(page.buffer, 0, page.buffer.byteLength, page.pageNumber * PAGE_SIZE);
|
|
99
|
+
}
|
|
100
|
+
send({
|
|
101
|
+
kind: "compactOk",
|
|
102
|
+
id: request.id
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "compactCommit": {
|
|
107
|
+
if (!compactFile) {
|
|
108
|
+
throw new Error("Compact file is not open");
|
|
109
|
+
}
|
|
110
|
+
await compactFile.sync();
|
|
111
|
+
await compactFile.close();
|
|
112
|
+
compactFile = undefined;
|
|
113
|
+
await rename(request.compactPath, request.dataPath);
|
|
114
|
+
if (dataFile) {
|
|
115
|
+
await dataFile.close();
|
|
116
|
+
}
|
|
117
|
+
dataFile = await open(request.dataPath, "r+");
|
|
118
|
+
await truncate(request.walPath, 0);
|
|
119
|
+
if (walFile) {
|
|
120
|
+
await walFile.close();
|
|
121
|
+
}
|
|
122
|
+
walFile = await open(request.walPath, "r+");
|
|
123
|
+
walSize = 0;
|
|
124
|
+
send({
|
|
125
|
+
kind: "compactOk",
|
|
126
|
+
id: request.id
|
|
127
|
+
});
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case "close": {
|
|
131
|
+
if (dataFile) {
|
|
132
|
+
await dataFile.sync();
|
|
133
|
+
await dataFile.close();
|
|
134
|
+
dataFile = undefined;
|
|
135
|
+
}
|
|
136
|
+
if (walFile) {
|
|
137
|
+
await walFile.sync();
|
|
138
|
+
await walFile.close();
|
|
139
|
+
walFile = undefined;
|
|
140
|
+
}
|
|
141
|
+
send({
|
|
142
|
+
kind: "closeOk",
|
|
143
|
+
id: request.id
|
|
144
|
+
});
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
fail(request.id, error);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export declare const PAGE_SIZE = 4096;
|
|
2
|
+
export declare const PAGE_HEADER_SIZE = 24;
|
|
3
|
+
export declare const PAGE_PAYLOAD_CAPACITY: number;
|
|
4
|
+
export declare const NO_PAGE = 4294967295;
|
|
5
|
+
export declare const PAGE_FREE = 0;
|
|
6
|
+
export declare const PAGE_RECORD_HEAD = 1;
|
|
7
|
+
export declare const PAGE_RECORD_CONTINUATION = 2;
|
|
8
|
+
export declare const OP_PUT = 1;
|
|
9
|
+
export declare const OP_DELETE = 2;
|
|
10
|
+
export interface StoredEntry {
|
|
11
|
+
pageNumber: number;
|
|
12
|
+
pageNumbers: number[];
|
|
13
|
+
}
|
|
14
|
+
export interface PendingWorkerRequest {
|
|
15
|
+
resolve: (response: WorkerResponse) => void;
|
|
16
|
+
reject: (error: Error) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface PendingBatch {
|
|
19
|
+
batchId: string;
|
|
20
|
+
records: Uint8Array[];
|
|
21
|
+
byteLength: number;
|
|
22
|
+
durable: boolean;
|
|
23
|
+
waiters: Array<() => void>;
|
|
24
|
+
}
|
|
25
|
+
export declare const WAL_VERSION = 1;
|
|
26
|
+
export declare const MANIFEST_VERSION = 1;
|
|
27
|
+
export type WorkerRequestInput = Omit<Extract<WorkerRequest, {
|
|
28
|
+
kind: "init";
|
|
29
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
30
|
+
kind: "readPages";
|
|
31
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
32
|
+
kind: "appendWal";
|
|
33
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
34
|
+
kind: "checkpoint";
|
|
35
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
36
|
+
kind: "compactBegin";
|
|
37
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
38
|
+
kind: "compactWrite";
|
|
39
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
40
|
+
kind: "compactCommit";
|
|
41
|
+
}>, "id"> | Omit<Extract<WorkerRequest, {
|
|
42
|
+
kind: "close";
|
|
43
|
+
}>, "id">;
|
|
44
|
+
export type Durability = "buffered" | "group" | "strict";
|
|
45
|
+
export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
46
|
+
export type WorkerRequestWithoutId = DistributiveOmit<WorkerRequest, "id">;
|
|
47
|
+
export interface FastKVOptions {
|
|
48
|
+
directory: string;
|
|
49
|
+
/**
|
|
50
|
+
* buffered: WAL write is queued; no fsync.
|
|
51
|
+
* group: every WAL batch is fsynced before its waiters resolve.
|
|
52
|
+
* strict: each durable write waits for the fsync of the batch containing it.
|
|
53
|
+
*/
|
|
54
|
+
durability: Durability;
|
|
55
|
+
walBatchBytes: number;
|
|
56
|
+
walBatchRecords: number;
|
|
57
|
+
walFlushIntervalMs: number;
|
|
58
|
+
walHighWaterBytes: number;
|
|
59
|
+
checkpointIntervalMs: number;
|
|
60
|
+
}
|
|
61
|
+
export interface PageWrite {
|
|
62
|
+
pageNumber: number;
|
|
63
|
+
buffer: Uint8Array;
|
|
64
|
+
}
|
|
65
|
+
export type WorkerRequest = {
|
|
66
|
+
kind: "init";
|
|
67
|
+
id: string;
|
|
68
|
+
dataPath: string;
|
|
69
|
+
walPath: string;
|
|
70
|
+
} | {
|
|
71
|
+
kind: "readPages";
|
|
72
|
+
id: string;
|
|
73
|
+
pageNumbers: number[];
|
|
74
|
+
} | {
|
|
75
|
+
kind: "appendWal";
|
|
76
|
+
id: string;
|
|
77
|
+
batchId: string;
|
|
78
|
+
bytes: Uint8Array;
|
|
79
|
+
durable: boolean;
|
|
80
|
+
} | {
|
|
81
|
+
kind: "checkpoint";
|
|
82
|
+
id: string;
|
|
83
|
+
pages: PageWrite[];
|
|
84
|
+
manifest: Uint8Array;
|
|
85
|
+
} | {
|
|
86
|
+
kind: "compactBegin";
|
|
87
|
+
id: string;
|
|
88
|
+
compactPath: string;
|
|
89
|
+
} | {
|
|
90
|
+
kind: "compactWrite";
|
|
91
|
+
id: string;
|
|
92
|
+
pages: PageWrite[];
|
|
93
|
+
} | {
|
|
94
|
+
kind: "compactCommit";
|
|
95
|
+
id: string;
|
|
96
|
+
dataPath: string;
|
|
97
|
+
compactPath: string;
|
|
98
|
+
manifest: Uint8Array;
|
|
99
|
+
walPath: string;
|
|
100
|
+
} | {
|
|
101
|
+
kind: "close";
|
|
102
|
+
id: string;
|
|
103
|
+
};
|
|
104
|
+
export type WorkerResponse = {
|
|
105
|
+
kind: "walOk";
|
|
106
|
+
id: string;
|
|
107
|
+
batchId: string;
|
|
108
|
+
walSize: number;
|
|
109
|
+
} | {
|
|
110
|
+
kind: "readPagesOk";
|
|
111
|
+
id: string;
|
|
112
|
+
pages: Uint8Array[];
|
|
113
|
+
} | {
|
|
114
|
+
kind: "checkpointOk";
|
|
115
|
+
id: string;
|
|
116
|
+
} | {
|
|
117
|
+
kind: "compactOk";
|
|
118
|
+
id: string;
|
|
119
|
+
} | {
|
|
120
|
+
kind: "initOk";
|
|
121
|
+
id: string;
|
|
122
|
+
} | {
|
|
123
|
+
kind: "closeOk";
|
|
124
|
+
id: string;
|
|
125
|
+
} | {
|
|
126
|
+
kind: "error";
|
|
127
|
+
id: string;
|
|
128
|
+
error: string;
|
|
129
|
+
};
|
|
130
|
+
export type WorkerErrorResponse = Extract<WorkerResponse, {
|
|
131
|
+
kind: "error";
|
|
132
|
+
}>;
|
|
133
|
+
export declare function isWorkerError(response: WorkerResponse): response is WorkerErrorResponse;
|
|
134
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/db/types.ts"],"names":[],"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CAAC;IACrB,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,WAAW,CAAC;IAClB,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,SAAS,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,OAAO,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAGR,MAAM,MAAM,mBAAmB,GAAG,OAAO,CACvC,cAAc,EACd;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAClB,CAAC;AAEF,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,cAAc,GACvB,QAAQ,IAAI,mBAAmB,CAEjC"}
|
package/dist/db/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const PAGE_SIZE = 4096;
|
|
2
|
+
export const PAGE_HEADER_SIZE = 24;
|
|
3
|
+
export const PAGE_PAYLOAD_CAPACITY = PAGE_SIZE - PAGE_HEADER_SIZE;
|
|
4
|
+
export const NO_PAGE = 0xffffffff;
|
|
5
|
+
export const PAGE_FREE = 0;
|
|
6
|
+
export const PAGE_RECORD_HEAD = 1;
|
|
7
|
+
export const PAGE_RECORD_CONTINUATION = 2;
|
|
8
|
+
export const OP_PUT = 1;
|
|
9
|
+
export const OP_DELETE = 2;
|
|
10
|
+
export const WAL_VERSION = 1;
|
|
11
|
+
export const MANIFEST_VERSION = 1;
|
|
12
|
+
export function isWorkerError(response) {
|
|
13
|
+
return response.kind === "error";
|
|
14
|
+
}
|
package/dist/db/wal.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface Manifest {
|
|
2
|
+
checkpointSequence: bigint;
|
|
3
|
+
pageCount: number;
|
|
4
|
+
}
|
|
5
|
+
export declare function encodeManifest(manifest: Manifest): Uint8Array;
|
|
6
|
+
export declare function decodeManifest(bytes: Uint8Array): Manifest | undefined;
|
|
7
|
+
export interface WalRecord {
|
|
8
|
+
sequence: bigint;
|
|
9
|
+
operation: number;
|
|
10
|
+
key: Uint8Array;
|
|
11
|
+
value: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
export declare function encodeWalRecord(record: WalRecord): Uint8Array;
|
|
14
|
+
export interface DecodedWalResult {
|
|
15
|
+
records: WalRecord[];
|
|
16
|
+
validBytes: number;
|
|
17
|
+
}
|
|
18
|
+
export declare function decodeWalRecords(bytes: Uint8Array, minimumSequence: bigint): DecodedWalResult;
|
|
19
|
+
export interface RecordChain {
|
|
20
|
+
pageNumbers: number[];
|
|
21
|
+
pages: Uint8Array[];
|
|
22
|
+
}
|
|
23
|
+
export declare function encodeRecordChain(key: Uint8Array, value: Uint8Array, allocatePage: () => number): RecordChain;
|
|
24
|
+
export interface DecodedRecord {
|
|
25
|
+
key: Uint8Array;
|
|
26
|
+
value: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
export declare function decodeRecordChain(readPage: (pageNumber: number) => Uint8Array | undefined, headPageNumber: number): DecodedRecord | undefined;
|
|
29
|
+
export declare function encodeFreePage(): Uint8Array;
|
|
30
|
+
//# sourceMappingURL=wal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wal.d.ts","sourceRoot":"","sources":["../../src/db/wal.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,QAAQ;IACxB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;CAClB;AACD,wBAAgB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,UAAU,CAW7D;AACD,wBAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,GAAG,QAAQ,GAAG,SAAS,CAuBtE;AAID,MAAM,WAAW,SAAS;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,UAAU,CAAC;IAChB,KAAK,EAAE,UAAU,CAAC;CAClB;AACD,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,GAAG,UAAU,CAmC7D;AACD,MAAM,WAAW,gBAAgB;IAChC,OAAO,EAAE,SAAS,EAAE,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACnB;AACD,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,GAAG,gBAAgB,CA+D7F;AAID,MAAM,WAAW,WAAW;IAC3B,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,KAAK,EAAE,UAAU,EAAE,CAAC;CACpB;AACD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,MAAM,GAAG,WAAW,CAoE7G;AACD,MAAM,WAAW,aAAa;IAC7B,GAAG,EAAE,UAAU,CAAC;IAChB,KAAK,EAAE,UAAU,CAAC;CAClB;AACD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,UAAU,GAAG,SAAS,EAAE,cAAc,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CA2D7I;AAQD,wBAAgB,cAAc,IAAI,UAAU,CAE3C"}
|
package/dist/db/wal.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { writeBigUint64, writeUint32, readUint32, readBigUint64, crc32 } from "./binary";
|
|
2
|
+
import { MANIFEST_VERSION, WAL_VERSION, PAGE_PAYLOAD_CAPACITY, PAGE_SIZE, PAGE_RECORD_HEAD, PAGE_RECORD_CONTINUATION, NO_PAGE, PAGE_HEADER_SIZE, PAGE_FREE } from "./types";
|
|
3
|
+
// MANIFEST
|
|
4
|
+
// #region ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
|
5
|
+
const MANIFEST_MAGIC = 0x464b5631; // FKV1
|
|
6
|
+
const MANIFEST_SIZE = 24;
|
|
7
|
+
export function encodeManifest(manifest) {
|
|
8
|
+
const bytes = new Uint8Array(MANIFEST_SIZE);
|
|
9
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
10
|
+
view.setUint32(0, MANIFEST_MAGIC, true);
|
|
11
|
+
view.setUint32(4, MANIFEST_VERSION, true);
|
|
12
|
+
writeBigUint64(bytes, 8, manifest.checkpointSequence);
|
|
13
|
+
writeUint32(bytes, 16, manifest.pageCount);
|
|
14
|
+
writeUint32(bytes, 20, crc32(bytes, 0, 20));
|
|
15
|
+
return bytes;
|
|
16
|
+
}
|
|
17
|
+
export function decodeManifest(bytes) {
|
|
18
|
+
if (bytes.length < MANIFEST_SIZE)
|
|
19
|
+
return undefined;
|
|
20
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
21
|
+
if (view.getUint32(0, true) !== MANIFEST_MAGIC) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (view.getUint32(4, true) !== MANIFEST_VERSION) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const storedChecksum = readUint32(bytes, 20);
|
|
28
|
+
if (storedChecksum !== crc32(bytes, 0, 20)) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
checkpointSequence: readBigUint64(bytes, 8),
|
|
33
|
+
pageCount: readUint32(bytes, 16),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function encodeWalRecord(record) {
|
|
37
|
+
// Single allocation: the frame is written in place, no body staging buffer.
|
|
38
|
+
const bodyLength = 1 + 1 + 8 + 4 + 4 + record.key.length + record.value.length;
|
|
39
|
+
const frame = new Uint8Array(4 + bodyLength + 4);
|
|
40
|
+
const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
41
|
+
view.setUint32(0, bodyLength, true);
|
|
42
|
+
let offset = 4;
|
|
43
|
+
view.setUint8(offset, WAL_VERSION);
|
|
44
|
+
offset += 1;
|
|
45
|
+
view.setUint8(offset, record.operation);
|
|
46
|
+
offset += 1;
|
|
47
|
+
view.setBigUint64(offset, record.sequence, true);
|
|
48
|
+
offset += 8;
|
|
49
|
+
view.setUint32(offset, record.key.length, true);
|
|
50
|
+
offset += 4;
|
|
51
|
+
view.setUint32(offset, record.value.length, true);
|
|
52
|
+
offset += 4;
|
|
53
|
+
frame.set(record.key, offset);
|
|
54
|
+
offset += record.key.length;
|
|
55
|
+
frame.set(record.value, offset);
|
|
56
|
+
view.setUint32(4 + bodyLength, crc32(frame, 4, 4 + bodyLength), true);
|
|
57
|
+
return frame;
|
|
58
|
+
}
|
|
59
|
+
export function decodeWalRecords(bytes, minimumSequence) {
|
|
60
|
+
const records = [];
|
|
61
|
+
let offset = 0;
|
|
62
|
+
while (offset + 4 <= bytes.length) {
|
|
63
|
+
const bodyLength = readUint32(bytes, offset);
|
|
64
|
+
const frameEnd = offset + 4 + bodyLength + 4;
|
|
65
|
+
if (bodyLength < 18 || frameEnd > bytes.length) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
const storedChecksum = readUint32(bytes, offset + 4 + bodyLength);
|
|
69
|
+
const actualChecksum = crc32(bytes, offset + 4, offset + 4 + bodyLength);
|
|
70
|
+
if (storedChecksum !== actualChecksum) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
const body = bytes.subarray(offset + 4, offset + 4 + bodyLength);
|
|
74
|
+
const bodyView = new DataView(body.buffer, body.byteOffset, body.byteLength);
|
|
75
|
+
const version = bodyView.getUint8(0);
|
|
76
|
+
if (version !== WAL_VERSION) {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
const operation = bodyView.getUint8(1);
|
|
80
|
+
const sequence = bodyView.getBigUint64(2, true);
|
|
81
|
+
const keyLength = bodyView.getUint32(10, true);
|
|
82
|
+
const valueLength = bodyView.getUint32(14, true);
|
|
83
|
+
if (sequence <= minimumSequence) {
|
|
84
|
+
offset = frameEnd;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const keyStart = 18;
|
|
88
|
+
const valueStart = keyStart + keyLength;
|
|
89
|
+
const valueEnd = valueStart + valueLength;
|
|
90
|
+
if (valueEnd > body.length) {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
records.push({
|
|
94
|
+
sequence,
|
|
95
|
+
operation,
|
|
96
|
+
key: body.subarray(keyStart, valueStart),
|
|
97
|
+
value: body.subarray(valueStart, valueEnd),
|
|
98
|
+
});
|
|
99
|
+
offset = frameEnd;
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
records,
|
|
103
|
+
validBytes: offset,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export function encodeRecordChain(key, value, allocatePage) {
|
|
107
|
+
// No payload staging buffer: key/value bytes are written straight into the
|
|
108
|
+
// destination pages. payload layout is [12-byte header][key][value].
|
|
109
|
+
const keyLength = key.length;
|
|
110
|
+
const valueLength = value.length;
|
|
111
|
+
const payloadLength = 12 + keyLength + valueLength;
|
|
112
|
+
const pageCount = Math.max(1, Math.ceil(payloadLength / PAGE_PAYLOAD_CAPACITY));
|
|
113
|
+
const pageNumbers = [];
|
|
114
|
+
const pages = [];
|
|
115
|
+
const valueChecksum = crc32(value);
|
|
116
|
+
const keyEndInPayload = 12 + keyLength;
|
|
117
|
+
for (let index = 0; index < pageCount; index++) {
|
|
118
|
+
const pageNumber = allocatePage();
|
|
119
|
+
const page = new Uint8Array(PAGE_SIZE);
|
|
120
|
+
const view = new DataView(page.buffer, page.byteOffset, page.byteLength);
|
|
121
|
+
const payloadStart = index * PAGE_PAYLOAD_CAPACITY;
|
|
122
|
+
const payloadEnd = Math.min(payloadLength, payloadStart + PAGE_PAYLOAD_CAPACITY);
|
|
123
|
+
const used = payloadEnd - payloadStart;
|
|
124
|
+
pageNumbers.push(pageNumber);
|
|
125
|
+
pages.push(page);
|
|
126
|
+
view.setUint32(0, index === 0 ? PAGE_RECORD_HEAD : PAGE_RECORD_CONTINUATION, true);
|
|
127
|
+
view.setUint32(4, NO_PAGE, true); // next, patched below once known
|
|
128
|
+
view.setUint32(8, used, true);
|
|
129
|
+
let dataOffset = PAGE_HEADER_SIZE;
|
|
130
|
+
let dataStart = payloadStart;
|
|
131
|
+
if (index === 0) {
|
|
132
|
+
view.setUint32(dataOffset, keyLength, true);
|
|
133
|
+
view.setUint32(dataOffset + 4, valueLength, true);
|
|
134
|
+
view.setUint32(dataOffset + 8, valueChecksum, true);
|
|
135
|
+
dataOffset += 12;
|
|
136
|
+
dataStart = 12;
|
|
137
|
+
}
|
|
138
|
+
if (dataStart < keyEndInPayload) {
|
|
139
|
+
const copyEnd = Math.min(payloadEnd, keyEndInPayload);
|
|
140
|
+
page.set(key.subarray(dataStart - 12, copyEnd - 12), dataOffset);
|
|
141
|
+
dataOffset += copyEnd - dataStart;
|
|
142
|
+
dataStart = copyEnd;
|
|
143
|
+
}
|
|
144
|
+
if (dataStart < payloadEnd) {
|
|
145
|
+
page.set(value.subarray(dataStart - keyEndInPayload, payloadEnd - keyEndInPayload), dataOffset);
|
|
146
|
+
}
|
|
147
|
+
view.setUint32(12, crc32(page, PAGE_HEADER_SIZE, PAGE_HEADER_SIZE + used), true);
|
|
148
|
+
}
|
|
149
|
+
// Patching the next pointer does not affect the checksum: crc covers
|
|
150
|
+
// [PAGE_HEADER_SIZE, ...), never the 16-byte header.
|
|
151
|
+
for (let index = 0; index < pageCount - 1; index++) {
|
|
152
|
+
const page = pages[index];
|
|
153
|
+
new DataView(page.buffer, page.byteOffset, page.byteLength).setUint32(4, pageNumbers[index + 1], true);
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
pageNumbers,
|
|
157
|
+
pages,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export function decodeRecordChain(readPage, headPageNumber) {
|
|
161
|
+
const chunks = [];
|
|
162
|
+
let pageNumber = headPageNumber;
|
|
163
|
+
let isHead = true;
|
|
164
|
+
while (pageNumber !== undefined && pageNumber !== NO_PAGE) {
|
|
165
|
+
const page = readPage(pageNumber);
|
|
166
|
+
if (!page)
|
|
167
|
+
return undefined;
|
|
168
|
+
const view = new DataView(page.buffer, page.byteOffset, page.byteLength);
|
|
169
|
+
const state = view.getUint32(0, true);
|
|
170
|
+
const nextPage = view.getUint32(4, true);
|
|
171
|
+
const used = view.getUint32(8, true);
|
|
172
|
+
const checksum = view.getUint32(12, true);
|
|
173
|
+
const expectedState = isHead ? PAGE_RECORD_HEAD : PAGE_RECORD_CONTINUATION;
|
|
174
|
+
if (state !== expectedState || used > PAGE_PAYLOAD_CAPACITY || checksum !== crc32(page, PAGE_HEADER_SIZE, PAGE_HEADER_SIZE + used)) {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
chunks.push(page.subarray(PAGE_HEADER_SIZE, PAGE_HEADER_SIZE + used));
|
|
178
|
+
pageNumber = nextPage === NO_PAGE ? undefined : nextPage;
|
|
179
|
+
isHead = false;
|
|
180
|
+
}
|
|
181
|
+
if (chunks.length === 0)
|
|
182
|
+
return undefined;
|
|
183
|
+
const payloadLength = chunks.reduce((total, chunk) => total + chunk.length, 0);
|
|
184
|
+
const payload = new Uint8Array(payloadLength);
|
|
185
|
+
let offset = 0;
|
|
186
|
+
for (const chunk of chunks) {
|
|
187
|
+
payload.set(chunk, offset);
|
|
188
|
+
offset += chunk.length;
|
|
189
|
+
}
|
|
190
|
+
if (payload.length < 12)
|
|
191
|
+
return undefined;
|
|
192
|
+
const payloadView = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
193
|
+
const keyLength = payloadView.getUint32(0, true);
|
|
194
|
+
const valueLength = payloadView.getUint32(4, true);
|
|
195
|
+
const valueChecksum = payloadView.getUint32(8, true);
|
|
196
|
+
const key = payload.subarray(12, 12 + keyLength);
|
|
197
|
+
const value = payload.subarray(12 + keyLength, 12 + keyLength + valueLength);
|
|
198
|
+
if (value.length !== valueLength)
|
|
199
|
+
return undefined;
|
|
200
|
+
if (crc32(value) !== valueChecksum)
|
|
201
|
+
return undefined;
|
|
202
|
+
return {
|
|
203
|
+
key,
|
|
204
|
+
value,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
// A free page is a constant: one shared, never-mutated buffer for every
|
|
208
|
+
// freed page instead of a fresh zeroed PAGE_SIZE allocation per page.
|
|
209
|
+
const FREE_PAGE = (() => {
|
|
210
|
+
const page = new Uint8Array(PAGE_SIZE);
|
|
211
|
+
new DataView(page.buffer, page.byteOffset, page.byteLength).setUint32(0, PAGE_FREE, true);
|
|
212
|
+
return page;
|
|
213
|
+
})();
|
|
214
|
+
export function encodeFreePage() {
|
|
215
|
+
return FREE_PAGE;
|
|
216
|
+
}
|
|
217
|
+
// #endregion
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA"}
|