-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
host.js
401 lines (336 loc) · 11.2 KB
/
host.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
'use strict'
const lpstream = require('@vweevers/length-prefixed-stream')
const ModuleError = require('module-error')
const { Duplex, finished } = require('readable-stream')
const { input, output } = require('./tags')
const rangeOptions = new Set(['gt', 'gte', 'lt', 'lte'])
const encodingOptions = Object.freeze({ keyEncoding: 'buffer', valueEncoding: 'buffer' })
const stateEvents = new Set(['opening', 'open', 'closing', 'closed'])
const kEnded = Symbol('ended')
const kClosed = Symbol('closed')
const kDb = Symbol('db')
const kOptions = Symbol('options')
const kMaxItemLength = Symbol('maxItemLength')
const kDataMessage = Symbol('dataMessage')
const kEndMessage = Symbol('endMessage')
const kHandleMany = Symbol('handleMany')
const kIterator = Symbol('iterator')
const kEncode = Symbol('encode')
const kMode = Symbol('mode')
const kBusy = Symbol('busy')
const kPendingSeek = Symbol('pendingSeek')
const kLimit = Symbol('limit')
const kReadAhead = Symbol('readAhead')
const noop = () => {}
const limbo = Symbol('limbo')
// TODO: make use of db.supports manifest
class ManyLevelHost {
constructor (db, options) {
this[kDb] = db
this[kOptions] = { ...options }
this[kOptions].readonly = !!this[kOptions].readonly
this[kOptions].preput = this[kOptions].preput || function (key, val, cb) { cb(null) }
this[kOptions].predel = this[kOptions].predel || function (key, cb) { cb(null) }
this[kOptions].prebatch = this[kOptions].prebatch || function (ops, cb) { cb(null) }
this[kOptions].events = (this[kOptions].events || getEvents(db)).filter(safeEvent)
}
createRpcStream (streamOptions) {
return createRpcStream(this[kDb], this[kOptions], streamOptions)
}
}
exports.ManyLevelHost = ManyLevelHost
function getEvents (db) {
const events = db.supports.events
return Object.keys(events).filter(k => events[k])
}
function safeEvent (event) {
return event && typeof event === 'string' && !stateEvents.has(event)
}
// TODO: support stream options (highWaterMark)
function createRpcStream (db, options, streamOptions) {
const readonly = options.readonly
const decode = lpstream.decode()
const encode = lpstream.encode()
const stream = Duplex.from({ writable: decode, readable: encode })
const preput = options.preput
const predel = options.predel
const prebatch = options.prebatch
db.open({ passive: true }, ready)
// TODO: send events to guest. Challenges:
// - Need to know encodings or emit encoded data; current abstract-level events don't suffice
// - Skip events triggered by guest itself
// - Support data of custom events, maybe with `cbor-x` and/or extensions via manifest
// - Include events emitted before open callback
// for (const event of options.events) {
// db.on(event, ...)
// }
return stream
function ready (err) {
if (stream.destroyed) return
if (err) return stream.destroy(err)
const iterators = new Map()
finished(stream, function () {
for (const iterator of iterators.values()) {
iterator.close()
}
iterators.clear()
})
decode.on('data', function (data) {
if (!data.length) return
const tag = data[0]
const encoding = input.encoding(tag)
if (!encoding) return
let req
try {
req = encoding.decode(data, 1)
} catch (err) {
return
}
switch (tag) {
case input.get: return onget(req)
case input.put: return readonly ? onreadonly(req) : onput(req)
case input.del: return readonly ? onreadonly(req) : ondel(req)
case input.batch: return readonly ? onreadonly(req) : onbatch(req)
case input.iterator: return oniterator(req)
case input.iteratorClose: return oniteratorclose(req)
case input.iteratorAck: return oniteratorack(req)
case input.iteratorSeek: return oniteratorseek(req)
case input.clear: return readonly ? onreadonly(req) : onclear(req)
case input.getMany: return ongetmany(req)
}
})
function callback (id, err, value) {
const msg = { id, error: errorCode(err), value }
encode.write(encodeMessage(msg, output.callback))
}
function getManyCallback (id, err, values) {
const msg = { id, error: errorCode(err), values }
encode.write(encodeMessage(msg, output.getManyCallback))
}
function onput (req) {
preput(req.key, req.value, function (err) {
if (err) return callback(req.id, err)
db.put(req.key, req.value, encodingOptions, function (err) {
callback(req.id, err, null)
})
})
}
function onget (req) {
db.get(req.key, encodingOptions, function (err, value) {
callback(req.id, err, value)
})
}
function ongetmany (req) {
db.getMany(req.keys, encodingOptions, function (err, values) {
getManyCallback(req.id, err, values.map(value => ({ value })))
})
}
function ondel (req) {
predel(req.key, function (err) {
if (err) return callback(req.id, err)
db.del(req.key, encodingOptions, function (err) {
callback(req.id, err)
})
})
}
function onreadonly (req) {
callback(req.id, new ModuleError('Database is readonly', { code: 'LEVEL_READONLY' }))
}
function onbatch (req) {
prebatch(req.ops, function (err) {
if (err) return callback(req.id, err)
db.batch(req.ops, encodingOptions, function (err) {
callback(req.id, err)
})
})
}
function oniterator ({ id, seq, options, consumed, bookmark, seek }) {
if (iterators.has(id)) return
const it = new Iterator(db, id, seq, options, consumed, encode)
iterators.set(id, it)
if (seek) {
it.seek(seek, seq)
} else if (bookmark) {
// Restart where previous iterator left off
it.seek(nextTarget(bookmark, options.reverse), seq)
} else {
it.next(true)
}
}
function oniteratorack ({ id, seq, consumed }) {
const it = iterators.get(id)
if (it === undefined || it.seq !== seq) return
it.pendingAcks = Math.max(0, it.pendingAcks - 1)
it.consumed = Math.max(it.consumed, consumed)
it.next(false)
}
function oniteratorseek ({ id, target, seq }) {
const it = iterators.get(id)
if (it === undefined) return
it.seek(target, seq)
}
function oniteratorclose ({ id }) {
const it = iterators.get(id)
iterators.delete(id)
if (it !== undefined) it.close()
}
function onclear (req) {
db.clear(cleanRangeOptions(req.options), function (err) {
callback(req.id, err)
})
}
}
}
class Iterator {
constructor (db, id, seq, options, consumed, encode) {
options = cleanRangeOptions(options)
// Because we read ahead (and can seek) we must do the limiting
const limit = options.limit
options.limit = Infinity
this[kMode] = options.keys && options.values ? 'iterator' : options.keys ? 'keys' : 'values'
this[kIterator] = db[this[kMode]](options)
this[kLimit] = limit < 0 ? Infinity : limit
this[kEncode] = encode
this[kBusy] = false
this[kMaxItemLength] = 1
this[kEnded] = false
this[kClosed] = false
this[kDataMessage] = { id, data: [], seq }
this[kEndMessage] = { id, seq }
this[kHandleMany] = this[kHandleMany].bind(this)
this[kPendingSeek] = null
this.seq = seq
this.consumed = consumed
this.pendingAcks = 0
}
next (first) {
if (this[kBusy] || this[kClosed]) return
if (this[kEnded] || this.pendingAcks > 1) return
// If limited, don't read more than that minus what the guest has consumed
let size = this[kLimit] - this.consumed
if (first) {
// Only want 1 entry initially, for early termination use cases
size = Math.min(1, size)
this[kReadAhead] = false
} else {
// Fill the stream's internal buffer
const ws = this[kEncode]
const room = Math.max(1, ws.writableHighWaterMark - ws.writableLength)
size = Math.min(size, Math.max(16, Math.round(room / this[kMaxItemLength])))
this[kReadAhead] = true
}
this[kBusy] = true
if (size <= 0) {
process.nextTick(this[kHandleMany], null, [])
} else {
this[kIterator].nextv(size, this[kHandleMany])
}
}
seek (target, seq) {
if (this[kClosed]) {
// Ignore request
} else if (this[kBusy]) {
this[kPendingSeek] = [target, seq]
} else {
this[kPendingSeek] = null
this[kEnded] = false
this.seq = seq
this.pendingAcks = 0
if (target === limbo) {
this[kBusy] = true
process.nextTick(this[kHandleMany], null, [])
} else {
this[kIterator].seek(target, encodingOptions)
this.next(true)
}
}
}
[kHandleMany] (err, items) {
this[kBusy] = false
if (this[kClosed]) {
// Ignore result
} else if (this[kPendingSeek] !== null) {
this.seek(...this[kPendingSeek])
} else if (err) {
const data = {
id: this[kDataMessage].id,
error: errorCode(err),
seq: this.seq
}
this[kEncode].write(encodeMessage(data, output.iteratorError))
} else if (items.length === 0) {
this[kEnded] = true
this[kEndMessage].seq = this.seq
this[kEncode].write(encodeMessage(this[kEndMessage], output.iteratorEnd))
} else {
this[kDataMessage].seq = this.seq
if (this[kMode] === 'iterator') {
const data = this[kDataMessage].data = new Array(items.length * 2)
let n = 0
for (const entry of items) {
data[n++] = entry[0]
data[n++] = entry[1]
}
} else {
this[kDataMessage].data = items
}
const buf = encodeMessage(this[kDataMessage], output.iteratorData)
const estimatedItemLength = Math.ceil(buf.length / items.length)
this[kEncode].write(buf)
this[kMaxItemLength] = Math.max(this[kMaxItemLength], estimatedItemLength)
this.pendingAcks++
if (this[kReadAhead]) {
this.next(false)
}
}
}
close () {
if (this[kClosed]) return
this[kClosed] = true
this[kIterator].close(noop)
}
}
function encodeMessage (msg, tag) {
const encoding = output.encoding(tag)
const buf = Buffer.allocUnsafe(encoding.encodingLength(msg) + 1)
buf[0] = tag
encoding.encode(msg, buf, 1)
return buf
}
// Adjust one byte so that we land on the key after target
function nextTarget (target, reverse) {
if (!reverse) {
const copy = Buffer.allocUnsafe(target.length + 1)
target.copy(copy, 0, 0, target.length)
copy[target.length] = 0
return copy
} else if (target.length === 0) {
return limbo
} else if (target[target.length - 1] > 0) {
target[target.length - 1]--
return target
} else {
return target.slice(0, target.length - 1)
}
}
function errorCode (err) {
if (err == null) {
return undefined
} else if (typeof err.code === 'string' && err.code.startsWith('LEVEL_')) {
return err.code
} else {
return 'LEVEL_REMOTE_ERROR'
}
}
function cleanRangeOptions (options) {
const result = {}
for (const k in options) {
if (!hasOwnProperty.call(options, k)) continue
if (!rangeOptions.has(k) || options[k] != null) {
result[k] = options[k]
}
}
result.keyEncoding = 'buffer'
result.valueEncoding = 'buffer'
return result
}