@aztec/kv-store 0.0.1-commit.9372f48 → 0.0.1-commit.949a33fd8

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.
Files changed (88) hide show
  1. package/dest/bench/shared_map_bench.d.ts +19 -0
  2. package/dest/bench/shared_map_bench.d.ts.map +1 -0
  3. package/dest/bench/shared_map_bench.js +91 -0
  4. package/dest/indexeddb/array.js +18 -6
  5. package/dest/indexeddb/index.d.ts +2 -2
  6. package/dest/indexeddb/index.d.ts.map +1 -1
  7. package/dest/indexeddb/map.d.ts +8 -2
  8. package/dest/indexeddb/map.d.ts.map +1 -1
  9. package/dest/indexeddb/map.js +23 -13
  10. package/dest/indexeddb/multi_map.d.ts +2 -1
  11. package/dest/indexeddb/multi_map.d.ts.map +1 -1
  12. package/dest/indexeddb/multi_map.js +16 -1
  13. package/dest/indexeddb/singleton.js +3 -1
  14. package/dest/indexeddb/store.d.ts +3 -3
  15. package/dest/indexeddb/store.d.ts.map +1 -1
  16. package/dest/indexeddb/store.js +6 -4
  17. package/dest/interfaces/map_test_suite.d.ts +1 -1
  18. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  19. package/dest/interfaces/map_test_suite.js +48 -2
  20. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  21. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  22. package/dest/interfaces/multi_map_test_suite.js +25 -0
  23. package/dest/lmdb/index.d.ts +2 -2
  24. package/dest/lmdb/index.d.ts.map +1 -1
  25. package/dest/lmdb/store.d.ts +3 -3
  26. package/dest/lmdb/store.d.ts.map +1 -1
  27. package/dest/lmdb/store.js +12 -8
  28. package/dest/lmdb-v2/factory.d.ts +2 -2
  29. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  30. package/dest/lmdb-v2/read_transaction.js +21 -19
  31. package/dest/sqlite-opfs/array.d.ts +21 -0
  32. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  33. package/dest/sqlite-opfs/array.js +128 -0
  34. package/dest/sqlite-opfs/index.d.ts +7 -0
  35. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  36. package/dest/sqlite-opfs/index.js +13 -0
  37. package/dest/sqlite-opfs/map.d.ts +35 -0
  38. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  39. package/dest/sqlite-opfs/map.js +163 -0
  40. package/dest/sqlite-opfs/messages.d.ts +58 -0
  41. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  42. package/dest/sqlite-opfs/messages.js +5 -0
  43. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  44. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  45. package/dest/sqlite-opfs/multi_map.js +67 -0
  46. package/dest/sqlite-opfs/set.d.ts +13 -0
  47. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  48. package/dest/sqlite-opfs/set.js +19 -0
  49. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  50. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  51. package/dest/sqlite-opfs/singleton.js +48 -0
  52. package/dest/sqlite-opfs/store.d.ts +70 -0
  53. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  54. package/dest/sqlite-opfs/store.js +242 -0
  55. package/dest/sqlite-opfs/worker.d.ts +2 -0
  56. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  57. package/dest/sqlite-opfs/worker.js +194 -0
  58. package/dest/utils.d.ts +1 -1
  59. package/dest/utils.d.ts.map +1 -1
  60. package/dest/utils.js +7 -1
  61. package/package.json +17 -13
  62. package/src/bench/shared_map_bench.ts +111 -0
  63. package/src/indexeddb/array.ts +4 -4
  64. package/src/indexeddb/index.ts +1 -1
  65. package/src/indexeddb/map.ts +24 -11
  66. package/src/indexeddb/multi_map.ts +15 -1
  67. package/src/indexeddb/singleton.ts +1 -1
  68. package/src/indexeddb/store.ts +13 -6
  69. package/src/interfaces/map_test_suite.ts +30 -2
  70. package/src/interfaces/multi_map_test_suite.ts +32 -0
  71. package/src/lmdb/index.ts +1 -1
  72. package/src/lmdb/store.ts +12 -8
  73. package/src/lmdb-v2/factory.ts +1 -1
  74. package/src/lmdb-v2/read_transaction.ts +23 -23
  75. package/src/sqlite-opfs/array.ts +124 -0
  76. package/src/sqlite-opfs/index.ts +27 -0
  77. package/src/sqlite-opfs/map.ts +163 -0
  78. package/src/sqlite-opfs/messages.ts +28 -0
  79. package/src/sqlite-opfs/multi_map.ts +74 -0
  80. package/src/sqlite-opfs/set.ts +29 -0
  81. package/src/sqlite-opfs/singleton.ts +48 -0
  82. package/src/sqlite-opfs/store.ts +248 -0
  83. package/src/sqlite-opfs/worker.ts +162 -0
  84. package/src/utils.ts +10 -0
  85. package/dest/config.d.ts +0 -17
  86. package/dest/config.d.ts.map +0 -1
  87. package/dest/config.js +0 -26
  88. package/src/config.ts +0 -36
