-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (127 loc) · 4.09 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
const through = require('through2')
const memdb = require('memdb')
const charwise = require('charwise')
const { EventEmitter } = require('events')
const debug = require('debug')('kappa-view-query')
const liveStream = require('level-live-stream')
const Explain = require('./explain')
const Filter = require('./filter')
const { isFunction } = require('./util')
module.exports = function KappaViewQuery (db = memdb(), opts = {}) {
const events = new EventEmitter()
const {
indexes = [],
validator = (msg) => msg,
keyEncoding = charwise
} = opts
const view = {
maxBatch: opts.maxBatch || 100,
map: (msgs, next) => {
var ops = []
msgs.forEach((msg) => {
msg = validator(msg)
if (!msg) return
indexes.forEach((idx) => {
var indexKeys = getIndexValues(msg, idx.value)
if (indexKeys.length) {
ops.push({
type: 'put',
key: [idx.key, ...indexKeys, msg.seq, msg.key],
value: [msg.key, msg.seq].join('@'),
keyEncoding,
})
}
function getIndexValues (msg, value) {
var child = value[0]
if (Array.isArray(child)) {
return value
.map((val) => getIndexValues(msg, val))
.reduce((acc, arr) => [...acc, ...arr], [])
.filter(Boolean)
} else if (typeof child === 'string') {
return [value.reduce((obj, val) => obj[val], msg)]
.filter(Boolean)
} else return []
}
})
})
db.batch(ops, next)
},
indexed: (msgs) => {
debug(`indexing ${JSON.stringify(msgs, null, 2)}`)
msgs.forEach((msg) => {
events.emit('update', msg)
})
},
api: {
read: (core, _opts) => {
var __opts = view.api.explain(core, _opts)
var source = __opts.createStream(__opts)
return Filter(source, _opts)
},
explain: (core, _opts) => {
var explain = Explain(indexes.map((idx) => Object.assign(idx, {
exact: typeof idx.exact === 'boolean' ? idx.exact : false,
createStream: (__opts) => {
var thru = through.obj(function (msg, enc, next) {
if (msg.sync) {
this.push(msg)
return next()
}
var msgId = msg.value
var [ feedId, sequence ] = msgId.split('@')
var feed = core._logs.feed(feedId)
var seq = Number(sequence)
feed.get(seq, (err, value) => {
if (err) return next()
var msg = validator({
key: feed.key.toString('hex'),
seq,
value
})
if (!msg) return next()
this.push(msg)
next()
})
})
var streamOpts = Object.assign(__opts, {
lte: [idx.key, ...__opts.lte],
gte: [idx.key, ...__opts.gte],
keyEncoding,
keys: true,
values: true
})
var stream = __opts.live
? liveStream(db, streamOpts)
: db.createReadStream(streamOpts)
stream.pipe(thru)
return thru
}
})))
return explain(_opts)
},
add: (core, _opts) => {
var isValid = _opts && isFunction(_opts.createStream) && Array.isArray(_opts.index || _opts.value)
if(!isValid) throw new Error('kappa-view-query.add: expected opts { index, createStream }')
_opts.value = _opts.index || _opts.value
indexes.push(_opts)
},
onUpdate: (core, cb) => {
events.on('update', cb)
},
storeState: (state, cb) => {
state = state.toString('base64')
db.put('state', state, cb)
},
fetchState: (cb) => {
db.get('state', function (err, state) {
if (err && err.notFound) cb()
else if (err) cb(err)
else cb(null, Buffer.from(state, 'base64'))
})
},
events
}
}
return view
}