-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
157 lines (142 loc) · 4.38 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
const toPull = require('stream-to-pull-stream')
const defer = require('pull-defer')
const pull = require('pull-stream')
const memdb = require('memdb')
const charwise = require('charwise')
const FlumeViewQuery = require('flumeview-query/inject')
const many = require('pull-many')
const { EventEmitter } = require('events')
const debug = require('debug')('kappa-view-pull-query')
module.exports = function KappaViewQuery (db, opts = {}) {
var events = new EventEmitter()
var {
indexes = [],
validator = (msg) => msg,
keyEncoding = charwise,
} = opts
var query
var view = {
maxBatch: opts.maxBatch || 100,
map: (msgs, next) => {
const ops = []
msgs.forEach((msg) => {
if (!validator(msg)) return
var msgId = `${msg.key}@${msg.seq}`
indexes.forEach((idx) => {
var indexKeys = getIndexValues(msg, idx.value)
if (indexKeys.length) {
ops.push({
type: 'put',
key: [idx.key, ...indexKeys],
value: msgId,
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 []
}
})
})
debug(`[INDEXING] ${JSON.stringify(ops)}`)
db.batch(ops, next)
},
indexed: (msgs) => {
msgs.forEach((msg) => events.emit('update', msg))
},
api: {
read: (core, _opts) => {
var __opts = view.api.explain(core, _opts)
var source = __opts.createStream(__opts)
return source
},
explain: (core, _opts) => {
query = query || FlumeViewQuery({
stream: (__opts) => {
var source = defer.source()
core.ready(() => {
source.resolve(
pull(
many(
core.feeds().map((feed) => (
pull(
toPull(feed.createReadStream(_opts)),
pull.map((value) => ({ value }))
)
))
)
)
)
})
return source
}
}, indexes.map((idx) => ({
key: idx.key,
value: idx.value,
exact: 'boolean' === typeof idx.exact ? idx.exact : false,
createStream: (_opts) => (
pull(
toPull(db.createReadStream(Object.assign(_opts, {
lte: [idx.key, ..._opts.lte],
gte: [idx.key, ..._opts.gte],
keyEncoding,
keys: true,
values: true
}))),
pull.asyncMap((msg, 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(err)
next(null, {
key: feed.key.toString('hex'),
seq,
value
})
})
})
)
)
})))
return query.explain(_opts)
},
add: (core, _opts) => {
if(!(opts && isFunction(opts.createStream) && Array.isArray(opts.index || opts.value))) {
throw new Error('kappa-view-pull-query.add: expected { index, createStream }')
}
opts.value = opts.index || opts.value
indexes.push(opts)
events.emit('add-index')
},
events
},
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'))
})
}
}
return view
}
function isFunction (variable) {
return typeof variable === 'function'
}