Skip to content

Commit

Permalink
Merge pull request #506 from Level/next-serialization2
Browse files Browse the repository at this point in the history
Update next to latest abstract-leveldown
  • Loading branch information
vweevers authored Sep 23, 2018
2 parents 7b0f6d9 + 1e363aa commit 1fd0bdb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
13 changes: 2 additions & 11 deletions iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ function Iterator (db, options) {

util.inherits(Iterator, AbstractIterator)

Iterator.prototype.seek = function (target) {
if (this._ended) {
throw new Error('cannot call seek() after end()')
}
if (this._nexting) {
throw new Error('cannot call seek() before next() has completed')
}
if (typeof target !== 'string' && !Buffer.isBuffer(target)) {
throw new Error('seek() requires a string or buffer key')
}
Iterator.prototype._seek = function (target) {
if (target.length === 0) {
throw new Error('cannot seek() to an empty key')
throw new Error('cannot seek() to an empty target')
}

this.cache = null
Expand Down
8 changes: 8 additions & 0 deletions leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ LevelDOWN.prototype._close = function (callback) {
this.binding.close(callback)
}

LevelDOWN.prototype._serializeKey = function (key) {
return Buffer.isBuffer(key) ? key : String(key)
}

LevelDOWN.prototype._serializeValue = function (value) {
return Buffer.isBuffer(value) ? value : String(value)
}

LevelDOWN.prototype._put = function (key, value, options, callback) {
this.binding.put(key, value, options, callback)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"main": "leveldown.js",
"dependencies": {
"abstract-leveldown": "level/abstract-leveldown#51af868",
"abstract-leveldown": "github:Level/abstract-leveldown#0f42686",
"bindings": "~1.3.0",
"fast-future": "~1.0.2",
"nan": "~2.11.0",
Expand Down
41 changes: 23 additions & 18 deletions test/iterator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ const iota = require('iota-array')
const lexi = require('lexicographic-integer')
const util = require('util')

make('iterator throws if key is not a string or buffer', function (db, t, done) {
var keys = [null, undefined, 1, true, false]
var pending = keys.length
make('iterator#seek throws if target is empty', function (db, t, done) {
var targets = ['', Buffer.alloc(0), []]
var pending = targets.length

keys.forEach(function (key) {
var error
targets.forEach(function (target) {
var ite = db.iterator()
var error

try {
ite.seek(key)
} catch (e) {
error = e
ite.seek(target)
} catch (err) {
error = err.message
}

t.ok(error, 'had error from seek()')
t.is(error, 'cannot seek() to an empty target', 'got error')
ite.end(end)
})

Expand Down Expand Up @@ -86,8 +86,8 @@ make('reverse seek from invalid range', function (db, t, done) {
ite.seek('zzz')
ite.next(function (err, key, value) {
t.error(err, 'no error')
t.same(key.toString(), 'two', 'end of iterator')
t.same(value.toString(), '2', 'end of iterator')
t.same(key.toString(), 'two')
t.same(value.toString(), '2')
ite.end(done)
})
})
Expand Down Expand Up @@ -132,17 +132,20 @@ make('iterator optimized for seek', function (db, t, done) {

make('iterator seek before next has completed', function (db, t, done) {
var ite = db.iterator()
var error

ite.next(function (err, key, value) {
t.error(err, 'no error from next()')
ite.end(done)
})
var error

try {
ite.seek('two')
} catch (e) {
error = e
} catch (err) {
error = err.message
}
t.ok(error, 'had error from seek() before next() has completed')

t.is(error, 'cannot call seek() before next() has completed', 'got error')
})

make('close db with open iterator', function (db, t, done) {
Expand Down Expand Up @@ -170,12 +173,14 @@ make('iterator seek after end', function (db, t, done) {
ite.end(function (err) {
t.error(err, 'no error from end()')
var error

try {
ite.seek('two')
} catch (e) {
error = e
} catch (err) {
error = err.message
}
t.ok(error, 'had error from seek() after end()')

t.is(error, 'cannot call seek() after end()', 'got error')
done()
})
})
Expand Down

0 comments on commit 1fd0bdb

Please sign in to comment.