@aztec/kv-store 0.0.0-test.1 → 0.0.1-commit.21caa21
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 +3 -0
- package/dest/indexeddb/index.d.ts +1 -1
- package/dest/indexeddb/index.js +1 -1
- package/dest/indexeddb/map.d.ts +12 -6
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +38 -60
- package/dest/indexeddb/multi_map.d.ts +13 -0
- package/dest/indexeddb/multi_map.d.ts.map +1 -0
- package/dest/indexeddb/multi_map.js +92 -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 +3 -1
- package/dest/indexeddb/store.d.ts +13 -14
- 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 +135 -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 +339 -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 +2 -1
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +2 -1
- package/dest/lmdb/array.d.ts +3 -2
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/counter.d.ts +1 -1
- package/dest/lmdb/counter.d.ts.map +1 -1
- package/dest/lmdb/index.d.ts +1 -1
- package/dest/lmdb/index.js +2 -2
- 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 +11 -31
- package/dest/lmdb-v2/array.d.ts +3 -2
- 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 +4 -7
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +16 -6
- package/dest/lmdb-v2/index.d.ts +1 -1
- package/dest/lmdb-v2/map.d.ts +11 -44
- 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 +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 +124 -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 +1 -1
- 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 +10 -9
- 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 +3 -5
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.d.ts +3 -5
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- 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 +3 -2
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +18 -9
- package/dest/utils.d.ts +1 -1
- package/package.json +21 -16
- 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 +91 -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 +45 -0
- package/src/interfaces/multi_map_test_suite.ts +342 -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 +51 -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 +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 +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,342 @@
|
|
|
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
|
+
async function getValueCount(
|
|
65
|
+
key: Key,
|
|
66
|
+
sut: AztecAsyncMultiMap<any, any> | AztecMultiMap<any, any> = multiMap,
|
|
67
|
+
): Promise<number> {
|
|
68
|
+
return await (sut as AztecAsyncMultiMap<any, any>).getValueCountAsync(key);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
it('should be able to set and get values', async () => {
|
|
72
|
+
await multiMap.set('foo', 'bar');
|
|
73
|
+
await multiMap.set('baz', 'qux');
|
|
74
|
+
|
|
75
|
+
expect(await get('foo')).to.equal('bar');
|
|
76
|
+
expect(await get('baz')).to.equal('qux');
|
|
77
|
+
expect(await get('quux')).to.equal(undefined);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should be able to set values if they do not exist', async () => {
|
|
81
|
+
expect(await multiMap.setIfNotExists('foo', 'bar')).to.equal(true);
|
|
82
|
+
expect(await multiMap.setIfNotExists('foo', 'baz')).to.equal(false);
|
|
83
|
+
|
|
84
|
+
expect(await get('foo')).to.equal('bar');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should be able to delete values', async () => {
|
|
88
|
+
await multiMap.set('foo', 'bar');
|
|
89
|
+
await multiMap.set('baz', 'qux');
|
|
90
|
+
|
|
91
|
+
await multiMap.delete('foo');
|
|
92
|
+
|
|
93
|
+
expect(await get('foo')).to.equal(undefined);
|
|
94
|
+
expect(await get('baz')).to.equal('qux');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should be able to get size of the map', async () => {
|
|
98
|
+
await multiMap.set('foo', 'bar');
|
|
99
|
+
expect(await size()).to.equal(1);
|
|
100
|
+
await multiMap.set('baz', 'qux');
|
|
101
|
+
expect(await size()).to.equal(2);
|
|
102
|
+
|
|
103
|
+
await multiMap.delete('foo');
|
|
104
|
+
expect(await size()).to.equal(1);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should be able to iterate over entries when there are no keys', async () => {
|
|
108
|
+
expect(await entries()).to.deep.equal([]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('should be able to iterate over entries', async () => {
|
|
112
|
+
await multiMap.set('foo', 'bar');
|
|
113
|
+
await multiMap.set('baz', 'qux');
|
|
114
|
+
|
|
115
|
+
expect(await entries()).to.deep.equal([
|
|
116
|
+
['baz', 'qux'],
|
|
117
|
+
['foo', 'bar'],
|
|
118
|
+
]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should be able to iterate over values', async () => {
|
|
122
|
+
await multiMap.set('foo', 'bar');
|
|
123
|
+
await multiMap.set('baz', 'quux');
|
|
124
|
+
|
|
125
|
+
expect(await values()).to.deep.equal(['quux', 'bar']);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should be able to iterate over keys', async () => {
|
|
129
|
+
await multiMap.set('foo', 'bar');
|
|
130
|
+
await multiMap.set('baz', 'qux');
|
|
131
|
+
|
|
132
|
+
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should be able to get multiple values for a single key', async () => {
|
|
136
|
+
await multiMap.set('foo', 'bar');
|
|
137
|
+
await multiMap.set('foo', 'baz');
|
|
138
|
+
|
|
139
|
+
expect(await getValues('foo')).to.deep.equal(['bar', 'baz']);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should ignore multiple identical values', async () => {
|
|
143
|
+
await multiMap.set('foo', 'bar');
|
|
144
|
+
await multiMap.set('foo', 'bar');
|
|
145
|
+
|
|
146
|
+
expect(await getValues('foo')).to.deep.equal(['bar']);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should be able to delete individual values for a single key', async () => {
|
|
150
|
+
await multiMap.set('foo', '1');
|
|
151
|
+
await multiMap.set('foo', '2');
|
|
152
|
+
await multiMap.set('foo', '3');
|
|
153
|
+
|
|
154
|
+
await multiMap.deleteValue('foo', '2');
|
|
155
|
+
|
|
156
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3']);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should be able to get size of the map with duplicate keys', async () => {
|
|
160
|
+
await multiMap.set('foo', '1');
|
|
161
|
+
await multiMap.set('foo', '2');
|
|
162
|
+
await multiMap.set('foo', '3');
|
|
163
|
+
expect(await size()).to.equal(3);
|
|
164
|
+
|
|
165
|
+
await multiMap.set('bar', '1');
|
|
166
|
+
await multiMap.set('bar', '2');
|
|
167
|
+
await multiMap.set('bar', '3');
|
|
168
|
+
expect(await size()).to.equal(6);
|
|
169
|
+
|
|
170
|
+
await multiMap.deleteValue('foo', '2');
|
|
171
|
+
expect(await size()).to.equal(5);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should be able to delete the last and first values for a key', async () => {
|
|
175
|
+
await multiMap.set('foo', '1');
|
|
176
|
+
await multiMap.set('foo', '2');
|
|
177
|
+
await multiMap.set('foo', '3');
|
|
178
|
+
|
|
179
|
+
await multiMap.deleteValue('foo', '1');
|
|
180
|
+
|
|
181
|
+
expect(await getValues('foo')).to.deep.equal(['2', '3']);
|
|
182
|
+
|
|
183
|
+
await multiMap.deleteValue('foo', '3');
|
|
184
|
+
|
|
185
|
+
expect(await getValues('foo')).to.deep.equal(['2']);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should be able to fully clear a key', async () => {
|
|
189
|
+
await multiMap.set('foo', '1');
|
|
190
|
+
await multiMap.set('foo', '2');
|
|
191
|
+
await multiMap.set('foo', '3');
|
|
192
|
+
|
|
193
|
+
await multiMap.deleteValue('foo', '1');
|
|
194
|
+
await multiMap.deleteValue('foo', '3');
|
|
195
|
+
await multiMap.deleteValue('foo', '2');
|
|
196
|
+
|
|
197
|
+
expect(await getValues('foo')).to.deep.equal([]);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should be able to insert after deletion', async () => {
|
|
201
|
+
await multiMap.set('foo', '1');
|
|
202
|
+
await multiMap.set('foo', '2');
|
|
203
|
+
await multiMap.set('foo', '3');
|
|
204
|
+
|
|
205
|
+
await multiMap.deleteValue('foo', '2');
|
|
206
|
+
await multiMap.set('foo', 'bar');
|
|
207
|
+
|
|
208
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3', 'bar']);
|
|
209
|
+
|
|
210
|
+
// Delete the just-added entry
|
|
211
|
+
await multiMap.deleteValue('foo', 'bar');
|
|
212
|
+
|
|
213
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3']);
|
|
214
|
+
|
|
215
|
+
// Reinsert the initially deleted key
|
|
216
|
+
await multiMap.set('foo', '2');
|
|
217
|
+
|
|
218
|
+
// LMDB and IndexedDB behave differently here, the former ordering by value and the latter by insertion. This is
|
|
219
|
+
// fine because there is no expectation for values in a multimap to be ordered.
|
|
220
|
+
const values = (await getValues('foo')).sort((a, b) => a.localeCompare(b));
|
|
221
|
+
expect(values).to.deep.equal(['1', '2', '3']);
|
|
222
|
+
|
|
223
|
+
// Fully clear the key
|
|
224
|
+
await multiMap.deleteValue('foo', '1');
|
|
225
|
+
await multiMap.deleteValue('foo', '3');
|
|
226
|
+
await multiMap.deleteValue('foo', '2');
|
|
227
|
+
|
|
228
|
+
// Insert some more
|
|
229
|
+
await multiMap.set('foo', 'baz');
|
|
230
|
+
await multiMap.set('foo', 'qux');
|
|
231
|
+
expect(await getValues('foo')).to.deep.equal(['baz', 'qux']);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('supports range queries', async () => {
|
|
235
|
+
await multiMap.set('a', 'a');
|
|
236
|
+
await multiMap.set('b', 'b');
|
|
237
|
+
await multiMap.set('c', 'c');
|
|
238
|
+
await multiMap.set('d', 'd');
|
|
239
|
+
|
|
240
|
+
expect(await keys({ start: 'b', end: 'c' })).to.deep.equal(['b']);
|
|
241
|
+
expect(await keys({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
|
|
242
|
+
expect(await keys({ end: 'c' })).to.deep.equal(['a', 'b']);
|
|
243
|
+
expect(await keys({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
|
|
244
|
+
expect(await keys({ start: 'b', limit: 1 })).to.deep.equal(['b']);
|
|
245
|
+
expect(await keys({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
|
|
246
|
+
expect(await keys({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('returns 0 for missing key', async () => {
|
|
250
|
+
expect(await getValueCount('missing')).to.equal(0);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('counts a single value', async () => {
|
|
254
|
+
await multiMap.set('foo', 'bar');
|
|
255
|
+
expect(await getValueCount('foo')).to.equal(1);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('counts multiple distinct values for same key', async () => {
|
|
259
|
+
await multiMap.set('foo', 'bar');
|
|
260
|
+
await multiMap.set('foo', 'baz');
|
|
261
|
+
await multiMap.set('foo', 'qux');
|
|
262
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('does not increase count for duplicate inserts', async () => {
|
|
266
|
+
await multiMap.set('foo', 'bar');
|
|
267
|
+
await multiMap.set('foo', 'bar');
|
|
268
|
+
await multiMap.set('foo', 'baz');
|
|
269
|
+
await multiMap.set('foo', 'baz');
|
|
270
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
271
|
+
expect(await getValues('foo')).to.have.members(['bar', 'baz']);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('decrements when deleting a single value', async () => {
|
|
275
|
+
await multiMap.set('foo', '1');
|
|
276
|
+
await multiMap.set('foo', '2');
|
|
277
|
+
await multiMap.set('foo', '3');
|
|
278
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
279
|
+
await multiMap.deleteValue('foo', '2');
|
|
280
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
281
|
+
expect(await getValues('foo')).to.have.members(['1', '3']);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('does not change count when deleting a non-existent value', async () => {
|
|
285
|
+
await multiMap.set('foo', '1');
|
|
286
|
+
await multiMap.set('foo', '3');
|
|
287
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
288
|
+
await multiMap.deleteValue('foo', '2');
|
|
289
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('clears all values when deleting a key', async () => {
|
|
293
|
+
await multiMap.set('foo', 'bar');
|
|
294
|
+
await multiMap.set('foo', 'baz');
|
|
295
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
296
|
+
await multiMap.delete('foo');
|
|
297
|
+
expect(await getValueCount('foo')).to.equal(0);
|
|
298
|
+
expect(await getValues('foo')).to.deep.equal([]);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('count equals enumerated values length', async () => {
|
|
302
|
+
await multiMap.set('foo', '1');
|
|
303
|
+
await multiMap.set('foo', '2');
|
|
304
|
+
const vals = await getValues('foo');
|
|
305
|
+
expect(await getValueCount('foo')).to.equal(vals.length);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('sum of per-key counts equals total size', async () => {
|
|
309
|
+
await multiMap.set('foo', '1');
|
|
310
|
+
await multiMap.set('foo', '2');
|
|
311
|
+
await multiMap.set('bar', '3');
|
|
312
|
+
await multiMap.set('bar', '4');
|
|
313
|
+
await multiMap.set('baz', '5');
|
|
314
|
+
|
|
315
|
+
const allKeys = await keys();
|
|
316
|
+
const uniqueKeys = Array.from(new Set(allKeys));
|
|
317
|
+
const counts = await Promise.all(uniqueKeys.map(k => getValueCount(k)));
|
|
318
|
+
const sum = counts.reduce((s, n) => s + n, 0);
|
|
319
|
+
expect(sum).to.equal(await size());
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('supports sparse slots: delete middle, reinsert new, count remains correct', async () => {
|
|
323
|
+
await multiMap.set('foo', '1');
|
|
324
|
+
await multiMap.set('foo', '2');
|
|
325
|
+
await multiMap.set('foo', '3');
|
|
326
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
327
|
+
await multiMap.deleteValue('foo', '2');
|
|
328
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
329
|
+
await multiMap.set('foo', '4');
|
|
330
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
331
|
+
expect(await getValues('foo')).to.have.members(['1', '3', '4']);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('multiple keys are independent', async () => {
|
|
335
|
+
await multiMap.set('foo', '1');
|
|
336
|
+
await multiMap.set('foo', '2');
|
|
337
|
+
await multiMap.set('bar', '3');
|
|
338
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
339
|
+
expect(await getValueCount('bar')).to.equal(1);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
}
|
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
|
}
|