forked from Frando/kappa-view-kv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (129 loc) · 3.74 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
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
var umkv = require('unordered-materialized-kv')
var EventEmitter = require('events').EventEmitter
var through = require('through2')
module.exports = KV
function KV (db, mapFn, opts) {
var kv = umkv(db)
var events = new EventEmitter()
opts = opts || {}
var idx = {
maxBatch: opts.maxBatch || 100,
map: function (msgs, next) {
var allOps = []
var pending = msgs.length + 1
for (var i = 0; i < msgs.length; i++) {
var msg = msgs[i]
mapFn(msg, function (err, ops) {
if (!ops) ops = []
done(err, ops, msg)
})
}
done(null, [])
function done (err, ops, msg) {
if (err) {
pending = Infinity
return next(err)
}
allOps.push.apply(allOps, ops)
if (!--pending) kv.batch(allOps, next)
}
},
indexed: function (msgs) {
for (var i = 0; i < msgs.length; i++) {
mapFn(msgs[i], function (err, ops) {
if (err || !ops || !ops.length) return
events.emit('update!' + ops[0].key, msgs[i])
events.emit('update', ops[0].key, msgs[i])
})
}
},
api: {
get: function (core, key, cb) {
const self = this
this.ready(function () {
kv.get(key, function (err, ids) {
if (err) return cb(err)
var res = []
var pending = ids.length + 1
for (var i = 0; i < ids.length; i++) {
var id = ids[i]
var [ key, sequence ] = id.split('@')
var feed = opts.getFeed(key)
var seq = Number(sequence)
;(function (feed, seq) {
feed.get(seq, function (err, value) {
if (err) return done(err)
done(null, {
key: feed.key.toString('hex'),
seq: seq,
value: value
})
})
})(feed, seq)
}
done()
function done (err, msg) {
if (err) {
pending = Infinity
return cb(err)
}
if (msg) res.push(msg)
if (!--pending) cb(null, res)
}
})
})
},
createReadStream: function (core) {
const flow = this
var t = through.obj(function (entry, _, next) {
var self = this
var pending = 1
var res = entry.value.split(',').forEach(function (value) {
pending++
var [ key, sequence ] = value.split('@')
var feed = opts.getFeed(key)
if (feed) {
var seq = Number(sequence)
feed.get(seq, function (err, val) {
if (!err) {
self.push({
key: entry.key.substring(2),
value: {
key: feed.key.toString('hex'),
seq: seq,
value: val
}
})
}
done()
})
}
})
done()
function done () {
if (!--pending) next()
}
})
db.createReadStream({gt: 'k!!', lt: 'k!~'}).pipe(t)
return t
},
onUpdateKey: function (core, key, cb) {
events.on('update!' + key, cb)
},
onUpdate: function (core, cb) {
events.on('update', cb)
}
},
storeState: function (state, cb) {
db.put('state', Buffer.from(state), cb)
},
fetchState: function (cb) {
db.get('state', function (err, state) {
if (err && err.notFound) cb()
else if (err) cb(err)
else cb(null, Buffer.from(state))
})
},
}
return idx
}