-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindexes.js
66 lines (58 loc) · 1.68 KB
/
indexes.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
var FlumeViewLevel = require('flumeview-level')
var u = require('./util')
var pull = require('pull-stream')
function clone (obj) {
var o = {}
for(var k in obj)
o[k] = obj[k]
return o
}
module.exports = function (version, opts) {
var filter = opts.filter || function () { return true }
var map = opts.map || function (item) { return item }
var exact = opts.exact !== false
var read
return function (log, name) {
var indexes = opts.indexes.map(function (e) {
return {
key: e.key,
value: e.value,
exact: 'boolean' === typeof e.exact ? e.exact : exact,
createStream: function (opts) {
opts = clone(opts)
opts.lte.unshift(e.key)
opts.gte.unshift(e.key)
opts.keys = true; opts.values = true
if(!(opts.lte[0] == e.key && opts.gte[0] == e.key))
throw new Error('index key missing from options')
return pull(
read(opts),
pull.map(function (data) {
return data.sync ? data : map(data.value)
})
)
}
}
})
var view = FlumeViewLevel(version || 3, function (data, seq) {
data = map(data)
if (!filter(data)) return []
var A = []
indexes.forEach(function (index) {
var a = [index.key]
for(var i = 0; i < index.value.length; i++) {
var key = index.value[i]
if(!u.has(key, data)) return []
a.push(u.get(key, data))
}
if(!index.exact) a.push(seq);
A.push(a)
})
return A
})(log, name)
read = view.read
view.methods.indexes = 'sync'
view.indexes = function () { return indexes }
return view
}
}