forked from dominictarr/level-sublevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub.js
276 lines (239 loc) · 6.98 KB
/
sub.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var ranges = require('string-range')
var fixRange = require('level-fix-range')
var xtend = require('xtend')
inherits(SubDB, EventEmitter)
function SubDB (db, prefix, options) {
if('string' === typeof options) {
console.error('db.sublevel(name, seperator<string>) is depreciated')
console.error('use db.sublevel(name, {sep: separator})) if you must')
options = {sep: options}
}
if(!(this instanceof SubDB)) return new SubDB(db, prefix, options)
if(!db) throw new Error('must provide db')
if(!prefix) throw new Error('must provide prefix')
options = options || {}
options.sep = options.sep || '\xff'
this._parent = db
this._options = options
this.options = options
this._prefix = prefix
this._root = root(this)
db.sublevels[prefix] = this
this.sublevels = {}
this.methods = {}
var self = this
this.hooks = {
pre: function () {
return self.pre.apply(self, arguments)
},
post: function () {
return self.post.apply(self, arguments)
}
}
}
var SDB = SubDB.prototype
SDB._key = function (key) {
var sep = this._options.sep
return sep
+ this._prefix
+ sep
+ key
}
SDB._getOptsAndCb = function (opts, cb) {
if (typeof opts == 'function') {
cb = opts
opts = {}
}
return { opts: xtend(opts, this._options), cb: cb }
}
SDB.sublevel = function (prefix, options) {
if(this.sublevels[prefix])
return this.sublevels[prefix]
return new SubDB(this, prefix, options || this._options)
}
SDB.put = function (key, value, opts, cb) {
var res = this._getOptsAndCb(opts, cb)
this._root.put(this.prefix(key), value, res.opts, res.cb)
}
SDB.get = function (key, opts, cb) {
var res = this._getOptsAndCb(opts, cb)
this._root.get(this.prefix(key), res.opts, res.cb)
}
SDB.del = function (key, opts, cb) {
var res = this._getOptsAndCb(opts, cb)
this._root.del(this.prefix(key), res.opts, res.cb)
}
SDB.batch = function (changes, opts, cb) {
if(!Array.isArray(changes))
throw new Error('batch must be passed an Array')
var self = this,
res = this._getOptsAndCb(opts, cb)
changes.forEach(function (ch) {
//OH YEAH, WE NEED TO VALIDATE THAT UPDATING THIS KEY/PREFIX IS ALLOWED
if('string' === typeof ch.prefix)
ch.key = ch.prefix + ch.key
else
ch.key = (ch.prefix || self).prefix(ch.key)
if(ch.prefix) ch.prefix = null
})
this._root.batch(changes, res.opts, res.cb)
}
SDB._getKeyEncoding = function () {
if(this.options.keyEncoding)
return this.options.keyEncoding
if(this._parent && this._parent._getKeyEncoding)
return this._parent._getKeyEncoding()
}
SDB._getValueEncoding = function () {
if(this.options.valueEncoding)
return this.options.valueEncoding
if(this._parent && this._parent._getValueEncoding)
return this._parent._getValueEncoding()
}
SDB.prefix = function (key) {
var sep = this._options.sep
return this._parent.prefix() + sep + this._prefix + sep + (key || '')
}
SDB.keyStream =
SDB.createKeyStream = function (opts) {
opts = opts || {}
opts.keys = true
opts.values = false
return this.createReadStream(opts)
}
SDB.valueStream =
SDB.createValueStream = function (opts) {
opts = opts || {}
opts.keys = false
opts.values = true
opts.keys = false
return this.createReadStream(opts)
}
function selectivelyMerge(_opts, opts) {
[ 'valueEncoding'
, 'encoding'
, 'keyEncoding'
, 'reverse'
, 'values'
, 'keys'
, 'limit'
, 'fillCache'
]
.forEach(function (k) {
if (opts.hasOwnProperty(k)) _opts[k] = opts[k]
})
}
SDB.readStream =
SDB.createReadStream = function (opts) {
opts = opts || {}
var r = root(this)
var p = this.prefix()
var _opts = ranges.prefix(opts, p)
selectivelyMerge(_opts, xtend(opts, this._options))
var s = r.createReadStream(_opts)
if(_opts.values === false) {
var read = s.read
if (read) {
s.read = function (size) {
var val = read.call(this, size)
if (val) val = val.substring(p.length)
return val
}
} else {
var emit = s.emit
s.emit = function (event, val) {
if(event === 'data') {
emit.call(this, 'data', val.substring(p.length))
} else
emit.call(this, event, val)
}
}
return s
} else if(_opts.keys === false)
return s
else {
var read = s.read
if (read) {
s.read = function (size) {
var d = read.call(this, size)
if (d) d.key = d.key.substring(p.length)
return d
}
} else {
s.on('data', function (d) {
//mutate the prefix!
//this doesn't work for createKeyStream admittedly.
d.key = d.key.substring(p.length)
})
}
return s
}
}
SDB.writeStream =
SDB.createWriteStream = function () {
var r = root(this)
var p = this.prefix()
var ws = r.createWriteStream.apply(r, arguments)
var write = ws.write
var encoding = this._options.encoding
var valueEncoding = this._options.valueEncoding
var keyEncoding = this._options.keyEncoding
// slight optimization, if no encoding was specified at all,
// which will be the case most times, make write not check at all
var nocheck = !encoding && !valueEncoding && !keyEncoding
ws.write = nocheck
? function (data) {
data.key = p + data.key
return write.call(ws, data)
}
: function (data) {
data.key = p + data.key
// not merging all options here since this happens on every write and things could get slowed down
// at this point we only consider encoding important to propagate
if (encoding && typeof data.encoding === 'undefined')
data.encoding = encoding
if (valueEncoding && typeof data.valueEncoding === 'undefined')
data.valueEncoding = valueEncoding
if (keyEncoding && typeof data.keyEncoding === 'undefined')
data.keyEncoding = keyEncoding
return write.call(ws, data)
}
return ws
}
SDB.approximateSize = function () {
var r = root(db)
return r.approximateSize.apply(r, arguments)
}
function root(db) {
if(!db._parent) return db
return root(db._parent)
}
SDB.pre = function (range, hook) {
if(!hook) hook = range, range = null
range = ranges.prefix(range, this.prefix(), this._options.sep)
var r = root(this._parent)
var p = this.prefix()
return r.hooks.pre(fixRange(range), function (ch, add, batch) {
hook({
key: ch.key.substring(p.length),
value: ch.value,
type: ch.type
}, function (ch, _p) {
//maybe remove the second add arg now
//that op can have prefix?
add(ch, ch.prefix ? _p : (_p || p))
}, batch)
})
}
SDB.post = function (range, hook) {
if(!hook) hook = range, range = null
var r = root(this._parent)
var p = this.prefix()
range = ranges.prefix(range, p, this._options.sep)
return r.hooks.post(fixRange(range), function (data) {
hook({key: data.key.substring(p.length), value: data.value, type: data.type})
})
}
var exports = module.exports = SubDB