-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
guest.js
526 lines (430 loc) · 12.2 KB
/
guest.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
'use strict'
const { AbstractLevel, AbstractIterator } = require('abstract-level')
const lpstream = require('@vweevers/length-prefixed-stream')
const ModuleError = require('module-error')
const { input, output } = require('./tags')
const { Duplex, pipeline, finished } = require('readable-stream')
const kExplicitClose = Symbol('explicitClose')
const kAbortRequests = Symbol('abortRequests')
const kEnded = Symbol('kEnded')
const kRemote = Symbol('remote')
const kAckMessage = Symbol('ackMessage')
const kEncode = Symbol('encode')
const kRef = Symbol('ref')
const kDb = Symbol('db')
const kRequests = Symbol('requests')
const kIterators = Symbol('iterators')
const kRetry = Symbol('retry')
const kRpcStream = Symbol('rpcStream')
const kFlushed = Symbol('flushed')
const kWrite = Symbol('write')
const kRequest = Symbol('request')
const kPending = Symbol('pending')
const kCallback = Symbol('callback')
const kSeq = Symbol('seq')
const kErrored = Symbol('errored')
const noop = function () {}
class ManyLevelGuest extends AbstractLevel {
constructor (options) {
const { retry, _remote, ...forward } = options || {}
super({
encodings: { buffer: true },
snapshots: !retry,
permanence: true,
seek: true,
createIfMissing: false,
errorIfExists: false
}, forward)
this[kIterators] = new IdMap()
this[kRequests] = new IdMap()
this[kRetry] = !!retry
this[kEncode] = lpstream.encode()
this[kRemote] = _remote || null
this[kRpcStream] = null
this[kRef] = null
this[kDb] = null
this[kExplicitClose] = false
}
get type () {
return 'many-level'
}
createRpcStream (opts) {
if (this[kRpcStream]) {
throw new Error('Only one rpc stream can be active')
}
if (!opts) opts = {}
this[kRef] = opts.ref || null
const self = this
const encode = this[kEncode]
const decode = lpstream.decode()
decode.on('data', function (data) {
if (!data.length) return
const tag = data[0]
const encoding = output.encoding(tag)
if (!encoding) return
let res
try {
res = encoding.decode(data, 1)
} catch (err) {
return
}
switch (tag) {
case output.callback:
oncallback(res)
break
case output.iteratorData:
oniteratordata(res)
break
case output.iteratorError:
oniteratordata(res)
break
case output.iteratorEnd:
oniteratorend(res)
break
case output.getManyCallback:
ongetmanycallback(res)
break
}
self[kFlushed]()
})
const proxy = Duplex.from({ writable: decode, readable: encode })
finished(proxy, cleanup)
this[kRpcStream] = proxy
return proxy
function cleanup () {
self[kRpcStream] = null
self[kEncode] = lpstream.encode()
if (!self[kRetry]) {
self[kAbortRequests]('Connection to leader lost', 'LEVEL_CONNECTION_LOST')
self[kFlushed]()
return
}
for (const req of self[kRequests].values()) {
self[kWrite](req)
}
for (const req of self[kIterators].values()) {
self[kWrite](req)
}
}
function oniteratordata (res) {
const req = self[kIterators].get(res.id)
if (!req || req.iterator[kSeq] !== res.seq) return
req.iterator[kPending].push(res)
if (req.iterator[kCallback]) req.iterator._next(req.iterator[kCallback])
}
function oniteratorend (res) {
const req = self[kIterators].get(res.id)
if (!req || req.iterator[kSeq] !== res.seq) return
// https://github.com/Level/abstract-level/issues/19
req.iterator[kEnded] = true
if (req.iterator[kCallback]) req.iterator._next(req.iterator[kCallback])
}
function oncallback (res) {
const req = self[kRequests].remove(res.id)
if (!req) return
if (res.error) req.callback(new ModuleError('Could not get value', { code: res.error }))
else req.callback(null, normalizeValue(res.value))
}
function ongetmanycallback (res) {
const req = self[kRequests].remove(res.id)
if (!req) return
if (res.error) req.callback(new ModuleError('Could not get values', { code: res.error }))
else req.callback(null, res.values.map(v => normalizeValue(v.value)))
}
}
// Alias for backwards compat with multileveldown
connect (...args) {
return this.createRpcStream(...args)
}
forward (db) {
// We forward calls to the private API of db, so it must support 'buffer'
for (const enc of ['keyEncoding', 'valueEncoding']) {
if (db[enc]('buffer').name !== 'buffer') {
throw new ModuleError(`Database must support non-transcoded 'buffer' ${enc}`, {
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
})
}
}
this[kDb] = db
}
isFlushed () {
return this[kRequests].size === 0 && this[kIterators].size === 0
}
[kFlushed] () {
if (!this.isFlushed()) return
this.emit('flush')
unref(this[kRef])
}
[kAbortRequests] (msg, code) {
for (const req of this[kRequests].clear()) {
req.callback(new ModuleError(msg, { code }))
}
for (const req of this[kIterators].clear()) {
// Cancel in-flight operation if any
const callback = req.iterator[kCallback]
req.iterator[kCallback] = null
if (callback) {
callback(new ModuleError(msg, { code }))
}
// Note: an in-flight operation would block close()
req.iterator.close(noop)
}
}
_get (key, opts, cb) {
// TODO: this and other methods assume db state matches our state
if (this[kDb]) return this[kDb]._get(key, opts, cb)
const req = {
tag: input.get,
id: 0,
key: key,
callback: cb
}
req.id = this[kRequests].add(req)
this[kWrite](req)
}
_getMany (keys, opts, cb) {
if (this[kDb]) return this[kDb]._getMany(keys, opts, cb)
const req = {
tag: input.getMany,
id: 0,
keys: keys,
callback: cb
}
req.id = this[kRequests].add(req)
this[kWrite](req)
}
_put (key, value, opts, cb) {
if (this[kDb]) return this[kDb]._put(key, value, opts, cb)
const req = {
tag: input.put,
id: 0,
key: key,
value: value,
callback: cb
}
req.id = this[kRequests].add(req)
this[kWrite](req)
}
_del (key, opts, cb) {
if (this[kDb]) return this[kDb]._del(key, opts, cb)
const req = {
tag: input.del,
id: 0,
key: key,
callback: cb
}
req.id = this[kRequests].add(req)
this[kWrite](req)
}
_batch (batch, opts, cb) {
if (this[kDb]) return this[kDb]._batch(batch, opts, cb)
const req = {
tag: input.batch,
id: 0,
ops: batch,
callback: cb
}
req.id = this[kRequests].add(req)
this[kWrite](req)
}
_clear (opts, cb) {
if (this[kDb]) return this[kDb]._clear(opts, cb)
const req = {
tag: input.clear,
id: 0,
options: opts,
callback: cb
}
req.id = this[kRequests].add(req)
this[kWrite](req)
}
[kWrite] (req) {
if (this[kRequests].size + this[kIterators].size === 1) ref(this[kRef])
const enc = input.encoding(req.tag)
const buf = Buffer.allocUnsafe(enc.encodingLength(req) + 1)
buf[0] = req.tag
enc.encode(req, buf, 1)
this[kEncode].write(buf)
}
_close (cb) {
// Even if forward() was used, still need to abort requests made before forward().
this[kExplicitClose] = true
this[kAbortRequests]('Aborted on database close()', 'LEVEL_DATABASE_NOT_OPEN')
if (this[kRpcStream]) {
finished(this[kRpcStream], () => {
this[kRpcStream] = null
this._close(cb)
})
this[kRpcStream].destroy()
} else if (this[kDb]) {
// To be safe, use close() not _close().
this[kDb].close(cb)
} else {
this.nextTick(cb)
}
}
_open (options, cb) {
if (this[kRemote]) {
// For tests only so does not need error handling
this[kExplicitClose] = false
const remote = this[kRemote]()
pipeline(
remote,
this.connect(),
remote,
() => {}
)
} else if (this[kExplicitClose]) {
throw new ModuleError('Cannot reopen many-level database after close()', {
code: 'LEVEL_NOT_SUPPORTED'
})
}
this.nextTick(cb)
}
iterator (options) {
if (this[kDb]) {
// TODO: this is 3x faster than doing it in _iterator(). Why?
return this[kDb].iterator(options)
} else {
return AbstractLevel.prototype.iterator.call(this, options)
}
}
_iterator (options) {
return new Iterator(this, options)
}
}
exports.ManyLevelGuest = ManyLevelGuest
class Iterator extends AbstractIterator {
constructor (db, options) {
// Need keys to know where to restart
if (db[kRetry]) options.keys = true
// Avoid spread operator because of https://bugs.chromium.org/p/chromium/issues/detail?id=1204540
super(db, Object.assign({}, options, { abortOnClose: true }))
this[kEnded] = false
this[kErrored] = false
this[kPending] = []
this[kCallback] = null
this[kSeq] = 0
const req = this[kRequest] = {
tag: input.iterator,
id: 0,
seq: 0,
iterator: this,
options,
consumed: 0,
bookmark: null,
seek: null
}
const ack = this[kAckMessage] = {
tag: input.iteratorAck,
id: 0,
seq: 0,
consumed: 0
}
req.id = this.db[kIterators].add(req)
ack.id = req.id
this.db[kWrite](req)
}
_seek (target, options) {
if (this[kErrored]) return
this[kPending] = []
this[kEnded] = false
// Ignore previous (in-flight) data
this[kRequest].seq = ++this[kSeq]
this[kAckMessage].seq = this[kRequest].seq
// For retries
this[kRequest].seek = target
this[kRequest].bookmark = null
this.db[kWrite]({
tag: input.iteratorSeek,
id: this[kRequest].id,
seq: this[kRequest].seq,
target
})
}
// TODO: implement optimized `nextv()`
_next (callback) {
this[kCallback] = null
if (this[kRequest].consumed >= this.limit || this[kErrored]) {
this.nextTick(callback)
} else if (this[kPending].length !== 0) {
const next = this[kPending][0]
const req = this[kRequest]
// TODO: document that error ends the iterator
if (next.error) {
this[kErrored] = true
this[kPending] = []
return this.nextTick(callback, new ModuleError('Could not read entry', {
code: next.error
}))
}
const consumed = ++req.consumed
const key = req.options.keys ? next.data.shift() : undefined
const val = req.options.values ? next.data.shift() : undefined
if (next.data.length === 0) {
this[kPending].shift()
// Acknowledge receipt. Not needed if we don't want more data.
if (consumed < this.limit) {
this[kAckMessage].consumed = consumed
this.db[kWrite](this[kAckMessage])
}
}
// Once we've consumed the result of a seek() it must not get retried
req.seek = null
if (this.db[kRetry]) {
req.bookmark = key
}
this.nextTick(callback, undefined, key, val)
} else if (this[kEnded]) {
this.nextTick(callback)
} else {
this[kCallback] = callback
}
}
_close (cb) {
this.db[kWrite]({ tag: input.iteratorClose, id: this[kRequest].id })
this.db[kIterators].remove(this[kRequest].id)
this.db[kFlushed]()
this.nextTick(cb)
}
}
function normalizeValue (value) {
return value === null ? undefined : value
}
function ref (r) {
if (r && r.ref) r.ref()
}
function unref (r) {
if (r && r.unref) r.unref()
}
class IdMap {
constructor () {
this._map = new Map()
this._seq = 0
}
get size () {
return this._map.size
}
add (item) {
if (this._seq >= 0xffffffff) this._seq = 0
this._map.set(++this._seq, item)
return this._seq
}
get (id) {
return this._map.get(id)
}
remove (id) {
const item = this._map.get(id)
if (item !== undefined) this._map.delete(id)
return item
}
values () {
return this._map.values()
}
clear () {
const values = Array.from(this._map.values())
this._map.clear()
this._seq = 0
return values
}
}