Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@ var AbstractIterator = require('abstract-leveldown').AbstractIterator
var inherits = require('inherits')
var Codec = require('level-codec')
var EncodingError = require('level-errors').EncodingError
var rangeMethods = ['approximateSize', 'compactRange']

module.exports = DB.default = DB

function DB (db, opts) {
if (!(this instanceof DB)) return new DB(db, opts)
AbstractLevelDOWN.call(this, '')

var manifest = db.supports || {}
var additionalMethods = manifest.additionalMethods || {}

AbstractLevelDOWN.call(this, manifest)

this.supports.encodings = true
this.supports.additionalMethods = {}

rangeMethods.forEach(function (m) {
// TODO (future major): remove this fallback
var fallback = typeof db[m] === 'function'

if (additionalMethods[m] || fallback) {
this.supports.additionalMethods[m] = true

this[m] = function (start, end, opts, cb) {
start = this.codec.encodeKey(start, opts)
end = this.codec.encodeKey(end, opts)
return this.db[m](start, end, opts, cb)
}
}
}, this)

opts = opts || {}
if (typeof opts.keyEncoding === 'undefined') opts.keyEncoding = 'utf8'
Expand Down Expand Up @@ -84,12 +107,6 @@ DB.prototype._clear = function (opts, callback) {
this.db.clear(opts, callback)
}

DB.prototype.approximateSize = function (start, end, opts, cb) {
start = this.codec.encodeKey(start, opts)
end = this.codec.encodeKey(end, opts)
return this.db.approximateSize(start, end, opts, cb)
}

function Iterator (db, opts) {
AbstractIterator.call(this, db)
this.codec = db.codec
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepublishOnly": "npm run dependency-check"
},
"dependencies": {
"abstract-leveldown": "^6.1.1",
"abstract-leveldown": "^6.2.1",
"inherits": "^2.0.3",
"level-codec": "^9.0.0",
"level-errors": "^2.0.0"
Expand Down
61 changes: 56 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,17 +579,68 @@ test('iterator catches decoding error from valueEncoding', function (t) {
})
})

test('approximateSize() encodes start and end', function (t) {
t.plan(2)
test('proxies approximateSize() if it exists', function (t) {
t.is(typeof encdown({ approximateSize: noop }).approximateSize, 'function')
t.ok(encdown({ approximateSize: noop }).supports.additionalMethods.approximateSize)
t.is(encdown({}).approximateSize, undefined)
t.notOk(encdown({}).supports.additionalMethods.approximateSize)
t.end()
})

var down = {
test('proxies compactRange() if it exists', function (t) {
t.is(typeof encdown({ compactRange: noop }).compactRange, 'function')
t.ok(encdown({ compactRange: noop }).supports.additionalMethods.compactRange)
t.is(encdown({}).compactRange, undefined)
t.notOk(encdown({}).supports.additionalMethods.compactRange)
t.end()
})

test('encodes start and end of approximateSize()', function (t) {
var db = encdown({
approximateSize: function (start, end) {
t.is(start, '1')
t.is(end, '2')
t.end()
}
}
})

db.approximateSize(1, 2, noop)
})

test('encodes start and end of compactRange()', function (t) {
var db = encdown({
compactRange: function (start, end) {
t.is(start, '1')
t.is(end, '2')
t.end()
}
})

db.compactRange(1, 2, noop)
})

test('encodes start and end of approximateSize() with custom encoding', function (t) {
var db = encdown({
approximateSize: function (start, end) {
t.is(start, '"a"')
t.is(end, '"b"')
t.end()
}
})

db.approximateSize('a', 'b', { keyEncoding: 'json' }, noop)
})

test('encodes start and end of compactRange() with custom encoding', function (t) {
var db = encdown({
compactRange: function (start, end) {
t.is(start, '"a"')
t.is(end, '"b"')
t.end()
}
})

encdown(down).approximateSize(1, 2, noop)
db.compactRange('a', 'b', { keyEncoding: 'json' }, noop)
})

test('encodes seek target', function (t) {
Expand Down