-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
140 lines (110 loc) · 3.11 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
var path = require('path')
var levelup = require('levelup')
var searchIndex = require('search-index')
var _ = require('lodash')
var mkdirp = require('mkdirp')
var jsonpath = require('jsonpath-plus')
var preprocess = require('./preprocess/preprocess.js')
var Cursor = require('./cursor.js')
function Yuno (opts, cb) {
if (!(this instanceof Yuno)) return new Yuno(opts, cb)
requiredOpts(opts, ['keyField', 'indexMap'], cb)
var self = this
function ready () {
if (cb) cb(null, self)
// TODO: events, self.emit('ready')
}
var docstoreOpts = opts.docstore || {
keyEncoding: 'string',
valueEncoding: 'json'
}
mkdirp.sync(opts.location)
this.docstorePath = path.join(opts.location, 'docstore')
this.docstore = levelup(this.docstorePath, docstoreOpts)
this.indexPath = path.join(opts.location, 'index')
var indexOpts = _.defaults(opts, {
indexPath: this.indexPath,
deletable: false,
fieldedSearch: false,
fieldsToStore: ['tokens'],
nGramLength: 1
})
searchIndex(indexOpts, (err, si) => {
if (err) return cb(err)
self.index = si
ready()
})
this.preprocessor = preprocess(opts)
this.keyField = opts.keyField || 'id'
}
Yuno.prototype.getKey = function (doc) {
return jsonpath({ json: doc, path: this.keyField })[0]
}
Yuno.prototype.putOp = function (doc) {
return { type: 'put', key: this.getKey(doc), value: doc }
}
// docs: array of documents to add
// opts: options for adding
Yuno.prototype.add = function (docs, opts, cb) {
var self = this
if (_.isFunction(opts)) cb = opts
if (_.isPlainObject(docs)) docs = [docs]
var errs = []
var docb = _.after(2, function () {
cb(errs.length > 0 ? errs[0] : null, docs.length)
})
var done = function (err) {
if (err) errs.push(err)
docb()
}
this.docstore.batch(docs.map((d) => {
return { type: 'put', key: '' + self.getKey(d), value: JSON.stringify(d) }
}), done)
this.index.add(docs.map((d) => {
return { id: self.getKey(d), tokens: self.preprocessor.process(d) }
}), done)
// process the docs for search indexing
}
Yuno.prototype.get = function (key, cb) {
this.docstore.get(key, cb)
}
Yuno.prototype.search = function (query, opts, cb) {
if (_.isFunction(opts)) {
cb = opts
opts = null
}
var cursor = Cursor(query, this, opts)
cursor.first(cb)
return cursor
}
Yuno.prototype.del = function (keys, cb) {
var self = this
if (!(_.isArray(keys))) keys = [keys]
if (_.isPlainObject(keys[0])) keys = keys.map((doc) => { self.getKey(doc) })
var errs = []
var done = _.after(2, function () {
cb(errs.length > 0 ? errs[0] : null)
})
this.docstore.batch(keys.map((key) => {
return { type: 'del', key: key }
}), done)
this.index.del(keys.map((key) => {
return { id: key }
}), done)
}
Yuno.prototype.close = function (cb) {
var errs = []
var done = _.after(2, function () {
cb(errs.length > 0 ? errs[0] : null)
})
this.docstore.close(done)
this.index.close(done)
}
function requiredOpts (opts, keys, cb) {
keys.forEach((key) => {
if (!opts[key]) {
cb(new Error(key + ' option is required'))
}
})
}
module.exports = Yuno