@@ -104,6 +104,38 @@ export function describeAztecMultiMap(
104
104
  expect(await size()).to.equal(1);
105
105
  });
106
106
 
107
+ it('returns 0 for empty multimap size', async () => {
108
+ expect(await size()).to.equal(0);
109
+ });
110
+
111
+ it('calculates size correctly with multiple values per key', async () => {
112
+ expect(await size()).to.equal(0);
113
+
114
+ // Add multiple values for same key
115
+ await multiMap.set('key1', 'value1');
116
+ expect(await size()).to.equal(1);
117
+ await multiMap.set('key1', 'value2');
118
+ expect(await size()).to.equal(2);
119
+ await multiMap.set('key1', 'value3');
120
+ expect(await size()).to.equal(3);
121
+
122
+ // Add values for different key
123
+ await multiMap.set('key2', 'value4');
124
+ expect(await size()).to.equal(4);
125
+
126
+ // Delete one value from key1
127
+ await multiMap.deleteValue('key1', 'value2');
128
+ expect(await size()).to.equal(3);
129
+
130
+ // Delete entire key
131
+ await multiMap.delete('key1');
132
+ expect(await size()).to.equal(1);
133
+
134
+ // Delete last key
135
+ await multiMap.delete('key2');
136
+ expect(await size()).to.equal(0);
137
+ });
138
+
107
139
  it('should be able to iterate over entries when there are no keys', async () => {
108
140
  expect(await entries()).to.deep.equal([]);
109
141
  });
package/src/lmdb/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { type Logger, createLogger } from '@aztec/foundation/log';
2
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
2
3
 
3
4
  import { join } from 'path';
4
5
 
5
- import type { DataStoreConfig } from '../config.js';
6
6
  import { initStoreForRollupAndSchemaVersion } from '../utils.js';
7
7
  import { AztecLmdbStore } from './store.js';
8
8
 
package/src/lmdb/store.ts CHANGED
@@ -147,21 +147,25 @@ export class AztecLmdbStore implements AztecKVStore, AztecAsyncKVStore {
147
147
  }
148
148
 
149
149
  /**
150
- * Clears all entries in the store & sub DBs.
150
+ * Clears all entries in the store & sub DBs atomically within a single transaction.
151
151
  */
152
152
  async clear() {
153
- await this.#data.clearAsync();
154
- await this.#multiMapData.clearAsync();
155
- await this.#rootDb.clearAsync();
153
+ await this.#rootDb.transaction(async () => {
154
+ await this.#data.clearAsync();
155
+ await this.#multiMapData.clearAsync();
156
+ await this.#rootDb.clearAsync();
157
+ });
156
158
  }
157
159
 
