@aztec/kv-store 0.0.1-commit.d1da697d6 → 0.0.1-commit.d20b825a7
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/dest/bench/shared_map_bench.d.ts +19 -0
- package/dest/bench/shared_map_bench.d.ts.map +1 -0
- package/dest/bench/shared_map_bench.js +91 -0
- package/dest/lmdb-v2/read_transaction.js +21 -19
- package/dest/sqlite-opfs/array.d.ts +21 -0
- package/dest/sqlite-opfs/array.d.ts.map +1 -0
- package/dest/sqlite-opfs/array.js +128 -0
- package/dest/sqlite-opfs/index.d.ts +7 -0
- package/dest/sqlite-opfs/index.d.ts.map +1 -0
- package/dest/sqlite-opfs/index.js +13 -0
- package/dest/sqlite-opfs/map.d.ts +35 -0
- package/dest/sqlite-opfs/map.d.ts.map +1 -0
- package/dest/sqlite-opfs/map.js +163 -0
- package/dest/sqlite-opfs/messages.d.ts +58 -0
- package/dest/sqlite-opfs/messages.d.ts.map +1 -0
- package/dest/sqlite-opfs/messages.js +5 -0
- package/dest/sqlite-opfs/multi_map.d.ts +16 -0
- package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
- package/dest/sqlite-opfs/multi_map.js +67 -0
- package/dest/sqlite-opfs/set.d.ts +13 -0
- package/dest/sqlite-opfs/set.d.ts.map +1 -0
- package/dest/sqlite-opfs/set.js +19 -0
- package/dest/sqlite-opfs/singleton.d.ts +13 -0
- package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
- package/dest/sqlite-opfs/singleton.js +48 -0
- package/dest/sqlite-opfs/store.d.ts +70 -0
- package/dest/sqlite-opfs/store.d.ts.map +1 -0
- package/dest/sqlite-opfs/store.js +242 -0
- package/dest/sqlite-opfs/worker.d.ts +2 -0
- package/dest/sqlite-opfs/worker.d.ts.map +1 -0
- package/dest/sqlite-opfs/worker.js +194 -0
- package/package.json +10 -7
- package/src/bench/shared_map_bench.ts +111 -0
- package/src/lmdb-v2/read_transaction.ts +23 -23
- package/src/sqlite-opfs/array.ts +124 -0
- package/src/sqlite-opfs/index.ts +27 -0
- package/src/sqlite-opfs/map.ts +163 -0
- package/src/sqlite-opfs/messages.ts +28 -0
- package/src/sqlite-opfs/multi_map.ts +74 -0
- package/src/sqlite-opfs/set.ts +29 -0
- package/src/sqlite-opfs/singleton.ts +48 -0
- package/src/sqlite-opfs/store.ts +248 -0
- package/src/sqlite-opfs/worker.ts +162 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import sqlite3InitModule, { type Database, type SAHPoolUtil, type Sqlite3Static } from '@sqlite.org/sqlite-wasm';
|
|
3
|
+
|
|
4
|
+
import type { ResultRow, SqlValue, WorkerRequest, WorkerResponse } from './messages.js';
|
|
5
|
+
|
|
6
|
+
const SCHEMA_SQL = `
|
|
7
|
+
CREATE TABLE IF NOT EXISTS data (
|
|
8
|
+
slot TEXT NOT NULL PRIMARY KEY,
|
|
9
|
+
container TEXT NOT NULL,
|
|
10
|
+
key BLOB NOT NULL,
|
|
11
|
+
key_count INTEGER NOT NULL,
|
|
12
|
+
hash TEXT NOT NULL,
|
|
13
|
+
value BLOB
|
|
14
|
+
) WITHOUT ROWID;
|
|
15
|
+
|
|
16
|
+
CREATE INDEX IF NOT EXISTS idx_container_key ON data(container, key);
|
|
17
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_container_key_count ON data(container, key, key_count);
|
|
18
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_container_key_hash ON data(container, key, hash);
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
const DEFAULT_SAH_POOL_DIRECTORY = '.aztec-kv';
|
|
22
|
+
const SAH_POOL_VFS_NAME = 'aztec-kv-opfs';
|
|
23
|
+
|
|
24
|
+
let sqlite3: Sqlite3Static | undefined;
|
|
25
|
+
let pool: SAHPoolUtil | undefined;
|
|
26
|
+
let db: Database | undefined;
|
|
27
|
+
let dbPath: string | undefined;
|
|
28
|
+
|
|
29
|
+
async function ensurePool(directory: string): Promise<SAHPoolUtil> {
|
|
30
|
+
sqlite3 ??= await sqlite3InitModule();
|
|
31
|
+
if (!pool) {
|
|
32
|
+
pool = await sqlite3.installOpfsSAHPoolVfs({
|
|
33
|
+
name: SAH_POOL_VFS_NAME,
|
|
34
|
+
directory,
|
|
35
|
+
initialCapacity: 8,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return pool;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function handleInit(dbName: string, ephemeral: boolean, directory?: string): Promise<void> {
|
|
42
|
+
sqlite3 ??= await sqlite3InitModule();
|
|
43
|
+
if (ephemeral) {
|
|
44
|
+
db = new sqlite3.oo1.DB(':memory:', 'c');
|
|
45
|
+
} else {
|
|
46
|
+
const p = await ensurePool(directory ?? DEFAULT_SAH_POOL_DIRECTORY);
|
|
47
|
+
dbPath = normalizeDbPath(dbName);
|
|
48
|
+
db = new p.OpfsSAHPoolDb(dbPath);
|
|
49
|
+
}
|
|
50
|
+
runSql(SCHEMA_SQL);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function handleClose(): void {
|
|
54
|
+
db?.close();
|
|
55
|
+
db = undefined;
|
|
56
|
+
dbPath = undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function handleExport(): Promise<Uint8Array> {
|
|
60
|
+
if (!db || !dbPath) {
|
|
61
|
+
throw new Error('SQLite worker: no database open to export');
|
|
62
|
+
}
|
|
63
|
+
if (!pool) {
|
|
64
|
+
throw new Error('SQLite worker: no SAH Pool available (ephemeral DBs cannot be exported)');
|
|
65
|
+
}
|
|
66
|
+
return await pool.exportFile(dbPath);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function handleDeleteDb(dbName: string): void {
|
|
70
|
+
const path = normalizeDbPath(dbName);
|
|
71
|
+
if (db && dbPath === path) {
|
|
72
|
+
db.close();
|
|
73
|
+
db = undefined;
|
|
74
|
+
dbPath = undefined;
|
|
75
|
+
}
|
|
76
|
+
// Ephemeral :memory: DBs never back a file — skip installing a pool just to unlink
|
|
77
|
+
// nothing. installOpfsSAHPoolVfs acquires an exclusive lock on the OPFS SAH
|
|
78
|
+
// directory, and under heavy test churn that can contend with workers from
|
|
79
|
+
// previous tests whose OPFS handles Chromium hasn't yet released, hanging the RPC
|
|
80
|
+
// and then the whole test run.
|
|
81
|
+
if (!pool) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
pool.unlink(path);
|
|
86
|
+
} catch {
|
|
87
|
+
// File may not exist; ignore.
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function requireDb(): Database {
|
|
92
|
+
if (!db) {
|
|
93
|
+
throw new Error('SQLite worker: no database open');
|
|
94
|
+
}
|
|
95
|
+
return db;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function runSql(sql: string, bind?: SqlValue[]): { changes: number } {
|
|
99
|
+
const conn = requireDb();
|
|
100
|
+
conn.exec({ sql, bind });
|
|
101
|
+
return { changes: conn.changes() };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function selectAll(sql: string, bind?: SqlValue[]): ResultRow[] {
|
|
105
|
+
const conn = requireDb();
|
|
106
|
+
const rows: ResultRow[] = [];
|
|
107
|
+
conn.exec({ sql, bind, rowMode: 'array', resultRows: rows });
|
|
108
|
+
return rows;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function normalizeDbPath(dbName: string): string {
|
|
112
|
+
return dbName.startsWith('/') ? dbName : `/${dbName}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function respond(msg: WorkerResponse): void {
|
|
116
|
+
(self as DedicatedWorkerGlobalScope).postMessage(msg);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
(self as DedicatedWorkerGlobalScope).onmessage = async (ev: MessageEvent<WorkerRequest>) => {
|
|
120
|
+
const req = ev.data;
|
|
121
|
+
try {
|
|
122
|
+
switch (req.type) {
|
|
123
|
+
case 'init':
|
|
124
|
+
await handleInit(req.dbName, req.ephemeral, req.poolDirectory);
|
|
125
|
+
return respond({ type: 'ok', id: req.id });
|
|
126
|
+
case 'close':
|
|
127
|
+
handleClose();
|
|
128
|
+
return respond({ type: 'ok', id: req.id });
|
|
129
|
+
case 'deleteDb':
|
|
130
|
+
handleDeleteDb(req.dbName);
|
|
131
|
+
return respond({ type: 'ok', id: req.id });
|
|
132
|
+
case 'run': {
|
|
133
|
+
const { changes } = runSql(req.sql, req.bind);
|
|
134
|
+
return respond({ type: 'ok', id: req.id, changes });
|
|
135
|
+
}
|
|
136
|
+
case 'all': {
|
|
137
|
+
const rows = selectAll(req.sql, req.bind);
|
|
138
|
+
return respond({ type: 'ok', id: req.id, rows });
|
|
139
|
+
}
|
|
140
|
+
case 'export': {
|
|
141
|
+
const bytes = await handleExport();
|
|
142
|
+
return respond({ type: 'ok', id: req.id, bytes });
|
|
143
|
+
}
|
|
144
|
+
case 'begin':
|
|
145
|
+
runSql('BEGIN');
|
|
146
|
+
return respond({ type: 'ok', id: req.id });
|
|
147
|
+
case 'commit':
|
|
148
|
+
runSql('COMMIT');
|
|
149
|
+
return respond({ type: 'ok', id: req.id });
|
|
150
|
+
case 'rollback':
|
|
151
|
+
runSql('ROLLBACK');
|
|
152
|
+
return respond({ type: 'ok', id: req.id });
|
|
153
|
+
default: {
|
|
154
|
+
const _exhaustive: never = req;
|
|
155
|
+
throw new Error(`Unknown request: ${JSON.stringify(_exhaustive)}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
} catch (err) {
|
|
159
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
160
|
+
respond({ type: 'err', id: req.id, message });
|
|
161
|
+
}
|
|
162
|
+
};
|