@aztec/kv-store 0.0.0-test.0 → 0.0.1-commit.0208eb9
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/config.d.ts +2 -2
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +5 -3
- package/dest/indexeddb/array.d.ts +3 -2
- package/dest/indexeddb/array.d.ts.map +1 -1
- package/dest/indexeddb/array.js +24 -7
- package/dest/indexeddb/index.d.ts +2 -2
- package/dest/indexeddb/index.d.ts.map +1 -1
- package/dest/indexeddb/index.js +4 -7
- package/dest/indexeddb/map.d.ts +12 -6
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +39 -61
- package/dest/indexeddb/multi_map.d.ts +14 -0
- package/dest/indexeddb/multi_map.d.ts.map +1 -0
- package/dest/indexeddb/multi_map.js +107 -0
- package/dest/indexeddb/set.d.ts +1 -1
- package/dest/indexeddb/set.d.ts.map +1 -1
- package/dest/indexeddb/singleton.d.ts +3 -2
- package/dest/indexeddb/singleton.d.ts.map +1 -1
- package/dest/indexeddb/singleton.js +6 -2
- package/dest/indexeddb/store.d.ts +15 -16
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +45 -42
- package/dest/interfaces/array.d.ts +5 -4
- package/dest/interfaces/array.d.ts.map +1 -1
- package/dest/interfaces/array.js +1 -3
- package/dest/interfaces/array_test_suite.d.ts +1 -1
- package/dest/interfaces/common.d.ts +11 -9
- package/dest/interfaces/common.d.ts.map +1 -1
- package/dest/interfaces/common.js +8 -3
- package/dest/interfaces/counter.d.ts +1 -1
- package/dest/interfaces/index.d.ts +4 -2
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/index.js +2 -0
- package/dest/interfaces/map.d.ts +21 -49
- package/dest/interfaces/map.d.ts.map +1 -1
- package/dest/interfaces/map.js +1 -1
- package/dest/interfaces/map_test_suite.d.ts +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +181 -70
- package/dest/interfaces/multi_map.d.ts +41 -0
- package/dest/interfaces/multi_map.d.ts.map +1 -0
- package/dest/interfaces/multi_map.js +3 -0
- package/dest/interfaces/multi_map_test_suite.d.ts +3 -0
- package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -0
- package/dest/interfaces/multi_map_test_suite.js +364 -0
- package/dest/interfaces/set.d.ts +1 -1
- package/dest/interfaces/set_test_suite.d.ts +1 -1
- package/dest/interfaces/singleton.d.ts +1 -1
- package/dest/interfaces/singleton_test_suite.d.ts +1 -1
- package/dest/interfaces/store.d.ts +18 -43
- package/dest/interfaces/store.d.ts.map +1 -1
- package/dest/interfaces/utils.d.ts +3 -1
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +3 -1
- package/dest/lmdb/array.d.ts +3 -2
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/array.js +4 -2
- package/dest/lmdb/counter.d.ts +1 -1
- package/dest/lmdb/counter.d.ts.map +1 -1
- package/dest/lmdb/index.d.ts +2 -2
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +5 -5
- package/dest/lmdb/map.d.ts +11 -23
- package/dest/lmdb/map.d.ts.map +1 -1
- package/dest/lmdb/map.js +15 -81
- package/dest/lmdb/multi_map.d.ts +13 -0
- package/dest/lmdb/multi_map.d.ts.map +1 -0
- package/dest/lmdb/multi_map.js +44 -0
- package/dest/lmdb/set.d.ts +1 -1
- package/dest/lmdb/set.d.ts.map +1 -1
- package/dest/lmdb/singleton.d.ts +1 -1
- package/dest/lmdb/singleton.d.ts.map +1 -1
- package/dest/lmdb/store.d.ts +9 -25
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +12 -32
- package/dest/lmdb-v2/array.d.ts +4 -3
- package/dest/lmdb-v2/array.d.ts.map +1 -1
- package/dest/lmdb-v2/array.js +4 -2
- package/dest/lmdb-v2/factory.d.ts +6 -9
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +26 -12
- package/dest/lmdb-v2/index.d.ts +1 -1
- package/dest/lmdb-v2/map.d.ts +12 -45
- package/dest/lmdb-v2/map.d.ts.map +1 -1
- package/dest/lmdb-v2/map.js +17 -104
- package/dest/lmdb-v2/message.d.ts +24 -5
- package/dest/lmdb-v2/message.d.ts.map +1 -1
- package/dest/lmdb-v2/message.js +6 -4
- package/dest/lmdb-v2/multi_map.d.ts +52 -0
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -0
- package/dest/lmdb-v2/multi_map.js +123 -0
- package/dest/lmdb-v2/read_transaction.d.ts +3 -1
- package/dest/lmdb-v2/read_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/read_transaction.js +34 -0
- package/dest/lmdb-v2/set.d.ts +15 -0
- package/dest/lmdb-v2/set.d.ts.map +1 -0
- package/dest/lmdb-v2/set.js +23 -0
- package/dest/lmdb-v2/singleton.d.ts +2 -2
- package/dest/lmdb-v2/singleton.d.ts.map +1 -1
- package/dest/lmdb-v2/singleton.js +1 -1
- package/dest/lmdb-v2/store.d.ts +13 -13
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +19 -29
- package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
- package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
- package/dest/lmdb-v2/tx-helpers.js +21 -0
- package/dest/lmdb-v2/utils.d.ts +3 -14
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/utils.js +0 -94
- package/dest/lmdb-v2/write_transaction.d.ts +3 -5
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.js +9 -5
- package/dest/stores/index.d.ts +2 -1
- package/dest/stores/index.d.ts.map +1 -1
- package/dest/stores/index.js +1 -0
- package/dest/stores/l2_tips_store.d.ts +24 -8
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +63 -45
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +51 -16
- package/package.json +25 -16
- package/src/config.ts +6 -4
- package/src/indexeddb/array.ts +9 -5
- package/src/indexeddb/index.ts +10 -8
- package/src/indexeddb/map.ts +38 -55
- package/src/indexeddb/multi_map.ts +105 -0
- package/src/indexeddb/singleton.ts +5 -2
- package/src/indexeddb/store.ts +71 -56
- package/src/interfaces/array.ts +5 -3
- package/src/interfaces/common.ts +20 -9
- package/src/interfaces/index.ts +3 -1
- package/src/interfaces/map.ts +19 -53
- package/src/interfaces/map_test_suite.ts +101 -44
- package/src/interfaces/multi_map.ts +45 -0
- package/src/interfaces/multi_map_test_suite.ts +374 -0
- package/src/interfaces/store.ts +18 -53
- package/src/interfaces/utils.ts +2 -0
- package/src/lmdb/array.ts +2 -1
- package/src/lmdb/index.ts +11 -6
- package/src/lmdb/map.ts +23 -94
- package/src/lmdb/multi_map.ts +51 -0
- package/src/lmdb/store.ts +24 -48
- package/src/lmdb-v2/array.ts +8 -3
- package/src/lmdb-v2/factory.ts +29 -18
- package/src/lmdb-v2/map.ts +30 -127
- package/src/lmdb-v2/message.ts +23 -0
- package/src/lmdb-v2/multi_map.ts +150 -0
- package/src/lmdb-v2/read_transaction.ts +40 -0
- package/src/lmdb-v2/set.ts +33 -0
- package/src/lmdb-v2/singleton.ts +6 -2
- package/src/lmdb-v2/store.ts +24 -42
- package/src/lmdb-v2/tx-helpers.ts +29 -0
- package/src/lmdb-v2/utils.ts +0 -118
- package/src/lmdb-v2/write_transaction.ts +11 -10
- package/src/stores/index.ts +2 -0
- package/src/stores/l2_tips_store.ts +67 -49
- package/src/utils.ts +79 -21
- package/dest/interfaces/store_test_suite.d.ts +0 -3
- package/dest/interfaces/store_test_suite.d.ts.map +0 -1
- package/dest/interfaces/store_test_suite.js +0 -37
- package/src/interfaces/store_test_suite.ts +0 -56
|
@@ -0,0 +1,105 @@
|
|
|
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 { IndexedDBAztecMap } from './map.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A multi map backed by IndexedDB.
|
|
9
|
+
*/
|
|
10
|
+
export class IndexedDBAztecMultiMap<K extends Key, V extends Value>
|
|
11
|
+
extends IndexedDBAztecMap<K, V>
|
|
12
|
+
implements AztecAsyncMultiMap<K, V>
|
|
13
|
+
{
|
|
14
|
+
override async set(key: K, val: V): Promise<void> {
|
|
15
|
+
// Inserting repeated values is a no-op
|
|
16
|
+
const exists = !!(await this.db
|
|
17
|
+
.index('hash')
|
|
18
|
+
.get(
|
|
19
|
+
IDBKeyRange.bound(
|
|
20
|
+
[this.container, this.normalizeKey(key), hash(val)],
|
|
21
|
+
[this.container, this.normalizeKey(key), hash(val)],
|
|
22
|
+
),
|
|
23
|
+
));
|
|
24
|
+
if (exists) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
// Get the maximum keyCount for the given key
|
|
28
|
+
// In order to support sparse multimaps, we cannot rely
|
|
29
|
+
// on just counting the number of entries for the key, since we would repeat slots
|
|
30
|
+
// if we delete an entry
|
|
31
|
+
// set -> container:key:0 (keyCount = 1)
|
|
32
|
+
// set -> container:key:1 (keyCount = 2)
|
|
33
|
+
// delete -> container:key:0 (keyCount = 1)
|
|
34
|
+
// set -> container:key:1 <--- already exists!
|
|
35
|
+
// Instead, we iterate in reverse order to get the last inserted entry
|
|
36
|
+
const index = this.db.index('keyCount');
|
|
37
|
+
const rangeQuery = IDBKeyRange.upperBound([this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER]);
|
|
38
|
+
const maxEntry = (await index.iterate(rangeQuery, 'prevunique').next()).value;
|
|
39
|
+
const count = maxEntry?.value?.keyCount ?? 0;
|
|
40
|
+
await this.db.put({
|
|
41
|
+
value: val,
|
|
42
|
+
hash: hash(val),
|
|
43
|
+
container: this.container,
|
|
44
|
+
key: this.normalizeKey(key),
|
|
45
|
+
keyCount: count + 1,
|
|
46
|
+
slot: this.slot(key, count),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async *getValuesAsync(key: K): AsyncIterableIterator<V> {
|
|
51
|
+
// Iterate over the whole range of keyCount for the given key
|
|
52
|
+
const index = this.db.index('keyCount');
|
|
53
|
+
const rangeQuery = IDBKeyRange.bound(
|
|
54
|
+
[this.container, this.normalizeKey(key), 0],
|
|
55
|
+
[this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER],
|
|
56
|
+
false,
|
|
57
|
+
false,
|
|
58
|
+
);
|
|
59
|
+
for await (const cursor of index.iterate(rangeQuery)) {
|
|
60
|
+
yield cursor.value.value as V;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getValueCountAsync(key: K): Promise<number> {
|
|
65
|
+
// Count entries over the keyCount index range for this key
|
|
66
|
+
const index = this.db.index('keyCount');
|
|
67
|
+
const rangeQuery = IDBKeyRange.bound(
|
|
68
|
+
[this.container, this.normalizeKey(key), 0],
|
|
69
|
+
[this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER],
|
|
70
|
+
false,
|
|
71
|
+
false,
|
|
72
|
+
);
|
|
73
|
+
return index.count(rangeQuery);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async deleteValue(key: K, val: V): Promise<void> {
|
|
77
|
+
// Since we know the value, we can hash it and directly query the "hash" index
|
|
78
|
+
// to avoid having to iterate over all the values
|
|
79
|
+
const fullKey = await this.db
|
|
80
|
+
.index('hash')
|
|
81
|
+
.getKey(
|
|
82
|
+
IDBKeyRange.bound(
|
|
83
|
+
[this.container, this.normalizeKey(key), hash(val)],
|
|
84
|
+
[this.container, this.normalizeKey(key), hash(val)],
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
if (fullKey) {
|
|
88
|
+
await this.db.delete(fullKey);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
override async delete(key: K): Promise<void> {
|
|
93
|
+
const index = this.db.index('keyCount');
|
|
94
|
+
const rangeQuery = IDBKeyRange.bound(
|
|
95
|
+
[this.container, this.normalizeKey(key), 0],
|
|
96
|
+
[this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER],
|
|
97
|
+
false,
|
|
98
|
+
false,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
for await (const cursor of index.iterate(rangeQuery)) {
|
|
102
|
+
await cursor.delete();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { IDBPDatabase, IDBPObjectStore } from 'idb';
|
|
2
|
+
import { hash } from 'ohash';
|
|
2
3
|
|
|
4
|
+
import type { Value } from '../interfaces/common.js';
|
|
3
5
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
4
6
|
import type { AztecIDBSchema } from './store.js';
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Stores a single value in IndexedDB.
|
|
8
10
|
*/
|
|
9
|
-
export class IndexedDBAztecSingleton<T> implements AztecAsyncSingleton<T> {
|
|
11
|
+
export class IndexedDBAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
|
|
10
12
|
#_db?: IDBPObjectStore<AztecIDBSchema, ['data'], 'data', 'readwrite'>;
|
|
11
13
|
#rootDB: IDBPDatabase<AztecIDBSchema>;
|
|
12
14
|
#container: string;
|
|
@@ -35,9 +37,10 @@ export class IndexedDBAztecSingleton<T> implements AztecAsyncSingleton<T> {
|
|
|
35
37
|
const result = await this.db.put({
|
|
36
38
|
container: this.#container,
|
|
37
39
|
slot: this.#slot,
|
|
38
|
-
key: this.#slot,
|
|
40
|
+
key: [this.#slot],
|
|
39
41
|
keyCount: 1,
|
|
40
42
|
value: val,
|
|
43
|
+
hash: hash(val),
|
|
41
44
|
});
|
|
42
45
|
return result !== undefined;
|
|
43
46
|
}
|
package/src/indexeddb/store.ts
CHANGED
|
@@ -1,26 +1,41 @@
|
|
|
1
1
|
import type { Logger } from '@aztec/foundation/log';
|
|
2
|
+
import { SerialQueue } from '@aztec/foundation/queue';
|
|
2
3
|
|
|
3
4
|
import { type DBSchema, type IDBPDatabase, deleteDB, openDB } from 'idb';
|
|
4
5
|
|
|
5
6
|
import type { AztecAsyncArray } from '../interfaces/array.js';
|
|
6
|
-
import type { Key, StoreSize } from '../interfaces/common.js';
|
|
7
|
+
import type { Key, StoreSize, Value } from '../interfaces/common.js';
|
|
7
8
|
import type { AztecAsyncCounter } from '../interfaces/counter.js';
|
|
8
|
-
import type { AztecAsyncMap
|
|
9
|
+
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
10
|
+
import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
|
|
9
11
|
import type { AztecAsyncSet } from '../interfaces/set.js';
|
|
10
12
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
11
13
|
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
12
14
|
import { IndexedDBAztecArray } from './array.js';
|
|
13
15
|
import { IndexedDBAztecMap } from './map.js';
|
|
16
|
+
import { IndexedDBAztecMultiMap } from './multi_map.js';
|
|
14
17
|
import { IndexedDBAztecSet } from './set.js';
|
|
15
18
|
import { IndexedDBAztecSingleton } from './singleton.js';
|
|
16
19
|
|
|
17
|
-
export type StoredData<V> = {
|
|
20
|
+
export type StoredData<V extends Value> = {
|
|
21
|
+
value: V;
|
|
22
|
+
container: string;
|
|
23
|
+
key: (string | number | Uint8Array)[];
|
|
24
|
+
keyCount: number;
|
|
25
|
+
slot: string;
|
|
26
|
+
hash: string;
|
|
27
|
+
};
|
|
18
28
|
|
|
19
29
|
export interface AztecIDBSchema extends DBSchema {
|
|
20
30
|
data: {
|
|
21
31
|
value: StoredData<any>;
|
|
22
32
|
key: string;
|
|
23
|
-
indexes: {
|
|
33
|
+
indexes: {
|
|
34
|
+
container: string;
|
|
35
|
+
key: any; // Runtime: (string | number | Uint8Array)[] - idb types don't support arrays
|
|
36
|
+
keyCount: number;
|
|
37
|
+
hash: string;
|
|
38
|
+
};
|
|
24
39
|
};
|
|
25
40
|
}
|
|
26
41
|
|
|
@@ -29,18 +44,27 @@ export interface AztecIDBSchema extends DBSchema {
|
|
|
29
44
|
*/
|
|
30
45
|
|
|
31
46
|
export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
32
|
-
#log: Logger;
|
|
33
47
|
#rootDB: IDBPDatabase<AztecIDBSchema>;
|
|
34
48
|
#name: string;
|
|
49
|
+
#txQueue: SerialQueue;
|
|
35
50
|
|
|
36
51
|
#containers = new Set<
|
|
37
|
-
|
|
52
|
+
| IndexedDBAztecArray<any>
|
|
53
|
+
| IndexedDBAztecMap<any, any>
|
|
54
|
+
| IndexedDBAztecMultiMap<any, any>
|
|
55
|
+
| IndexedDBAztecSet<any>
|
|
56
|
+
| IndexedDBAztecSingleton<any>
|
|
38
57
|
>();
|
|
39
58
|
|
|
40
|
-
constructor(
|
|
59
|
+
constructor(
|
|
60
|
+
rootDB: IDBPDatabase<AztecIDBSchema>,
|
|
61
|
+
public readonly isEphemeral: boolean,
|
|
62
|
+
name: string,
|
|
63
|
+
) {
|
|
41
64
|
this.#rootDB = rootDB;
|
|
42
|
-
this.#log = log;
|
|
43
65
|
this.#name = name;
|
|
66
|
+
this.#txQueue = new SerialQueue();
|
|
67
|
+
this.#txQueue.start();
|
|
44
68
|
}
|
|
45
69
|
/**
|
|
46
70
|
* Creates a new AztecKVStore backed by IndexedDB. The path to the database is optional. If not provided,
|
|
@@ -53,50 +77,32 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
53
77
|
* @returns The store
|
|
54
78
|
*/
|
|
55
79
|
static async open(log: Logger, name?: string, ephemeral: boolean = false): Promise<AztecIndexedDBStore> {
|
|
56
|
-
name = name && !ephemeral ? name :
|
|
80
|
+
name = name && !ephemeral ? name : globalThis.crypto.getRandomValues(new Uint8Array(16)).join('');
|
|
57
81
|
log.debug(`Opening IndexedDB ${ephemeral ? 'temp ' : ''}database with name ${name}`);
|
|
58
82
|
const rootDB = await openDB<AztecIDBSchema>(name, 1, {
|
|
59
83
|
upgrade(db) {
|
|
60
84
|
const objectStore = db.createObjectStore('data', { keyPath: 'slot' });
|
|
61
85
|
|
|
62
86
|
objectStore.createIndex('key', ['container', 'key'], { unique: false });
|
|
63
|
-
|
|
87
|
+
// Keep count of the maximum number of keys ever inserted in the container
|
|
88
|
+
// This allows unique slots for repeated keys, which is useful for multi-maps
|
|
89
|
+
objectStore.createIndex('keyCount', ['container', 'key', 'keyCount'], { unique: true });
|
|
90
|
+
// Keep an index on the pair key-hash for a given container, allowing us to efficiently
|
|
91
|
+
// delete unique values from multi-maps
|
|
92
|
+
objectStore.createIndex('hash', ['container', 'key', 'hash'], { unique: true });
|
|
64
93
|
},
|
|
65
94
|
});
|
|
66
95
|
|
|
67
|
-
const kvStore = new AztecIndexedDBStore(rootDB, ephemeral,
|
|
96
|
+
const kvStore = new AztecIndexedDBStore(rootDB, ephemeral, name);
|
|
68
97
|
return kvStore;
|
|
69
98
|
}
|
|
70
99
|
|
|
71
|
-
/**
|
|
72
|
-
* Forks the current DB into a new DB by backing it up to a temporary location and opening a new indexedb.
|
|
73
|
-
* @returns A new AztecIndexedDBStore.
|
|
74
|
-
*/
|
|
75
|
-
async fork(): Promise<AztecAsyncKVStore> {
|
|
76
|
-
const forkedStore = await AztecIndexedDBStore.open(this.#log, undefined, true);
|
|
77
|
-
this.#log.verbose(`Forking store to ${forkedStore.#name}`);
|
|
78
|
-
|
|
79
|
-
// Copy old data to new store
|
|
80
|
-
const oldData = this.#rootDB.transaction('data').store;
|
|
81
|
-
const dataToWrite = [];
|
|
82
|
-
for await (const cursor of oldData.iterate()) {
|
|
83
|
-
dataToWrite.push(cursor.value);
|
|
84
|
-
}
|
|
85
|
-
const tx = forkedStore.#rootDB.transaction('data', 'readwrite').store;
|
|
86
|
-
for (const data of dataToWrite) {
|
|
87
|
-
await tx.add(data);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
this.#log.debug(`Forked store at ${forkedStore.#name} opened successfully`);
|
|
91
|
-
return forkedStore;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
100
|
/**
|
|
95
101
|
* Creates a new AztecMap in the store.
|
|
96
102
|
* @param name - Name of the map
|
|
97
103
|
* @returns A new AztecMap
|
|
98
104
|
*/
|
|
99
|
-
openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V> {
|
|
105
|
+
openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V> {
|
|
100
106
|
const map = new IndexedDBAztecMap<K, V>(this.#rootDB, name);
|
|
101
107
|
this.#containers.add(map);
|
|
102
108
|
return map;
|
|
@@ -118,8 +124,8 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
118
124
|
* @param name - Name of the map
|
|
119
125
|
* @returns A new AztecMultiMap
|
|
120
126
|
*/
|
|
121
|
-
openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V> {
|
|
122
|
-
const multimap = new
|
|
127
|
+
openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V> {
|
|
128
|
+
const multimap = new IndexedDBAztecMultiMap<K, V>(this.#rootDB, name);
|
|
123
129
|
this.#containers.add(multimap);
|
|
124
130
|
return multimap;
|
|
125
131
|
}
|
|
@@ -133,7 +139,7 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
133
139
|
* @param name - Name of the array
|
|
134
140
|
* @returns A new AztecArray
|
|
135
141
|
*/
|
|
136
|
-
openArray<T>(name: string): AztecAsyncArray<T> {
|
|
142
|
+
openArray<T extends Value>(name: string): AztecAsyncArray<T> {
|
|
137
143
|
const array = new IndexedDBAztecArray<T>(this.#rootDB, name);
|
|
138
144
|
this.#containers.add(array);
|
|
139
145
|
return array;
|
|
@@ -144,7 +150,7 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
144
150
|
* @param name - Name of the singleton
|
|
145
151
|
* @returns A new AztecSingleton
|
|
146
152
|
*/
|
|
147
|
-
openSingleton<T>(name: string): AztecAsyncSingleton<T> {
|
|
153
|
+
openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T> {
|
|
148
154
|
const singleton = new IndexedDBAztecSingleton<T>(this.#rootDB, name);
|
|
149
155
|
this.#containers.add(singleton);
|
|
150
156
|
return singleton;
|
|
@@ -155,22 +161,27 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
155
161
|
* @param callback - Function to execute in a transaction
|
|
156
162
|
* @returns A promise that resolves to the return value of the callback
|
|
157
163
|
*/
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
for
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
164
|
+
transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
|
|
165
|
+
// We can only have one transaction at a time for the same store
|
|
166
|
+
// So we need to wait for the current one to finish
|
|
167
|
+
return this.#txQueue.put(async () => {
|
|
168
|
+
const tx = this.#rootDB.transaction('data', 'readwrite');
|
|
169
|
+
for (const container of this.#containers) {
|
|
170
|
+
container.db = tx.store;
|
|
171
|
+
}
|
|
172
|
+
// Avoid awaiting this promise so it doesn't get scheduled in the next microtask
|
|
173
|
+
// By then, the tx would be closed
|
|
174
|
+
const runningPromise = callback();
|
|
175
|
+
// Wait for the transaction to finish
|
|
176
|
+
await tx.done;
|
|
177
|
+
for (const container of this.#containers) {
|
|
178
|
+
container.db = undefined;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Return the result of the callback.
|
|
182
|
+
// Tx is guaranteed to already be closed, so the await doesn't hurt anything here
|
|
183
|
+
return await runningPromise;
|
|
184
|
+
});
|
|
174
185
|
}
|
|
175
186
|
|
|
176
187
|
/**
|
|
@@ -188,10 +199,14 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
|
|
|
188
199
|
}
|
|
189
200
|
|
|
190
201
|
estimateSize(): Promise<StoreSize> {
|
|
191
|
-
return Promise.resolve({ mappingSize: 0, actualSize: 0, numItems: 0 });
|
|
202
|
+
return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
|
|
192
203
|
}
|
|
193
204
|
|
|
194
205
|
close(): Promise<void> {
|
|
195
206
|
return Promise.resolve();
|
|
196
207
|
}
|
|
208
|
+
|
|
209
|
+
backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
|
|
210
|
+
throw new Error('Method not implemented.');
|
|
211
|
+
}
|
|
197
212
|
}
|
package/src/interfaces/array.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { Value } from './common.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* An array backed by a persistent store. Can not have any holes in it.
|
|
3
5
|
*/
|
|
4
|
-
interface BaseAztecArray<T> {
|
|
6
|
+
interface BaseAztecArray<T extends Value> {
|
|
5
7
|
/**
|
|
6
8
|
* Pushes values to the end of the array
|
|
7
9
|
* @param vals - The values to push to the end of the array
|
|
@@ -27,7 +29,7 @@ interface BaseAztecArray<T> {
|
|
|
27
29
|
/**
|
|
28
30
|
* An array backed by a persistent store. Can not have any holes in it.
|
|
29
31
|
*/
|
|
30
|
-
export interface AztecAsyncArray<T> extends BaseAztecArray<T> {
|
|
32
|
+
export interface AztecAsyncArray<T extends Value> extends BaseAztecArray<T> {
|
|
31
33
|
/**
|
|
32
34
|
* The size of the array
|
|
33
35
|
*/
|
|
@@ -58,7 +60,7 @@ export interface AztecAsyncArray<T> extends BaseAztecArray<T> {
|
|
|
58
60
|
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
export interface AztecArray<T> extends BaseAztecArray<T> {
|
|
63
|
+
export interface AztecArray<T extends Value> extends BaseAztecArray<T> {
|
|
62
64
|
/**
|
|
63
65
|
* The size of the array
|
|
64
66
|
*/
|
package/src/interfaces/common.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
export type Key = string | number | Array<string | number>;
|
|
1
|
+
/** The key type for use with the kv-store */
|
|
2
|
+
export type Key = string | number | Uint8Array | Array<string | number>;
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
9
|
-
export type
|
|
4
|
+
export type Value = NonNullable<any>;
|
|
5
|
+
|
|
6
|
+
/** A range of keys of arbitrary type. */
|
|
7
|
+
export type CustomRange<K> = {
|
|
10
8
|
/** The key of the first item to include */
|
|
11
9
|
start?: K;
|
|
12
10
|
/** The key of the last item to include */
|
|
@@ -17,4 +15,17 @@ export type Range<K extends Key = Key> = {
|
|
|
17
15
|
limit?: number;
|
|
18
16
|
};
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
/** Maps a custom range into a range of valid key types to iterate over. */
|
|
19
|
+
export function mapRange<CK, K extends Key = Key>(range: CustomRange<CK>, mapFn: (key: CK) => K): Range<K> {
|
|
20
|
+
return {
|
|
21
|
+
start: range.start ? mapFn(range.start) : undefined,
|
|
22
|
+
end: range.end ? mapFn(range.end) : undefined,
|
|
23
|
+
reverse: range.reverse,
|
|
24
|
+
limit: range.limit,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A range of keys to iterate over. */
|
|
29
|
+
export type Range<K extends Key = Key> = CustomRange<K>;
|
|
30
|
+
|
|
31
|
+
export type StoreSize = { mappingSize: number; physicalFileSize: number; actualSize: number; numItems: number };
|
package/src/interfaces/index.ts
CHANGED
|
@@ -4,4 +4,6 @@ export * from './counter.js';
|
|
|
4
4
|
export * from './singleton.js';
|
|
5
5
|
export * from './store.js';
|
|
6
6
|
export * from './set.js';
|
|
7
|
-
export
|
|
7
|
+
export * from './multi_map.js';
|
|
8
|
+
export { mapRange } from './common.js';
|
|
9
|
+
export type { CustomRange, Range, StoreSize } from './common.js';
|
package/src/interfaces/map.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Key, Range } from './common.js';
|
|
1
|
+
import type { Key, Range, Value } from './common.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A map backed by a persistent store.
|
|
5
5
|
*/
|
|
6
|
-
interface AztecBaseMap<K extends Key, V> {
|
|
6
|
+
interface AztecBaseMap<K extends Key, V extends Value> {
|
|
7
7
|
/**
|
|
8
8
|
* Sets the value at the given key.
|
|
9
9
|
* @param key - The key to set the value at
|
|
@@ -11,6 +11,12 @@ interface AztecBaseMap<K extends Key, V> {
|
|
|
11
11
|
*/
|
|
12
12
|
set(key: K, val: V): Promise<void>;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Sets the values at the given keys.
|
|
16
|
+
* @param entries - The entries to set
|
|
17
|
+
*/
|
|
18
|
+
setMany(entries: { key: K; value: V }[]): Promise<void>;
|
|
19
|
+
|
|
14
20
|
/**
|
|
15
21
|
* Sets the value at the given key if it does not already exist.
|
|
16
22
|
* @param key - The key to set the value at
|
|
@@ -24,13 +30,19 @@ interface AztecBaseMap<K extends Key, V> {
|
|
|
24
30
|
*/
|
|
25
31
|
delete(key: K): Promise<void>;
|
|
26
32
|
}
|
|
27
|
-
export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
33
|
+
export interface AztecMap<K extends Key, V extends Value> extends AztecBaseMap<K, V> {
|
|
28
34
|
/**
|
|
29
35
|
* Gets the value at the given key.
|
|
30
36
|
* @param key - The key to get the value from
|
|
31
37
|
*/
|
|
32
38
|
get(key: K): V | undefined;
|
|
33
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Gets the current size of the map.
|
|
42
|
+
* @returns The size of the map
|
|
43
|
+
*/
|
|
44
|
+
size(): number;
|
|
45
|
+
|
|
34
46
|
/**
|
|
35
47
|
* Checks if a key exists in the map.
|
|
36
48
|
* @param key - The key to check
|
|
@@ -62,44 +74,10 @@ export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
|
62
74
|
clear(): Promise<void>;
|
|
63
75
|
}
|
|
64
76
|
|
|
65
|
-
export interface AztecMapWithSize<K extends Key, V> extends AztecMap<K, V> {
|
|
66
|
-
/**
|
|
67
|
-
* Gets the size of the map.
|
|
68
|
-
* @returns The size of the map
|
|
69
|
-
*/
|
|
70
|
-
size(): number;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* A map backed by a persistent store that can have multiple values for a single key.
|
|
75
|
-
*/
|
|
76
|
-
export interface AztecMultiMap<K extends Key, V> extends AztecMap<K, V> {
|
|
77
|
-
/**
|
|
78
|
-
* Gets all the values at the given key.
|
|
79
|
-
* @param key - The key to get the values from
|
|
80
|
-
*/
|
|
81
|
-
getValues(key: K): IterableIterator<V>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Deletes a specific value at the given key.
|
|
85
|
-
* @param key - The key to delete the value at
|
|
86
|
-
* @param val - The value to delete
|
|
87
|
-
*/
|
|
88
|
-
deleteValue(key: K, val: V): Promise<void>;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface AztecMultiMapWithSize<K extends Key, V> extends AztecMultiMap<K, V> {
|
|
92
|
-
/**
|
|
93
|
-
* Gets the size of the map.
|
|
94
|
-
* @returns The size of the map
|
|
95
|
-
*/
|
|
96
|
-
size(): number;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
77
|
/**
|
|
100
78
|
* A map backed by a persistent store.
|
|
101
79
|
*/
|
|
102
|
-
export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
80
|
+
export interface AztecAsyncMap<K extends Key, V extends Value> extends AztecBaseMap<K, V> {
|
|
103
81
|
/**
|
|
104
82
|
* Gets the value at the given key.
|
|
105
83
|
* @param key - The key to get the value from
|
|
@@ -130,22 +108,10 @@ export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
|
130
108
|
* @param range - The range of keys to iterate over
|
|
131
109
|
*/
|
|
132
110
|
keysAsync(range?: Range<K>): AsyncIterableIterator<K>;
|
|
133
|
-
}
|
|
134
111
|
|
|
135
|
-
/**
|
|
136
|
-
* A map backed by a persistent store that can have multiple values for a single key.
|
|
137
|
-
*/
|
|
138
|
-
export interface AztecAsyncMultiMap<K extends Key, V> extends AztecAsyncMap<K, V> {
|
|
139
112
|
/**
|
|
140
|
-
* Gets
|
|
141
|
-
* @
|
|
142
|
-
*/
|
|
143
|
-
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Deletes a specific value at the given key.
|
|
147
|
-
* @param key - The key to delete the value at
|
|
148
|
-
* @param val - The value to delete
|
|
113
|
+
* Gets the current size of the map.
|
|
114
|
+
* @returns The size of the map
|
|
149
115
|
*/
|
|
150
|
-
|
|
116
|
+
sizeAsync(): Promise<number>;
|
|
151
117
|
}
|