@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
|
@@ -3,7 +3,7 @@ import { toArray } from '@aztec/foundation/iterable';
|
|
|
3
3
|
import { expect } from 'chai';
|
|
4
4
|
|
|
5
5
|
import type { Key, Range } from './common.js';
|
|
6
|
-
import type { AztecAsyncMap,
|
|
6
|
+
import type { AztecAsyncMap, AztecMap } from './map.js';
|
|
7
7
|
import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
|
|
8
8
|
import { isSyncStore } from './utils.js';
|
|
9
9
|
|
|
@@ -14,11 +14,11 @@ export function describeAztecMap(
|
|
|
14
14
|
) {
|
|
15
15
|
describe(testName, () => {
|
|
16
16
|
let store: AztecKVStore | AztecAsyncKVStore;
|
|
17
|
-
let map:
|
|
17
|
+
let map: AztecMap<Key, string> | AztecAsyncMap<Key, string>;
|
|
18
18
|
|
|
19
19
|
beforeEach(async () => {
|
|
20
20
|
store = await getStore();
|
|
21
|
-
map = store.
|
|
21
|
+
map = store.openMap<string, string>('test');
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
afterEach(async () => {
|
|
@@ -27,32 +27,32 @@ export function describeAztecMap(
|
|
|
27
27
|
|
|
28
28
|
async function get(key: Key, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
29
29
|
return isSyncStore(store) && !forceAsync
|
|
30
|
-
? (sut as
|
|
31
|
-
: await (sut as
|
|
30
|
+
? (sut as AztecMap<any, any>).get(key)
|
|
31
|
+
: await (sut as AztecAsyncMap<any, any>).getAsync(key);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
async function
|
|
34
|
+
async function size(sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
35
35
|
return isSyncStore(store) && !forceAsync
|
|
36
|
-
?
|
|
37
|
-
: await
|
|
36
|
+
? (sut as AztecMap<any, any>).size()
|
|
37
|
+
: await (sut as AztecAsyncMap<any, any>).sizeAsync();
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async function
|
|
40
|
+
async function entries() {
|
|
41
41
|
return isSyncStore(store) && !forceAsync
|
|
42
|
-
? await toArray((map as
|
|
43
|
-
: await toArray((map as
|
|
42
|
+
? await toArray((map as AztecMap<any, any>).entries())
|
|
43
|
+
: await toArray((map as AztecAsyncMap<any, any>).entriesAsync());
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
async function
|
|
46
|
+
async function values() {
|
|
47
47
|
return isSyncStore(store) && !forceAsync
|
|
48
|
-
? await toArray((
|
|
49
|
-
: await toArray((
|
|
48
|
+
? await toArray((map as AztecMap<any, any>).values())
|
|
49
|
+
: await toArray((map as AztecAsyncMap<any, any>).valuesAsync());
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
async function
|
|
52
|
+
async function keys(range?: Range<Key>, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
53
53
|
return isSyncStore(store) && !forceAsync
|
|
54
|
-
? await toArray((
|
|
55
|
-
: await toArray((
|
|
54
|
+
? await toArray((sut as AztecMap<any, any>).keys(range))
|
|
55
|
+
: await toArray((sut as AztecAsyncMap<any, any>).keysAsync(range));
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
it('should be able to set and get values', async () => {
|
|
@@ -64,6 +64,22 @@ export function describeAztecMap(
|
|
|
64
64
|
expect(await get('quux')).to.equal(undefined);
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
it('should be able to set many values', async () => {
|
|
68
|
+
const pairs = Array.from({ length: 100 }, (_, i) => ({ key: `key${i}`, value: `value${i}` }));
|
|
69
|
+
await map.setMany(pairs);
|
|
70
|
+
|
|
71
|
+
for (const { key, value } of pairs) {
|
|
72
|
+
expect(await get(key)).to.equal(value);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should be able to overwrite values', async () => {
|
|
77
|
+
await map.set('foo', 'bar');
|
|
78
|
+
await map.set('foo', 'baz');
|
|
79
|
+
|
|
80
|
+
expect(await get('foo')).to.equal('baz');
|
|
81
|
+
});
|
|
82
|
+
|
|
67
83
|
it('should be able to set values if they do not exist', async () => {
|
|
68
84
|
expect(await map.setIfNotExists('foo', 'bar')).to.equal(true);
|
|
69
85
|
expect(await map.setIfNotExists('foo', 'baz')).to.equal(false);
|
|
@@ -81,6 +97,43 @@ export function describeAztecMap(
|
|
|
81
97
|
expect(await get('baz')).to.equal('qux');
|
|
82
98
|
});
|
|
83
99
|
|
|
100
|
+
it('should be able to return size of the map', async () => {
|
|
101
|
+
await map.set('foo', 'bar');
|
|
102
|
+
expect(await size()).to.equal(1);
|
|
103
|
+
await map.set('baz', 'qux');
|
|
104
|
+
expect(await size()).to.equal(2);
|
|
105
|
+
|
|
106
|
+
await map.delete('foo');
|
|
107
|
+
expect(await size()).to.equal(1);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('returns 0 for empty map size', async () => {
|
|
111
|
+
expect(await size()).to.equal(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('calculates size correctly across multiple operations', async () => {
|
|
115
|
+
expect(await size()).to.equal(0);
|
|
116
|
+
|
|
117
|
+
// Add items
|
|
118
|
+
await map.set('a', 'value1');
|
|
119
|
+
await map.set('b', 'value2');
|
|
120
|
+
await map.set('c', 'value3');
|
|
121
|
+
expect(await size()).to.equal(3);
|
|
122
|
+
|
|
123
|
+
// Update existing (size should not change)
|
|
124
|
+
await map.set('b', 'updated');
|
|
125
|
+
expect(await size()).to.equal(3);
|
|
126
|
+
|
|
127
|
+
// Delete some
|
|
128
|
+
await map.delete('a');
|
|
129
|
+
expect(await size()).to.equal(2);
|
|
130
|
+
|
|
131
|
+
// Delete all
|
|
132
|
+
await map.delete('b');
|
|
133
|
+
await map.delete('c');
|
|
134
|
+
expect(await size()).to.equal(0);
|
|
135
|
+
});
|
|
136
|
+
|
|
84
137
|
it('should be able to iterate over entries when there are no keys', async () => {
|
|
85
138
|
expect(await entries()).to.deep.equal([]);
|
|
86
139
|
});
|
|
@@ -109,35 +162,39 @@ export function describeAztecMap(
|
|
|
109
162
|
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
110
163
|
});
|
|
111
164
|
|
|
112
|
-
it('should be able to
|
|
113
|
-
await map.set('
|
|
114
|
-
await map.set('
|
|
165
|
+
it('should be able to iterate over string keys that represent numbers', async () => {
|
|
166
|
+
await map.set('0x22', 'bar');
|
|
167
|
+
await map.set('0x31', 'qux');
|
|
115
168
|
|
|
116
|
-
expect(await
|
|
169
|
+
expect(await keys()).to.deep.equal(['0x22', '0x31']);
|
|
117
170
|
});
|
|
118
171
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
172
|
+
for (const [name, data] of [
|
|
173
|
+
['chars', ['a', 'b', 'c', 'd']],
|
|
174
|
+
['numbers', [1, 2, 3, 4]],
|
|
175
|
+
['negative numbers', [-4, -3, -2, -1]],
|
|
176
|
+
['strings', ['aaa', 'bbb', 'ccc', 'ddd']],
|
|
177
|
+
['zero-based numbers', [0, 1, 2, 3]],
|
|
178
|
+
['large numbers', [100, 999, 1000, 1001]],
|
|
179
|
+
['mixed negative and positive', [-1000, -1, 1, 1000]],
|
|
180
|
+
]) {
|
|
181
|
+
it(`supports range queries over ${name} keys`, async () => {
|
|
182
|
+
const [a, b, c, d] = data;
|
|
183
|
+
|
|
184
|
+
await map.set(a, 'a');
|
|
185
|
+
await map.set(b, 'b');
|
|
186
|
+
await map.set(c, 'c');
|
|
187
|
+
await map.set(d, 'd');
|
|
188
|
+
|
|
189
|
+
expect(await keys()).to.deep.equal([a, b, c, d]);
|
|
190
|
+
expect(await keys({ start: b, end: c })).to.deep.equal([b]);
|
|
191
|
+
expect(await keys({ start: b })).to.deep.equal([b, c, d]);
|
|
192
|
+
expect(await keys({ end: c })).to.deep.equal([a, b]);
|
|
193
|
+
expect(await keys({ start: b, end: c, reverse: true })).to.deep.equal([c]);
|
|
194
|
+
expect(await keys({ start: b, limit: 1 })).to.deep.equal([b]);
|
|
195
|
+
expect(await keys({ start: b, reverse: true })).to.deep.equal([d, c]);
|
|
196
|
+
expect(await keys({ end: b, reverse: true })).to.deep.equal([b, a]);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
142
199
|
});
|
|
143
200
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Key, Value } from './common.js';
|
|
2
|
+
import type { AztecAsyncMap, AztecMap } from './map.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A map backed by a persistent store that can have multiple values for a single key.
|
|
6
|
+
*/
|
|
7
|
+
export interface AztecMultiMap<K extends Key, V extends Value> extends AztecMap<K, V> {
|
|
8
|
+
/**
|
|
9
|
+
* Gets all the values at the given key.
|
|
10
|
+
* @param key - The key to get the values from
|
|
11
|
+
*/
|
|
12
|
+
getValues(key: K): IterableIterator<V>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Deletes a specific value at the given key.
|
|
16
|
+
* @param key - The key to delete the value at
|
|
17
|
+
* @param val - The value to delete
|
|
18
|
+
*/
|
|
19
|
+
deleteValue(key: K, val: V): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A map backed by a persistent store that can have multiple values for a single key.
|
|
24
|
+
*/
|
|
25
|
+
export interface AztecAsyncMultiMap<K extends Key, V extends Value> extends AztecAsyncMap<K, V> {
|
|
26
|
+
/**
|
|
27
|
+
* Gets all the values at the given key.
|
|
28
|
+
* @param key - The key to get the values from
|
|
29
|
+
*/
|
|
30
|
+
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Gets the number of values at the given key.
|
|
34
|
+
* @param key - The key to get the number of values from
|
|
35
|
+
* @returns The number of values at the given key
|
|
36
|
+
*/
|
|
37
|
+
getValueCountAsync(key: K): Promise<number>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Deletes a specific value at the given key.
|
|
41
|
+
* @param key - The key to delete the value at
|
|
42
|
+
* @param val - The value to delete
|
|
43
|
+
*/
|
|
44
|
+
deleteValue(key: K, val: V): Promise<void>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,374 @@
|
|
|
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('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
|
+
|
|
139
|
+
it('should be able to iterate over entries when there are no keys', async () => {
|
|
140
|
+
expect(await entries()).to.deep.equal([]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should be able to iterate over entries', async () => {
|
|
144
|
+
await multiMap.set('foo', 'bar');
|
|
145
|
+
await multiMap.set('baz', 'qux');
|
|
146
|
+
|
|
147
|
+
expect(await entries()).to.deep.equal([
|
|
148
|
+
['baz', 'qux'],
|
|
149
|
+
['foo', 'bar'],
|
|
150
|
+
]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should be able to iterate over values', async () => {
|
|
154
|
+
await multiMap.set('foo', 'bar');
|
|
155
|
+
await multiMap.set('baz', 'quux');
|
|
156
|
+
|
|
157
|
+
expect(await values()).to.deep.equal(['quux', 'bar']);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should be able to iterate over keys', async () => {
|
|
161
|
+
await multiMap.set('foo', 'bar');
|
|
162
|
+
await multiMap.set('baz', 'qux');
|
|
163
|
+
|
|
164
|
+
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should be able to get multiple values for a single key', async () => {
|
|
168
|
+
await multiMap.set('foo', 'bar');
|
|
169
|
+
await multiMap.set('foo', 'baz');
|
|
170
|
+
|
|
171
|
+
expect(await getValues('foo')).to.deep.equal(['bar', 'baz']);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should ignore multiple identical values', async () => {
|
|
175
|
+
await multiMap.set('foo', 'bar');
|
|
176
|
+
await multiMap.set('foo', 'bar');
|
|
177
|
+
|
|
178
|
+
expect(await getValues('foo')).to.deep.equal(['bar']);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should be able to delete individual values for a single 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', '2');
|
|
187
|
+
|
|
188
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3']);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should be able to get size of the map with duplicate keys', async () => {
|
|
192
|
+
await multiMap.set('foo', '1');
|
|
193
|
+
await multiMap.set('foo', '2');
|
|
194
|
+
await multiMap.set('foo', '3');
|
|
195
|
+
expect(await size()).to.equal(3);
|
|
196
|
+
|
|
197
|
+
await multiMap.set('bar', '1');
|
|
198
|
+
await multiMap.set('bar', '2');
|
|
199
|
+
await multiMap.set('bar', '3');
|
|
200
|
+
expect(await size()).to.equal(6);
|
|
201
|
+
|
|
202
|
+
await multiMap.deleteValue('foo', '2');
|
|
203
|
+
expect(await size()).to.equal(5);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should be able to delete the last and first values for a key', async () => {
|
|
207
|
+
await multiMap.set('foo', '1');
|
|
208
|
+
await multiMap.set('foo', '2');
|
|
209
|
+
await multiMap.set('foo', '3');
|
|
210
|
+
|
|
211
|
+
await multiMap.deleteValue('foo', '1');
|
|
212
|
+
|
|
213
|
+
expect(await getValues('foo')).to.deep.equal(['2', '3']);
|
|
214
|
+
|
|
215
|
+
await multiMap.deleteValue('foo', '3');
|
|
216
|
+
|
|
217
|
+
expect(await getValues('foo')).to.deep.equal(['2']);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should be able to fully clear a key', async () => {
|
|
221
|
+
await multiMap.set('foo', '1');
|
|
222
|
+
await multiMap.set('foo', '2');
|
|
223
|
+
await multiMap.set('foo', '3');
|
|
224
|
+
|
|
225
|
+
await multiMap.deleteValue('foo', '1');
|
|
226
|
+
await multiMap.deleteValue('foo', '3');
|
|
227
|
+
await multiMap.deleteValue('foo', '2');
|
|
228
|
+
|
|
229
|
+
expect(await getValues('foo')).to.deep.equal([]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('should be able to insert after deletion', async () => {
|
|
233
|
+
await multiMap.set('foo', '1');
|
|
234
|
+
await multiMap.set('foo', '2');
|
|
235
|
+
await multiMap.set('foo', '3');
|
|
236
|
+
|
|
237
|
+
await multiMap.deleteValue('foo', '2');
|
|
238
|
+
await multiMap.set('foo', 'bar');
|
|
239
|
+
|
|
240
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3', 'bar']);
|
|
241
|
+
|
|
242
|
+
// Delete the just-added entry
|
|
243
|
+
await multiMap.deleteValue('foo', 'bar');
|
|
244
|
+
|
|
245
|
+
expect(await getValues('foo')).to.deep.equal(['1', '3']);
|
|
246
|
+
|
|
247
|
+
// Reinsert the initially deleted key
|
|
248
|
+
await multiMap.set('foo', '2');
|
|
249
|
+
|
|
250
|
+
// LMDB and IndexedDB behave differently here, the former ordering by value and the latter by insertion. This is
|
|
251
|
+
// fine because there is no expectation for values in a multimap to be ordered.
|
|
252
|
+
const values = (await getValues('foo')).sort((a, b) => a.localeCompare(b));
|
|
253
|
+
expect(values).to.deep.equal(['1', '2', '3']);
|
|
254
|
+
|
|
255
|
+
// Fully clear the key
|
|
256
|
+
await multiMap.deleteValue('foo', '1');
|
|
257
|
+
await multiMap.deleteValue('foo', '3');
|
|
258
|
+
await multiMap.deleteValue('foo', '2');
|
|
259
|
+
|
|
260
|
+
// Insert some more
|
|
261
|
+
await multiMap.set('foo', 'baz');
|
|
262
|
+
await multiMap.set('foo', 'qux');
|
|
263
|
+
expect(await getValues('foo')).to.deep.equal(['baz', 'qux']);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('supports range queries', async () => {
|
|
267
|
+
await multiMap.set('a', 'a');
|
|
268
|
+
await multiMap.set('b', 'b');
|
|
269
|
+
await multiMap.set('c', 'c');
|
|
270
|
+
await multiMap.set('d', 'd');
|
|
271
|
+
|
|
272
|
+
expect(await keys({ start: 'b', end: 'c' })).to.deep.equal(['b']);
|
|
273
|
+
expect(await keys({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
|
|
274
|
+
expect(await keys({ end: 'c' })).to.deep.equal(['a', 'b']);
|
|
275
|
+
expect(await keys({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
|
|
276
|
+
expect(await keys({ start: 'b', limit: 1 })).to.deep.equal(['b']);
|
|
277
|
+
expect(await keys({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
|
|
278
|
+
expect(await keys({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('returns 0 for missing key', async () => {
|
|
282
|
+
expect(await getValueCount('missing')).to.equal(0);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('counts a single value', async () => {
|
|
286
|
+
await multiMap.set('foo', 'bar');
|
|
287
|
+
expect(await getValueCount('foo')).to.equal(1);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('counts multiple distinct values for same key', async () => {
|
|
291
|
+
await multiMap.set('foo', 'bar');
|
|
292
|
+
await multiMap.set('foo', 'baz');
|
|
293
|
+
await multiMap.set('foo', 'qux');
|
|
294
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('does not increase count for duplicate inserts', async () => {
|
|
298
|
+
await multiMap.set('foo', 'bar');
|
|
299
|
+
await multiMap.set('foo', 'bar');
|
|
300
|
+
await multiMap.set('foo', 'baz');
|
|
301
|
+
await multiMap.set('foo', 'baz');
|
|
302
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
303
|
+
expect(await getValues('foo')).to.have.members(['bar', 'baz']);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('decrements when deleting a single value', async () => {
|
|
307
|
+
await multiMap.set('foo', '1');
|
|
308
|
+
await multiMap.set('foo', '2');
|
|
309
|
+
await multiMap.set('foo', '3');
|
|
310
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
311
|
+
await multiMap.deleteValue('foo', '2');
|
|
312
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
313
|
+
expect(await getValues('foo')).to.have.members(['1', '3']);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('does not change count when deleting a non-existent value', async () => {
|
|
317
|
+
await multiMap.set('foo', '1');
|
|
318
|
+
await multiMap.set('foo', '3');
|
|
319
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
320
|
+
await multiMap.deleteValue('foo', '2');
|
|
321
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('clears all values when deleting a key', async () => {
|
|
325
|
+
await multiMap.set('foo', 'bar');
|
|
326
|
+
await multiMap.set('foo', 'baz');
|
|
327
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
328
|
+
await multiMap.delete('foo');
|
|
329
|
+
expect(await getValueCount('foo')).to.equal(0);
|
|
330
|
+
expect(await getValues('foo')).to.deep.equal([]);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('count equals enumerated values length', async () => {
|
|
334
|
+
await multiMap.set('foo', '1');
|
|
335
|
+
await multiMap.set('foo', '2');
|
|
336
|
+
const vals = await getValues('foo');
|
|
337
|
+
expect(await getValueCount('foo')).to.equal(vals.length);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it('sum of per-key counts equals total size', async () => {
|
|
341
|
+
await multiMap.set('foo', '1');
|
|
342
|
+
await multiMap.set('foo', '2');
|
|
343
|
+
await multiMap.set('bar', '3');
|
|
344
|
+
await multiMap.set('bar', '4');
|
|
345
|
+
await multiMap.set('baz', '5');
|
|
346
|
+
|
|
347
|
+
const allKeys = await keys();
|
|
348
|
+
const uniqueKeys = Array.from(new Set(allKeys));
|
|
349
|
+
const counts = await Promise.all(uniqueKeys.map(k => getValueCount(k)));
|
|
350
|
+
const sum = counts.reduce((s, n) => s + n, 0);
|
|
351
|
+
expect(sum).to.equal(await size());
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('supports sparse slots: delete middle, reinsert new, count remains correct', async () => {
|
|
355
|
+
await multiMap.set('foo', '1');
|
|
356
|
+
await multiMap.set('foo', '2');
|
|
357
|
+
await multiMap.set('foo', '3');
|
|
358
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
359
|
+
await multiMap.deleteValue('foo', '2');
|
|
360
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
361
|
+
await multiMap.set('foo', '4');
|
|
362
|
+
expect(await getValueCount('foo')).to.equal(3);
|
|
363
|
+
expect(await getValues('foo')).to.have.members(['1', '3', '4']);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('multiple keys are independent', async () => {
|
|
367
|
+
await multiMap.set('foo', '1');
|
|
368
|
+
await multiMap.set('foo', '2');
|
|
369
|
+
await multiMap.set('bar', '3');
|
|
370
|
+
expect(await getValueCount('foo')).to.equal(2);
|
|
371
|
+
expect(await getValueCount('bar')).to.equal(1);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
}
|