-
Notifications
You must be signed in to change notification settings - Fork 4
/
inject.js
62 lines (56 loc) · 1.66 KB
/
inject.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
'use strict'
var pull = require('pull-stream')
var Explain = require('./explain')
var Filter = require('./filter')
var isArray = Array.isArray
function isFunction (f) { return 'function' == typeof f }
function clone (obj) {
var o = {}
for(var k in obj)
o[k] = obj[k]
return o
}
//sorted index.
//split this into TWO modules. flumeview-links and flumeview-query
module.exports = function (log, indexes) {
if(!Array.isArray(indexes)) throw new Error('indexes should be an array')
//answer this query by reading the entire log.
//not efficient, but still returns the correct answer
function fullScan (log, opts) {
return log.stream({
values: true, seqs: false,
//TODO test coverage for live/old - the tests arn't right for live when the log starts as empty
old: (opts.old !== false),
live: (opts.live === true || opts.old === false),
reverse: opts.reverse
})
}
function createFilter(source, opts) {
return pull(
source,
isArray(opts.query) ? mfr(opts.query) : pull.through(),
opts.limit && pull.take(opts.limit)
)
}
var view
return view = {
read: function (opts) {
var _opts = view.explain(opts)
return Filter(_opts.createStream(_opts), opts)
},
explain: Explain(indexes, function (opts) {
opts.seqs = false; opts.values = true
return log.stream(opts)
}),
add: function (opts) {
if(!(
opts &&
isFunction(opts.createStream) &&
isArray(opts.index || opts.value)
))
throw new Error('flumeview-query.add: expected {index, createStream}')
opts.value = opts.index || opts.value
indexes.push(opts)
}
}
}