@aztec-labs/kv-store 6.0.0-nightly.20260829

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