158
160
  /**
159
- * Drops the database & sub DBs.
161
+ * Drops the database & sub DBs atomically within a single transaction.
160
162
  */
161
163
  async drop() {
162
- await this.#data.drop();
163
- await this.#multiMapData.drop();
164
- await this.#rootDb.drop();
164
+ await this.#rootDb.transaction(async () => {
165
+ await this.#data.drop();
166
+ await this.#multiMapData.drop();
167
+ await this.#rootDb.drop();
168
+ });
165
169
  }
166
170
 
167
171
  /**
@@ -1,12 +1,12 @@
1
1
  import { EthAddress } from '@aztec/foundation/eth-address';
2
2
  import { type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
3
  import { DatabaseVersionManager } from '@aztec/stdlib/database-version/manager';
4
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
4
5
 
5
6
  import { mkdir, mkdtemp, rm } from 'fs/promises';
6
7
  import { tmpdir } from 'os';
7
8
  import { join } from 'path';
8
9
 
9
- import type { DataStoreConfig } from '../config.js';
10
10
  import { AztecLMDBStoreV2 } from './store.js';
11
11
 
12
12
  const MAX_READERS = 16;
@@ -66,20 +66,20 @@ export class ReadTransaction {
66
66
  ): AsyncIterable<[Uint8Array, T]> {
67
67
  this.assertIsOpen();
68
68
 
69
- const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
70
- key: startKey,
71
- reverse,
72
- count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
73
- onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
74
- db,
75
- });
76
-
77
- const cursor = response.cursor;
78
- let entries = response.entries;
79
- let done = typeof cursor !== 'number';
80
- let count = 0;
81
-
69
+ let cursor: number | undefined;
82
70
  try {
71
+ const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
72
+ key: startKey,
73
+ reverse,
74
+ count: typeof limit === 'number' ? Math.min(limit, CURSOR_PAGE_SIZE) : CURSOR_PAGE_SIZE,
75
+ onePage: typeof limit === 'number' && limit < CURSOR_PAGE_SIZE,
76
+ db,
77
+ });
78
+
79
+ cursor = response.cursor ?? undefined;
80
+ let entries = response.entries;
81
+ let done = typeof cursor !== 'number';
82
+ let count = 0;
83
83
  // emit the first page and any subsequent pages in a while loop
84
84
  // NB: end contition is in the middle of the while loop
85
85
  while (entries.length > 0) {
@@ -125,17 +125,17 @@ export class ReadTransaction {
125
125
  async #countEntries(db: string, startKey: Uint8Array, endKey: Uint8Array, reverse: boolean): Promise<number> {
126
126
  this.assertIsOpen();
127
127
 
128
- const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
129
- key: startKey,
130
- reverse,
131
- count: 0,
132
- onePage: false,
133
- db,
134
- });
135
-
136
- const cursor = response.cursor;
137
-
128
+ let cursor: number | undefined;
138
129
  try {
130
+ const response = await this.channel.sendMessage(LMDBMessageType.START_CURSOR, {
131
+ key: startKey,
132
+ reverse,
133
+ count: 0,
134
+ onePage: false,
135
+ db,
136
+ });
137
+
138
+ cursor = response.cursor ?? undefined;
139
139
  if (!cursor) {
140
140
  return 0;
141
141
  }
@@ -0,0 +1,124 @@
1
+ import { Encoder } from 'msgpackr';
2
+ import { hash } from 'ohash';
3
+ import { toBufferKey } from 'ordered-binary';
4
+
5
+ import type { AztecAsyncArray } from '../interfaces/array.js';
6
+ import type { Value } from '../interfaces/common.js';
7
+ import type { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ /**
10
+ * Persistent array backed by SQLite. Entries share a common `key` (the array name)
11
+ * and are ordered by `key_count`, which doubles as the 1-indexed slot number.
12
+ */
13
+ export class SQLiteOPFSAztecArray<T extends Value> implements AztecAsyncArray<T> {
14
+ readonly #name: string;
15
+ readonly #container: string;
16
+ readonly #encoder = new Encoder();
17
+
18
+ constructor(
19
+ private readonly store: AztecSQLiteOPFSStore,
20
+ name: string,
21
+ ) {
22
+ this.#name = name;
23
+ this.#container = `array:${name}`;
24
+ }
25
+
26
+ async lengthAsync(): Promise<number> {
27
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
28
+ this.#container,
29
+ this.#encodedKey(),
30
+ ]);
31
+ return Number(rows[0]?.[0] ?? 0);
32
+ }
33
+
34
+ async push(...vals: T[]): Promise<number> {
35
+ if (vals.length === 0) {
36
+ return this.lengthAsync();
37
+ }
38
+ return await this.store.transactionAsync(async () => {
39
+ let length = await this.lengthAsync();
40
+ for (const val of vals) {
41
+ await this.store.runAsync(
42
+ `INSERT INTO data (slot, container, key, key_count, hash, value)
43
+ VALUES (?, ?, ?, ?, ?, ?)`,
44
+ [this.#slot(length), this.#container, this.#encodedKey(), length + 1, hash(val), this.#encoder.pack(val)],
45
+ );
46
+ length += 1;
47
+ }
48
+ return length;
49
+ });
50
+ }
51
+
52
+ async pop(): Promise<T | undefined> {
53
+ return await this.store.transactionAsync(async () => {
54
+ const length = await this.lengthAsync();
55
+ if (length === 0) {
56
+ return undefined;
57
+ }
58
+ const slot = this.#slot(length - 1);
59
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [slot]);
60
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [slot]);
61
+ const raw = rows[0]?.[0];
62
+ return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
63
+ });
64
+ }
65
+
66
+ async atAsync(index: number): Promise<T | undefined> {
67
+ const length = await this.lengthAsync();
68
+ const resolved = index < 0 ? length + index : index;
69
+ if (resolved < 0 || resolved >= length) {
70
+ return undefined;
71
+ }
72
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot(resolved)]);
73
+ const raw = rows[0]?.[0];
74
+ return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
75
+ }
76
+
77
+ async setAt(index: number, val: T): Promise<boolean> {
78
+ return await this.store.transactionAsync(async () => {
79
+ const length = await this.lengthAsync();
80
+ const resolved = index < 0 ? length + index : index;
81
+ if (resolved < 0 || resolved >= length) {
82
+ return false;
83
+ }
84
+ await this.store.runAsync(
85
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
86
+ VALUES (?, ?, ?, ?, ?, ?)`,
87
+ [this.#slot(resolved), this.#container, this.#encodedKey(), resolved + 1, hash(val), this.#encoder.pack(val)],
88
+ );
89
+ return true;
90
+ });
91
+ }
92
+
93
+ async *entriesAsync(): AsyncIterableIterator<[number, T]> {
94
+ const rows = await this.store.allAsync(
95
+ 'SELECT key_count, value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
96
+ [this.#container, this.#encodedKey()],
97
+ );
98
+ for (const row of rows) {
99
+ const keyCount = Number(row[0]);
100
+ const raw = row[1];
101
+ if (raw instanceof Uint8Array) {
102
+ yield [keyCount - 1, this.#encoder.unpack(raw) as T];
103
+ }
104
+ }
105
+ }
106
+
107
+ async *valuesAsync(): AsyncIterableIterator<T> {
108
+ for await (const [, val] of this.entriesAsync()) {
109
+ yield val;
110
+ }
111
+ }
112
+
113
+ [Symbol.asyncIterator](): AsyncIterableIterator<T> {
114
+ return this.valuesAsync();
115
+ }
116
+
117
+ #encodedKey(): Buffer {
118
+ return toBufferKey([this.#name]);
119
+ }
120
+
121
+ #slot(index: number): string {
122
+ return `array:${this.#name}:slot:${index}`;
123
+ }
124
+ }
@@ -0,0 +1,27 @@
1
+ import { type Logger, createLogger } from '@aztec/foundation/log';
2
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
3
+
4
+ import { initStoreForRollupAndSchemaVersion } from '../utils.js';
5
+ import { AztecSQLiteOPFSStore } from './store.js';
6
+
7
+ export { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ export async function createStore(
10
+ name: string,
11
+ config: DataStoreConfig,
12
+ schemaVersion: number | undefined = undefined,
13
+ log: Logger = createLogger('kv-store'),
14
+ ) {
15
+ const { dataDirectory } = config;
16
+ log.info(
17
+ dataDirectory
18
+ ? `Creating ${name} SQLite-OPFS data store with map size ${config.dataStoreMapSizeKb} KB`
19
+ : `Creating ${name} ephemeral SQLite-OPFS data store with map size ${config.dataStoreMapSizeKb} KB`,
20
+ );
21
+ const store = await AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), name, false);
22
+ return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts?.rollupAddress, log);
23
+ }
24
+
25
+ export function openTmpStore(ephemeral: boolean = false): Promise<AztecSQLiteOPFSStore> {
26
+ return AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), undefined, ephemeral);
27
+ }
@@ -0,0 +1,163 @@
1
+ import { Buffer } from 'buffer';
2
+ import { Encoder } from 'msgpackr';
3
+ import { hash } from 'ohash';
4
+ import { fromBufferKey, toBufferKey } from 'ordered-binary';
5
+
6
+ import type { Key, Range, Value } from '../interfaces/common.js';
7
+ import type { AztecAsyncMap } from '../interfaces/map.js';
8
+ import type { SqlValue } from './messages.js';
9
+ import type { AztecSQLiteOPFSStore } from './store.js';
10
+
11
+ /** A map backed by SQLite in OPFS. Mirrors `IndexedDBAztecMap`. */
12
+ export class SQLiteOPFSAztecMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
13
+ protected readonly name: string;
14
+ protected readonly container: string;
15
+ protected readonly encoder = new Encoder();
16
+
17
+ constructor(
18
+ protected readonly store: AztecSQLiteOPFSStore,
19
+ mapName: string,
20
+ ) {
21
+ this.name = mapName;
22
+ this.container = `map:${mapName}`;
23
+ }
24
+
25
+ async getAsync(key: K): Promise<V | undefined> {
26
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
27
+ if (rows.length === 0) {
28
+ return undefined;
29
+ }
30
+ const raw = rows[0][0];
31
+ return raw == null ? undefined : this.decodeValue(raw);
32
+ }
33
+
34
+ async hasAsync(key: K): Promise<boolean> {
35
+ const rows = await this.store.allAsync('SELECT 1 FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
36
+ return rows.length > 0;
37
+ }
38
+
39
+ async sizeAsync(): Promise<number> {
40
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ?', [this.container]);
41
+ return Number(rows[0]?.[0] ?? 0);
42
+ }
43
+
44
+ async set(key: K, val: V): Promise<void> {
45
+ await this.store.runAsync(
46
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
47
+ VALUES (?, ?, ?, ?, ?, ?)`,
48
+ [this.slot(key), this.container, this.encodedKey(key), 1, hash(val), this.encoder.pack(val)],
49
+ );
50
+ }
51
+
52
+ async setMany(entries: { key: K; value: V }[]): Promise<void> {
53
+ if (entries.length === 0) {
54
+ return;
55
+ }
56
+ await this.store.transactionAsync(async () => {
57
+ for (const { key, value } of entries) {
58
+ await this.set(key, value);
59
+ }
60
+ });
61
+ }
62
+
63
+ swap(_key: K, _fn: (val: V | undefined) => V): Promise<void> {
64
+ throw new Error('Not implemented');
65
+ }
66
+
67
+ async setIfNotExists(key: K, val: V): Promise<boolean> {
68
+ return await this.store.transactionAsync(async () => {
69
+ if (await this.hasAsync(key)) {
70
+ return false;
71
+ }
72
+ await this.set(key, val);
73
+ return true;
74
+ });
75
+ }
76
+
77
+ async delete(key: K): Promise<void> {
78
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.slot(key)]);
79
+ }
80
+
81
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
82
+ const rows = await this.rangeQuery(range);
83
+ for (const row of rows) {
84
+ const [_slot, keyBlob, value] = row;
85
+ if (keyBlob == null || value == null) {
86
+ continue;
87
+ }
88
+ yield [this.decodeKey(keyBlob), this.decodeValue(value)];
89
+ }
90
+ }
91
+
92
+ async *valuesAsync(range: Range<K> = {}): AsyncIterableIterator<V> {
93
+ for await (const [, value] of this.entriesAsync(range)) {
94
+ yield value;
95
+ }
96
+ }
97
+
98
+ async *keysAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
99
+ for await (const [key] of this.entriesAsync(range)) {
100
+ yield key;
101
+ }
102
+ }
103
+
104
+ protected async rangeQuery(range: Range<K>): Promise<Array<[string, Uint8Array, Uint8Array | null]>> {
105
+ // Inclusivity flips with direction to match the IndexedDB backend:
106
+ // forward: [start, end) reverse: (start, end]
107
+ // That asymmetry is load-bearing — tests pin the exact inclusivity at boundaries.
108
+ const reverse = !!range.reverse;
109
+ const parts: string[] = ['container = ?'];
110
+ const bind: SqlValue[] = [this.container];
111
+ if (range.start !== undefined) {
112
+ parts.push(reverse ? 'key > ?' : 'key >= ?');
113
+ bind.push(this.encodedKey(range.start));
114
+ }
115
+ if (range.end !== undefined) {
116
+ parts.push(reverse ? 'key <= ?' : 'key < ?');
117
+ bind.push(this.encodedKey(range.end));
118
+ }
119
+ const order = reverse ? 'DESC' : 'ASC';
120
+ let sql = `SELECT slot, key, value FROM data WHERE ${parts.join(' AND ')} ORDER BY key ${order}, key_count ${order}`;
121
+ if (range.limit !== undefined) {
122
+ sql += ' LIMIT ?';
123
+ bind.push(range.limit);
124
+ }
125
+ const rows = await this.store.allAsync(sql, bind);
126
+ return rows.map(r => [String(r[0]), r[1] as Uint8Array, r[2] as Uint8Array | null]);
127
+ }
128
+
129
+ protected decodeValue(val: SqlValue): V {
130
+ if (!(val instanceof Uint8Array)) {
131
+ return val as V;
132
+ }
133
+ const unpacked = this.encoder.unpack(val);
134
+ // msgpackr returns plain Uint8Array in browsers for packed Buffers. Callers that
135
+ // stored Buffers (walletDB uses Buffer.from(...).toString('utf8') round-trips)
136
+ // rely on Buffer-flavored behavior — re-wrap at the storage boundary, mirroring
137
+ // IndexedDBAztecMap.restoreBuffers.
138
+ if (unpacked instanceof Uint8Array && !Buffer.isBuffer(unpacked)) {
139
+ return Buffer.from(unpacked) as V;
140
+ }
141
+ return unpacked as V;
142
+ }
143
+
144
+ protected decodeKey(raw: Uint8Array): K {
145
+ const parsed = fromBufferKey(Buffer.from(raw));
146
+ if (Array.isArray(parsed)) {
147
+ return (parsed.length > 1 ? parsed : parsed[0]) as K;
148
+ }
149
+ return parsed as K;
150
+ }
151
+
152
+ protected encodedKey(key: K): Buffer {
153
+ return toBufferKey(this.normalizeKey(key));
154
+ }
155
+
156
+ protected normalizeKey(key: K): (string | number | Uint8Array)[] {
157
+ return Array.isArray(key) ? key : [key];
158
+ }
159
+
160
+ protected slot(key: K, index: number = 0): string {
161
+ return `map:${this.name}:slot:${this.normalizeKey(key)}:${index}`;
162
+ }
163
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * RPC protocol between the main thread and the SQLite worker.
3
+ * All requests carry a unique `id`; responses echo the same id so the main thread
4
+ * can resolve the right pending promise.
5
+ */
6
+
7
+ /** Matches `@sqlite.org/sqlite-wasm`'s internal SqlValue type. Boolean is not a native SQLite type. */
8
+ export type SqlValue = string | number | bigint | null | Uint8Array;
9
+
10
+ /** A row returned in 'array' rowMode — one value per column, in select order. */
11
+ export type ResultRow = SqlValue[];
12
+
13
+ export type WorkerRequest =
14
+ | { type: 'init'; id: number; dbName: string; ephemeral: boolean; poolDirectory?: string }
15
+ | { type: 'close'; id: number }
16
+ | { type: 'deleteDb'; id: number; dbName: string }
17
+ | { type: 'run'; id: number; sql: string; bind?: SqlValue[] }
18
+ | { type: 'all'; id: number; sql: string; bind?: SqlValue[] }
19
+ | { type: 'export'; id: number }
20
+ | { type: 'begin'; id: number }
21
+ | { type: 'commit'; id: number }
22
+ | { type: 'rollback'; id: number };
23
+
24
+ export type WorkerResponse =
25
+ | { type: 'ok'; id: number; rows?: ResultRow[]; changes?: number; bytes?: Uint8Array }
26
+ | { type: 'err'; id: number; message: string };
27
+
28
+ export type WorkerRequestType = WorkerRequest['type'];
@@ -0,0 +1,74 @@
1
+ import { hash } from 'ohash';
2
+
3
+ import type { Key, Value } from '../interfaces/common.js';
4
+ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
5
+ import { SQLiteOPFSAztecMap } from './map.js';
6
+
7
+ /**
8
+ * Multi-map backed by SQLite. Extends the base map with always-incrementing
9
+ * `key_count` per-key so the same slot is never reused across deletions — this
10
+ * matches the IndexedDB backend's sparse-multi-map semantics.
11
+ */
12
+ export class SQLiteOPFSAztecMultiMap<K extends Key, V extends Value>
13
+ extends SQLiteOPFSAztecMap<K, V>
14
+ implements AztecAsyncMultiMap<K, V>
15
+ {
16
+ override async set(key: K, val: V): Promise<void> {
17
+ const valueHash = hash(val);
18
+ await this.store.transactionAsync(async () => {
19
+ const exists = await this.store.allAsync(
20
+ 'SELECT 1 FROM data WHERE container = ? AND key = ? AND hash = ? LIMIT 1',
21
+ [this.container, this.encodedKey(key), valueHash],
22
+ );
23
+ if (exists.length > 0) {
24
+ return;
25
+ }
26
+ const maxRow = await this.store.allAsync('SELECT MAX(key_count) FROM data WHERE container = ? AND key = ?', [
27
+ this.container,
28
+ this.encodedKey(key),
29
+ ]);
30
+ const count = Number(maxRow[0]?.[0] ?? 0);
31
+ await this.store.runAsync(
32
+ `INSERT INTO data (slot, container, key, key_count, hash, value)
33
+ VALUES (?, ?, ?, ?, ?, ?)`,
34
+ [this.slot(key, count), this.container, this.encodedKey(key), count + 1, valueHash, this.encoder.pack(val)],
35
+ );
36
+ });
37
+ }
38
+
39
+ async *getValuesAsync(key: K): AsyncIterableIterator<V> {
40
+ const rows = await this.store.allAsync(
41
+ 'SELECT value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
42
+ [this.container, this.encodedKey(key)],
43
+ );
44
+ for (const row of rows) {
45
+ const raw = row[0];
46
+ if (raw instanceof Uint8Array) {
47
+ yield this.decodeValue(raw);
48
+ }
49
+ }
50
+ }
51
+
52
+ async getValueCountAsync(key: K): Promise<number> {
53
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
54
+ this.container,
55
+ this.encodedKey(key),
56
+ ]);
57
+ return Number(rows[0]?.[0] ?? 0);
58
+ }
59
+
60
+ async deleteValue(key: K, val: V): Promise<void> {
61
+ await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ? AND hash = ?', [
62
+ this.container,
63
+ this.encodedKey(key),
64
+ hash(val),
65
+ ]);
66
+ }
67
+
68
+ override async delete(key: K): Promise<void> {
69
+ await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ?', [
70
+ this.container,
71
+ this.encodedKey(key),
72
+ ]);
73
+ }
74
+ }
@@ -0,0 +1,29 @@
1
+ import type { Key, Range } from '../interfaces/common.js';
2
+ import type { AztecAsyncSet } from '../interfaces/set.js';
3
+ import { SQLiteOPFSAztecMap } from './map.js';
4
+ import type { AztecSQLiteOPFSStore } from './store.js';
5
+
6
+ /** Set backed by SQLite. Composes a Map<K, true>. */
7
+ export class SQLiteOPFSAztecSet<K extends Key> implements AztecAsyncSet<K> {
8
+ readonly #map: SQLiteOPFSAztecMap<K, boolean>;
9
+
10
+ constructor(store: AztecSQLiteOPFSStore, name: string) {
11
+ this.#map = new SQLiteOPFSAztecMap<K, boolean>(store, name);
12
+ }
13
+
14
+ hasAsync(key: K): Promise<boolean> {
15
+ return this.#map.hasAsync(key);
16
+ }
17
+
18
+ add(key: K): Promise<void> {
19
+ return this.#map.set(key, true);
20
+ }
21
+
22
+ delete(key: K): Promise<void> {
23
+ return this.#map.delete(key);
24
+ }
25
+
26
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
27
+ yield* this.#map.keysAsync(range);
28
+ }
29
+ }
@@ -0,0 +1,48 @@
1
+ import { Encoder } from 'msgpackr';
2
+ import { hash } from 'ohash';
3
+ import { toBufferKey } from 'ordered-binary';
4
+
5
+ import type { Value } from '../interfaces/common.js';
6
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
7
+ import type { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ /** Stores a single value identified by `name`. */
10
+ export class SQLiteOPFSAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
11
+ readonly #container: string;
12
+ readonly #slot: string;
13
+ readonly #encoder = new Encoder();
14
+
15
+ constructor(
16
+ private readonly store: AztecSQLiteOPFSStore,
17
+ name: string,
18
+ ) {
19
+ this.#container = `singleton:${name}`;
20
+ this.#slot = `singleton:${name}:value`;
21
+ }
22
+
23
+ async getAsync(): Promise<T | undefined> {
24
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot]);
25
+ if (rows.length === 0) {
26
+ return undefined;
27
+ }
28
+ const raw = rows[0][0];
29
+ if (raw instanceof Uint8Array) {
30
+ return this.#encoder.unpack(raw) as T;
31
+ }
32
+ return undefined;
33
+ }
34
+
35
+ async set(val: T): Promise<boolean> {
36
+ const { changes } = await this.store.runAsync(
37
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
38
+ VALUES (?, ?, ?, ?, ?, ?)`,
39
+ [this.#slot, this.#container, toBufferKey([this.#slot]), 1, hash(val), this.#encoder.pack(val)],
40
+ );
41
+ return changes > 0;
42
+ }
43
+
44
+ async delete(): Promise<boolean> {
45
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.#slot]);
46
+ return true;
47
+ }
48
+ }