|
1 | | -const { pack, unpack } = require('msgpackr') |
2 | | -const { mkdirSync } = require('fs') |
3 | | -const { Cursor, Env } = require('node-gyp-build')(__dirname) |
| 1 | +const { pack, unpack } = require('msgpackr') |
| 2 | +const { mkdirSync } = require('fs') |
| 3 | +const { Cursor, Env } = require('node-gyp-build')(__dirname) |
| 4 | +const { AsyncLocalStorage } = require('async_hooks') |
4 | 5 |
|
5 | 6 | function asBinary(buffer) { |
6 | 7 | return { |
@@ -69,138 +70,168 @@ class Iterator { |
69 | 70 | } |
70 | 71 |
|
71 | 72 | /** |
72 | | - * This class enforces a very specific usage of lmdb. All keys are utf8 encoded |
73 | | - * buffers and all values are encoded with msgpack. Transaction handling is also |
74 | | - * intentionally simplified to just one current transaction, no nesting, with |
75 | | - * the assumption that there is only one active client. This is the pattern that |
| 73 | + * This class enforces a very specific usage of lmdb. All keys are |
| 74 | + * utf8 encoded buffers and all values are encoded with |
| 75 | + * msgpack. Transaction handling is also intentionally simplified to |
| 76 | + * just one current transaction, no nesting. This is the pattern that |
76 | 77 | * adset-consumer uses in its current form. |
77 | 78 | */ |
78 | 79 | class Store { |
79 | 80 | // TODO: add in support for a persistent readonly transaction for the lifetime |
80 | 81 | // of the Store instance - Engines will need this |
81 | 82 | // TODO: maybe refactor to split env/dbi params |
82 | 83 | // TODO: support additional dbis |
| 84 | + |
| 85 | + /** |
| 86 | + * @param {object} options |
| 87 | + * @param {boolean} options.create |
| 88 | + * @param {number} options.mapSize |
| 89 | + * @param {string} options.name |
| 90 | + * @param {boolean} options.noReadAhead |
| 91 | + * @param {string} options.path |
| 92 | + * @param {AsyncLocalStorage} context |
| 93 | + */ |
83 | 94 | constructor({ |
84 | 95 | create = false, |
85 | 96 | mapSize, |
86 | 97 | name, |
87 | 98 | noReadAhead = false, |
88 | 99 | path |
89 | | - }) { |
| 100 | + }, context) { |
90 | 101 | this.env = new Env() |
91 | 102 | this.env.open({ path, mapSize, noReadAhead }) |
92 | 103 | this.dbi = this.env.openDbi({ name, create, keyIsBuffer: true }) |
93 | | - this.txn = null |
| 104 | + |
| 105 | + if (context) { |
| 106 | + this.context = context |
| 107 | + } else { |
| 108 | + console.log('no context supplied, creating our own local storage context') |
| 109 | + this.context = new AsyncLocalStorage() |
| 110 | + } |
94 | 111 | } |
95 | 112 |
|
| 113 | + /** |
| 114 | + * |
| 115 | + * @param {string} key |
| 116 | + * @param {object} value |
| 117 | + */ |
96 | 118 | put(key, value) { |
97 | | - this.transact(() => { |
98 | | - try { |
99 | | - const keyBuffer = Buffer.from(key, 'utf8') |
100 | | - let valueBuffer |
101 | | - if (value && value['\x10binary-data\x02']) |
102 | | - valueBuffer = value['\x10binary-data\x02'] |
103 | | - else |
104 | | - valueBuffer = pack(value) |
105 | | - this.txn.putBinary(this.dbi, keyBuffer, valueBuffer) |
106 | | - } catch (error) { |
107 | | - console.error('Error storing value:', error) |
108 | | - } |
| 119 | + this.transact((txn) => { |
| 120 | + const keyBuffer = Buffer.from(key, 'utf8') |
| 121 | + let valueBuffer |
| 122 | + if (value && value['\x10binary-data\x02']) |
| 123 | + valueBuffer = value['\x10binary-data\x02'] |
| 124 | + else |
| 125 | + valueBuffer = pack(value) |
| 126 | + txn.putBinary(this.dbi, keyBuffer, valueBuffer) |
109 | 127 | }) |
110 | 128 | } |
111 | 129 |
|
| 130 | + /** |
| 131 | + * |
| 132 | + * @param {object} key |
| 133 | + * @returns |
| 134 | + */ |
112 | 135 | get(key) { |
113 | | - return this.transact(() => { |
114 | | - try { |
115 | | - const keyBuffer = Buffer.from(key, 'utf8') |
116 | | - const value = this.txn.getBinary(this.dbi, keyBuffer) |
117 | | - return value == null ? null : unpack(value) |
118 | | - } catch (error) { |
119 | | - console.error('Error retrieving value:', error) |
120 | | - return null |
121 | | - } |
| 136 | + return this.transact((txn) => { |
| 137 | + const keyBuffer = Buffer.from(key, 'utf8') |
| 138 | + const value = txn.getBinary(this.dbi, keyBuffer) |
| 139 | + return value == null ? null : unpack(value) |
122 | 140 | }, true) |
123 | 141 | } |
124 | 142 |
|
| 143 | + /** |
| 144 | + * |
| 145 | + * @param {object} key |
| 146 | + * @returns |
| 147 | + */ |
125 | 148 | del(key) { |
126 | | - return this.transact(() => { |
127 | | - try { |
128 | | - const keyBuffer = Buffer.from(key, 'utf8') |
129 | | - if (this.txn.getBinary(this.dbi, keyBuffer) != null) { |
130 | | - this.txn.del(this.dbi, keyBuffer) |
131 | | - return true |
132 | | - } else { |
133 | | - return false |
134 | | - } |
135 | | - } catch (error) { |
136 | | - console.error('Error deleting value:', error) |
| 149 | + return this.transact((txn) => { |
| 150 | + const keyBuffer = Buffer.from(key, 'utf8') |
| 151 | + if (txn.getBinary(this.dbi, keyBuffer) != null) { |
| 152 | + txn.del(this.dbi, keyBuffer) |
| 153 | + return true |
| 154 | + } else { |
| 155 | + return false |
137 | 156 | } |
138 | 157 | }) |
139 | 158 | } |
140 | 159 |
|
141 | 160 | /** |
142 | | - * Wrapper function for a transaction. This is only sensible in adset-consumer |
143 | | - * as there is only ever one thread of control working on the Store instance |
144 | | - * at a time. |
145 | | - * @param {*} f |
| 161 | + * Wrapper function for a transaction. |
| 162 | + * @param {*} f Function to execute within a txn |
| 163 | + * @param {boolean} [readOnly=false] Set to true if txn is readOnly |
146 | 164 | */ |
147 | | - transact(f, readonly = false) { |
| 165 | + transact(f, readOnly = false) { |
148 | 166 | let ownTxn = false |
149 | | - if (!this.txn) { |
150 | | - this.txn = this.env.beginTxn() |
| 167 | + let store = this.context.getStore() |
| 168 | + if (!store) { |
| 169 | + store = {} |
| 170 | + this.context.enterWith(store) |
| 171 | + } |
| 172 | + let txn = store.txn |
| 173 | + if (!txn) { |
| 174 | + txn = this.env.beginTxn({ readOnly }) |
| 175 | + store.txn = txn |
151 | 176 | ownTxn = true |
152 | 177 | } |
153 | 178 |
|
154 | 179 | try { |
155 | | - const result = f() |
| 180 | + const result = f(txn) |
156 | 181 | if (ownTxn) { |
157 | | - if (readonly) { |
158 | | - this.txn.abort() |
159 | | - } |
160 | | - else { |
161 | | - this.txn.commit() |
162 | | - } |
| 182 | + if (readOnly) |
| 183 | + txn.abort() |
| 184 | + else |
| 185 | + txn.commit() |
163 | 186 | } |
164 | 187 | return result |
165 | 188 | } catch (error) { |
166 | | - console.error('Transaction aborted due to an error:', error.message) |
167 | | - this.txn.abort() |
| 189 | + if (ownTxn) { |
| 190 | + console.error('transaction aborted:', error.message) |
| 191 | + txn.abort() |
| 192 | + } |
168 | 193 | throw error |
169 | 194 | } finally { |
170 | 195 | if (ownTxn) |
171 | | - this.txn = null |
| 196 | + store.txn = null |
172 | 197 | } |
173 | 198 | } |
174 | 199 |
|
175 | | - async transactAsync(f, readonly = false) { |
| 200 | + async transactAsync(f, readOnly = false) { |
176 | 201 | let ownTxn = false |
177 | | - if (!this.txn) { |
178 | | - this.txn = this.env.beginTxn() |
| 202 | + let store = this.context.getStore() |
| 203 | + if (!store) { |
| 204 | + store = {} |
| 205 | + this.context.enterWith(store) |
| 206 | + } |
| 207 | + let txn = store.txn |
| 208 | + if (!txn) { |
| 209 | + txn = this.env.beginTxn({ readOnly }) |
| 210 | + store.txn = txn |
179 | 211 | ownTxn = true |
180 | 212 | } |
181 | 213 |
|
182 | 214 | try { |
183 | | - const result = await f() |
| 215 | + const result = await f(txn) |
184 | 216 | if (ownTxn) { |
185 | | - if (readonly) { |
186 | | - this.txn.abort() |
187 | | - } |
188 | | - else { |
189 | | - this.txn.commit() |
190 | | - } |
| 217 | + if (readOnly) |
| 218 | + txn.abort() |
| 219 | + else |
| 220 | + txn.commit() |
191 | 221 | } |
192 | 222 | return result |
193 | 223 | } catch (error) { |
194 | | - console.error('Transaction aborted due to an error:', error.message) |
195 | | - this.txn.abort() |
| 224 | + if (ownTxn) { |
| 225 | + console.error('transaction aborted:', error.message) |
| 226 | + txn.abort() |
| 227 | + } |
196 | 228 | throw error |
197 | 229 | } finally { |
198 | 230 | if (ownTxn) |
199 | | - this.txn = null |
| 231 | + store.txn = null |
200 | 232 | } |
201 | 233 | } |
202 | 234 |
|
203 | | - |
204 | 235 | iterate() { |
205 | 236 | return new Iterator(this.env, this.dbi) |
206 | 237 | } |
@@ -239,8 +270,8 @@ class Store { |
239 | 270 | } |
240 | 271 |
|
241 | 272 | getCount() { |
242 | | - return this.transact(() => { |
243 | | - return this.dbi.stat(this.txn)?.entryCount |
| 273 | + return this.transact((txn) => { |
| 274 | + return this.dbi.stat(txn)?.entryCount |
244 | 275 | }) |
245 | 276 | } |
246 | 277 |
|
|
0 commit comments