-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
268 lines (243 loc) · 7.63 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
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
'use strict'
var pull = require('pull-stream')
var Level = require('level')
var charwise = require('charwise')
var Write = require('pull-write')
var pl = require('pull-level')
var Obv = require('obz')
var path = require('path')
var Paramap = require('pull-paramap')
var ltgt = require('ltgt')
var explain = require('explain-error')
const noop = () => {};
module.exports = function (version, map) {
return function (log, name) {
var dir = path.dirname(log.filename)
var db, writer
var META = '\x00'
var since = Obv()
var closed, outdated
function create () {
closed = false
if (!log.filename) {
throw new Error(
'flumeview-level can only be used with a log that provides a directory'
)
}
return Level(path.join(dir, name), {
keyEncoding: charwise,
valueEncoding: 'json'
})
}
function close (cb) {
if (typeof cb !== 'function') {
cb = noop
}
closed = true
// todo: move this bit into pull-write
if (outdated) {
db.close(cb)
} else if (writer) {
writer.abort(function () {
db.close((err) => {
cb(err)
})
})
} else if (!db) {
cb()
} else {
since.once(function () {
db.close(cb)
})
}
}
function destroy (cb) {
// FlumeDB restarts the stream as soon as the stream is cancelled, so the
// rebuild happens before `writer.abort()` calls back. This means that we
// must run `since.set(-1)` before aborting the stream.
since.set(-1)
// The `clear()` method must run before the stream is closed, otherwise
// you can have a situation where you:
//
// 1. Flumeview-Level closes the stream
// 2. FlumeDB restarts the stream
// 3. Flumeview-Level processes a message
// 4. Flumeview-Level runs `db.clear()` and deletes that message.
db.clear(cb)
}
if (closed) return
db = create()
db.get(META, { keyEncoding: 'utf8' }, function (err, value) {
if (err) since.set(-1)
else if (value.version === version) since.set(value.since)
else {
// version has changed, wipe db and start over.
outdated = true
destroy()
}
})
return {
since: since,
methods: { get: 'async', read: 'source' },
createSink: function (cb) {
return (writer = Write(
function (batch, cb) {
if (closed) {
return cb(new Error('database closed while index was building'))
}
db.batch(batch, function (err) {
if (err) return cb(err)
since.set(batch[0].value.since)
// callback to anyone waiting for this point.
cb()
})
},
function reduce (batch, data) {
if (data.sync) return batch
var seq = data.seq
if (!batch) {
batch = [
{
key: META,
value: { version: version, since: seq },
valueEncoding: 'json',
keyEncoding: 'utf8',
type: 'put'
}
]
}
// map must return an array (like flatmap) with zero or more values
var indexed = map(data.value, data.seq)
batch = batch.concat(
indexed.map(function (key) {
return { key: key, value: seq, type: 'put' }
})
)
batch[0].value.since = Math.max(batch[0].value.since, seq)
return batch
},
512,
cb
))
},
get: function (key, cb) {
// wait until the log has been processed up to the current point.
db.get(key, function (err, seq) {
if (err && err.name === 'NotFoundError') return cb(err)
if (err) {
return cb(
explain(err, 'flumeview-level.get: key not found:' + key)
)
}
log.get(seq, function (err, value) {
if (err) {
if (err.code === 'flumelog:deleted') {
return db.del(key, (delErr) => {
if (delErr) {
return cb(
explain(
delErr,
'when trying to delete:' +
key +
'at since:' +
log.since.value
)
)
}
cb(err, null, seq)
})
}
return cb(
explain(
err,
'flumeview-level.get: index for: ' +
key +
'pointed at:' +
seq +
'but log error'
)
)
} else {
cb(null, value, seq)
}
})
})
},
read: function (opts) {
var keys = opts.keys !== false
var values = opts.values !== false
var seqs = opts.seqs !== false
opts.keys = true
opts.values = true
// TODO: preserve whatever the user passed in on opts...
var lower = ltgt.lowerBound(opts)
if (lower == null) opts.gt = null
function format (key, seq, value) {
return keys && values && seqs
? { key: key, seq: seq, value: value }
: keys && values
? { key: key, value: value }
: keys && seqs
? { key: key, seq: seq }
: seqs && values
? { seq: seq, value: value }
: keys
? key
: seqs
? seq
: value
}
return pull(
pl.read(db, opts),
pull.filter(function (op) {
// this is an ugly hack! ); but it stops the index metadata appearing in the live stream
return op.key !== META
}),
values
? pull(
Paramap(function (data, cb) {
if (data.sync) return cb(null, data)
if (data.type === 'del') return cb(null, null)
log.get(data.value, function (err, value) {
if (err) {
if (err.code === 'flumelog:deleted') {
return db.del(data.key, (delErr) => {
if (delErr) {
return cb(
explain(
err,
'when trying to delete:' +
data.key +
'at since:' +
log.since.value
)
)
}
cb(null, null)
})
}
cb(
explain(
err,
'when trying to retrive:' +
data.key +
'at since:' +
log.since.value
)
)
} else cb(null, format(data.key, data.value, value))
})
}),
pull.filter()
)
: pull.map(function (data) {
return format(data.key, data.value, null)
})
)
},
close,
destroy
// put, del, batch - leave these out for now, since the indexes just map.
}
}
}