Skip to content

Commit

Permalink
keep codec on the db as db._codec, also, support lt, lte, gt, gte
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Jun 16, 2014
1 parent 897c020 commit 3eab049
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
22 changes: 11 additions & 11 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ LevelUP.prototype.get = function (key_, options, callback) {
}

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

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

this.db.get(key, options, function (err, value) {
if (err) {
Expand All @@ -201,7 +201,7 @@ LevelUP.prototype.get = function (key_, options, callback) {
}
if (callback) {
try {
value = codec.decodeValue(value, options)
value = self._codec.decodeValue(value, options)
} catch (e) {
return callback(new EncodingError(e))
}
Expand Down Expand Up @@ -235,8 +235,8 @@ LevelUP.prototype.put = function (key_, value_, options, callback) {
}

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

this.db.put(key, value, options, function (err) {
if (err) {
Expand Down Expand Up @@ -272,7 +272,7 @@ LevelUP.prototype.del = function (key_, options, callback) {
}

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

this.db.del(key, options, function (err) {
if (err) {
Expand Down Expand Up @@ -333,11 +333,11 @@ LevelUP.prototype.batch = function (arr_, options, callback) {
|| vEnc != 'utf8' && vEnc != 'binary') {
o = {
type: e.type
, key: codec.encodeKey(e.key, options, e)
, key: self._codec.encodeKey(e.key, options, e)
}

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

return o
} else {
Expand Down Expand Up @@ -372,8 +372,8 @@ LevelUP.prototype.approximateSize = function (start_, end_, callback) {
)
}

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

if (!this._isOpening() && !this.isOpen()) {
return dispatchError(
Expand Down Expand Up @@ -402,7 +402,6 @@ LevelUP.prototype.createReadStream = function (options) {
, function (options) {
return self.db.iterator(options)
}
, codec
)
}

Expand All @@ -418,6 +417,7 @@ LevelUP.prototype.createValueStream = function (options) {

LevelUP.prototype.writeStream =
LevelUP.prototype.createWriteStream = function (options) {
//XXX is extend needed here?
return new WriteStream(extend(options), this)
}

Expand Down
16 changes: 12 additions & 4 deletions lib/read-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,33 @@ var Readable = require('readable-stream').Readable
}
, makeNoData = function () { return null }

function ReadStream (options, db, iteratorFactory, codec) {
function ReadStream (options, db, iteratorFactory) {
if (!(this instanceof ReadStream))
return new ReadStream(options, db, iteratorFactory, codec)

Readable.call(this, { objectMode: true, highWaterMark: options.highWaterMark })

// purely to keep `db` around until we're done so it's not GCed if the user doesn't keep a ref
this._db = db
this._codec = codec
this._codec = options.codec || db._codec

options = this._options = extend(defaultOptions, options)

this._keyEncoding = options.keyEncoding || options.encoding
this._valueEncoding = options.valueEncoding || options.encoding

if (typeof this._options.start != 'undefined')
this._options.start = codec.encodeKey(this._options.start, this._options)
this._options.start = this._codec.encodeKey(this._options.start, this._options)
if (typeof this._options.end != 'undefined')
this._options.end = codec.encodeKey(this._options.end, this._options)
this._options.end = this._codec.encodeKey(this._options.end, this._options)
if (typeof this._options.gte != 'undefined')
this._options.gte = this._codec.encodeKey(this._options.gte, this._options)
if (typeof this._options.gt != 'undefined')
this._options.gt = this._codec.encodeKey(this._options.gt, this._options)
if (typeof this._options.lte != 'undefined')
this._options.lte = this._codec.encodeKey(this._options.lte, this._options)
if (typeof this._options.lt != 'undefined')
this._options.lt = this._codec.encodeKey(this._options.lt, this._options)
if (typeof this._options.limit != 'number')
this._options.limit = -1

Expand Down

0 comments on commit 3eab049

Please sign in to comment.