-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
69 lines (61 loc) · 1.62 KB
/
index.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
var Writable = require('readable-stream').Writable
var pump = require('pump')
module.exports = createIndex
function createIndex (ldb, opts) {
return {
maxBatch: 100 || opts.maxBatch,
map: function (msgs, next) {//opts.map,
var ops = []
msgs.forEach(function (msg) {
var res = opts.map(msg)
if (res && Array.isArray(res)) {
res = res.map(function (r) {
return {
type: 'put',
key: r[0],
value: r[1]
}
})
ops.push.apply(ops, res)
}
})
ldb.batch(ops, next)
},
api: opts.api,
indexed: function (msgs) {
if (opts.indexed) opts.indexed(msgs)
},
storeState: function (state, cb) {
state = state.toString('base64')
ldb.put('state', state, cb)
},
fetchState: function (cb) {
ldb.get('state', function (err, state) {
if (err && err.notFound) cb()
else if (err) cb(err)
else cb(null, Buffer.from(state, 'base64'))
})
},
clearIndex: opts.clearIndex || function (cb) {
var batch = []
var maxSize = 5000
pump(ldb.createKeyStream(), new Writable({
objectMode: true,
write: function (key, enc, next) {
batch.push({ type: 'del', key })
if (batch.length >= maxSize) {
ldb.batch(batch, next)
} else next()
},
final: function (next) {
if (batch.length > 0) ldb.batch(batch, next)
else next()
}
}), ondone)
function ondone (err) {
if (err) cb(err)
else cb()
}
}
}
}