-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
198 lines (171 loc) · 5.05 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var extend = require('xtend')
var hook = require('level-hookdown')
var existy = require('existy')
var Transform = require('stream').Transform || require('readable-stream').Transform
var isArray = Array.isArray
module.exports = AutoIndex
function puts (opr) {
return (
opr.type === 'put' || (!existy(opr.type) && existy(opr.value) && existy(opr.value))
)
}
function existyKeys (operation) {
return existy(operation.key)
}
function empty (v) {
return !!v
}
function AutoIndex (db, idb, reduce, opts) {
if (!opts) opts = {}
var hdb = !db.prehooks || !isArray(db.prehooks) ? hook(db) : db
var multiKey = opts.multi
if (typeof reduce !== 'function') {
throw new Error('Reduce argument must be a function')
}
function index (operation, cb) {
var key
var keyBatch
if (operation.type === 'put') {
key = reduce(operation.value)
if (key && multiKey) {
if (!Array.isArray(key)) throw new Error('Reducer must return an array of keys for a multiKey index')
keyBatch = key.filter(empty).map(
function (k) {
return { key: k, value: operation.key, type: 'put' }
}
)
return idb.batch(keyBatch, cb)
} else if (key) {
return idb.put(key, operation.key, cb)
}
return process.nextTick(cb)
} else if (operation.type === 'del') {
db.get(operation.key, function (err, value) {
if (!err || err.type === 'NotFoundError') {
key = reduce(value)
if (key && multiKey) {
if (!Array.isArray(key)) throw new Error('Reducer must return an array of keys for a multiKey index')
keyBatch = key.filter(empty).map(
function (k) {
return { key: k, type: 'del' }
}
)
return idb.batch(keyBatch, cb)
} else if (key) {
return idb.del(key, key, cb)
}
return cb()
} else if (err) {
cb(err)
} else {
cb()
}
})
} else if (operation.type === 'batch') {
// todo handle dels
var idxBatch = operation.array.filter(puts).map(function (opr) {
return extend(opr, { key: reduce(opr.value), value: opr.key })
})
if (multiKey) {
idxBatch = idxBatch.reduce(function (flattened, opr) {
if (!opr.key) return flattened
if (!Array.isArray(opr.key)) throw new Error('Reducer must return an array of keys for a multiKey index')
opr.key.filter(empty).forEach(function (k) {
flattened.push(extend(opr, { key: k }))
})
return flattened
}, [])
}
idb.batch(idxBatch.filter(existyKeys), cb)
}
}
hdb.prehooks.push(index)
var secondary = {}
secondary.db = hdb
secondary.idb = idb
secondary.manifest = {
methods: {
get: { type: 'async' },
del: { type: 'async' },
createValueStream: { type: 'readable' },
createKeyStream: { type: 'readable' },
createReadStream: { type: 'readable' }
}
}
secondary.get = op('get')
secondary.del = op('del')
function op (type) {
return function (key, opts, fn) {
if (typeof opts === 'function') {
fn = opts
opts = {}
}
idb.get(key, function (err, value) {
if (err) return fn(err)
db[type](value, opts, fn)
})
}
}
secondary.createValueStream = function (opts) {
if (!opts) opts = {}
opts.keys = false
return secondary.createReadStream(opts)
}
secondary.createKeyStream = function (opts) {
if (!opts) opts = {}
opts.values = false
return secondary.createReadStream(opts)
}
secondary.createReadStream = function (opts) {
opts = opts || {}
var tr = Transform({ objectMode: true })
tr._transform = function (chunk, enc, done) {
var dbKey = chunk.value
var idbKey = chunk.key
if (opts.values === false) {
done(null, dbKey)
return
}
db.get(dbKey, function (err, value) {
if (err && err.type === 'NotFoundError') {
idb.del(idbKey, done)
} else if (err) {
done(err)
} else {
emit()
}
function emit () {
if (opts.keys === false) {
done(null, value)
} else {
done(null, {
key: dbKey,
value: value
})
}
}
})
}
var opts2 = extend({}, opts)
opts2.keys = opts2.values = true
idb.createReadStream(opts2).pipe(tr)
return tr
}
return secondary
}
module.exports.keyReducer = keyReducer
function keyReducer (reducerString) {
function keyRdc (value) {
return value[reducerString]
}
return keyRdc
}
module.exports.multiKeyReducer = multiKeyReducer
function multiKeyReducer (multiFieldName, primaryKeyFieldName) {
return function multiKeyrdc (document) {
if (!document || !document[multiFieldName] || !Array.isArray(document[multiFieldName])) return
return document[multiFieldName].map(function (tag) {
return [tag, document[primaryKeyFieldName]].join('!')
})
}
}