-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (69 loc) · 2.01 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
var LevelView = require('flumeview-level')
var pull = require('pull-stream')
var Pushable = require('pull-pushable')
var paramap = require('pull-paramap')
function isString (s) {
return 'string' === typeof s
}
module.exports = function (version, length, map) {
length = length || 3
var match = /\w+/ig
var create = LevelView(version, function (data, seq) {
var words = {}
var text = map(data)
if(text) {
for(var word; word = match.exec(text); ) {
word = word[0]
if(word && word.length >= length && !words[word]) words[word] = true
}
return Object.keys(words).sort().map(function (e) {
return [e.toLowerCase(), -seq]
})
}
return []
})
return function (log, name) {
var index = create(log, name)
index.methods = index.methods || {}
index.methods.query = 'source'
index.query = function (opts) {
if(isString(opts)) {
opts = {query: opts}
}
var pushable = Pushable(function () {
while(aborts.length) aborts.shift()()
})
var terms = opts.query.trim().split(/[^\w]+/).filter(Boolean).filter(function (e) {
return e.length >= length
})
var mask = 0, found = {}, ended = 0
var aborts = terms.map(function (term, i) {
mask |= 1 << i
var sink
pull(
index.read({
gt: [term, null], lte: [term+'~', undefined],
keys: true, values: false
}),
sink = pull.drain(function (data) {
if(found[data.seq] == mask) return
if((found[data.seq] |= (1 << i)) == mask)
pushable.push(+data.seq)
}, function (end) {
if(++ ended== terms.length)
pushable.end()
})
)
return function () {
sink.abort(function () {})
}
})
return pull(
pushable,
opts.values !== false ? paramap(log.get): pull.through(),
opts.limit ? pull.take(opts.limit) : pull.through()
)
}
return index
}
}