@aztec/kv-store 0.65.2 → 0.67.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/indexeddb/array.d.ts +21 -0
- package/dest/indexeddb/array.d.ts.map +1 -0
- package/dest/indexeddb/array.js +96 -0
- package/dest/indexeddb/index.d.ts +7 -0
- package/dest/indexeddb/index.d.ts.map +1 -0
- package/dest/indexeddb/index.js +22 -0
- package/dest/indexeddb/map.d.ts +26 -0
- package/dest/indexeddb/map.d.ts.map +1 -0
- package/dest/indexeddb/map.js +104 -0
- package/dest/indexeddb/set.d.ts +17 -0
- package/dest/indexeddb/set.d.ts.map +1 -0
- package/dest/indexeddb/set.js +25 -0
- package/dest/indexeddb/singleton.d.ts +16 -0
- package/dest/indexeddb/singleton.d.ts.map +1 -0
- package/dest/indexeddb/singleton.js +42 -0
- package/dest/indexeddb/store.d.ts +100 -0
- package/dest/indexeddb/store.d.ts.map +1 -0
- package/dest/indexeddb/store.js +156 -0
- package/dest/interfaces/array.d.ts +43 -11
- package/dest/interfaces/array.d.ts.map +1 -1
- package/dest/interfaces/array_test_suite.d.ts +3 -0
- package/dest/interfaces/array_test_suite.d.ts.map +1 -0
- package/dest/interfaces/array_test_suite.js +97 -0
- package/dest/interfaces/counter.d.ts +21 -1
- package/dest/interfaces/counter.d.ts.map +1 -1
- package/dest/interfaces/map.d.ts +62 -12
- package/dest/interfaces/map.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.d.ts +3 -0
- package/dest/interfaces/map_test_suite.d.ts.map +1 -0
- package/dest/interfaces/map_test_suite.js +114 -0
- package/dest/interfaces/set.d.ts +23 -7
- package/dest/interfaces/set.d.ts.map +1 -1
- package/dest/interfaces/set_test_suite.d.ts +3 -0
- package/dest/interfaces/set_test_suite.d.ts.map +1 -0
- package/dest/interfaces/set_test_suite.js +56 -0
- package/dest/interfaces/singleton.d.ts +14 -5
- package/dest/interfaces/singleton.d.ts.map +1 -1
- package/dest/interfaces/singleton_test_suite.d.ts +3 -0
- package/dest/interfaces/singleton_test_suite.d.ts.map +1 -0
- package/dest/interfaces/singleton_test_suite.js +30 -0
- package/dest/interfaces/store.d.ts +71 -8
- package/dest/interfaces/store.d.ts.map +1 -1
- package/dest/interfaces/store_test_suite.d.ts +3 -0
- package/dest/interfaces/store_test_suite.d.ts.map +1 -0
- package/dest/interfaces/store_test_suite.js +36 -0
- package/dest/interfaces/utils.d.ts +16 -0
- package/dest/interfaces/utils.d.ts.map +1 -0
- package/dest/interfaces/utils.js +19 -0
- package/dest/lmdb/array.d.ts +7 -2
- package/dest/lmdb/array.d.ts.map +1 -1
- package/dest/lmdb/array.js +20 -1
- package/dest/lmdb/counter.d.ts +5 -2
- package/dest/lmdb/counter.d.ts.map +1 -1
- package/dest/lmdb/counter.js +10 -1
- package/dest/lmdb/index.d.ts +10 -0
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +28 -1
- package/dest/lmdb/map.d.ts +8 -2
- package/dest/lmdb/map.d.ts.map +1 -1
- package/dest/lmdb/map.js +27 -1
- package/dest/lmdb/set.d.ts +4 -2
- package/dest/lmdb/set.d.ts.map +1 -1
- package/dest/lmdb/set.js +9 -1
- package/dest/lmdb/singleton.d.ts +3 -2
- package/dest/lmdb/singleton.d.ts.map +1 -1
- package/dest/lmdb/singleton.js +4 -1
- package/dest/lmdb/store.d.ts +21 -13
- package/dest/lmdb/store.d.ts.map +1 -1
- package/dest/lmdb/store.js +23 -19
- package/dest/stores/l2_tips_store.d.ts +2 -2
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +12 -12
- package/dest/utils.d.ts +8 -7
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +6 -25
- package/package.json +54 -38
- package/src/config.ts +1 -1
- package/src/indexeddb/array.ts +118 -0
- package/src/indexeddb/index.ts +29 -0
- package/src/indexeddb/map.ts +142 -0
- package/src/indexeddb/set.ts +37 -0
- package/src/indexeddb/singleton.ts +49 -0
- package/src/indexeddb/store.ts +192 -0
- package/src/interfaces/array.ts +48 -12
- package/src/interfaces/array_test_suite.ts +126 -0
- package/src/interfaces/counter.ts +23 -1
- package/src/interfaces/map.ts +69 -14
- package/src/interfaces/map_test_suite.ts +154 -0
- package/src/interfaces/set.ts +25 -8
- package/src/interfaces/set_test_suite.ts +77 -0
- package/src/interfaces/singleton.ts +14 -6
- package/src/interfaces/singleton_test_suite.ts +42 -0
- package/src/interfaces/store.ts +78 -8
- package/src/interfaces/store_test_suite.ts +52 -0
- package/src/interfaces/utils.ts +21 -0
- package/src/lmdb/array.ts +26 -2
- package/src/lmdb/counter.ts +14 -2
- package/src/lmdb/index.ts +36 -0
- package/src/lmdb/map.ts +34 -2
- package/src/lmdb/set.ts +12 -2
- package/src/lmdb/singleton.ts +6 -2
- package/src/lmdb/store.ts +39 -32
- package/src/stores/l2_tips_store.ts +16 -16
- package/src/utils.ts +8 -36
package/src/interfaces/map.ts
CHANGED
|
@@ -3,20 +3,7 @@ import { type Key, type Range } from './common.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* A map backed by a persistent store.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Gets the value at the given key.
|
|
9
|
-
* @param key - The key to get the value from
|
|
10
|
-
*/
|
|
11
|
-
get(key: K): V | undefined;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Checks if a key exists in the map.
|
|
15
|
-
* @param key - The key to check
|
|
16
|
-
* @returns True if the key exists, false otherwise
|
|
17
|
-
*/
|
|
18
|
-
has(key: K): boolean;
|
|
19
|
-
|
|
6
|
+
interface AztecBaseMap<K extends Key, V> {
|
|
20
7
|
/**
|
|
21
8
|
* Sets the value at the given key.
|
|
22
9
|
* @param key - The key to set the value at
|
|
@@ -43,6 +30,20 @@ export interface AztecMap<K extends Key, V> {
|
|
|
43
30
|
* @param key - The key to delete the value at
|
|
44
31
|
*/
|
|
45
32
|
delete(key: K): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
35
|
+
/**
|
|
36
|
+
* Gets the value at the given key.
|
|
37
|
+
* @param key - The key to get the value from
|
|
38
|
+
*/
|
|
39
|
+
get(key: K): V | undefined;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Checks if a key exists in the map.
|
|
43
|
+
* @param key - The key to check
|
|
44
|
+
* @returns True if the key exists, false otherwise
|
|
45
|
+
*/
|
|
46
|
+
has(key: K): boolean;
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
49
|
* Iterates over the map's key-value entries in the key's natural order
|
|
@@ -80,3 +81,57 @@ export interface AztecMultiMap<K extends Key, V> extends AztecMap<K, V> {
|
|
|
80
81
|
*/
|
|
81
82
|
deleteValue(key: K, val: V): Promise<void>;
|
|
82
83
|
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* A map backed by a persistent store.
|
|
87
|
+
*/
|
|
88
|
+
export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
|
|
89
|
+
/**
|
|
90
|
+
* Gets the value at the given key.
|
|
91
|
+
* @param key - The key to get the value from
|
|
92
|
+
*/
|
|
93
|
+
getAsync(key: K): Promise<V | undefined>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Checks if a key exists in the map.
|
|
97
|
+
* @param key - The key to check
|
|
98
|
+
* @returns True if the key exists, false otherwise
|
|
99
|
+
*/
|
|
100
|
+
hasAsync(key: K): Promise<boolean>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Iterates over the map's key-value entries in the key's natural order
|
|
104
|
+
* @param range - The range of keys to iterate over
|
|
105
|
+
*/
|
|
106
|
+
entriesAsync(range?: Range<K>): AsyncIterableIterator<[K, V]>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Iterates over the map's values in the key's natural order
|
|
110
|
+
* @param range - The range of keys to iterate over
|
|
111
|
+
*/
|
|
112
|
+
valuesAsync(range?: Range<K>): AsyncIterableIterator<V>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Iterates over the map's keys in the key's natural order
|
|
116
|
+
* @param range - The range of keys to iterate over
|
|
117
|
+
*/
|
|
118
|
+
keysAsync(range?: Range<K>): AsyncIterableIterator<K>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* A map backed by a persistent store that can have multiple values for a single key.
|
|
123
|
+
*/
|
|
124
|
+
export interface AztecAsyncMultiMap<K extends Key, V> extends AztecAsyncMap<K, V> {
|
|
125
|
+
/**
|
|
126
|
+
* Gets all the values at the given key.
|
|
127
|
+
* @param key - The key to get the values from
|
|
128
|
+
*/
|
|
129
|
+
getValuesAsync(key: K): AsyncIterableIterator<V>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Deletes a specific value at the given key.
|
|
133
|
+
* @param key - The key to delete the value at
|
|
134
|
+
* @param val - The value to delete
|
|
135
|
+
*/
|
|
136
|
+
deleteValue(key: K, val: V): Promise<void>;
|
|
137
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
2
|
+
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
|
|
5
|
+
import { type Key, type Range } from './common.js';
|
|
6
|
+
import { type AztecAsyncMap, type AztecAsyncMultiMap, type AztecMap, type AztecMultiMap } from './map.js';
|
|
7
|
+
import { type AztecAsyncKVStore, type AztecKVStore } from './store.js';
|
|
8
|
+
import { isSyncStore } from './utils.js';
|
|
9
|
+
|
|
10
|
+
export function describeAztecMap(
|
|
11
|
+
testName: string,
|
|
12
|
+
getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
|
|
13
|
+
forceAsync: boolean = false,
|
|
14
|
+
) {
|
|
15
|
+
describe(testName, () => {
|
|
16
|
+
let store: AztecKVStore | AztecAsyncKVStore;
|
|
17
|
+
let map: AztecMultiMap<Key, string> | AztecAsyncMultiMap<Key, string>;
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
store = await getStore();
|
|
21
|
+
map = store.openMultiMap<string | [number, string], string>('test');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
async function get(key: Key, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
25
|
+
return isSyncStore(store) && !forceAsync
|
|
26
|
+
? (sut as AztecMultiMap<any, any>).get(key)
|
|
27
|
+
: await (sut as AztecAsyncMultiMap<any, any>).getAsync(key);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function entries() {
|
|
31
|
+
return isSyncStore(store) && !forceAsync
|
|
32
|
+
? await toArray((map as AztecMultiMap<any, any>).entries())
|
|
33
|
+
: await toArray((map as AztecAsyncMultiMap<any, any>).entriesAsync());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function values() {
|
|
37
|
+
return isSyncStore(store) && !forceAsync
|
|
38
|
+
? await toArray((map as AztecMultiMap<any, any>).values())
|
|
39
|
+
: await toArray((map as AztecAsyncMultiMap<any, any>).valuesAsync());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function keys(range?: Range<Key>, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
|
|
43
|
+
return isSyncStore(store) && !forceAsync
|
|
44
|
+
? await toArray((sut as AztecMultiMap<any, any>).keys(range))
|
|
45
|
+
: await toArray((sut as AztecAsyncMultiMap<any, any>).keysAsync(range));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function getValues(key: Key) {
|
|
49
|
+
return isSyncStore(store) && !forceAsync
|
|
50
|
+
? await toArray((map as AztecMultiMap<any, any>).getValues(key))
|
|
51
|
+
: await toArray((map as AztecAsyncMultiMap<any, any>).getValuesAsync(key));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
it('should be able to set and get values', async () => {
|
|
55
|
+
await map.set('foo', 'bar');
|
|
56
|
+
await map.set('baz', 'qux');
|
|
57
|
+
|
|
58
|
+
expect(await get('foo')).to.equal('bar');
|
|
59
|
+
expect(await get('baz')).to.equal('qux');
|
|
60
|
+
expect(await get('quux')).to.equal(undefined);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should be able to set values if they do not exist', async () => {
|
|
64
|
+
expect(await map.setIfNotExists('foo', 'bar')).to.equal(true);
|
|
65
|
+
expect(await map.setIfNotExists('foo', 'baz')).to.equal(false);
|
|
66
|
+
|
|
67
|
+
expect(await get('foo')).to.equal('bar');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should be able to delete values', async () => {
|
|
71
|
+
await map.set('foo', 'bar');
|
|
72
|
+
await map.set('baz', 'qux');
|
|
73
|
+
|
|
74
|
+
await map.delete('foo');
|
|
75
|
+
|
|
76
|
+
expect(await get('foo')).to.equal(undefined);
|
|
77
|
+
expect(await get('baz')).to.equal('qux');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should be able to iterate over entries when there are no keys', async () => {
|
|
81
|
+
expect(await entries()).to.deep.equal([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should be able to iterate over entries', async () => {
|
|
85
|
+
await map.set('foo', 'bar');
|
|
86
|
+
await map.set('baz', 'qux');
|
|
87
|
+
|
|
88
|
+
expect(await entries()).to.deep.equal([
|
|
89
|
+
['baz', 'qux'],
|
|
90
|
+
['foo', 'bar'],
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should be able to iterate over values', async () => {
|
|
95
|
+
await map.set('foo', 'bar');
|
|
96
|
+
await map.set('baz', 'quux');
|
|
97
|
+
|
|
98
|
+
expect(await values()).to.deep.equal(['quux', 'bar']);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should be able to iterate over keys', async () => {
|
|
102
|
+
await map.set('foo', 'bar');
|
|
103
|
+
await map.set('baz', 'qux');
|
|
104
|
+
|
|
105
|
+
expect(await keys()).to.deep.equal(['baz', 'foo']);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should be able to get multiple values for a single key', async () => {
|
|
109
|
+
await map.set('foo', 'bar');
|
|
110
|
+
await map.set('foo', 'baz');
|
|
111
|
+
|
|
112
|
+
expect(await getValues('foo')).to.deep.equal(['bar', 'baz']);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should be able to delete individual values for a single key', async () => {
|
|
116
|
+
await map.set('foo', 'bar');
|
|
117
|
+
await map.set('foo', 'baz');
|
|
118
|
+
|
|
119
|
+
await map.deleteValue('foo', 'bar');
|
|
120
|
+
|
|
121
|
+
expect(await getValues('foo')).to.deep.equal(['baz']);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('supports tuple keys', async () => {
|
|
125
|
+
// Use a new map because key structure has changed
|
|
126
|
+
const tupleMap = store.openMap<[number, string], string>('test-tuple');
|
|
127
|
+
|
|
128
|
+
await tupleMap.set([5, 'bar'], 'val');
|
|
129
|
+
await tupleMap.set([0, 'foo'], 'val');
|
|
130
|
+
|
|
131
|
+
expect(await keys(undefined, tupleMap)).to.deep.equal([
|
|
132
|
+
[0, 'foo'],
|
|
133
|
+
[5, 'bar'],
|
|
134
|
+
]);
|
|
135
|
+
|
|
136
|
+
expect(await get([5, 'bar'], tupleMap)).to.equal('val');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('supports range queries', async () => {
|
|
140
|
+
await map.set('a', 'a');
|
|
141
|
+
await map.set('b', 'b');
|
|
142
|
+
await map.set('c', 'c');
|
|
143
|
+
await map.set('d', 'd');
|
|
144
|
+
|
|
145
|
+
expect(await keys({ start: 'b', end: 'c' })).to.deep.equal(['b']);
|
|
146
|
+
expect(await keys({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
|
|
147
|
+
expect(await keys({ end: 'c' })).to.deep.equal(['a', 'b']);
|
|
148
|
+
expect(await keys({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
|
|
149
|
+
expect(await keys({ start: 'b', limit: 1 })).to.deep.equal(['b']);
|
|
150
|
+
expect(await keys({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
|
|
151
|
+
expect(await keys({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
package/src/interfaces/set.ts
CHANGED
|
@@ -3,14 +3,7 @@ import { type Key, type Range } from './common.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* A set backed by a persistent store.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Checks if a key exists in the set.
|
|
9
|
-
* @param key - The key to check
|
|
10
|
-
* @returns True if the key exists, false otherwise
|
|
11
|
-
*/
|
|
12
|
-
has(key: K): boolean;
|
|
13
|
-
|
|
6
|
+
interface AztecBaseSet<K extends Key> {
|
|
14
7
|
/**
|
|
15
8
|
* Adds the given value.
|
|
16
9
|
* @param key - The key to add.
|
|
@@ -22,6 +15,15 @@ export interface AztecSet<K extends Key> {
|
|
|
22
15
|
* @param key - The key to delete.
|
|
23
16
|
*/
|
|
24
17
|
delete(key: K): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AztecSet<K extends Key> extends AztecBaseSet<K> {
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a key exists in the set.
|
|
23
|
+
* @param key - The key to check
|
|
24
|
+
* @returns True if the key exists, false otherwise
|
|
25
|
+
*/
|
|
26
|
+
has(key: K): boolean;
|
|
25
27
|
|
|
26
28
|
/**
|
|
27
29
|
* Iterates over the sets's keys entries in the key's natural order
|
|
@@ -29,3 +31,18 @@ export interface AztecSet<K extends Key> {
|
|
|
29
31
|
*/
|
|
30
32
|
entries(range?: Range<K>): IterableIterator<K>;
|
|
31
33
|
}
|
|
34
|
+
|
|
35
|
+
export interface AztecAsyncSet<K extends Key> extends AztecBaseSet<K> {
|
|
36
|
+
/**
|
|
37
|
+
* Checks if a key exists in the set.
|
|
38
|
+
* @param key - The key to check
|
|
39
|
+
* @returns True if the key exists, false otherwise
|
|
40
|
+
*/
|
|
41
|
+
hasAsync(key: K): Promise<boolean>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Iterates over the sets's keys entries in the key's natural order
|
|
45
|
+
* @param range - The range of keys to iterate over
|
|
46
|
+
*/
|
|
47
|
+
entriesAsync(range?: Range<K>): AsyncIterableIterator<K>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { toArray } from '@aztec/foundation/iterable';
|
|
2
|
+
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
|
|
5
|
+
import { type Range } from './common.js';
|
|
6
|
+
import { type AztecAsyncSet, type AztecSet } from './set.js';
|
|
7
|
+
import { type AztecAsyncKVStore, type AztecKVStore } from './store.js';
|
|
8
|
+
import { isSyncStore } from './utils.js';
|
|
9
|
+
|
|
10
|
+
export function describeAztecSet(
|
|
11
|
+
testName: string,
|
|
12
|
+
getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
|
|
13
|
+
forceAsync: boolean = false,
|
|
14
|
+
) {
|
|
15
|
+
describe(testName, () => {
|
|
16
|
+
let store: AztecKVStore | AztecAsyncKVStore;
|
|
17
|
+
let set: AztecSet<string> | AztecAsyncSet<string>;
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
store = await getStore();
|
|
21
|
+
set = store.openSet<string>('test');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
async function has(key: string) {
|
|
25
|
+
return isSyncStore(store) && !forceAsync
|
|
26
|
+
? (set as AztecSet<string>).has(key)
|
|
27
|
+
: await (set as AztecAsyncSet<string>).hasAsync(key);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function entries(range?: Range<any>) {
|
|
31
|
+
return isSyncStore(store) && !forceAsync
|
|
32
|
+
? await toArray((set as AztecSet<string>).entries(range))
|
|
33
|
+
: await toArray((set as AztecAsyncSet<string>).entriesAsync(range));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
it('should be able to set and get values', async () => {
|
|
37
|
+
await set.add('foo');
|
|
38
|
+
await set.add('baz');
|
|
39
|
+
|
|
40
|
+
expect(await has('foo')).to.equal(true);
|
|
41
|
+
expect(await has('baz')).to.equal(true);
|
|
42
|
+
expect(await has('bar')).to.equal(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should be able to delete values', async () => {
|
|
46
|
+
await set.add('foo');
|
|
47
|
+
await set.add('baz');
|
|
48
|
+
|
|
49
|
+
await set.delete('foo');
|
|
50
|
+
|
|
51
|
+
expect(await has('foo')).to.equal(false);
|
|
52
|
+
expect(await has('baz')).to.equal(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should be able to iterate over entries', async () => {
|
|
56
|
+
await set.add('baz');
|
|
57
|
+
await set.add('foo');
|
|
58
|
+
|
|
59
|
+
expect(await entries()).to.deep.equal(['baz', 'foo']);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('supports range queries', async () => {
|
|
63
|
+
await set.add('a');
|
|
64
|
+
await set.add('b');
|
|
65
|
+
await set.add('c');
|
|
66
|
+
await set.add('d');
|
|
67
|
+
|
|
68
|
+
expect(await entries({ start: 'b', end: 'c' })).to.deep.equal(['b']);
|
|
69
|
+
expect(await entries({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
|
|
70
|
+
expect(await entries({ end: 'c' })).to.deep.equal(['a', 'b']);
|
|
71
|
+
expect(await entries({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
|
|
72
|
+
expect(await entries({ start: 'b', limit: 1 })).to.deep.equal(['b']);
|
|
73
|
+
expect(await entries({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
|
|
74
|
+
expect(await entries({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -2,12 +2,7 @@
|
|
|
2
2
|
* Represents a singleton value in the database.
|
|
3
3
|
* Note: The singleton loses type info so it's recommended to serialize to buffer when storing it.
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Gets the value.
|
|
8
|
-
*/
|
|
9
|
-
get(): T | undefined;
|
|
10
|
-
|
|
5
|
+
interface AztecBaseSingleton<T> {
|
|
11
6
|
/**
|
|
12
7
|
* Sets the value.
|
|
13
8
|
* @param val - The new value
|
|
@@ -19,3 +14,16 @@ export interface AztecSingleton<T> {
|
|
|
19
14
|
*/
|
|
20
15
|
delete(): Promise<boolean>;
|
|
21
16
|
}
|
|
17
|
+
export interface AztecSingleton<T> extends AztecBaseSingleton<T> {
|
|
18
|
+
/**
|
|
19
|
+
* Gets the value.
|
|
20
|
+
*/
|
|
21
|
+
get(): T | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface AztecAsyncSingleton<T> extends AztecBaseSingleton<T> {
|
|
25
|
+
/**
|
|
26
|
+
* Gets the value.
|
|
27
|
+
*/
|
|
28
|
+
getAsync(): Promise<T | undefined>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
|
|
3
|
+
import { type AztecAsyncSingleton, type AztecSingleton } from './singleton.js';
|
|
4
|
+
import { type AztecAsyncKVStore, type AztecKVStore } from './store.js';
|
|
5
|
+
import { isSyncStore } from './utils.js';
|
|
6
|
+
|
|
7
|
+
export function describeAztecSingleton(
|
|
8
|
+
testName: string,
|
|
9
|
+
getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
|
|
10
|
+
forceAsync: boolean = false,
|
|
11
|
+
) {
|
|
12
|
+
describe(testName, () => {
|
|
13
|
+
let store: AztecKVStore | AztecAsyncKVStore;
|
|
14
|
+
let singleton: AztecSingleton<string> | AztecAsyncSingleton<string>;
|
|
15
|
+
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
store = await getStore();
|
|
18
|
+
singleton = store.openSingleton<string>('test');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
async function get() {
|
|
22
|
+
return isSyncStore(store) && !forceAsync
|
|
23
|
+
? (singleton as AztecSingleton<string>).get()
|
|
24
|
+
: await (singleton as AztecAsyncSingleton<string>).getAsync();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
it('returns undefined if the value is not set', async () => {
|
|
28
|
+
expect(await get()).to.equal(undefined);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should be able to set and get values', async () => {
|
|
32
|
+
expect(await singleton.set('foo')).to.equal(true);
|
|
33
|
+
expect(await get()).to.equal('foo');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('overwrites the value if it is set again', async () => {
|
|
37
|
+
expect(await singleton.set('foo')).to.equal(true);
|
|
38
|
+
expect(await singleton.set('bar')).to.equal(true);
|
|
39
|
+
expect(await get()).to.equal('bar');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
package/src/interfaces/store.ts
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
|
-
import { type AztecArray } from './array.js';
|
|
1
|
+
import { type AztecArray, type AztecAsyncArray } from './array.js';
|
|
2
2
|
import { type Key } from './common.js';
|
|
3
|
-
import { type AztecCounter } from './counter.js';
|
|
4
|
-
import { type AztecMap, type AztecMultiMap } from './map.js';
|
|
5
|
-
import { type AztecSet } from './set.js';
|
|
6
|
-
import { type AztecSingleton } from './singleton.js';
|
|
3
|
+
import { type AztecAsyncCounter, type AztecCounter } from './counter.js';
|
|
4
|
+
import { type AztecAsyncMap, type AztecAsyncMultiMap, type AztecMap, type AztecMultiMap } from './map.js';
|
|
5
|
+
import { type AztecAsyncSet, type AztecSet } from './set.js';
|
|
6
|
+
import { type AztecAsyncSingleton, type AztecSingleton } from './singleton.js';
|
|
7
7
|
|
|
8
8
|
/** A key-value store */
|
|
9
9
|
export interface AztecKVStore {
|
|
10
|
+
syncGetters: true;
|
|
10
11
|
/**
|
|
11
12
|
* Creates a new map.
|
|
12
13
|
* @param name - The name of the map
|
|
13
14
|
* @returns The map
|
|
14
15
|
*/
|
|
15
|
-
openMap<K extends
|
|
16
|
+
openMap<K extends Key, V>(name: string): AztecMap<K, V>;
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Creates a new set.
|
|
19
20
|
* @param name - The name of the set
|
|
20
21
|
* @returns The set
|
|
21
22
|
*/
|
|
22
|
-
openSet<K extends
|
|
23
|
+
openSet<K extends Key>(name: string): AztecSet<K>;
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Creates a new multi-map.
|
|
26
27
|
* @param name - The name of the multi-map
|
|
27
28
|
* @returns The multi-map
|
|
28
29
|
*/
|
|
29
|
-
openMultiMap<K extends
|
|
30
|
+
openMultiMap<K extends Key, V>(name: string): AztecMultiMap<K, V>;
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* Creates a new array.
|
|
@@ -74,3 +75,72 @@ export interface AztecKVStore {
|
|
|
74
75
|
*/
|
|
75
76
|
estimateSize(): { mappingSize: number; actualSize: number; numItems: number };
|
|
76
77
|
}
|
|
78
|
+
|
|
79
|
+
export interface AztecAsyncKVStore {
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new map.
|
|
82
|
+
* @param name - The name of the map
|
|
83
|
+
* @returns The map
|
|
84
|
+
*/
|
|
85
|
+
openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V>;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates a new set.
|
|
89
|
+
* @param name - The name of the set
|
|
90
|
+
* @returns The set
|
|
91
|
+
*/
|
|
92
|
+
openSet<K extends Key>(name: string): AztecAsyncSet<K>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new multi-map.
|
|
96
|
+
* @param name - The name of the multi-map
|
|
97
|
+
* @returns The multi-map
|
|
98
|
+
*/
|
|
99
|
+
openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Creates a new array.
|
|
103
|
+
* @param name - The name of the array
|
|
104
|
+
* @returns The array
|
|
105
|
+
*/
|
|
106
|
+
openArray<T>(name: string): AztecAsyncArray<T>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Creates a new singleton.
|
|
110
|
+
* @param name - The name of the singleton
|
|
111
|
+
* @returns The singleton
|
|
112
|
+
*/
|
|
113
|
+
openSingleton<T>(name: string): AztecAsyncSingleton<T>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Creates a new count map.
|
|
117
|
+
* @param name - name of the counter
|
|
118
|
+
*/
|
|
119
|
+
openCounter<K extends Key>(name: string): AztecAsyncCounter<K>;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Starts a transaction. All calls to read/write data while in a transaction are queued and executed atomically.
|
|
123
|
+
* @param callback - The callback to execute in a transaction
|
|
124
|
+
*/
|
|
125
|
+
transactionAsync<T extends Exclude<any, Promise<any>>>(callback: () => Promise<T>): Promise<T>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Clears all entries in the store
|
|
129
|
+
*/
|
|
130
|
+
clear(): Promise<void>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Forks the store.
|
|
134
|
+
*/
|
|
135
|
+
fork(): Promise<AztecAsyncKVStore>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Deletes the store
|
|
139
|
+
*/
|
|
140
|
+
delete(): Promise<void>;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Estimates the size of the store in bytes.
|
|
144
|
+
*/
|
|
145
|
+
estimateSize(): { mappingSize: number; actualSize: number; numItems: number };
|
|
146
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
|
|
3
|
+
import { type AztecAsyncSingleton, type AztecSingleton } from './singleton.js';
|
|
4
|
+
import { type AztecAsyncKVStore, type AztecKVStore } from './store.js';
|
|
5
|
+
import { isSyncStore } from './utils.js';
|
|
6
|
+
|
|
7
|
+
export function describeAztecStore(
|
|
8
|
+
testName: string,
|
|
9
|
+
getPersistentStore: () => Promise<AztecKVStore | AztecAsyncKVStore>,
|
|
10
|
+
getPersistentNoPathStore: () => Promise<AztecKVStore | AztecAsyncKVStore>,
|
|
11
|
+
getEphemeralStore: () => Promise<AztecKVStore | AztecAsyncKVStore>,
|
|
12
|
+
) {
|
|
13
|
+
describe(testName, () => {
|
|
14
|
+
async function get(
|
|
15
|
+
store: AztecKVStore | AztecAsyncKVStore,
|
|
16
|
+
singleton: AztecSingleton<string> | AztecAsyncSingleton<string>,
|
|
17
|
+
) {
|
|
18
|
+
return isSyncStore(store)
|
|
19
|
+
? (singleton as AztecSingleton<string>).get()
|
|
20
|
+
: await (singleton as AztecAsyncSingleton<string>).getAsync();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const itForks = async (store: AztecKVStore | AztecAsyncKVStore) => {
|
|
24
|
+
const singleton = store.openSingleton<string>('singleton');
|
|
25
|
+
await singleton.set('foo');
|
|
26
|
+
|
|
27
|
+
const forkedStore = await store.fork();
|
|
28
|
+
const forkedSingleton = forkedStore.openSingleton<string>('singleton');
|
|
29
|
+
expect(await get(store, singleton)).to.equal('foo');
|
|
30
|
+
await forkedSingleton.set('bar');
|
|
31
|
+
expect(await get(store, singleton)).to.equal('foo');
|
|
32
|
+
expect(await get(forkedStore, forkedSingleton)).to.equal('bar');
|
|
33
|
+
await forkedSingleton.delete();
|
|
34
|
+
expect(await get(store, singleton)).to.equal('foo');
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
it('forks a persistent store', async () => {
|
|
38
|
+
const store = await getPersistentStore();
|
|
39
|
+
await itForks(store);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('forks a persistent store with no path', async () => {
|
|
43
|
+
const store = await getPersistentNoPathStore();
|
|
44
|
+
await itForks(store);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('forks an ephemeral store', async () => {
|
|
48
|
+
const store = await getEphemeralStore();
|
|
49
|
+
await itForks(store);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type AztecAsyncKVStore, type AztecKVStore } from './store.js';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
|
+
export const mockLogger = {
|
|
5
|
+
debug: (msg: string, data: any) => console.log(msg, data),
|
|
6
|
+
info: (msg: string, data: any) => console.log(msg, data),
|
|
7
|
+
warn: (msg: string, data: any) => console.log(msg, data),
|
|
8
|
+
error: (msg: string, data: any) => console.error(msg, data),
|
|
9
|
+
fatal: (msg: string, data: any) => console.error(msg, data),
|
|
10
|
+
silent: (_msg: string, _data: any) => {},
|
|
11
|
+
verbose: (msg: string, data: any) => console.log(msg, data),
|
|
12
|
+
trace: (msg: string, data: any) => console.log(msg, data),
|
|
13
|
+
level: 'trace' as const,
|
|
14
|
+
isLevelEnabled: (_level: string) => true,
|
|
15
|
+
module: 'kv-store:mock-logger',
|
|
16
|
+
};
|
|
17
|
+
/* eslint-enable no-console */
|
|
18
|
+
|
|
19
|
+
export function isSyncStore(store: AztecKVStore | AztecAsyncKVStore): store is AztecAsyncKVStore {
|
|
20
|
+
return (store as AztecKVStore).syncGetters === true;
|
|
21
|
+
}
|