-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
abstract-chained-batch.js
420 lines (341 loc) · 13.3 KB
/
abstract-chained-batch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
'use strict'
const combineErrors = require('maybe-combine-errors')
const ModuleError = require('module-error')
const { getOptions, emptyOptions, noop } = require('./lib/common')
const { prefixDescendantKey, isDescendant } = require('./lib/prefixes')
const { PrewriteBatch } = require('./lib/prewrite-batch')
const kStatus = Symbol('status')
const kPublicOperations = Symbol('publicOperations')
const kLegacyOperations = Symbol('legacyOperations')
const kPrivateOperations = Symbol('privateOperations')
const kClosePromise = Symbol('closePromise')
const kLength = Symbol('length')
const kPrewriteRun = Symbol('prewriteRun')
const kPrewriteBatch = Symbol('prewriteBatch')
const kPrewriteData = Symbol('prewriteData')
const kAddMode = Symbol('addMode')
class AbstractChainedBatch {
constructor (db, options) {
if (typeof db !== 'object' || db === null) {
const hint = db === null ? 'null' : typeof db
throw new TypeError(`The first argument must be an abstract-level database, received ${hint}`)
}
const enableWriteEvent = db.listenerCount('write') > 0
const enablePrewriteHook = !db.hooks.prewrite.noop
// Operations for write event. We can skip populating this array (and cloning of
// operations, which is the expensive part) if there are 0 write event listeners.
this[kPublicOperations] = enableWriteEvent ? [] : null
// Operations for legacy batch event. If user opted-in to write event or prewrite
// hook, skip legacy batch event. We can't skip the batch event based on listener
// count, because a listener may be added between put() or del() and write().
this[kLegacyOperations] = enableWriteEvent || enablePrewriteHook ? null : []
this[kLength] = 0
this[kStatus] = 'open'
this[kClosePromise] = null
this[kAddMode] = getOptions(options, emptyOptions).add === true
if (enablePrewriteHook) {
// Use separate arrays to collect operations added by hook functions, because
// we wait to apply those until write(). Store these arrays in PrewriteData which
// exists to separate internal data from the public PrewriteBatch interface.
const data = new PrewriteData([], enableWriteEvent ? [] : null)
this[kPrewriteData] = data
this[kPrewriteBatch] = new PrewriteBatch(db, data[kPrivateOperations], data[kPublicOperations])
this[kPrewriteRun] = db.hooks.prewrite.run // TODO: document why
} else {
this[kPrewriteData] = null
this[kPrewriteBatch] = null
this[kPrewriteRun] = null
}
this.db = db
this.db.attachResource(this)
}
get length () {
if (this[kPrewriteData] !== null) {
return this[kLength] + this[kPrewriteData].length
} else {
return this[kLength]
}
}
put (key, value, options) {
assertStatus(this)
options = getOptions(options, emptyOptions)
const delegated = options.sublevel != null
const db = delegated ? options.sublevel : this.db
const original = options
const keyError = db._checkKey(key)
const valueError = db._checkValue(value)
if (keyError != null) throw keyError
if (valueError != null) throw valueError
// Avoid spread operator because of https://bugs.chromium.org/p/chromium/issues/detail?id=1204540
const op = Object.assign({}, options, {
type: 'put',
key,
value,
keyEncoding: db.keyEncoding(options.keyEncoding),
valueEncoding: db.valueEncoding(options.valueEncoding)
})
if (this[kPrewriteRun] !== null) {
try {
// Note: we could have chosen to recurse here so that prewriteBatch.put() would
// call this.put(). But then operations added by hook functions would be inserted
// before rather than after user operations. Instead we process those operations
// lazily in write(). This does hurt the only performance benefit benefit of a
// chained batch though, which is that it avoids blocking the event loop with
// more than one operation at a time. On the other hand, if operations added by
// hook functions are adjacent (i.e. sorted) committing them should be faster.
this[kPrewriteRun](op, this[kPrewriteBatch])
// Normalize encodings again in case they were modified
op.keyEncoding = db.keyEncoding(op.keyEncoding)
op.valueEncoding = db.valueEncoding(op.valueEncoding)
} catch (err) {
throw new ModuleError('The prewrite hook failed on batch.put()', {
code: 'LEVEL_HOOK_ERROR',
cause: err
})
}
}
// Encode data for private API
const keyEncoding = op.keyEncoding
const preencodedKey = keyEncoding.encode(op.key)
const keyFormat = keyEncoding.format
// If the sublevel is not a descendant then forward that option to the parent db
// so that we don't erroneously add our own prefix to the key of the operation.
const siblings = delegated && !isDescendant(op.sublevel, this.db) && op.sublevel !== this.db
const encodedKey = delegated && !siblings
? prefixDescendantKey(preencodedKey, keyFormat, db, this.db)
: preencodedKey
const valueEncoding = op.valueEncoding
const encodedValue = valueEncoding.encode(op.value)
const valueFormat = valueEncoding.format
// Only prefix once
if (delegated && !siblings) {
op.sublevel = null
}
// If the sublevel is not a descendant then we shouldn't emit events
if (this[kPublicOperations] !== null && !siblings) {
// Clone op before we mutate it for the private API
const publicOperation = Object.assign({}, op)
publicOperation.encodedKey = encodedKey
publicOperation.encodedValue = encodedValue
if (delegated) {
// Ensure emitted data makes sense in the context of this db
publicOperation.key = encodedKey
publicOperation.value = encodedValue
publicOperation.keyEncoding = this.db.keyEncoding(keyFormat)
publicOperation.valueEncoding = this.db.valueEncoding(valueFormat)
}
this[kPublicOperations].push(publicOperation)
} else if (this[kLegacyOperations] !== null && !siblings) {
const legacyOperation = Object.assign({}, original)
legacyOperation.type = 'put'
legacyOperation.key = key
legacyOperation.value = value
this[kLegacyOperations].push(legacyOperation)
}
// If we're forwarding the sublevel option then don't prefix the key yet
op.key = siblings ? encodedKey : this.db.prefixKey(encodedKey, keyFormat, true)
op.value = encodedValue
op.keyEncoding = keyFormat
op.valueEncoding = valueFormat
if (this[kAddMode]) {
this._add(op)
} else {
// This "operation as options" trick avoids further cloning
this._put(op.key, encodedValue, op)
}
// Increment only on success
this[kLength]++
return this
}
_put (key, value, options) {}
del (key, options) {
assertStatus(this)
options = getOptions(options, emptyOptions)
const delegated = options.sublevel != null
const db = delegated ? options.sublevel : this.db
const original = options
const keyError = db._checkKey(key)
if (keyError != null) throw keyError
// Avoid spread operator because of https://bugs.chromium.org/p/chromium/issues/detail?id=1204540
const op = Object.assign({}, options, {
type: 'del',
key,
keyEncoding: db.keyEncoding(options.keyEncoding)
})
if (this[kPrewriteRun] !== null) {
try {
this[kPrewriteRun](op, this[kPrewriteBatch])
// Normalize encoding again in case it was modified
op.keyEncoding = db.keyEncoding(op.keyEncoding)
} catch (err) {
throw new ModuleError('The prewrite hook failed on batch.del()', {
code: 'LEVEL_HOOK_ERROR',
cause: err
})
}
}
// Encode data for private API
const keyEncoding = op.keyEncoding
const preencodedKey = keyEncoding.encode(op.key)
const keyFormat = keyEncoding.format
const encodedKey = delegated ? prefixDescendantKey(preencodedKey, keyFormat, db, this.db) : preencodedKey
// Prevent double prefixing
if (delegated) op.sublevel = null
if (this[kPublicOperations] !== null) {
// Clone op before we mutate it for the private API
const publicOperation = Object.assign({}, op)
publicOperation.encodedKey = encodedKey
if (delegated) {
// Ensure emitted data makes sense in the context of this db
publicOperation.key = encodedKey
publicOperation.keyEncoding = this.db.keyEncoding(keyFormat)
}
this[kPublicOperations].push(publicOperation)
} else if (this[kLegacyOperations] !== null) {
const legacyOperation = Object.assign({}, original)
legacyOperation.type = 'del'
legacyOperation.key = key
this[kLegacyOperations].push(legacyOperation)
}
op.key = this.db.prefixKey(encodedKey, keyFormat, true)
op.keyEncoding = keyFormat
if (this[kAddMode]) {
this._add(op)
} else {
// This "operation as options" trick avoids further cloning
this._del(op.key, op)
}
// Increment only on success
this[kLength]++
return this
}
_del (key, options) {}
_add (op) {}
clear () {
assertStatus(this)
this._clear()
if (this[kPublicOperations] !== null) this[kPublicOperations] = []
if (this[kLegacyOperations] !== null) this[kLegacyOperations] = []
if (this[kPrewriteData] !== null) this[kPrewriteData].clear()
this[kLength] = 0
return this
}
_clear () {}
async write (options) {
assertStatus(this)
options = getOptions(options)
if (this[kLength] === 0) {
return this.close()
} else {
this[kStatus] = 'writing'
// Prepare promise in case close() is called in the mean time
const close = prepareClose(this)
try {
// Process operations added by prewrite hook functions
if (this[kPrewriteData] !== null) {
const publicOperations = this[kPrewriteData][kPublicOperations]
const privateOperations = this[kPrewriteData][kPrivateOperations]
const length = this[kPrewriteData].length
for (let i = 0; i < length; i++) {
const op = privateOperations[i]
// We can _add(), _put() or _del() even though status is now 'writing' because
// status isn't exposed to the private API, so there's no difference in state
// from that perspective, unless an implementation overrides the public write()
// method at its own risk.
if (this[kAddMode]) {
this._add(op)
} else if (op.type === 'put') {
this._put(op.key, op.value, op)
} else {
this._del(op.key, op)
}
}
if (publicOperations !== null && length !== 0) {
this[kPublicOperations] = this[kPublicOperations].concat(publicOperations)
}
}
await this._write(options)
} catch (err) {
close()
try {
await this[kClosePromise]
} catch (closeErr) {
// eslint-disable-next-line no-ex-assign
err = combineErrors([err, closeErr])
}
throw err
}
close()
// Emit after initiating the closing, because event may trigger a
// db close which in turn triggers (idempotently) closing this batch.
if (this[kPublicOperations] !== null) {
this.db.emit('write', this[kPublicOperations])
} else if (this[kLegacyOperations] !== null) {
this.db.emit('batch', this[kLegacyOperations])
}
return this[kClosePromise]
}
}
async _write (options) {}
async close () {
if (this[kClosePromise] !== null) {
// First caller of close() or write() is responsible for error
return this[kClosePromise].catch(noop)
} else {
// Wrap promise to avoid race issues on recursive calls
prepareClose(this)()
return this[kClosePromise]
}
}
async _close () {}
}
const prepareClose = function (batch) {
let close
batch[kClosePromise] = new Promise((resolve, reject) => {
close = () => {
privateClose(batch).then(resolve, reject)
}
})
return close
}
const privateClose = async function (batch) {
batch[kStatus] = 'closing'
await batch._close()
batch.db.detachResource(batch)
}
class PrewriteData {
constructor (privateOperations, publicOperations) {
this[kPrivateOperations] = privateOperations
this[kPublicOperations] = publicOperations
}
get length () {
return this[kPrivateOperations].length
}
clear () {
// Clear operation arrays if present.
for (const k of [kPublicOperations, kPrivateOperations]) {
const ops = this[k]
if (ops !== null) {
// Keep array alive because PrewriteBatch has a reference to it
ops.splice(0, ops.length)
}
}
}
}
const assertStatus = function (batch) {
if (batch[kStatus] !== 'open') {
throw new ModuleError('Batch is not open: cannot change operations after write() or close()', {
code: 'LEVEL_BATCH_NOT_OPEN'
})
}
// Can technically be removed, because it's no longer possible to call db.batch() when
// status is not 'open', and db.close() closes the batch. Keep for now, in case of
// unforseen userland behaviors.
if (batch.db.status !== 'open') {
/* istanbul ignore next */
throw new ModuleError('Database is not open', {
code: 'LEVEL_DATABASE_NOT_OPEN'
})
}
}
exports.AbstractChainedBatch = AbstractChainedBatch