@aztec/kv-store 0.0.0-test.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.
Files changed (166) hide show
  1. package/README.md +10 -0
  2. package/dest/config.d.ts +17 -0
  3. package/dest/config.d.ts.map +1 -0
  4. package/dest/config.js +24 -0
  5. package/dest/indexeddb/array.d.ts +21 -0
  6. package/dest/indexeddb/array.d.ts.map +1 -0
  7. package/dest/indexeddb/array.js +104 -0
  8. package/dest/indexeddb/index.d.ts +7 -0
  9. package/dest/indexeddb/index.d.ts.map +1 -0
  10. package/dest/indexeddb/index.js +19 -0
  11. package/dest/indexeddb/map.d.ts +26 -0
  12. package/dest/indexeddb/map.d.ts.map +1 -0
  13. package/dest/indexeddb/map.js +133 -0
  14. package/dest/indexeddb/set.d.ts +17 -0
  15. package/dest/indexeddb/set.d.ts.map +1 -0
  16. package/dest/indexeddb/set.js +24 -0
  17. package/dest/indexeddb/singleton.d.ts +16 -0
  18. package/dest/indexeddb/singleton.d.ts.map +1 -0
  19. package/dest/indexeddb/singleton.js +37 -0
  20. package/dest/indexeddb/store.d.ts +97 -0
  21. package/dest/indexeddb/store.d.ts.map +1 -0
  22. package/dest/indexeddb/store.js +164 -0
  23. package/dest/interfaces/array.d.ts +80 -0
  24. package/dest/interfaces/array.d.ts.map +1 -0
  25. package/dest/interfaces/array.js +3 -0
  26. package/dest/interfaces/array_test_suite.d.ts +3 -0
  27. package/dest/interfaces/array_test_suite.d.ts.map +1 -0
  28. package/dest/interfaces/array_test_suite.js +104 -0
  29. package/dest/interfaces/common.d.ts +23 -0
  30. package/dest/interfaces/common.d.ts.map +1 -0
  31. package/dest/interfaces/common.js +3 -0
  32. package/dest/interfaces/counter.d.ts +59 -0
  33. package/dest/interfaces/counter.d.ts.map +1 -0
  34. package/dest/interfaces/counter.js +1 -0
  35. package/dest/interfaces/index.d.ts +8 -0
  36. package/dest/interfaces/index.d.ts.map +1 -0
  37. package/dest/interfaces/index.js +6 -0
  38. package/dest/interfaces/map.d.ts +134 -0
  39. package/dest/interfaces/map.d.ts.map +1 -0
  40. package/dest/interfaces/map.js +3 -0
  41. package/dest/interfaces/map_test_suite.d.ts +3 -0
  42. package/dest/interfaces/map_test_suite.d.ts.map +1 -0
  43. package/dest/interfaces/map_test_suite.js +151 -0
  44. package/dest/interfaces/set.d.ts +44 -0
  45. package/dest/interfaces/set.d.ts.map +1 -0
  46. package/dest/interfaces/set.js +1 -0
  47. package/dest/interfaces/set_test_suite.d.ts +3 -0
  48. package/dest/interfaces/set_test_suite.d.ts.map +1 -0
  49. package/dest/interfaces/set_test_suite.js +96 -0
  50. package/dest/interfaces/singleton.d.ts +29 -0
  51. package/dest/interfaces/singleton.d.ts.map +1 -0
  52. package/dest/interfaces/singleton.js +4 -0
  53. package/dest/interfaces/singleton_test_suite.d.ts +3 -0
  54. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -0
  55. package/dest/interfaces/singleton_test_suite.js +30 -0
  56. package/dest/interfaces/store.d.ts +145 -0
  57. package/dest/interfaces/store.d.ts.map +1 -0
  58. package/dest/interfaces/store.js +1 -0
  59. package/dest/interfaces/store_test_suite.d.ts +3 -0
  60. package/dest/interfaces/store_test_suite.d.ts.map +1 -0
  61. package/dest/interfaces/store_test_suite.js +37 -0
  62. package/dest/interfaces/utils.d.ts +16 -0
  63. package/dest/interfaces/utils.d.ts.map +1 -0
  64. package/dest/interfaces/utils.js +16 -0
  65. package/dest/lmdb/array.d.ts +23 -0
  66. package/dest/lmdb/array.d.ts.map +1 -0
  67. package/dest/lmdb/array.js +117 -0
  68. package/dest/lmdb/counter.d.ts +19 -0
  69. package/dest/lmdb/counter.d.ts.map +1 -0
  70. package/dest/lmdb/counter.js +50 -0
  71. package/dest/lmdb/index.d.ts +12 -0
  72. package/dest/lmdb/index.d.ts.map +1 -0
  73. package/dest/lmdb/index.js +25 -0
  74. package/dest/lmdb/map.d.ts +52 -0
  75. package/dest/lmdb/map.d.ts.map +1 -0
  76. package/dest/lmdb/map.js +222 -0
  77. package/dest/lmdb/set.d.ts +18 -0
  78. package/dest/lmdb/set.d.ts.map +1 -0
  79. package/dest/lmdb/set.js +32 -0
  80. package/dest/lmdb/singleton.d.ts +14 -0
  81. package/dest/lmdb/singleton.d.ts.map +1 -0
  82. package/dest/lmdb/singleton.js +26 -0
  83. package/dest/lmdb/store.d.ts +110 -0
  84. package/dest/lmdb/store.d.ts.map +1 -0
  85. package/dest/lmdb/store.js +216 -0
  86. package/dest/lmdb-v2/array.d.ts +18 -0
  87. package/dest/lmdb-v2/array.d.ts.map +1 -0
  88. package/dest/lmdb-v2/array.js +101 -0
  89. package/dest/lmdb-v2/factory.d.ts +12 -0
  90. package/dest/lmdb-v2/factory.d.ts.map +1 -0
  91. package/dest/lmdb-v2/factory.js +60 -0
  92. package/dest/lmdb-v2/index.d.ts +3 -0
  93. package/dest/lmdb-v2/index.d.ts.map +1 -0
  94. package/dest/lmdb-v2/index.js +2 -0
  95. package/dest/lmdb-v2/map.d.ts +86 -0
  96. package/dest/lmdb-v2/map.d.ts.map +1 -0
  97. package/dest/lmdb-v2/map.js +193 -0
  98. package/dest/lmdb-v2/message.d.ts +112 -0
  99. package/dest/lmdb-v2/message.d.ts.map +1 -0
  100. package/dest/lmdb-v2/message.js +18 -0
  101. package/dest/lmdb-v2/read_transaction.d.ts +14 -0
  102. package/dest/lmdb-v2/read_transaction.d.ts.map +1 -0
  103. package/dest/lmdb-v2/read_transaction.js +101 -0
  104. package/dest/lmdb-v2/singleton.d.ts +12 -0
  105. package/dest/lmdb-v2/singleton.d.ts.map +1 -0
  106. package/dest/lmdb-v2/singleton.js +31 -0
  107. package/dest/lmdb-v2/store.d.ts +43 -0
  108. package/dest/lmdb-v2/store.d.ts.map +1 -0
  109. package/dest/lmdb-v2/store.js +181 -0
  110. package/dest/lmdb-v2/utils.d.ts +19 -0
  111. package/dest/lmdb-v2/utils.d.ts.map +1 -0
  112. package/dest/lmdb-v2/utils.js +126 -0
  113. package/dest/lmdb-v2/write_transaction.d.ts +19 -0
  114. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -0
  115. package/dest/lmdb-v2/write_transaction.js +254 -0
  116. package/dest/stores/index.d.ts +2 -0
  117. package/dest/stores/index.d.ts.map +1 -0
  118. package/dest/stores/index.js +1 -0
  119. package/dest/stores/l2_tips_store.d.ts +13 -0
  120. package/dest/stores/l2_tips_store.d.ts.map +1 -0
  121. package/dest/stores/l2_tips_store.js +65 -0
  122. package/dest/utils.d.ts +12 -0
  123. package/dest/utils.d.ts.map +1 -0
  124. package/dest/utils.js +24 -0
  125. package/package.json +99 -0
  126. package/src/config.ts +34 -0
  127. package/src/indexeddb/array.ts +118 -0
  128. package/src/indexeddb/index.ts +29 -0
  129. package/src/indexeddb/map.ts +142 -0
  130. package/src/indexeddb/set.ts +37 -0
  131. package/src/indexeddb/singleton.ts +49 -0
  132. package/src/indexeddb/store.ts +197 -0
  133. package/src/interfaces/array.ts +90 -0
  134. package/src/interfaces/array_test_suite.ts +130 -0
  135. package/src/interfaces/common.ts +20 -0
  136. package/src/interfaces/counter.ts +65 -0
  137. package/src/interfaces/index.ts +7 -0
  138. package/src/interfaces/map.ts +151 -0
  139. package/src/interfaces/map_test_suite.ts +143 -0
  140. package/src/interfaces/set.ts +48 -0
  141. package/src/interfaces/set_test_suite.ts +81 -0
  142. package/src/interfaces/singleton.ts +29 -0
  143. package/src/interfaces/singleton_test_suite.ts +46 -0
  144. package/src/interfaces/store.ts +177 -0
  145. package/src/interfaces/store_test_suite.ts +56 -0
  146. package/src/interfaces/utils.ts +21 -0
  147. package/src/lmdb/array.ts +139 -0
  148. package/src/lmdb/counter.ts +67 -0
  149. package/src/lmdb/index.ts +37 -0
  150. package/src/lmdb/map.ts +248 -0
  151. package/src/lmdb/set.ts +45 -0
  152. package/src/lmdb/singleton.ts +35 -0
  153. package/src/lmdb/store.ts +263 -0
  154. package/src/lmdb-v2/array.ts +115 -0
  155. package/src/lmdb-v2/factory.ts +98 -0
  156. package/src/lmdb-v2/index.ts +2 -0
  157. package/src/lmdb-v2/map.ts +233 -0
  158. package/src/lmdb-v2/message.ts +146 -0
  159. package/src/lmdb-v2/read_transaction.ts +116 -0
  160. package/src/lmdb-v2/singleton.ts +34 -0
  161. package/src/lmdb-v2/store.ts +237 -0
  162. package/src/lmdb-v2/utils.ts +150 -0
  163. package/src/lmdb-v2/write_transaction.ts +314 -0
  164. package/src/stores/index.ts +1 -0
  165. package/src/stores/l2_tips_store.ts +70 -0
  166. package/src/utils.ts +40 -0
