@aztec/kv-store 0.0.0-test.1 → 0.0.1-commit.1142ef1
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 +6 -1
- 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/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 +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 +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 +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 +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 +11 -11
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +17 -28
- 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 +1 -1
- package/package.json +23 -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 +24 -48
- package/src/lmdb-v2/array.ts +8 -3
- package/src/lmdb-v2/factory.ts +17 -10
- 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 +21 -40
- 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/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
package/src/interfaces/map.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Key, Range } from './common.js';
|
|
1
|
+
import type { Key, Range, Value } from './common.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A map backed by a persistent store.
|
|
5
5
|
*/
|
|
6
|
-
interface AztecBaseMap<K extends Key, V> {
|
|
6
|
+
interface AztecBaseMap<K extends Key, V extends Value> {
|
|
7
7
|
/**
|
|
8
8
|
* Sets the value at the given key.
|
|
9
9
|
* @param key - The key to set the value at
|
|
@@ -11,6 +11,12 @@ interface AztecBaseMap<K extends Key, V> {
|
|
|
11
11
|
*/
|
|
12
12
|
set(key: K, val: V): Promise<void>;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Sets the values at the given keys.
|
|
16
|
+
* @param entries - The entries to set
|
|
17
|
+
*/
|
|
18
|
+
setMany(entries: { key: K; value: V }[]): Promise<void>;
|
|
19
|
+
|
|
14
20
|
/**
|
|
15
21
|
* Sets the value at the given key if it does not already exist.
|
|
16
22
|
* @param key - The key to set the value at
|
|
@@ -24,13 +30,19 @@ interface AztecBaseMap<K extends Key, V> {
|
|
|
24
30
|
*/
|
|
25
31
|
delete(key: K): Promise<void>;
|
|
26
32
|
}
|
|
27
|
-
export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
33
|
+
export interface AztecMap<K extends Key, V extends Value> extends AztecBaseMap<K, V> {
|
|
28
34
|
/**
|
|
29
35
|
* Gets the value at the given key.
|
|
30
36
|
* @param key - The key to get the value from
|
|
31
37
|
*/
|
|
32
38
|
get(key: K): V | undefined;
|
|
33
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Gets the current size of the map.
|
|
42
|
+
* @returns The size of the map
|
|
43
|
+
*/
|
|
44
|
+
size(): number;
|
|
45
|
+
|
|
34
46
|
/**
|
|
35
47
|
* Checks if a key exists in the map.
|
|
36
48
|
* @param key - The key to check
|
|
@@ -62,44 +74,10 @@ export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
|
62
74
|
clear(): Promise<void>;
|
|
63
75
|
}
|
|
64
76
|
|
|
65
|
-
export interface AztecMapWithSize<K extends Key, V> extends AztecMap<K, V> {
|
|
66
|
-
/**
|
|
67
|
-
* Gets the size of the map.
|
|
68
|
-
* @returns The size of the map
|
|
69
|
-
*/
|
|
70
|
-
size(): number;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* A map backed by a persistent store that can have multiple values for a single key.
|
|
75
|
-
*/
|
|
76
|
-
export interface AztecMultiMap<K extends Key, V> extends AztecMap<K, V> {
|
|
77
|
-
/**
|
|
78
|
-
* Gets all the values at the given key.
|
|
79
|
-
* @param key - The key to get the values from
|
|
80
|
-
*/
|
|
81
|
-
getValues(key: K): IterableIterator<V>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Deletes a specific value at the given key.
|
|
85
|
-
* @param key - The key to delete the value at
|
|
86
|
-
* @param val - The value to delete
|
|
87
|
-
*/
|
|
88
|
-
deleteValue(key: K, val: V): Promise<void>;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface AztecMultiMapWithSize<K extends Key, V> extends AztecMultiMap<K, V> {
|
|
92
|
-
/**
|
|
93
|
-
* Gets the size of the map.
|
|
94
|
-
* @returns The size of the map
|
|
95
|
-
*/
|
|
96
|
-
size(): number;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
77
|
/**
|
|
100
78
|
* A map backed by a persistent store.
|
|
101
79
|
*/
|
|
102
|
-
export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
80
|
+
export interface AztecAsyncMap<K extends Key, V extends Value> extends AztecBaseMap<K, V> {
|
|
103
81
|
/**
|
|
104
82
|
* Gets the value at the given key.
|
|
105
83
|
* @param key - The key to get the value from
|
|
@@ -130,22 +108,10 @@ export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
|
130
108
|
* @param range - The range of keys to iterate over
|
|
131
109
|
*/
|
|
132
110
|
keysAsync(range?: Range<K>): AsyncIterableIterator<K>;
|
|
133
|
-
}
|
|
134
111
|
|
|
135
|
-
/**
|
|
136
|
-
* A map backed by a persistent store that can have multiple values for a single key.
|
|
137
|
-
*/
|
|
138
|
-
export interface AztecAsyncMultiMap<K extends Key, V> extends AztecAsyncMap<K, V> {
|
|
139
112
|
/**
|
|
140
|
-
* Gets
|
|
141
|
-
* @
|
|
142
|
-
*/
|
|
143
|
-
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Deletes a specific value at the given key.
|
|
147
|
-
* @param key - The key to delete the value at
|
|
148
|
-
* @param val - The value to delete
|
|
113
|
+
* Gets the current size of the map.
|
|
114
|
+
* @returns The size of the map
|
|
149
115
|
*/
|
|
150
|
-
|
|
116
|
+
sizeAsync(): Promise<number>;
|
|
151
117
|
}
|
|
@@ -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,16 @@ 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
|
+
|
|
84
110
|
it('should be able to iterate over entries when there are no keys', async () => {
|
|
85
111
|
expect(await entries()).to.deep.equal([]);
|
|
86
112
|
});
|
|
@@ -109,35 +135,38 @@ export function describeAztecMap(
|
|
|
109
135
|
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
110
136
|
});
|
|
111
137
|
|
|
112
|
-
it('should be able to
|
|
113
|
-
await map.set('
|
|
114
|
-
await map.set('
|
|
138
|
+
it('should be able to iterate over string keys that represent numbers', async () => {
|
|
139
|
+
await map.set('0x22', 'bar');
|
|
140
|
+
await map.set('0x31', 'qux');
|
|
115
141
|
|
|
116
|
-
expect(await
|
|
142
|
+
expect(await keys()).to.deep.equal(['0x22', '0x31']);
|
|
117
143
|
});
|
|
118
144
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
145
|
+
for (const [name, data] of [
|
|
146
|
+
['chars', ['a', 'b', 'c', 'd']],
|
|
147
|
+
['numbers', [1, 2, 3, 4]],
|
|
148
|
+
// disabled because indexeddb sorts lexigographically
|
|
149
|
+
// ['negative numbers', [-4, -3, -2, -1]],
|
|
150
|
+
['strings', ['aaa', 'bbb', 'ccc', 'ddd']],
|
|
151
|
+
['zero-based numbers', [0, 1, 2, 3]],
|
|
152
|
+
]) {
|
|
153
|
+
it(`supports range queries over ${name} keys`, async () => {
|
|
154
|
+
const [a, b, c, d] = data;
|
|
155
|
+
|
|
156
|
+
await map.set(a, 'a');
|
|
157
|
+
await map.set(b, 'b');
|
|
158
|
+
await map.set(c, 'c');
|
|
159
|
+
await map.set(d, 'd');
|
|
160
|
+
|
|
161
|
+
expect(await keys()).to.deep.equal([a, b, c, d]);
|
|
162
|
+
expect(await keys({ start: b, end: c })).to.deep.equal([b]);
|
|
163
|
+
expect(await keys({ start: b })).to.deep.equal([b, c, d]);
|
|
164
|
+
expect(await keys({ end: c })).to.deep.equal([a, b]);
|
|
165
|
+
expect(await keys({ start: b, end: c, reverse: true })).to.deep.equal([c]);
|
|
166
|
+
expect(await keys({ start: b, limit: 1 })).to.deep.equal([b]);
|
|
167
|
+
expect(await keys({ start: b, reverse: true })).to.deep.equal([d, c]);
|
|
168
|
+
expect(await keys({ end: b, reverse: true })).to.deep.equal([b, a]);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
142
171
|
});
|
|
143
172
|
}
|
|
@@ -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,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
|
+
}
|