Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 187711c

Browse files
committed
use level-codec
1 parent f1a5b95 commit 187711c

File tree

11 files changed

+55
-249
lines changed

11 files changed

+55
-249
lines changed

lib/batch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Batch (levelup, codec) {
1818
}
1919

2020
Batch.prototype.put = function (key_, value_, options) {
21-
options = getOptions(this._levelup, options)
21+
options = getOptions(options)
2222

2323
var key = this._codec.encodeKey(key_, options)
2424
, value = this._codec.encodeValue(value_, options)
@@ -34,7 +34,7 @@ Batch.prototype.put = function (key_, value_, options) {
3434
}
3535

3636
Batch.prototype.del = function (key_, options) {
37-
options = getOptions(this._levelup, options)
37+
options = getOptions(options)
3838

3939
var key = this._codec.encodeKey(key_, options)
4040

lib/codec.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

lib/encodings.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

lib/levelup.js

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var EventEmitter = require('events').EventEmitter
2020
, ReadStream = require('./read-stream')
2121
, util = require('./util')
2222
, Batch = require('./batch')
23-
, codec = require('./codec')
23+
, Codec = require('level-codec')
2424

2525
, getOptions = util.getOptions
2626
, defaultOptions = util.defaultOptions
@@ -75,9 +75,9 @@ function LevelUP (location, options, callback) {
7575
throw error
7676
}
7777

78-
options = getOptions(this, options)
78+
options = getOptions(options)
7979
this.options = extend(defaultOptions, options)
80-
this._codec = options.codec || codec
80+
this._codec = new Codec(this.options)
8181
this._status = 'new'
8282
// set this.location as enumerable but not configurable or writable
8383
prr(this, 'location', location, 'e')
@@ -205,10 +205,10 @@ LevelUP.prototype.get = function (key_, options, callback) {
205205
return readError(this
206206
, 'get() requires key and callback arguments', callback)
207207

208-
options = util.getOptions(this, options)
208+
options = util.getOptions(options)
209209
key = this._codec.encodeKey(key_, options)
210210

211-
options.asBuffer = this._codec.isValueAsBuffer(options)
211+
options.asBuffer = this._codec.valueAsBuffer(options)
212212

213213
this.db.get(key, options, function (err, value) {
214214
if (err) {
@@ -244,7 +244,7 @@ LevelUP.prototype.put = function (key_, value_, options, callback) {
244244
if (maybeError(this, options, callback))
245245
return
246246

247-
options = getOptions(this, options)
247+
options = getOptions(options)
248248
key = this._codec.encodeKey(key_, options)
249249
value = this._codec.encodeValue(value_, options)
250250

@@ -271,7 +271,7 @@ LevelUP.prototype.del = function (key_, options, callback) {
271271
if (maybeError(this, options, callback))
272272
return
273273

274-
options = getOptions(this, options)
274+
options = getOptions(options)
275275
key = this._codec.encodeKey(key_, options)
276276

277277
this.db.del(key, options, function (err) {
@@ -292,7 +292,7 @@ LevelUP.prototype.batch = function (arr_, options, callback) {
292292
, arr
293293

294294
if (!arguments.length)
295-
return new Batch(this, codec)
295+
return new Batch(this, this._codec)
296296

297297
callback = getCallback(options, callback)
298298

@@ -302,38 +302,8 @@ LevelUP.prototype.batch = function (arr_, options, callback) {
302302
if (maybeError(this, options, callback))
303303
return
304304

305-
options = getOptions(this, options)
306-
keyEnc = options.keyEncoding
307-
valueEnc = options.valueEncoding
308-
309-
arr = arr_.map(function (e) {
310-
if (e.type === undefined || e.key === undefined)
311-
return {}
312-
313-
// inherit encoding
314-
var kEnc = e.keyEncoding || keyEnc
315-
, vEnc = e.valueEncoding || e.encoding || valueEnc
316-
, o
317-
318-
// If we're not dealing with plain utf8 strings or plain
319-
// Buffers then we have to do some work on the array to
320-
// encode the keys and/or values. This includes JSON types.
321-
322-
if (kEnc != 'utf8' && kEnc != 'binary'
323-
|| vEnc != 'utf8' && vEnc != 'binary') {
324-
o = {
325-
type: e.type
326-
, key: self._codec.encodeKey(e.key, options, e)
327-
}
328-
329-
if (e.value !== undefined)
330-
o.value = self._codec.encodeValue(e.value, options, e)
331-
332-
return o
333-
} else {
334-
return e
335-
}
336-
})
305+
options = getOptions(options)
306+
arr = self._codec.encodeBatch(arr_, options)
337307

338308
this.db.batch(arr, options, function (err) {
339309
if (err) {
@@ -354,14 +324,14 @@ LevelUP.prototype.approximateSize = function (start_, end_, options, callback) {
354324

355325
callback = getCallback(options, callback)
356326

357-
options = getOptions(options, callback)
327+
options = getOptions(options)
358328

359329
if (start_ === null || start_ === undefined
360330
|| end_ === null || end_ === undefined || 'function' !== typeof callback)
361331
return readError(this, 'approximateSize() requires start, end and callback arguments', callback)
362332

363-
start = this._codec.encodeKey(start_, this.options)
364-
end = this._codec.encodeKey(end_, this.options)
333+
start = this._codec.encodeKey(start_, options)
334+
end = this._codec.encodeKey(end_, options)
365335

366336
this.db.approximateSize(start, end, function (err, size) {
367337
if (err) {
@@ -381,37 +351,37 @@ LevelUP.prototype.createReadStream = function (options) {
381351
options.valueEncoding = options.valueEncoding || options.encoding
382352

383353
if (isDefined(options.start))
384-
options.start = this._codec.encodeKey(options.start, options)
354+
options.start = this._codec.encodeKey(options.start, [options])
385355
if (isDefined(options.end))
386-
options.end = this._codec.encodeKey(options.end, options)
356+
options.end = this._codec.encodeKey(options.end, [options])
387357
if (isDefined(options.gte))
388-
options.gte = this._codec.encodeKey(options.gte, options)
358+
options.gte = this._codec.encodeKey(options.gte, [options])
389359
if (isDefined(options.gt))
390-
options.gt = this._codec.encodeKey(options.gt, options)
360+
options.gt = this._codec.encodeKey(options.gt, [options])
391361
if (isDefined(options.lte))
392-
options.lte = this._codec.encodeKey(options.lte, options)
362+
options.lte = this._codec.encodeKey(options.lte, [options])
393363
if (isDefined(options.lt))
394-
options.lt = this._codec.encodeKey(options.lt, options)
364+
options.lt = this._codec.encodeKey(options.lt, [options])
395365
if ('number' !== typeof options.limit)
396366
options.limit = -1
397367

398-
options.keyAsBuffer = this._codec.isKeyAsBuffer(options)
399-
options.valueAsBuffer = this._codec.isValueAsBuffer(options)
368+
options.keyAsBuffer = this._codec.keyAsBuffer([options])
369+
options.valueAsBuffer = this._codec.valueAsBuffer([options])
400370

401371
var makeData = options.keys && options.values
402372
? function (key, value) {
403373
return {
404-
key: self._codec.decodeKey(key, options)
405-
, value: self._codec.decodeValue(value, options)
374+
key: self._codec.decodeKey(key, [options])
375+
, value: self._codec.decodeValue(value, [options])
406376
}
407377
}
408378
: options.keys
409379
? function (key) {
410-
return self._codec.decodeKey(key, options)
380+
return self._codec.decodeKey(key, [options])
411381
}
412382
: options.values
413383
? function (_, value) {
414-
return self._codec.decodeValue(value, options)
384+
return self._codec.decodeValue(value, [options])
415385
}
416386
: function () {}
417387

@@ -449,7 +419,7 @@ function utilStatic (name) {
449419
}
450420

451421
module.exports = LevelUP
452-
module.exports.errors = require('./errors');
422+
module.exports.errors = require('./errors')
453423
// DEPRECATED: prefer accessing LevelDOWN for this: require('leveldown').destroy()
454424
module.exports.destroy = utilStatic('destroy')
455425
// DEPRECATED: prefer accessing LevelDOWN for this: require('leveldown').repair()

lib/util.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
var extend = require('xtend')
88
, LevelUPError = require('./errors').LevelUPError
9-
, encodings = require('./encodings')
109
, defaultOptions = {
1110
createIfMissing : true
1211
, errorIfExists : false
@@ -16,22 +15,13 @@ var extend = require('xtend')
1615
}
1716

1817
, leveldown
19-
, encodingOpts = (function () {
20-
var eo = {}
21-
for(var e in encodings)
22-
eo[e] = {valueEncoding: encodings[e]}
23-
return eo
24-
}())
2518

26-
function getOptions (levelup, options) {
27-
var s = typeof options == 'string' // just an encoding
28-
if (!s && options && options.encoding && !options.valueEncoding)
29-
options.valueEncoding = options.encoding
30-
return extend(
31-
(levelup && levelup.options) || {}
32-
, s ? encodingOpts[options] || encodingOpts[defaultOptions.valueEncoding]
33-
: options
34-
)
19+
function getOptions (options) {
20+
if (typeof options == 'string')
21+
options = { valueEncoding: options }
22+
if (typeof options != 'object')
23+
options = {}
24+
return options
3525
}
3626

3727
function getLevelDOWN () {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"deferred-leveldown": "~0.2.0",
3838
"errno": "~0.1.1",
39+
"level-codec": "^5.0.0",
3940
"prr": "~0.0.0",
4041
"readable-stream": "~1.0.26",
4142
"semver": "~2.3.1",

0 commit comments

Comments
 (0)