@@ -0,0 +1,164 @@
1
+ import { deleteDB, openDB } from 'idb';
2
+ import { IndexedDBAztecArray } from './array.js';
3
+ import { IndexedDBAztecMap } from './map.js';
4
+ import { IndexedDBAztecSet } from './set.js';
5
+ import { IndexedDBAztecSingleton } from './singleton.js';
6
+ /**
7
+ * A key-value store backed by IndexedDB.
8
+ */ export class AztecIndexedDBStore {
9
+ isEphemeral;
10
+ #log;
11
+ #rootDB;
12
+ #name;
13
+ #containers;
14
+ constructor(rootDB, isEphemeral, log, name){
15
+ this.isEphemeral = isEphemeral;
16
+ this.#containers = new Set();
17
+ this.#rootDB = rootDB;
18
+ this.#log = log;
19
+ this.#name = name;
20
+ }
21
+ /**
22
+ * Creates a new AztecKVStore backed by IndexedDB. The path to the database is optional. If not provided,
23
+ * the database will be stored in a temporary location and be deleted when the process exists.
24
+ *
25
+ *
26
+ * @param path - A path on the disk to store the database. Optional
27
+ * @param ephemeral - true if the store should only exist in memory and not automatically be flushed to disk. Optional
28
+ * @param log - A logger to use. Optional
29
+ * @returns The store
30
+ */ static async open(log, name, ephemeral = false) {
31
+ name = name && !ephemeral ? name : self.crypto.getRandomValues(new Uint8Array(16)).join('');
32
+ log.debug(`Opening IndexedDB ${ephemeral ? 'temp ' : ''}database with name ${name}`);
33
+ const rootDB = await openDB(name, 1, {
34
+ upgrade (db) {
35
+ const objectStore = db.createObjectStore('data', {
36
+ keyPath: 'slot'
37
+ });
38
+ objectStore.createIndex('key', [
39
+ 'container',
40
+ 'key'
41
+ ], {
42
+ unique: false
43
+ });
44
+ objectStore.createIndex('keyCount', [
45
+ 'container',
46
+ 'key',
47
+ 'keyCount'
48
+ ], {
49
+ unique: false
50
+ });
51
+ }
52
+ });
53
+ const kvStore = new AztecIndexedDBStore(rootDB, ephemeral, log, name);
54
+ return kvStore;
55
+ }
56
+ /**
57
+ * Forks the current DB into a new DB by backing it up to a temporary location and opening a new indexedb.
58
+ * @returns A new AztecIndexedDBStore.
59
+ */ async fork() {
60
+ const forkedStore = await AztecIndexedDBStore.open(this.#log, undefined, true);
61
+ this.#log.verbose(`Forking store to ${forkedStore.#name}`);
62
+ // Copy old data to new store
63
+ const oldData = this.#rootDB.transaction('data').store;
64
+ const dataToWrite = [];
65
+ for await (const cursor of oldData.iterate()){
66
+ dataToWrite.push(cursor.value);
67
+ }
68
+ const tx = forkedStore.#rootDB.transaction('data', 'readwrite').store;
69
+ for (const data of dataToWrite){
70
+ await tx.add(data);
71
+ }
72
+ this.#log.debug(`Forked store at ${forkedStore.#name} opened successfully`);
73
+ return forkedStore;
74
+ }
75
+ /**
76
+ * Creates a new AztecMap in the store.
77
+ * @param name - Name of the map
78
+ * @returns A new AztecMap
79
+ */ openMap(name) {
80
+ const map = new IndexedDBAztecMap(this.#rootDB, name);
81
+ this.#containers.add(map);
82
+ return map;
83
+ }
84
+ /**
85
+ * Creates a new AztecSet in the store.
86
+ * @param name - Name of the set
87
+ * @returns A new AztecSet
88
+ */ openSet(name) {
89
+ const set = new IndexedDBAztecSet(this.#rootDB, name);
90
+ this.#containers.add(set);
91
+ return set;
92
+ }
93
+ /**
94
+ * Creates a new AztecMultiMap in the store. A multi-map stores multiple values for a single key automatically.
95
+ * @param name - Name of the map
96
+ * @returns A new AztecMultiMap
97
+ */ openMultiMap(name) {
98
+ const multimap = new IndexedDBAztecMap(this.#rootDB, name);
99
+ this.#containers.add(multimap);
100
+ return multimap;
101
+ }
102
+ openCounter(_name) {
103
+ throw new Error('Method not implemented.');
104
+ }
105
+ /**
106
+ * Creates a new AztecArray in the store.
107
+ * @param name - Name of the array
108
+ * @returns A new AztecArray
109
+ */ openArray(name) {
110
+ const array = new IndexedDBAztecArray(this.#rootDB, name);
111
+ this.#containers.add(array);
112
+ return array;
113
+ }
114
+ /**
115
+ * Creates a new AztecSingleton in the store.
116
+ * @param name - Name of the singleton
117
+ * @returns A new AztecSingleton
118
+ */ openSingleton(name) {
119
+ const singleton = new IndexedDBAztecSingleton(this.#rootDB, name);
120
+ this.#containers.add(singleton);
121
+ return singleton;
122
+ }
123
+ /**
124
+ * Runs a callback in a transaction.
125
+ * @param callback - Function to execute in a transaction
126
+ * @returns A promise that resolves to the return value of the callback
127
+ */ async transactionAsync(callback) {
128
+ const tx = this.#rootDB.transaction('data', 'readwrite');
129
+ for (const container of this.#containers){
130
+ container.db = tx.store;
131
+ }
132
+ // Avoid awaiting this promise so it doesn't get scheduled in the next microtask
133
+ // By then, the tx would be closed
134
+ const runningPromise = callback();
135
+ // Wait for the transaction to finish
136
+ await tx.done;
137
+ for (const container of this.#containers){
138
+ container.db = undefined;
139
+ }
140
+ // Return the result of the callback.
141
+ // Tx is guaranteed to already be closed, so the await doesn't hurt anything here
142
+ return await runningPromise;
143
+ }
144
+ /**
145
+ * Clears all entries in the store & sub DBs.
146
+ */ async clear() {
147
+ await this.#rootDB.transaction('data', 'readwrite').store.clear();
148
+ }
149
+ /** Deletes this store and removes the database */ delete() {
150
+ this.#containers.clear();
151
+ this.#rootDB.close();
152
+ return deleteDB(this.#name);
153
+ }
154
+ estimateSize() {
155
+ return Promise.resolve({
156
+ mappingSize: 0,
157
+ actualSize: 0,
158
+ numItems: 0
159
+ });
160
+ }
161
+ close() {
162
+ return Promise.resolve();
163
+ }
164
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * An array backed by a persistent store. Can not have any holes in it.
3
+ */
4
+ interface BaseAztecArray<T> {
5
+ /**
6
+ * Pushes values to the end of the array
7
+ * @param vals - The values to push to the end of the array
8
+ * @returns The new length of the array
9
+ */
10
+ push(...vals: T[]): Promise<number>;
11
+ /**
12
+ * Pops a value from the end of the array.
13
+ * @returns The value that was popped, or undefined if the array was empty
14
+ */
15
+ pop(): Promise<T | undefined>;
16
+ /**
17
+ * Updates the value at the given index. Index can be in the range [-length, length - 1).
18
+ * @param index - The index to set the value at
19
+ * @param val - The value to set
20
+ * @returns Whether the value was set
21
+ */
22
+ setAt(index: number, val: T): Promise<boolean>;
23
+ }
24
+ /**
25
+ * An array backed by a persistent store. Can not have any holes in it.
26
+ */
27
+ export interface AztecAsyncArray<T> extends BaseAztecArray<T> {
28
+ /**
29
+ * The size of the array
30
+ */
31
+ lengthAsync(): Promise<number>;
32
+ /**
33
+ * Gets the value at the given index. Index can be in the range [-length, length - 1).
34
+ * If the index is negative, it will be treated as an offset from the end of the array.
35
+ *
36
+ * @param index - The index to get the value from
37
+ * @returns The value at the given index or undefined if the index is out of bounds
38
+ */
39
+ atAsync(index: number): Promise<T | undefined>;
40
+ /**
41
+ * Iterates over the array with indexes.
42
+ */
43
+ entriesAsync(): AsyncIterableIterator<[number, T]>;
44
+ /**
45
+ * Iterates over the array.
46
+ */
47
+ valuesAsync(): AsyncIterableIterator<T>;
48
+ /**
49
+ * Iterates over the array.
50
+ */
51
+ [Symbol.asyncIterator](): AsyncIterableIterator<T>;
52
+ }
53
+ export interface AztecArray<T> extends BaseAztecArray<T> {
54
+ /**
55
+ * The size of the array
56
+ */
57
+ length: number;
58
+ /**
59
+ * Gets the value at the given index. Index can be in the range [-length, length - 1).
60
+ * If the index is negative, it will be treated as an offset from the end of the array.
61
+ *
62
+ * @param index - The index to get the value from
63
+ * @returns The value at the given index or undefined if the index is out of bounds
64
+ */
65
+ at(index: number): T | undefined;
66
+ /**
67
+ * Iterates over the array with indexes.
68
+ */
69
+ entries(): IterableIterator<[number, T]>;
70
+ /**
71
+ * Iterates over the array.
72
+ */
73
+ values(): IterableIterator<T>;
74
+ /**
75
+ * Iterates over the array.
76
+ */
77
+ [Symbol.iterator](): IterableIterator<T>;
78
+ }
79
+ export {};
80
+ //# sourceMappingURL=array.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/interfaces/array.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,UAAU,cAAc,CAAC,CAAC;IACxB;;;;OAIG;IACH,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEpC;;;OAGG;IACH,GAAG,IAAI,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAE9B;;;;;OAKG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,CAAE,SAAQ,cAAc,CAAC,CAAC,CAAC;IAC3D;;OAEG;IACH,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAE/B;;;;;;OAMG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAE/C;;OAEG;IACH,YAAY,IAAI,qBAAqB,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;IAEnD;;OAEG;IACH,WAAW,IAAI,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAExC;;OAEG;IACH,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,qBAAqB,CAAC,CAAC,CAAC,CAAC;CACpD;AAED,MAAM,WAAW,UAAU,CAAC,CAAC,CAAE,SAAQ,cAAc,CAAC,CAAC,CAAC;IACtD;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;;;;OAMG;IACH,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;IAEjC;;OAEG;IACH,OAAO,IAAI,gBAAgB,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;IAEzC;;OAEG;IACH,MAAM,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAE9B;;OAEG;IACH,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC;CAC1C"}
@@ -0,0 +1,3 @@
1
+ /**
2
+ * An array backed by a persistent store. Can not have any holes in it.
3
+ */ export { };
@@ -0,0 +1,3 @@
1
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
2
+ export declare function describeAztecArray(testName: string, getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>, forceAsync?: boolean): void;
3
+ //# sourceMappingURL=array_test_suite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array_test_suite.d.ts","sourceRoot":"","sources":["../../src/interfaces/array_test_suite.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAGlE,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,YAAY,GAAG,OAAO,CAAC,iBAAiB,CAAC,EACzD,UAAU,GAAE,OAAe,QAsH5B"}
@@ -0,0 +1,104 @@
1
+ import { toArray } from '@aztec/foundation/iterable';
2
+ import { expect } from 'chai';
3
+ import { isSyncStore } from './utils.js';
4
+ export function describeAztecArray(testName, getStore, forceAsync = false) {
5
+ describe(testName, ()=>{
6
+ let store;
7
+ let arr;
8
+ beforeEach(async ()=>{
9
+ store = await getStore();
10
+ arr = store.openArray('test');
11
+ });
12
+ afterEach(async ()=>{
13
+ await store.delete();
14
+ });
15
+ async function length(sut = arr) {
16
+ return isSyncStore(store) && !forceAsync ? sut.length : await sut.lengthAsync();
17
+ }
18
+ async function at(index) {
19
+ return isSyncStore(store) && !forceAsync ? arr.at(index) : await arr.atAsync(index);
20
+ }
21
+ async function entries() {
22
+ return isSyncStore(store) && !forceAsync ? await toArray(arr.entries()) : await toArray(arr.entriesAsync());
23
+ }
24
+ async function values(sut = arr) {
25
+ return isSyncStore(store) && !forceAsync ? await toArray(sut.values()) : await toArray(sut.valuesAsync());
26
+ }
27
+ it('should be able to push and pop values', async ()=>{
28
+ await arr.push(1);
29
+ await arr.push(2);
30
+ await arr.push(3);
31
+ expect(await length()).to.equal(3);
32
+ expect(await arr.pop()).to.equal(3);
33
+ expect(await arr.pop()).to.equal(2);
34
+ expect(await arr.pop()).to.equal(1);
35
+ expect(await arr.pop()).to.equal(undefined);
36
+ });
37
+ it('should be able to get values by index', async ()=>{
38
+ await arr.push(1);
39
+ await arr.push(2);
40
+ await arr.push(3);
41
+ expect(await at(0)).to.equal(1);
42
+ expect(await at(1)).to.equal(2);
43
+ expect(await at(2)).to.equal(3);
44
+ expect(await at(3)).to.equal(undefined);
45
+ expect(await at(-1)).to.equal(3);
46
+ expect(await at(-2)).to.equal(2);
47
+ expect(await at(-3)).to.equal(1);
48
+ expect(await at(-4)).to.equal(undefined);
49
+ });
50
+ it('should be able to set values by index', async ()=>{
51
+ await arr.push(1);
52
+ await arr.push(2);
53
+ await arr.push(3);
54
+ expect(await arr.setAt(0, 4)).to.equal(true);
55
+ expect(await arr.setAt(1, 5)).to.equal(true);
56
+ expect(await arr.setAt(2, 6)).to.equal(true);
57
+ expect(await arr.setAt(3, 7)).to.equal(false);
58
+ expect(await at(0)).to.equal(4);
59
+ expect(await at(1)).to.equal(5);
60
+ expect(await at(2)).to.equal(6);
61
+ expect(await at(3)).to.equal(undefined);
62
+ expect(await arr.setAt(-1, 8)).to.equal(true);
63
+ expect(await arr.setAt(-2, 9)).to.equal(true);
64
+ expect(await arr.setAt(-3, 10)).to.equal(true);
65
+ expect(await arr.setAt(-4, 11)).to.equal(false);
66
+ expect(await at(-1)).to.equal(8);
67
+ expect(await at(-2)).to.equal(9);
68
+ expect(await at(-3)).to.equal(10);
69
+ expect(await at(-4)).to.equal(undefined);
70
+ });
71
+ it('should be able to iterate over values', async ()=>{
72
+ await arr.push(1);
73
+ await arr.push(2);
74
+ await arr.push(3);
75
+ expect(await values()).to.deep.equal([
76
+ 1,
77
+ 2,
78
+ 3
79
+ ]);
80
+ expect(await entries()).to.deep.equal([
81
+ [
82
+ 0,
83
+ 1
84
+ ],
85
+ [
86
+ 1,
87
+ 2
88
+ ],
89
+ [
90
+ 2,
91
+ 3
92
+ ]
93
+ ]);
94
+ });
95
+ it('should be able to restore state', async ()=>{
96
+ await arr.push(1);
97
+ await arr.push(2);
98
+ await arr.push(3);
99
+ const arr2 = store.openArray('test');
100
+ expect(await length(arr2)).to.equal(3);
101
+ expect(await values(arr2)).to.deep.equal(await values());
102
+ });
103
+ });
104
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The key type for use with the kv-store
3
+ */
4
+ export type Key = string | number | Array<string | number>;
5
+ /**
6
+ * A range of keys to iterate over.
7
+ */
8
+ export type Range<K extends Key = Key> = {
9
+ /** The key of the first item to include */
10
+ start?: K;
11
+ /** The key of the last item to include */
12
+ end?: K;
13
+ /** Whether to iterate in reverse */
14
+ reverse?: boolean;
15
+ /** The maximum number of items to iterate over */
16
+ limit?: number;
17
+ };
18
+ export type StoreSize = {
19
+ mappingSize: number;
20
+ actualSize: number;
21
+ numItems: number;
22
+ };
23
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/interfaces/common.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,GAAG,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;AAE3D;;GAEG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI;IACvC,2CAA2C;IAC3C,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,0CAA0C;IAC1C,GAAG,CAAC,EAAE,CAAC,CAAC;IACR,oCAAoC;IACpC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kDAAkD;IAClD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ /**
2
+ * The key type for use with the kv-store
3
+ */ export { };
@@ -0,0 +1,59 @@
1
+ import type { Key, Range } from './common.js';
2
+ /**
3
+ * A map that counts how many times it sees a key. Once 0 is reached, that key is removed from the map.
4
+ * Iterating over the map will only return keys that have a count over 0.
5
+ *
6
+ * Keys are stored in sorted order
7
+ */
8
+ interface AztecBaseCounter<K extends Key = Key> {
9
+ /**
10
+ * Resets the count of the given key to the given value.
11
+ * @param key - The key to reset
12
+ * @param value - The value to reset the key to
13
+ */
14
+ set(key: K, value: number): Promise<void>;
15
+ /**
16
+ * Updates the count of the given key by the given delta. This can be used to increment or decrement the count.
17
+ * Once a key's count reaches 0, it is removed from the map.
18
+ *
19
+ * @param key - The key to update
20
+ * @param delta - The amount to modify the key by
21
+ */
22
+ update(key: K, delta: number): Promise<void>;
23
+ }
24
+ export interface AztecCounter<K extends Key = Key> extends AztecBaseCounter<K> {
25
+ /**
26
+ * Gets the current count.
27
+ * @param key - The key to get the count of
28
+ */
29
+ get(key: K): number;
30
+ /**
31
+ * Returns keys in the map in sorted order. Only returns keys that have been seen at least once.
32
+ * @param range - The range of keys to iterate over
33
+ */
34
+ keys(range: Range<K>): IterableIterator<K>;
35
+ /**
36
+ * Returns keys and their counts in the map sorted by the key. Only returns keys that have been seen at least once.
37
+ * @param range - The range of keys to iterate over
38
+ */
39
+ entries(range: Range<K>): IterableIterator<[K, number]>;
40
+ }
41
+ export interface AztecAsyncCounter<K extends Key = Key> extends AztecBaseCounter<K> {
42
+ /**
43
+ * Gets the current count.
44
+ * @param key - The key to get the count of
45
+ */
46
+ getAsync(key: K): Promise<number>;
47
+ /**
48
+ * Returns keys in the map in sorted order. Only returns keys that have been seen at least once.
49
+ * @param range - The range of keys to iterate over
50
+ */
51
+ keysAsync(range: Range<K>): AsyncIterableIterator<K>;
52
+ /**
53
+ * Returns keys and their counts in the map sorted by the key. Only returns keys that have been seen at least once.
54
+ * @param range - The range of keys to iterate over
55
+ */
56
+ entriesAsync(range: Range<K>): AsyncIterableIterator<[K, number]>;
57
+ }
58
+ export {};
59
+ //# sourceMappingURL=counter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"counter.d.ts","sourceRoot":"","sources":["../../src/interfaces/counter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAE9C;;;;;GAKG;AACH,UAAU,gBAAgB,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG;IAC5C;;;;OAIG;IACH,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1C;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9C;AAED,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IAC5E;;;OAGG;IACH,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC;IAEpB;;;OAGG;IACH,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAE3C;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;CACzD;AAED,MAAM,WAAW,iBAAiB,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IACjF;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAElC;;;OAGG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAErD;;;OAGG;IACH,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;CACnE"}
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,8 @@
1
+ export * from './array.js';
2
+ export * from './map.js';
3
+ export * from './counter.js';
4
+ export * from './singleton.js';
5
+ export * from './store.js';
6
+ export * from './set.js';
7
+ export type { Range, StoreSize } from './common.js';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/interfaces/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,UAAU,CAAC;AACzB,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from './array.js';
2
+ export * from './map.js';
3
+ export * from './counter.js';
4
+ export * from './singleton.js';
5
+ export * from './store.js';
6
+ export * from './set.js';
@@ -0,0 +1,134 @@
1
+ import type { Key, Range } from './common.js';
2
+ /**
3
+ * A map backed by a persistent store.
4
+ */
5
+ interface AztecBaseMap<K extends Key, V> {
6
+ /**
7
+ * Sets the value at the given key.
8
+ * @param key - The key to set the value at
9
+ * @param val - The value to set
10
+ */
11
+ set(key: K, val: V): Promise<void>;
12
+ /**
13
+ * Sets the value at the given key if it does not already exist.
14
+ * @param key - The key to set the value at
15
+ * @param val - The value to set
16
+ */
17
+ setIfNotExists(key: K, val: V): Promise<boolean>;
18
+ /**
19
+ * Deletes the value at the given key.
20
+ * @param key - The key to delete the value at
21
+ */
22
+ delete(key: K): Promise<void>;
23
+ }
24
+ export interface AztecMap<K extends Key, V> extends AztecBaseMap<K, V> {
25
+ /**
26
+ * Gets the value at the given key.
27
+ * @param key - The key to get the value from
28
+ */
29
+ get(key: K): V | undefined;
30
+ /**
31
+ * Checks if a key exists in the map.
32
+ * @param key - The key to check
33
+ * @returns True if the key exists, false otherwise
34
+ */
35
+ has(key: K): boolean;
36
+ /**
37
+ * Iterates over the map's key-value entries in the key's natural order
38
+ * @param range - The range of keys to iterate over
39
+ */
40
+ entries(range?: Range<K>): IterableIterator<[K, V]>;
41
+ /**
42
+ * Iterates over the map's values in the key's natural order
43
+ * @param range - The range of keys to iterate over
44
+ */
45
+ values(range?: Range<K>): IterableIterator<V>;
46
+ /**
47
+ * Iterates over the map's keys in the key's natural order
48
+ * @param range - The range of keys to iterate over
49
+ */
50
+ keys(range?: Range<K>): IterableIterator<K>;
51
+ /**
52
+ * Clears the map.
53
+ */
54
+ clear(): Promise<void>;
55
+ }
56
+ export interface AztecMapWithSize<K extends Key, V> extends AztecMap<K, V> {
57
+ /**
58
+ * Gets the size of the map.
59
+ * @returns The size of the map
60
+ */
61
+ size(): number;
62
+ }
63
+ /**
64
+ * A map backed by a persistent store that can have multiple values for a single key.
65
+ */
66
+ export interface AztecMultiMap<K extends Key, V> extends AztecMap<K, V> {
67
+ /**
68
+ * Gets all the values at the given key.
69
+ * @param key - The key to get the values from
70
+ */
71
+ getValues(key: K): IterableIterator<V>;
72
+ /**
73
+ * Deletes a specific value at the given key.
74
+ * @param key - The key to delete the value at
75
+ * @param val - The value to delete
76
+ */
77
+ deleteValue(key: K, val: V): Promise<void>;
78
+ }
79
+ export interface AztecMultiMapWithSize<K extends Key, V> extends AztecMultiMap<K, V> {
80
+ /**
81
+ * Gets the size of the map.
82
+ * @returns The size of the map
83
+ */
84
+ size(): number;
85
+ }
86
+ /**
87
+ * A map backed by a persistent store.
88
+ */
89
+ export interface AztecAsyncMap<K extends Key, V> extends AztecBaseMap<K, V> {
90
+ /**
91
+ * Gets the value at the given key.
92
+ * @param key - The key to get the value from
93
+ */
94
+ getAsync(key: K): Promise<V | undefined>;
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
+ * Iterates over the map's key-value entries in the key's natural order
103
+ * @param range - The range of keys to iterate over
104
+ */
105
+ entriesAsync(range?: Range<K>): AsyncIterableIterator<[K, V]>;
106
+ /**
107
+ * Iterates over the map's values in the key's natural order
108
+ * @param range - The range of keys to iterate over
109
+ */
110
+ valuesAsync(range?: Range<K>): AsyncIterableIterator<V>;
111
+ /**
112
+ * Iterates over the map's keys in the key's natural order
113
+ * @param range - The range of keys to iterate over
114
+ */
115
+ keysAsync(range?: Range<K>): AsyncIterableIterator<K>;
116
+ }
117
+ /**
118
+ * A map backed by a persistent store that can have multiple values for a single key.
119
+ */
120
+ export interface AztecAsyncMultiMap<K extends Key, V> extends AztecAsyncMap<K, V> {
121
+ /**
122
+ * Gets all the values at the given key.
123
+ * @param key - The key to get the values from
124
+ */
125
+ getValuesAsync(key: K): AsyncIterableIterator<V>;
126
+ /**
127
+ * Deletes a specific value at the given key.
128
+ * @param key - The key to delete the value at
129
+ * @param val - The value to delete
130
+ */
131
+ deleteValue(key: K, val: V): Promise<void>;
132
+ }
133
+ export {};
134
+ //# sourceMappingURL=map.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../../src/interfaces/map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAE9C;;GAEG;AACH,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,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/E;;;OAGG;IACH,cAAc,CAAC,GAAG,EAAE,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAEjD;;;;OAIG;IACH,WAAW,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5C"}
@@ -0,0 +1,3 @@
1
+ /**
2
+ * A map backed by a persistent store that can have multiple values for a single key.
3
+ */ export { };
@@ -0,0 +1,3 @@
1
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
2
+ export declare function describeAztecMap(testName: string, getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>, forceAsync?: boolean): void;
3
+ //# sourceMappingURL=map_test_suite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map_test_suite.d.ts","sourceRoot":"","sources":["../../src/interfaces/map_test_suite.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAGlE,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,YAAY,GAAG,OAAO,CAAC,iBAAiB,CAAC,EACzD,UAAU,GAAE,OAAe,QAkI5B"}