@aztec/kv-store 0.0.0-test.1 → 0.0.1-commit.b655e406
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 +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +5 -3
- package/dest/indexeddb/array.d.ts +2 -1
- package/dest/indexeddb/array.d.ts.map +1 -1
- package/dest/indexeddb/array.js +3 -0
- package/dest/indexeddb/index.js +1 -1
- package/dest/indexeddb/map.d.ts +11 -5
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +38 -60
- package/dest/indexeddb/multi_map.d.ts +12 -0
- package/dest/indexeddb/multi_map.d.ts.map +1 -0
- package/dest/indexeddb/multi_map.js +78 -0
- package/dest/indexeddb/singleton.d.ts +2 -1
- package/dest/indexeddb/singleton.d.ts.map +1 -1
- package/dest/indexeddb/singleton.js +3 -1
- package/dest/indexeddb/store.d.ts +12 -13
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +45 -42
- package/dest/interfaces/array.d.ts +4 -3
- package/dest/interfaces/array.d.ts.map +1 -1
- package/dest/interfaces/array.js +1 -3
- package/dest/interfaces/common.d.ts +10 -8
- package/dest/interfaces/common.d.ts.map +1 -1
- package/dest/interfaces/common.js +8 -3
- package/dest/interfaces/index.d.ts +3 -1
- package/dest/interfaces/index.d.ts.map +1 -1
- package/dest/interfaces/index.js +2 -0
- package/dest/interfaces/map.d.ts +20 -48
- package/dest/interfaces/map.d.ts.map +1 -1
- package/dest/interfaces/map.js +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +135 -70
- package/dest/interfaces/multi_map.d.ts +35 -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 +245 -0
- package/dest/interfaces/store.d.ts +17 -42
- package/dest/interfaces/store.d.ts.map +1 -1
- package/dest/interfaces/utils.d.ts +1 -0
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +2 -1
- package/dest/lmdb/array.d.ts +2 -1
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/index.js +2 -2
- package/dest/lmdb/map.d.ts +10 -22
- package/dest/lmdb/map.d.ts.map +1 -1
- package/dest/lmdb/map.js +15 -81
- package/dest/lmdb/multi_map.d.ts +12 -0
- package/dest/lmdb/multi_map.d.ts.map +1 -0
- package/dest/lmdb/multi_map.js +29 -0
- package/dest/lmdb/store.d.ts +7 -22
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +11 -31
- package/dest/lmdb-v2/array.d.ts +2 -1
- package/dest/lmdb-v2/array.d.ts.map +1 -1
- package/dest/lmdb-v2/array.js +1 -0
- package/dest/lmdb-v2/factory.d.ts +1 -1
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +16 -6
- package/dest/lmdb-v2/map.d.ts +10 -43
- package/dest/lmdb-v2/map.d.ts.map +1 -1
- package/dest/lmdb-v2/map.js +17 -103
- package/dest/lmdb-v2/message.d.ts +23 -4
- 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 +51 -0
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -0
- package/dest/lmdb-v2/multi_map.js +113 -0
- package/dest/lmdb-v2/read_transaction.d.ts +2 -0
- 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.map +1 -1
- package/dest/lmdb-v2/singleton.js +1 -0
- package/dest/lmdb-v2/store.d.ts +9 -8
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +19 -7
- package/dest/lmdb-v2/utils.d.ts +2 -4
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.d.ts +2 -4
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- package/dest/stores/index.d.ts +1 -0
- package/dest/stores/index.d.ts.map +1 -1
- package/dest/stores/index.js +1 -0
- package/dest/stores/l2_tips_store.d.ts +2 -1
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +18 -9
- package/package.json +18 -14
- package/src/config.ts +6 -4
- package/src/indexeddb/array.ts +5 -1
- package/src/indexeddb/index.ts +2 -2
- package/src/indexeddb/map.ts +35 -53
- package/src/indexeddb/multi_map.ts +79 -0
- package/src/indexeddb/singleton.ts +4 -1
- package/src/indexeddb/store.ts +66 -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 +73 -44
- package/src/interfaces/multi_map.ts +38 -0
- package/src/interfaces/multi_map_test_suite.ts +242 -0
- package/src/interfaces/store.ts +18 -53
- package/src/interfaces/utils.ts +1 -0
- package/src/lmdb/array.ts +2 -1
- package/src/lmdb/index.ts +3 -3
- package/src/lmdb/map.ts +23 -94
- package/src/lmdb/multi_map.ts +35 -0
- package/src/lmdb/store.ts +23 -47
- package/src/lmdb-v2/array.ts +7 -2
- package/src/lmdb-v2/factory.ts +17 -10
- package/src/lmdb-v2/map.ts +29 -126
- package/src/lmdb-v2/message.ts +23 -0
- package/src/lmdb-v2/multi_map.ts +141 -0
- package/src/lmdb-v2/read_transaction.ts +40 -0
- package/src/lmdb-v2/set.ts +33 -0
- package/src/lmdb-v2/singleton.ts +5 -1
- package/src/lmdb-v2/store.ts +22 -14
- package/src/lmdb-v2/write_transaction.ts +2 -2
- package/src/stores/index.ts +2 -0
- package/src/stores/l2_tips_store.ts +18 -9
- 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,242 @@
|
|
|
1
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
2
|
+
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
|
|
5
|
+
import type { Key, Range } from './common.js';
|
|
6
|
+
import type { AztecAsyncMultiMap, AztecMultiMap } from './multi_map.js';
|
|
7
|
+
import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
|
|
8
|
+
import { isSyncStore } from './utils.js';
|
|
9
|
+
|
|
10
|
+
export function describeAztecMultiMap(
|
|
11
|
+
testName: string,
|
|
12
|
+
getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
|
|
13
|
+
forceAsync: boolean = false,
|
|
14
|
+
) {
|
|
15
|
+
describe(testName, () => {
|
|
16
|
+
let store: AztecKVStore | AztecAsyncKVStore;
|
|
17
|
+
let multiMap: AztecMultiMap<Key, string> | AztecAsyncMultiMap<Key, string>;
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
store = await getStore();
|
|
21
|
+
multiMap = store.openMultiMap<string, string>('test');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(async () => {
|
|
25
|
+
await store.delete();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
async function get(key: Key, sut: AztecAsyncMultiMap<any, any> | AztecMultiMap<any, any> = multiMap) {
|
|
29
|
+
return isSyncStore(store) && !forceAsync
|
|
30
|
+
? (sut as AztecMultiMap<any, any>).get(key)
|
|
31
|
+
: await (sut as AztecAsyncMultiMap<any, any>).getAsync(key);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function size(sut: AztecAsyncMultiMap<any, any> | AztecMultiMap<any, any> = multiMap) {
|
|
35
|
+
return isSyncStore(store) && !forceAsync
|
|
36
|
+
? (sut as AztecMultiMap<any, any>).size()
|
|
37
|
+
: await (sut as AztecAsyncMultiMap<any, any>).sizeAsync();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function entries() {
|
|
41
|
+
return isSyncStore(store) && !forceAsync
|
|
42
|
+
? await toArray((multiMap as AztecMultiMap<any, any>).entries())
|
|
43
|
+
: await toArray((multiMap as AztecAsyncMultiMap<any, any>).entriesAsync());
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function values() {
|
|
47
|
+
return isSyncStore(store) && !forceAsync
|
|
48
|
+
? await toArray((multiMap as AztecMultiMap<any, any>).values())
|
|
49
|
+
: await toArray((multiMap as AztecAsyncMultiMap<any, any>).valuesAsync());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function keys(range?: Range<Key>, sut: AztecAsyncMultiMap<any, any> | AztecMultiMap<any, any> = multiMap) {
|
|
53
|
+
return isSyncStore(store) && !forceAsync
|
|
54
|
+
? await toArray((sut as AztecMultiMap<any, any>).keys(range))
|
|
55
|
+
: await toArray((sut as AztecAsyncMultiMap<any, any>).keysAsync(range));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function getValues(key: Key) {
|
|
59
|
+
return isSyncStore(store) && !forceAsync
|
|
60
|
+
? await toArray((multiMap as AztecMultiMap<any, any>).getValues(key))
|
|
61
|
+
: await toArray((multiMap as AztecAsyncMultiMap<any, any>).getValuesAsync(key));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
it('should be able to set and get values', async () => {
|
|
65
|
+
await multiMap.set('foo', 'bar');
|
|
66
|
+
await multiMap.set('baz', 'qux');
|
|
67
|
+
|
|
68
|
+
expect(await get('foo')).to.equal('bar');
|
|
69
|
+
expect(await get('baz')).to.equal('qux');
|
|
70
|
+
expect(await get('quux')).to.equal(undefined);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should be able to set values if they do not exist', async () => {
|
|
74
|
+
expect(await multiMap.setIfNotExists('foo', 'bar')).to.equal(true);
|
|
75
|
+
expect(await multiMap.setIfNotExists('foo', 'baz')).to.equal(false);
|
|
76
|
+
|
|
77
|
+
expect(await get('foo')).to.equal('bar');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should be able to delete values', async () => {
|
|
81
|
+
await multiMap.set('foo', 'bar');
|
|
82
|
+
await multiMap.set('baz', 'qux');
|
|
83
|
+
|
|
84
|
+
await multiMap.delete('foo');
|
|
85
|
+
|
|
86
|
+
expect(await get('foo')).to.equal(undefined);
|
|
87
|
+
expect(await get('baz')).to.equal('qux');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should be able to get size of the map', async () => {
|
|
91
|
+
await multiMap.set('foo', 'bar');
|
|
92
|
+
expect(await size()).to.equal(1);
|
|
93
|
+
await multiMap.set('baz', 'qux');
|
|
94
|
+
expect(await size()).to.equal(2);
|
|
95
|
+
|
|
96
|
+
await multiMap.delete('foo');
|
|
97
|
+
expect(await size()).to.equal(1);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should be able to iterate over entries when there are no keys', async () => {
|
|
101
|
+
expect(await entries()).to.deep.equal([]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should be able to iterate over entries', async () => {
|
|
105
|
+
await multiMap.set('foo', 'bar');
|
|
106
|
+
await multiMap.set('baz', 'qux');
|
|
107
|
+
|
|
108
|
+
expect(await entries()).to.deep.equal([
|
|
109
|
+
['baz', 'qux'],
|
|
110
|
+
['foo', 'bar'],
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should be able to iterate over values', async () => {
|
|
115
|
+
await multiMap.set('foo', 'bar');
|
|
116
|
+
await multiMap.set('baz', 'quux');
|
|
117
|
+
|
|
118
|
+
expect(await values()).to.deep.equal(['quux', 'bar']);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should be able to iterate over keys', async () => {
|
|
122
|
+
await multiMap.set('foo', 'bar');
|
|
123
|
+
await multiMap.set('baz', 'qux');
|
|
124
|
+
|
|
125
|
+
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should be able to get multiple values for a single key', async () => {
|
|
129
|
+
await multiMap.set('foo', 'bar');
|
|
130
|
+
await multiMap.set('foo', 'baz');
|
|
131
|
+
|
|
132
|
+
expect(await getValues('foo')).to.deep.equal(['bar', 'baz']);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should ignore multiple identical values', async () => {
|
|
136
|
+
await multiMap.set('foo', 'bar');
|
|
137
|
+
await multiMap.set('foo', 'bar');
|
|
138
|
+
|
|
139
|
+
expect(await getValues('foo')).to.deep.equal(['bar']);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should be able to delete individual values for a single key', async () => {
|
|
143
|
+
await multiMap.set('foo', '1');
|
|
144
|
+
await multiMap.set('foo', '2');
|
|
145
|
+
await multiMap.set('foo', '3');
|
|
146
|
+
|
|
147
|
+
await multiMap.deleteValue('foo', '2');
|
|
148
|
+
|
|
149
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3']);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should be able to get size of the map with duplicate keys', async () => {
|
|
153
|
+
await multiMap.set('foo', '1');
|
|
154
|
+
await multiMap.set('foo', '2');
|
|
155
|
+
await multiMap.set('foo', '3');
|
|
156
|
+
expect(await size()).to.equal(3);
|
|
157
|
+
|
|
158
|
+
await multiMap.set('bar', '1');
|
|
159
|
+
await multiMap.set('bar', '2');
|
|
160
|
+
await multiMap.set('bar', '3');
|
|
161
|
+
expect(await size()).to.equal(6);
|
|
162
|
+
|
|
163
|
+
await multiMap.deleteValue('foo', '2');
|
|
164
|
+
expect(await size()).to.equal(5);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should be able to delete the last and first values for a key', async () => {
|
|
168
|
+
await multiMap.set('foo', '1');
|
|
169
|
+
await multiMap.set('foo', '2');
|
|
170
|
+
await multiMap.set('foo', '3');
|
|
171
|
+
|
|
172
|
+
await multiMap.deleteValue('foo', '1');
|
|
173
|
+
|
|
174
|
+
expect(await getValues('foo')).to.deep.equal(['2', '3']);
|
|
175
|
+
|
|
176
|
+
await multiMap.deleteValue('foo', '3');
|
|
177
|
+
|
|
178
|
+
expect(await getValues('foo')).to.deep.equal(['2']);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should be able to fully clear a key', async () => {
|
|
182
|
+
await multiMap.set('foo', '1');
|
|
183
|
+
await multiMap.set('foo', '2');
|
|
184
|
+
await multiMap.set('foo', '3');
|
|
185
|
+
|
|
186
|
+
await multiMap.deleteValue('foo', '1');
|
|
187
|
+
await multiMap.deleteValue('foo', '3');
|
|
188
|
+
await multiMap.deleteValue('foo', '2');
|
|
189
|
+
|
|
190
|
+
expect(await getValues('foo')).to.deep.equal([]);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('should be able to insert after deletion', async () => {
|
|
194
|
+
await multiMap.set('foo', '1');
|
|
195
|
+
await multiMap.set('foo', '2');
|
|
196
|
+
await multiMap.set('foo', '3');
|
|
197
|
+
|
|
198
|
+
await multiMap.deleteValue('foo', '2');
|
|
199
|
+
await multiMap.set('foo', 'bar');
|
|
200
|
+
|
|
201
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3', 'bar']);
|
|
202
|
+
|
|
203
|
+
// Delete the just-added entry
|
|
204
|
+
await multiMap.deleteValue('foo', 'bar');
|
|
205
|
+
|
|
206
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3']);
|
|
207
|
+
|
|
208
|
+
// Reinsert the initially deleted key
|
|
209
|
+
await multiMap.set('foo', '2');
|
|
210
|
+
|
|
211
|
+
// LMDB and IndexedDB behave differently here, the former ordering by value and the latter by insertion. This is
|
|
212
|
+
// fine because there is no expectation for values in a multimap to be ordered.
|
|
213
|
+
const values = (await getValues('foo')).sort((a, b) => a.localeCompare(b));
|
|
214
|
+
expect(values).to.deep.equal(['1', '2', '3']);
|
|
215
|
+
|
|
216
|
+
// Fully clear the key
|
|
217
|
+
await multiMap.deleteValue('foo', '1');
|
|
218
|
+
await multiMap.deleteValue('foo', '3');
|
|
219
|
+
await multiMap.deleteValue('foo', '2');
|
|
220
|
+
|
|
221
|
+
// Insert some more
|
|
222
|
+
await multiMap.set('foo', 'baz');
|
|
223
|
+
await multiMap.set('foo', 'qux');
|
|
224
|
+
expect(await getValues('foo')).to.deep.equal(['baz', 'qux']);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('supports range queries', async () => {
|
|
228
|
+
await multiMap.set('a', 'a');
|
|
229
|
+
await multiMap.set('b', 'b');
|
|
230
|
+
await multiMap.set('c', 'c');
|
|
231
|
+
await multiMap.set('d', 'd');
|
|
232
|
+
|
|
233
|
+
expect(await keys({ start: 'b', end: 'c' })).to.deep.equal(['b']);
|
|
234
|
+
expect(await keys({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
|
|
235
|
+
expect(await keys({ end: 'c' })).to.deep.equal(['a', 'b']);
|
|
236
|
+
expect(await keys({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
|
|
237
|
+
expect(await keys({ start: 'b', limit: 1 })).to.deep.equal(['b']);
|
|
238
|
+
expect(await keys({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
|
|
239
|
+
expect(await keys({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
}
|
package/src/interfaces/store.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
import type { AztecArray, AztecAsyncArray } from './array.js';
|
|
2
|
-
import type { Key, StoreSize } from './common.js';
|
|
2
|
+
import type { Key, StoreSize, Value } from './common.js';
|
|
3
3
|
import type { AztecAsyncCounter, AztecCounter } from './counter.js';
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
AztecAsyncMultiMap,
|
|
7
|
-
AztecMap,
|
|
8
|
-
AztecMapWithSize,
|
|
9
|
-
AztecMultiMap,
|
|
10
|
-
AztecMultiMapWithSize,
|
|
11
|
-
} from './map.js';
|
|
4
|
+
import type { AztecAsyncMap, AztecMap } from './map.js';
|
|
5
|
+
import type { AztecAsyncMultiMap, AztecMultiMap } from './multi_map.js';
|
|
12
6
|
import type { AztecAsyncSet, AztecSet } from './set.js';
|
|
13
7
|
import type { AztecAsyncSingleton, AztecSingleton } from './singleton.js';
|
|
14
8
|
|
|
@@ -20,7 +14,7 @@ export interface AztecKVStore {
|
|
|
20
14
|
* @param name - The name of the map
|
|
21
15
|
* @returns The map
|
|
22
16
|
*/
|
|
23
|
-
openMap<K extends Key, V>(name: string): AztecMap<K, V>;
|
|
17
|
+
openMap<K extends Key, V extends Value>(name: string): AztecMap<K, V>;
|
|
24
18
|
|
|
25
19
|
/**
|
|
26
20
|
* Creates a new set.
|
|
@@ -34,35 +28,21 @@ export interface AztecKVStore {
|
|
|
34
28
|
* @param name - The name of the multi-map
|
|
35
29
|
* @returns The multi-map
|
|
36
30
|
*/
|
|
37
|
-
openMultiMap<K extends Key, V>(name: string): AztecMultiMap<K, V>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Creates a new multi-map with size.
|
|
41
|
-
* @param name - The name of the multi-map
|
|
42
|
-
* @returns The multi-map
|
|
43
|
-
*/
|
|
44
|
-
openMultiMapWithSize<K extends Key, V>(name: string): AztecMultiMapWithSize<K, V>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Creates a new map with size.
|
|
48
|
-
* @param name - The name of the map
|
|
49
|
-
* @returns The map
|
|
50
|
-
*/
|
|
51
|
-
openMapWithSize<K extends Key, V>(name: string): AztecMapWithSize<K, V>;
|
|
31
|
+
openMultiMap<K extends Key, V extends Value>(name: string): AztecMultiMap<K, V>;
|
|
52
32
|
|
|
53
33
|
/**
|
|
54
34
|
* Creates a new array.
|
|
55
35
|
* @param name - The name of the array
|
|
56
36
|
* @returns The array
|
|
57
37
|
*/
|
|
58
|
-
openArray<T>(name: string): AztecArray<T>;
|
|
38
|
+
openArray<T extends Value>(name: string): AztecArray<T>;
|
|
59
39
|
|
|
60
40
|
/**
|
|
61
41
|
* Creates a new singleton.
|
|
62
42
|
* @param name - The name of the singleton
|
|
63
43
|
* @returns The singleton
|
|
64
44
|
*/
|
|
65
|
-
openSingleton<T>(name: string): AztecSingleton<T>;
|
|
45
|
+
openSingleton<T extends Value>(name: string): AztecSingleton<T>;
|
|
66
46
|
|
|
67
47
|
/**
|
|
68
48
|
* Creates a new count map.
|
|
@@ -81,11 +61,6 @@ export interface AztecKVStore {
|
|
|
81
61
|
*/
|
|
82
62
|
clear(): Promise<void>;
|
|
83
63
|
|
|
84
|
-
/**
|
|
85
|
-
* Forks the store.
|
|
86
|
-
*/
|
|
87
|
-
fork(): Promise<AztecKVStore>;
|
|
88
|
-
|
|
89
64
|
/**
|
|
90
65
|
* Deletes the store
|
|
91
66
|
*/
|
|
@@ -108,7 +83,7 @@ export interface AztecAsyncKVStore {
|
|
|
108
83
|
* @param name - The name of the map
|
|
109
84
|
* @returns The map
|
|
110
85
|
*/
|
|
111
|
-
openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V>;
|
|
86
|
+
openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V>;
|
|
112
87
|
|
|
113
88
|
/**
|
|
114
89
|
* Creates a new set.
|
|
@@ -122,21 +97,21 @@ export interface AztecAsyncKVStore {
|
|
|
122
97
|
* @param name - The name of the multi-map
|
|
123
98
|
* @returns The multi-map
|
|
124
99
|
*/
|
|
125
|
-
openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V>;
|
|
100
|
+
openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V>;
|
|
126
101
|
|
|
127
102
|
/**
|
|
128
103
|
* Creates a new array.
|
|
129
104
|
* @param name - The name of the array
|
|
130
105
|
* @returns The array
|
|
131
106
|
*/
|
|
132
|
-
openArray<T>(name: string): AztecAsyncArray<T>;
|
|
107
|
+
openArray<T extends Value>(name: string): AztecAsyncArray<T>;
|
|
133
108
|
|
|
134
109
|
/**
|
|
135
110
|
* Creates a new singleton.
|
|
136
111
|
* @param name - The name of the singleton
|
|
137
112
|
* @returns The singleton
|
|
138
113
|
*/
|
|
139
|
-
openSingleton<T>(name: string): AztecAsyncSingleton<T>;
|
|
114
|
+
openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T>;
|
|
140
115
|
|
|
141
116
|
/**
|
|
142
117
|
* Creates a new count map.
|
|
@@ -150,28 +125,18 @@ export interface AztecAsyncKVStore {
|
|
|
150
125
|
*/
|
|
151
126
|
transactionAsync<T extends Exclude<any, Promise<any>>>(callback: () => Promise<T>): Promise<T>;
|
|
152
127
|
|
|
153
|
-
/**
|
|
154
|
-
* Clears all entries in the store
|
|
155
|
-
*/
|
|
128
|
+
/** Clears all entries in the store */
|
|
156
129
|
clear(): Promise<void>;
|
|
157
130
|
|
|
158
|
-
/**
|
|
159
|
-
* Forks the store.
|
|
160
|
-
*/
|
|
161
|
-
fork(): Promise<AztecAsyncKVStore>;
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Deletes the store
|
|
165
|
-
*/
|
|
131
|
+
/** Deletes the store */
|
|
166
132
|
delete(): Promise<void>;
|
|
167
133
|
|
|
168
|
-
/**
|
|
169
|
-
* Estimates the size of the store in bytes.
|
|
170
|
-
*/
|
|
134
|
+
/** Estimates the size of the store in bytes. */
|
|
171
135
|
estimateSize(): Promise<StoreSize>;
|
|
172
136
|
|
|
173
|
-
/**
|
|
174
|
-
* Closes the store
|
|
175
|
-
*/
|
|
137
|
+
/** Closes the store */
|
|
176
138
|
close(): Promise<void>;
|
|
139
|
+
|
|
140
|
+
/** Backups the store to the target folder.*/
|
|
141
|
+
backupTo(dstPath: string, compact?: boolean): Promise<void>;
|
|
177
142
|
}
|
package/src/interfaces/utils.ts
CHANGED
package/src/lmdb/array.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Database, Key } from 'lmdb';
|
|
2
2
|
|
|
3
3
|
import type { AztecArray, AztecAsyncArray } from '../interfaces/array.js';
|
|
4
|
+
import type { Value } from '../interfaces/common.js';
|
|
4
5
|
import { LmdbAztecSingleton } from './singleton.js';
|
|
5
6
|
|
|
6
7
|
/** The shape of a key that stores a value in an array */
|
|
@@ -9,7 +10,7 @@ type ArrayIndexSlot = ['array', string, 'slot', number];
|
|
|
9
10
|
/**
|
|
10
11
|
* An persistent array backed by LMDB.
|
|
11
12
|
*/
|
|
12
|
-
export class LmdbAztecArray<T> implements AztecArray<T>, AztecAsyncArray<T> {
|
|
13
|
+
export class LmdbAztecArray<T extends Value> implements AztecArray<T>, AztecAsyncArray<T> {
|
|
13
14
|
#db: Database<T, ArrayIndexSlot>;
|
|
14
15
|
#name: string;
|
|
15
16
|
#length: LmdbAztecSingleton<number>;
|
package/src/lmdb/index.ts
CHANGED
|
@@ -16,11 +16,11 @@ export function createStore(name: string, config: DataStoreConfig, log: Logger =
|
|
|
16
16
|
|
|
17
17
|
log.info(
|
|
18
18
|
dataDirectory
|
|
19
|
-
? `Creating ${name} data store at directory ${dataDirectory} with map size ${config.
|
|
20
|
-
: `Creating ${name} ephemeral data store with map size ${config.
|
|
19
|
+
? `Creating ${name} data store at directory ${dataDirectory} with map size ${config.dataStoreMapSizeKb} KB`
|
|
20
|
+
: `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKb} KB`,
|
|
21
21
|
);
|
|
22
22
|
|
|
23
|
-
const store = AztecLmdbStore.open(dataDirectory, config.
|
|
23
|
+
const store = AztecLmdbStore.open(dataDirectory, config.dataStoreMapSizeKb, false);
|
|
24
24
|
if (config.l1Contracts?.rollupAddress) {
|
|
25
25
|
return initStoreForRollup(store, config.l1Contracts.rollupAddress, log);
|
|
26
26
|
}
|
package/src/lmdb/map.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Database, RangeOptions } from 'lmdb';
|
|
2
2
|
|
|
3
|
-
import type { Key, Range } from '../interfaces/common.js';
|
|
4
|
-
import type {
|
|
3
|
+
import type { Key, Range, Value } from '../interfaces/common.js';
|
|
4
|
+
import type { AztecAsyncMap, AztecMap } from '../interfaces/map.js';
|
|
5
5
|
|
|
6
6
|
/** The slot where a key-value entry would be stored */
|
|
7
7
|
type MapValueSlot<K extends Key | Buffer> = ['map', string, 'slot', K];
|
|
@@ -9,7 +9,7 @@ type MapValueSlot<K extends Key | Buffer> = ['map', string, 'slot', K];
|
|
|
9
9
|
/**
|
|
10
10
|
* A map backed by LMDB.
|
|
11
11
|
*/
|
|
12
|
-
export class LmdbAztecMap<K extends Key, V> implements
|
|
12
|
+
export class LmdbAztecMap<K extends Key, V extends Value> implements AztecMap<K, V>, AztecAsyncMap<K, V> {
|
|
13
13
|
protected db: Database<[K, V], MapValueSlot<K>>;
|
|
14
14
|
protected name: string;
|
|
15
15
|
|
|
@@ -39,26 +39,6 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
39
39
|
return Promise.resolve(this.get(key));
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
*getValues(key: K): IterableIterator<V> {
|
|
43
|
-
const transaction = this.db.useReadTransaction();
|
|
44
|
-
try {
|
|
45
|
-
const values = this.db.getValues(this.slot(key), {
|
|
46
|
-
transaction,
|
|
47
|
-
});
|
|
48
|
-
for (const value of values) {
|
|
49
|
-
yield value?.[1];
|
|
50
|
-
}
|
|
51
|
-
} finally {
|
|
52
|
-
transaction.done();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async *getValuesAsync(key: K): AsyncIterableIterator<V> {
|
|
57
|
-
for (const value of this.getValues(key)) {
|
|
58
|
-
yield value;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
42
|
has(key: K): boolean {
|
|
63
43
|
return this.db.doesExist(this.slot(key));
|
|
64
44
|
}
|
|
@@ -71,6 +51,12 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
71
51
|
await this.db.put(this.slot(key), [key, val]);
|
|
72
52
|
}
|
|
73
53
|
|
|
54
|
+
async setMany(entries: { key: K; value: V }[]): Promise<void> {
|
|
55
|
+
for (const { key, value } of entries) {
|
|
56
|
+
await this.set(key, value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
74
60
|
swap(key: K, fn: (val: V | undefined) => V): Promise<void> {
|
|
75
61
|
return this.db.childTransaction(() => {
|
|
76
62
|
const slot = this.slot(key);
|
|
@@ -90,10 +76,6 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
90
76
|
await this.db.remove(this.slot(key));
|
|
91
77
|
}
|
|
92
78
|
|
|
93
|
-
async deleteValue(key: K, val: V): Promise<void> {
|
|
94
|
-
await this.db.remove(this.slot(key), [key, val]);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
79
|
*entries(range: Range<K> = {}): IterableIterator<[K, V]> {
|
|
98
80
|
const transaction = this.db.useReadTransaction();
|
|
99
81
|
|
|
@@ -106,16 +88,16 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
106
88
|
? this.slot(range.end)
|
|
107
89
|
: this.endSentinel
|
|
108
90
|
: range.start
|
|
109
|
-
|
|
110
|
-
|
|
91
|
+
? this.slot(range.start)
|
|
92
|
+
: this.startSentinel;
|
|
111
93
|
|
|
112
94
|
const end = reverse
|
|
113
95
|
? range.start
|
|
114
96
|
? this.slot(range.start)
|
|
115
97
|
: this.startSentinel
|
|
116
98
|
: range.end
|
|
117
|
-
|
|
118
|
-
|
|
99
|
+
? this.slot(range.end)
|
|
100
|
+
: this.endSentinel;
|
|
119
101
|
|
|
120
102
|
const lmdbRange: RangeOptions = {
|
|
121
103
|
start,
|
|
@@ -137,7 +119,7 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
137
119
|
}
|
|
138
120
|
}
|
|
139
121
|
|
|
140
|
-
async *entriesAsync(range?: Range<K>
|
|
122
|
+
async *entriesAsync(range?: Range<K>): AsyncIterableIterator<[K, V]> {
|
|
141
123
|
for (const entry of this.entries(range)) {
|
|
142
124
|
yield entry;
|
|
143
125
|
}
|
|
@@ -155,6 +137,15 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
155
137
|
}
|
|
156
138
|
}
|
|
157
139
|
|
|
140
|
+
size(): number {
|
|
141
|
+
const iterator = this.db.getRange({ start: this.startSentinel, end: this.endSentinel });
|
|
142
|
+
return iterator.asArray.length;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
sizeAsync(): Promise<number> {
|
|
146
|
+
return Promise.resolve(this.size());
|
|
147
|
+
}
|
|
148
|
+
|
|
158
149
|
*keys(range: Range<K> = {}): IterableIterator<K> {
|
|
159
150
|
for (const [key, _] of this.entries(range)) {
|
|
160
151
|
yield key;
|
|
@@ -184,65 +175,3 @@ export class LmdbAztecMap<K extends Key, V> implements AztecMultiMap<K, V>, Azte
|
|
|
184
175
|
}
|
|
185
176
|
}
|
|
186
177
|
}
|
|
187
|
-
|
|
188
|
-
export class LmdbAztecMapWithSize<K extends Key, V>
|
|
189
|
-
extends LmdbAztecMap<K, V>
|
|
190
|
-
implements AztecMapWithSize<K, V>, AztecAsyncMultiMap<K, V>
|
|
191
|
-
{
|
|
192
|
-
#sizeCache?: number;
|
|
193
|
-
|
|
194
|
-
constructor(rootDb: Database, mapName: string) {
|
|
195
|
-
super(rootDb, mapName);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
override async set(key: K, val: V): Promise<void> {
|
|
199
|
-
await this.db.childTransaction(() => {
|
|
200
|
-
const exists = this.db.doesExist(this.slot(key));
|
|
201
|
-
this.db.putSync(this.slot(key), [key, val], {
|
|
202
|
-
appendDup: true,
|
|
203
|
-
});
|
|
204
|
-
if (!exists) {
|
|
205
|
-
this.#sizeCache = undefined; // Invalidate cache
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
override async delete(key: K): Promise<void> {
|
|
211
|
-
await this.db.childTransaction(async () => {
|
|
212
|
-
const exists = this.db.doesExist(this.slot(key));
|
|
213
|
-
if (exists) {
|
|
214
|
-
await this.db.remove(this.slot(key));
|
|
215
|
-
this.#sizeCache = undefined; // Invalidate cache
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
override async deleteValue(key: K, val: V): Promise<void> {
|
|
221
|
-
await this.db.childTransaction(async () => {
|
|
222
|
-
const exists = this.db.doesExist(this.slot(key));
|
|
223
|
-
if (exists) {
|
|
224
|
-
await this.db.remove(this.slot(key), [key, val]);
|
|
225
|
-
this.#sizeCache = undefined; // Invalidate cache
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Gets the size of the map by counting entries.
|
|
232
|
-
* @returns The number of entries in the map
|
|
233
|
-
*/
|
|
234
|
-
size(): number {
|
|
235
|
-
if (this.#sizeCache === undefined) {
|
|
236
|
-
this.#sizeCache = this.db.getCount({
|
|
237
|
-
start: this.startSentinel,
|
|
238
|
-
end: this.endSentinel,
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
return this.#sizeCache;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Reset cache on clear/drop operations
|
|
245
|
-
clearCache() {
|
|
246
|
-
this.#sizeCache = undefined;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Key, Value } from '../interfaces/common.js';
|
|
2
|
+
import type { AztecAsyncMultiMap, AztecMultiMap } from '../interfaces/multi_map.js';
|
|
3
|
+
import { LmdbAztecMap } from './map.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A map backed by LMDB.
|
|
7
|
+
*/
|
|
8
|
+
export class LmdbAztecMultiMap<K extends Key, V extends Value>
|
|
9
|
+
extends LmdbAztecMap<K, V>
|
|
10
|
+
implements AztecMultiMap<K, V>, AztecAsyncMultiMap<K, V>
|
|
11
|
+
{
|
|
12
|
+
*getValues(key: K): IterableIterator<V> {
|
|
13
|
+
const transaction = this.db.useReadTransaction();
|
|
14
|
+
try {
|
|
15
|
+
const values = this.db.getValues(this.slot(key), {
|
|
16
|
+
transaction,
|
|
17
|
+
});
|
|
18
|
+
for (const value of values) {
|
|
19
|
+
yield value?.[1];
|
|
20
|
+
}
|
|
21
|
+
} finally {
|
|
22
|
+
transaction.done();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async *getValuesAsync(key: K): AsyncIterableIterator<V> {
|
|
27
|
+
for (const value of this.getValues(key)) {
|
|
28
|
+
yield value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async deleteValue(key: K, val: V): Promise<void> {
|
|
33
|
+
await this.db.remove(this.slot(key), [key, val]);
|
|
34
|
+
}
|
|
35
|
+
}
|