Skip to content

Commit

Permalink
use level-codec
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Mar 24, 2015
1 parent f1a5b95 commit 187711c
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 249 deletions.
4 changes: 2 additions & 2 deletions lib/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Batch (levelup, codec) {
}

Batch.prototype.put = function (key_, value_, options) {
options = getOptions(this._levelup, options)
options = getOptions(options)

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

Batch.prototype.del = function (key_, options) {
options = getOptions(this._levelup, options)
options = getOptions(options)

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

Expand Down
83 changes: 0 additions & 83 deletions lib/codec.js

This file was deleted.

72 changes: 0 additions & 72 deletions lib/encodings.js

This file was deleted.

82 changes: 26 additions & 56 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var EventEmitter = require('events').EventEmitter
, ReadStream = require('./read-stream')
, util = require('./util')
, Batch = require('./batch')
, codec = require('./codec')
, Codec = require('level-codec')

, getOptions = util.getOptions
, defaultOptions = util.defaultOptions
Expand Down Expand Up @@ -75,9 +75,9 @@ function LevelUP (location, options, callback) {
throw error
}

options = getOptions(this, options)
options = getOptions(options)
this.options = extend(defaultOptions, options)
this._codec = options.codec || codec
this._codec = new Codec(this.options)
this._status = 'new'
// set this.location as enumerable but not configurable or writable
prr(this, 'location', location, 'e')
Expand Down Expand Up @@ -205,10 +205,10 @@ LevelUP.prototype.get = function (key_, options, callback) {
return readError(this
, 'get() requires key and callback arguments', callback)

options = util.getOptions(this, options)
options = util.getOptions(options)
key = this._codec.encodeKey(key_, options)

options.asBuffer = this._codec.isValueAsBuffer(options)
options.asBuffer = this._codec.valueAsBuffer(options)

this.db.get(key, options, function (err, value) {
if (err) {
Expand Down Expand Up @@ -244,7 +244,7 @@ LevelUP.prototype.put = function (key_, value_, options, callback) {
if (maybeError(this, options, callback))
return

options = getOptions(this, options)
options = getOptions(options)
key = this._codec.encodeKey(key_, options)
value = this._codec.encodeValue(value_, options)

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

options = getOptions(this, options)
options = getOptions(options)
key = this._codec.encodeKey(key_, options)

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

if (!arguments.length)
return new Batch(this, codec)
return new Batch(this, this._codec)

callback = getCallback(options, callback)

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

options = getOptions(this, options)
keyEnc = options.keyEncoding
valueEnc = options.valueEncoding

arr = arr_.map(function (e) {
if (e.type === undefined || e.key === undefined)
return {}

// inherit encoding
var kEnc = e.keyEncoding || keyEnc
, vEnc = e.valueEncoding || e.encoding || valueEnc
, o

// If we're not dealing with plain utf8 strings or plain
// Buffers then we have to do some work on the array to
// encode the keys and/or values. This includes JSON types.

if (kEnc != 'utf8' && kEnc != 'binary'
|| vEnc != 'utf8' && vEnc != 'binary') {
o = {
type: e.type
, key: self._codec.encodeKey(e.key, options, e)
}

if (e.value !== undefined)
o.value = self._codec.encodeValue(e.value, options, e)

return o
} else {
return e
}
})
options = getOptions(options)
arr = self._codec.encodeBatch(arr_, options)

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

callback = getCallback(options, callback)

options = getOptions(options, callback)
options = getOptions(options)

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

start = this._codec.encodeKey(start_, this.options)
end = this._codec.encodeKey(end_, this.options)
start = this._codec.encodeKey(start_, options)
end = this._codec.encodeKey(end_, options)

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

if (isDefined(options.start))
options.start = this._codec.encodeKey(options.start, options)
options.start = this._codec.encodeKey(options.start, [options])
if (isDefined(options.end))
options.end = this._codec.encodeKey(options.end, options)
options.end = this._codec.encodeKey(options.end, [options])
if (isDefined(options.gte))
options.gte = this._codec.encodeKey(options.gte, options)
options.gte = this._codec.encodeKey(options.gte, [options])
if (isDefined(options.gt))
options.gt = this._codec.encodeKey(options.gt, options)
options.gt = this._codec.encodeKey(options.gt, [options])
if (isDefined(options.lte))
options.lte = this._codec.encodeKey(options.lte, options)
options.lte = this._codec.encodeKey(options.lte, [options])
if (isDefined(options.lt))
options.lt = this._codec.encodeKey(options.lt, options)
options.lt = this._codec.encodeKey(options.lt, [options])
if ('number' !== typeof options.limit)
options.limit = -1

options.keyAsBuffer = this._codec.isKeyAsBuffer(options)
options.valueAsBuffer = this._codec.isValueAsBuffer(options)
options.keyAsBuffer = this._codec.keyAsBuffer([options])
options.valueAsBuffer = this._codec.valueAsBuffer([options])

var makeData = options.keys && options.values
? function (key, value) {
return {
key: self._codec.decodeKey(key, options)
, value: self._codec.decodeValue(value, options)
key: self._codec.decodeKey(key, [options])
, value: self._codec.decodeValue(value, [options])
}
}
: options.keys
? function (key) {
return self._codec.decodeKey(key, options)
return self._codec.decodeKey(key, [options])
}
: options.values
? function (_, value) {
return self._codec.decodeValue(value, options)
return self._codec.decodeValue(value, [options])
}
: function () {}

Expand Down Expand Up @@ -449,7 +419,7 @@ function utilStatic (name) {
}

module.exports = LevelUP
module.exports.errors = require('./errors');
module.exports.errors = require('./errors')
// DEPRECATED: prefer accessing LevelDOWN for this: require('leveldown').destroy()
module.exports.destroy = utilStatic('destroy')
// DEPRECATED: prefer accessing LevelDOWN for this: require('leveldown').repair()
Expand Down
22 changes: 6 additions & 16 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

var extend = require('xtend')
, LevelUPError = require('./errors').LevelUPError
, encodings = require('./encodings')
, defaultOptions = {
createIfMissing : true
, errorIfExists : false
Expand All @@ -16,22 +15,13 @@ var extend = require('xtend')
}

, leveldown
, encodingOpts = (function () {
var eo = {}
for(var e in encodings)
eo[e] = {valueEncoding: encodings[e]}
return eo
}())

function getOptions (levelup, options) {
var s = typeof options == 'string' // just an encoding
if (!s && options && options.encoding && !options.valueEncoding)
options.valueEncoding = options.encoding
return extend(
(levelup && levelup.options) || {}
, s ? encodingOpts[options] || encodingOpts[defaultOptions.valueEncoding]
: options
)
function getOptions (options) {
if (typeof options == 'string')
options = { valueEncoding: options }
if (typeof options != 'object')
options = {}
return options
}

function getLevelDOWN () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"deferred-leveldown": "~0.2.0",
"errno": "~0.1.1",
"level-codec": "^5.0.0",
"prr": "~0.0.0",
"readable-stream": "~1.0.26",
"semver": "~2.3.1",
Expand Down
Loading

0 comments on commit 187711c

Please sign in to comment.