@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 @@
|
|
|
1
|
+
{"version":3,"file":"multi_map.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/multi_map.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C;;;;GAIG;AACH,qBAAa,uBAAuB,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,KAAK,CACjE,SAAQ,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAC/B,YAAW,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC;IAEpB,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAqBhD;IAEM,cAAc,CAAC,GAAG,EAAE,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAWtD;IAEK,kBAAkB,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAMhD;IAEK,WAAW,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAM/C;IAEc,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAK3C;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { hash } from 'ohash';
|
|
2
|
+
import { SQLiteOPFSAztecMap } from './map.js';
|
|
3
|
+
/**
|
|
4
|
+
* Multi-map backed by SQLite. Extends the base map with always-incrementing
|
|
5
|
+
* `key_count` per-key so the same slot is never reused across deletions — this
|
|
6
|
+
* matches the IndexedDB backend's sparse-multi-map semantics.
|
|
7
|
+
*/ export class SQLiteOPFSAztecMultiMap extends SQLiteOPFSAztecMap {
|
|
8
|
+
async set(key, val) {
|
|
9
|
+
const valueHash = hash(val);
|
|
10
|
+
await this.store.transactionAsync(async ()=>{
|
|
11
|
+
const exists = await this.store.allAsync('SELECT 1 FROM data WHERE container = ? AND key = ? AND hash = ? LIMIT 1', [
|
|
12
|
+
this.container,
|
|
13
|
+
this.encodedKey(key),
|
|
14
|
+
valueHash
|
|
15
|
+
]);
|
|
16
|
+
if (exists.length > 0) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const maxRow = await this.store.allAsync('SELECT MAX(key_count) FROM data WHERE container = ? AND key = ?', [
|
|
20
|
+
this.container,
|
|
21
|
+
this.encodedKey(key)
|
|
22
|
+
]);
|
|
23
|
+
const count = Number(maxRow[0]?.[0] ?? 0);
|
|
24
|
+
await this.store.runAsync(`INSERT INTO data (slot, container, key, key_count, hash, value)
|
|
25
|
+
VALUES (?, ?, ?, ?, ?, ?)`, [
|
|
26
|
+
this.slot(key, count),
|
|
27
|
+
this.container,
|
|
28
|
+
this.encodedKey(key),
|
|
29
|
+
count + 1,
|
|
30
|
+
valueHash,
|
|
31
|
+
this.encoder.pack(val)
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async *getValuesAsync(key) {
|
|
36
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC', [
|
|
37
|
+
this.container,
|
|
38
|
+
this.encodedKey(key)
|
|
39
|
+
]);
|
|
40
|
+
for (const row of rows){
|
|
41
|
+
const raw = row[0];
|
|
42
|
+
if (raw instanceof Uint8Array) {
|
|
43
|
+
yield this.decodeValue(raw);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async getValueCountAsync(key) {
|
|
48
|
+
const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
|
|
49
|
+
this.container,
|
|
50
|
+
this.encodedKey(key)
|
|
51
|
+
]);
|
|
52
|
+
return Number(rows[0]?.[0] ?? 0);
|
|
53
|
+
}
|
|
54
|
+
async deleteValue(key, val) {
|
|
55
|
+
await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ? AND hash = ?', [
|
|
56
|
+
this.container,
|
|
57
|
+
this.encodedKey(key),
|
|
58
|
+
hash(val)
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
async delete(key) {
|
|
62
|
+
await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ?', [
|
|
63
|
+
this.container,
|
|
64
|
+
this.encodedKey(key)
|
|
65
|
+
]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Key, Range } from '../interfaces/common.js';
|
|
2
|
+
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
3
|
+
import type { AztecSQLiteOPFSStore } from './store.js';
|
|
4
|
+
/** Set backed by SQLite. Composes a Map<K, true>. */
|
|
5
|
+
export declare class SQLiteOPFSAztecSet<K extends Key> implements AztecAsyncSet<K> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(store: AztecSQLiteOPFSStore, name: string);
|
|
8
|
+
hasAsync(key: K): Promise<boolean>;
|
|
9
|
+
add(key: K): Promise<void>;
|
|
10
|
+
delete(key: K): Promise<void>;
|
|
11
|
+
entriesAsync(range?: Range<K>): AsyncIterableIterator<K>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2V0LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9z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AM,YAAY,CAAC;AAEvD,qDAAqD;AACrD,qBAAa,kBAAkB,CAAC,CAAC,SAAS,GAAG,CAAE,YAAW,aAAa,CAAC,CAAC,CAAC;;IAGxE,YAAY,KAAK,EAAE,oBAAoB,EAAE,IAAI,EAAE,MAAM,EAEpD;IAED,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAEjC;IAED,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAEzB;IAED,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5B;IAEM,YAAY,CAAC,KAAK,GAAE,KAAK,CAAC,CAAC,CAAM,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAElE;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { SQLiteOPFSAztecMap } from './map.js';
|
|
2
|
+
/** Set backed by SQLite. Composes a Map<K, true>. */ export class SQLiteOPFSAztecSet {
|
|
3
|
+
#map;
|
|
4
|
+
constructor(store, name){
|
|
5
|
+
this.#map = new SQLiteOPFSAztecMap(store, name);
|
|
6
|
+
}
|
|
7
|
+
hasAsync(key) {
|
|
8
|
+
return this.#map.hasAsync(key);
|
|
9
|
+
}
|
|
10
|
+
add(key) {
|
|
11
|
+
return this.#map.set(key, true);
|
|
12
|
+
}
|
|
13
|
+
delete(key) {
|
|
14
|
+
return this.#map.delete(key);
|
|
15
|
+
}
|
|
16
|
+
async *entriesAsync(range = {}) {
|
|
17
|
+
yield* this.#map.keysAsync(range);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Value } from '../interfaces/common.js';
|
|
2
|
+
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
3
|
+
import type { AztecSQLiteOPFSStore } from './store.js';
|
|
4
|
+
/** Stores a single value identified by `name`. */
|
|
5
|
+
export declare class SQLiteOPFSAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
|
|
6
|
+
#private;
|
|
7
|
+
private readonly store;
|
|
8
|
+
constructor(store: AztecSQLiteOPFSStore, name: string);
|
|
9
|
+
getAsync(): Promise<T | undefined>;
|
|
10
|
+
set(val: T): Promise<boolean>;
|
|
11
|
+
delete(): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2luZ2xldG9uLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3FsaXRlLW9wZnMvc2luZ2xldG9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUlBLE9BQU8sS0FBSyxFQUFFLEtBQUssRUFBRSxNQUFNLHlCQUF5QixDQUFDO0FBQ3JELE9BQU8sS0FBSyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sNEJBQTRCLENBQUM7QUFDdEUsT0FBTyxLQUFLLEVBQUUsb0JBQW9CLEVBQUUsTUFBTSxZQUFZLENBQUM7QUFFdkQsa0RBQWtEO0FBQ2xELHFCQUFhLHdCQUF3QixDQUFDLENBQUMsU0FBUyxLQUFLLENBQUUsWUFBVyxtQkFBbUIsQ0FBQyxDQUFDLENBQUM7O0lBTXBGLE9BQU8sQ0FBQyxRQUFRLENBQUMsS0FBSztJQUR4QixZQUNtQixLQUFLLEVBQUUsb0JBQW9CLEVBQzVDLElBQUksRUFBRSxNQUFNLEVBSWI7SUFFSyxRQUFRLElBQUksT0FBTyxDQUFDLENBQUMsR0FBRyxTQUFTLENBQUMsQ0FVdkM7SUFFSyxHQUFHLENBQUMsR0FBRyxFQUFFLENBQUMsR0FBRyxPQUFPLENBQUMsT0FBTyxDQUFDLENBT2xDO0lBRUssTUFBTSxJQUFJLE9BQU8sQ0FBQyxPQUFPLENBQUMsQ0FHL0I7Q0FDRiJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/singleton.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvD,kDAAkD;AAClD,qBAAa,wBAAwB,CAAC,CAAC,SAAS,KAAK,CAAE,YAAW,mBAAmB,CAAC,CAAC,CAAC;;IAMpF,OAAO,CAAC,QAAQ,CAAC,KAAK;IADxB,YACmB,KAAK,EAAE,oBAAoB,EAC5C,IAAI,EAAE,MAAM,EAIb;IAEK,QAAQ,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAUvC;IAEK,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAOlC;IAEK,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,CAG/B;CACF"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Encoder } from 'msgpackr';
|
|
2
|
+
import { hash } from 'ohash';
|
|
3
|
+
import { toBufferKey } from 'ordered-binary';
|
|
4
|
+
/** Stores a single value identified by `name`. */ export class SQLiteOPFSAztecSingleton {
|
|
5
|
+
store;
|
|
6
|
+
#container;
|
|
7
|
+
#slot;
|
|
8
|
+
#encoder;
|
|
9
|
+
constructor(store, name){
|
|
10
|
+
this.store = store;
|
|
11
|
+
this.#encoder = new Encoder();
|
|
12
|
+
this.#container = `singleton:${name}`;
|
|
13
|
+
this.#slot = `singleton:${name}:value`;
|
|
14
|
+
}
|
|
15
|
+
async getAsync() {
|
|
16
|
+
const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [
|
|
17
|
+
this.#slot
|
|
18
|
+
]);
|
|
19
|
+
if (rows.length === 0) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const raw = rows[0][0];
|
|
23
|
+
if (raw instanceof Uint8Array) {
|
|
24
|
+
return this.#encoder.unpack(raw);
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
async set(val) {
|
|
29
|
+
const { changes } = await this.store.runAsync(`INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
|
|
30
|
+
VALUES (?, ?, ?, ?, ?, ?)`, [
|
|
31
|
+
this.#slot,
|
|
32
|
+
this.#container,
|
|
33
|
+
toBufferKey([
|
|
34
|
+
this.#slot
|
|
35
|
+
]),
|
|
36
|
+
1,
|
|
37
|
+
hash(val),
|
|
38
|
+
this.#encoder.pack(val)
|
|
39
|
+
]);
|
|
40
|
+
return changes > 0;
|
|
41
|
+
}
|
|
42
|
+
async delete() {
|
|
43
|
+
await this.store.runAsync('DELETE FROM data WHERE slot = ?', [
|
|
44
|
+
this.#slot
|
|
45
|
+
]);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Logger } from '@aztec/foundation/log';
|
|
2
|
+
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
3
|
+
import type { Key, StoreSize, Value } from '../interfaces/common.js';
|
|
4
|
+
import type { AztecAsyncCounter } from '../interfaces/counter.js';
|
|
5
|
+
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
6
|
+
import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
7
|
+
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
8
|
+
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
9
|
+
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
10
|
+
import type { ResultRow, SqlValue } from './messages.js';
|
|
11
|
+
/**
|
|
12
|
+
* Main-thread handle for a SQLite database persisted to OPFS via the `opfs-sahpool`
|
|
13
|
+
* VFS. Owns a dedicated Web Worker (the SAH Pool VFS requires Worker context) and
|
|
14
|
+
* routes every SQL op through it via typed postMessage RPC.
|
|
15
|
+
*
|
|
16
|
+
* Transaction ordering is guaranteed by a `SerialQueue` on the main thread combined
|
|
17
|
+
* with an `#inTx` flag: outside a `transactionAsync` block, each op acquires the
|
|
18
|
+
* queue for its own auto-commit; inside a block, the outer call holds the queue and
|
|
19
|
+
* nested ops bypass it to avoid deadlock.
|
|
20
|
+
*/
|
|
21
|
+
export declare class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
|
|
22
|
+
#private;
|
|
23
|
+
readonly isEphemeral: boolean;
|
|
24
|
+
private constructor();
|
|
25
|
+
/**
|
|
26
|
+
* Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
|
|
27
|
+
* is true the database lives only in memory and is lost when the worker terminates.
|
|
28
|
+
* Pass `poolDirectory` to place the SAH Pool in a non-default OPFS subdirectory —
|
|
29
|
+
* required when multiple stores coexist in the same tab, because the SAH Pool holds
|
|
30
|
+
* an exclusive lock on its directory.
|
|
31
|
+
*/
|
|
32
|
+
static open(log: Logger, name?: string, ephemeral?: boolean, poolDirectory?: string): Promise<AztecSQLiteOPFSStore>;
|
|
33
|
+
openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V>;
|
|
34
|
+
openSet<K extends Key>(name: string): AztecAsyncSet<K>;
|
|
35
|
+
openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V>;
|
|
36
|
+
openCounter<K extends Key>(_name: string): AztecAsyncCounter<K>;
|
|
37
|
+
openArray<T extends Value>(name: string): AztecAsyncArray<T>;
|
|
38
|
+
openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T>;
|
|
39
|
+
transactionAsync<T>(callback: () => Promise<T>): Promise<T>;
|
|
40
|
+
clear(): Promise<void>;
|
|
41
|
+
delete(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
|
|
44
|
+
* numbers cheaply via `PRAGMA page_count` / `page_size` / `freelist_count` and
|
|
45
|
+
* `SELECT COUNT(*) FROM data`, which would populate `physicalFileSize`, `actualSize`,
|
|
46
|
+
* and `numItems` meaningfully (`mappingSize` stays 0 — it's an LMDB mmap concept).
|
|
47
|
+
* Upgrade when any caller actually consumes these values; all current consumers
|
|
48
|
+
* tolerate zeros.
|
|
49
|
+
*/
|
|
50
|
+
estimateSize(): Promise<StoreSize>;
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
backupTo(_dstPath: string, _compact?: boolean): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Returns a raw SQLite image (bytes suitable for writing as a `.sqlite` file and
|
|
55
|
+
* opening in any SQLite tool). Works only for non-ephemeral DBs because the OPFS
|
|
56
|
+
* SAH Pool has to be initialized. Useful for inspection/debugging.
|
|
57
|
+
*/
|
|
58
|
+
exportDb(): Promise<Uint8Array>;
|
|
59
|
+
/**
|
|
60
|
+
* Runs a write statement (INSERT/UPDATE/DELETE/DDL). If called inside a
|
|
61
|
+
* `transactionAsync` block, bypasses the queue; otherwise acquires it so the
|
|
62
|
+
* op runs in its own auto-commit.
|
|
63
|
+
*/
|
|
64
|
+
runAsync(sql: string, bind?: SqlValue[]): Promise<{
|
|
65
|
+
changes: number;
|
|
66
|
+
}>;
|
|
67
|
+
/** Runs a SELECT statement and returns rows in array row-mode. */
|
|
68
|
+
allAsync(sql: string, bind?: SqlValue[]): Promise<ResultRow[]>;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RvcmUuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zcWxpdGUtb3Bmcy9zdG9yZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUdwRCxPQUFPLEtBQUssRUFBRSxlQUFlLEVBQUUsTUF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SAAS,CAAC,CAEjC;IAEK,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAS3B;IAED,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5D;IAED;;;;OAIG;IACG,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC,CAMpC;IAED;;;;OAIG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAMrE;IAED,kEAAkE;IAClE,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC,CAI7D;CAqCF"}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
2
|
+
import { SQLiteOPFSAztecArray } from './array.js';
|
|
3
|
+
import { SQLiteOPFSAztecMap } from './map.js';
|
|
4
|
+
import { SQLiteOPFSAztecMultiMap } from './multi_map.js';
|
|
5
|
+
import { SQLiteOPFSAztecSet } from './set.js';
|
|
6
|
+
import { SQLiteOPFSAztecSingleton } from './singleton.js';
|
|
7
|
+
/**
|
|
8
|
+
* Main-thread handle for a SQLite database persisted to OPFS via the `opfs-sahpool`
|
|
9
|
+
* VFS. Owns a dedicated Web Worker (the SAH Pool VFS requires Worker context) and
|
|
10
|
+
* routes every SQL op through it via typed postMessage RPC.
|
|
11
|
+
*
|
|
12
|
+
* Transaction ordering is guaranteed by a `SerialQueue` on the main thread combined
|
|
13
|
+
* with an `#inTx` flag: outside a `transactionAsync` block, each op acquires the
|
|
14
|
+
* queue for its own auto-commit; inside a block, the outer call holds the queue and
|
|
15
|
+
* nested ops bypass it to avoid deadlock.
|
|
16
|
+
*/ export class AztecSQLiteOPFSStore {
|
|
17
|
+
isEphemeral;
|
|
18
|
+
#worker;
|
|
19
|
+
#pending;
|
|
20
|
+
#txQueue;
|
|
21
|
+
#name;
|
|
22
|
+
#log;
|
|
23
|
+
#nextId;
|
|
24
|
+
#inTx;
|
|
25
|
+
#closed;
|
|
26
|
+
constructor(worker, name, log, isEphemeral){
|
|
27
|
+
this.isEphemeral = isEphemeral;
|
|
28
|
+
this.#pending = new Map();
|
|
29
|
+
this.#txQueue = new SerialQueue();
|
|
30
|
+
this.#nextId = 0;
|
|
31
|
+
this.#inTx = false;
|
|
32
|
+
this.#closed = false;
|
|
33
|
+
this.#worker = worker;
|
|
34
|
+
this.#name = name;
|
|
35
|
+
this.#log = log;
|
|
36
|
+
this.#worker.onmessage = (ev)=>{
|
|
37
|
+
const { id } = ev.data;
|
|
38
|
+
const handler = this.#pending.get(id);
|
|
39
|
+
if (!handler) {
|
|
40
|
+
this.#log.warn(`SQLite worker: no pending handler for id ${id}`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.#pending.delete(id);
|
|
44
|
+
handler.resolve(ev.data);
|
|
45
|
+
};
|
|
46
|
+
this.#worker.onerror = (ev)=>{
|
|
47
|
+
this.#log.error(`SQLite worker crashed: ${ev.message}`);
|
|
48
|
+
this.#rejectPending(`SQLite worker crashed: ${ev.message}`);
|
|
49
|
+
};
|
|
50
|
+
this.#txQueue.start();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
|
|
54
|
+
* is true the database lives only in memory and is lost when the worker terminates.
|
|
55
|
+
* Pass `poolDirectory` to place the SAH Pool in a non-default OPFS subdirectory —
|
|
56
|
+
* required when multiple stores coexist in the same tab, because the SAH Pool holds
|
|
57
|
+
* an exclusive lock on its directory.
|
|
58
|
+
*/ static async open(log, name, ephemeral = false, poolDirectory) {
|
|
59
|
+
const dbName = name && !ephemeral ? name : `tmp-${globalThis.crypto.getRandomValues(new Uint8Array(8)).join('')}`;
|
|
60
|
+
log.debug(`Opening SQLite-OPFS ${ephemeral ? 'ephemeral ' : ''}database ${dbName}`);
|
|
61
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
62
|
+
type: 'module'
|
|
63
|
+
});
|
|
64
|
+
const store = new AztecSQLiteOPFSStore(worker, dbName, log, ephemeral);
|
|
65
|
+
await store.#sendRequest({
|
|
66
|
+
type: 'init',
|
|
67
|
+
id: store.#allocId(),
|
|
68
|
+
dbName,
|
|
69
|
+
ephemeral,
|
|
70
|
+
poolDirectory
|
|
71
|
+
});
|
|
72
|
+
return store;
|
|
73
|
+
}
|
|
74
|
+
openMap(name) {
|
|
75
|
+
return new SQLiteOPFSAztecMap(this, name);
|
|
76
|
+
}
|
|
77
|
+
openSet(name) {
|
|
78
|
+
return new SQLiteOPFSAztecSet(this, name);
|
|
79
|
+
}
|
|
80
|
+
openMultiMap(name) {
|
|
81
|
+
return new SQLiteOPFSAztecMultiMap(this, name);
|
|
82
|
+
}
|
|
83
|
+
openCounter(_name) {
|
|
84
|
+
throw new Error('Method not implemented.');
|
|
85
|
+
}
|
|
86
|
+
openArray(name) {
|
|
87
|
+
return new SQLiteOPFSAztecArray(this, name);
|
|
88
|
+
}
|
|
89
|
+
openSingleton(name) {
|
|
90
|
+
return new SQLiteOPFSAztecSingleton(this, name);
|
|
91
|
+
}
|
|
92
|
+
transactionAsync(callback) {
|
|
93
|
+
// Nested calls join the outer transaction — SQLite does not support nested BEGIN,
|
|
94
|
+
// and re-acquiring the SerialQueue while the outer call holds it would deadlock.
|
|
95
|
+
// Errors in the nested callback propagate to the outer catch, which rolls back the
|
|
96
|
+
// whole thing (the standard "nested tx = savepoint-free join" semantic).
|
|
97
|
+
if (this.#inTx) {
|
|
98
|
+
return callback();
|
|
99
|
+
}
|
|
100
|
+
return this.#txQueue.put(async ()=>{
|
|
101
|
+
this.#inTx = true;
|
|
102
|
+
await this.#sendRequest({
|
|
103
|
+
type: 'begin',
|
|
104
|
+
id: this.#allocId()
|
|
105
|
+
});
|
|
106
|
+
try {
|
|
107
|
+
const result = await callback();
|
|
108
|
+
await this.#sendRequest({
|
|
109
|
+
type: 'commit',
|
|
110
|
+
id: this.#allocId()
|
|
111
|
+
});
|
|
112
|
+
return result;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
await this.#sendRequest({
|
|
115
|
+
type: 'rollback',
|
|
116
|
+
id: this.#allocId()
|
|
117
|
+
}).catch((rollbackErr)=>this.#log.warn(`SQLite ROLLBACK failed: ${rollbackErr instanceof Error ? rollbackErr.message : rollbackErr}`));
|
|
118
|
+
throw err;
|
|
119
|
+
} finally{
|
|
120
|
+
this.#inTx = false;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
async clear() {
|
|
125
|
+
await this.runAsync('DELETE FROM data');
|
|
126
|
+
}
|
|
127
|
+
async delete() {
|
|
128
|
+
if (this.#closed) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.#closed = true;
|
|
132
|
+
await this.#txQueue.end();
|
|
133
|
+
await this.#sendRequest({
|
|
134
|
+
type: 'deleteDb',
|
|
135
|
+
id: this.#allocId(),
|
|
136
|
+
dbName: this.#name
|
|
137
|
+
}).catch((err)=>this.#log.warn(`SQLite deleteDb failed: ${err instanceof Error ? err.message : err}`));
|
|
138
|
+
this.#worker.terminate();
|
|
139
|
+
this.#rejectPending('SQLite store deleted');
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
|
|
143
|
+
* numbers cheaply via `PRAGMA page_count` / `page_size` / `freelist_count` and
|
|
144
|
+
* `SELECT COUNT(*) FROM data`, which would populate `physicalFileSize`, `actualSize`,
|
|
145
|
+
* and `numItems` meaningfully (`mappingSize` stays 0 — it's an LMDB mmap concept).
|
|
146
|
+
* Upgrade when any caller actually consumes these values; all current consumers
|
|
147
|
+
* tolerate zeros.
|
|
148
|
+
*/ estimateSize() {
|
|
149
|
+
return Promise.resolve({
|
|
150
|
+
mappingSize: 0,
|
|
151
|
+
physicalFileSize: 0,
|
|
152
|
+
actualSize: 0,
|
|
153
|
+
numItems: 0
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
async close() {
|
|
157
|
+
if (this.#closed) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.#closed = true;
|
|
161
|
+
await this.#txQueue.end();
|
|
162
|
+
await this.#sendRequest({
|
|
163
|
+
type: 'close',
|
|
164
|
+
id: this.#allocId()
|
|
165
|
+
}).catch(()=>{});
|
|
166
|
+
this.#worker.terminate();
|
|
167
|
+
this.#rejectPending('SQLite store closed');
|
|
168
|
+
}
|
|
169
|
+
backupTo(_dstPath, _compact) {
|
|
170
|
+
throw new Error('Method not implemented.');
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Returns a raw SQLite image (bytes suitable for writing as a `.sqlite` file and
|
|
174
|
+
* opening in any SQLite tool). Works only for non-ephemeral DBs because the OPFS
|
|
175
|
+
* SAH Pool has to be initialized. Useful for inspection/debugging.
|
|
176
|
+
*/ async exportDb() {
|
|
177
|
+
const resp = await this.#sendRequest({
|
|
178
|
+
type: 'export',
|
|
179
|
+
id: this.#allocId()
|
|
180
|
+
});
|
|
181
|
+
if (!('bytes' in resp) || !resp.bytes) {
|
|
182
|
+
throw new Error('exportDb: worker returned no bytes');
|
|
183
|
+
}
|
|
184
|
+
return resp.bytes;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Runs a write statement (INSERT/UPDATE/DELETE/DDL). If called inside a
|
|
188
|
+
* `transactionAsync` block, bypasses the queue; otherwise acquires it so the
|
|
189
|
+
* op runs in its own auto-commit.
|
|
190
|
+
*/ runAsync(sql, bind) {
|
|
191
|
+
const send = ()=>this.#sendRequest({
|
|
192
|
+
type: 'run',
|
|
193
|
+
id: this.#allocId(),
|
|
194
|
+
sql,
|
|
195
|
+
bind
|
|
196
|
+
}).then((r)=>({
|
|
197
|
+
changes: 'changes' in r ? r.changes ?? 0 : 0
|
|
198
|
+
}));
|
|
199
|
+
return this.#inTx ? send() : this.#txQueue.put(send);
|
|
200
|
+
}
|
|
201
|
+
/** Runs a SELECT statement and returns rows in array row-mode. */ allAsync(sql, bind) {
|
|
202
|
+
const send = ()=>this.#sendRequest({
|
|
203
|
+
type: 'all',
|
|
204
|
+
id: this.#allocId(),
|
|
205
|
+
sql,
|
|
206
|
+
bind
|
|
207
|
+
}).then((r)=>'rows' in r ? r.rows ?? [] : []);
|
|
208
|
+
return this.#inTx ? send() : this.#txQueue.put(send);
|
|
209
|
+
}
|
|
210
|
+
#allocId() {
|
|
211
|
+
return ++this.#nextId;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Reject any in-flight requests with `reason`, so callers awaiting a response to a
|
|
215
|
+
* request sent to a now-terminated worker don't hang forever. Called from
|
|
216
|
+
* close()/delete() and from the worker.onerror handler.
|
|
217
|
+
*/ #rejectPending(reason) {
|
|
218
|
+
if (this.#pending.size === 0) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const err = new Error(reason);
|
|
222
|
+
for (const { reject } of this.#pending.values()){
|
|
223
|
+
reject(err);
|
|
224
|
+
}
|
|
225
|
+
this.#pending.clear();
|
|
226
|
+
}
|
|
227
|
+
#sendRequest(req) {
|
|
228
|
+
return new Promise((resolve, reject)=>{
|
|
229
|
+
this.#pending.set(req.id, {
|
|
230
|
+
resolve: (resp)=>{
|
|
231
|
+
if (resp.type === 'err') {
|
|
232
|
+
reject(new Error(resp.message));
|
|
233
|
+
} else {
|
|
234
|
+
resolve(resp);
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
reject
|
|
238
|
+
});
|
|
239
|
+
this.#worker.postMessage(req);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/sqlite-opfs/worker.ts"],"names":[],"mappings":""}
|