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

Commit

Permalink
Add db.getMany(keys)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Sep 28, 2021
1 parent e75a500 commit fb1c5a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
18 changes: 13 additions & 5 deletions leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ function SubDown (db, prefix, opts) {
// Inherit manifest from parent db
...manifest,

// We support this on the levelup interface, but not on the
// abstract-leveldown interface.
deferredOpen: false,

// Disable unsupported features
getMany: false,
keyIterator: false,
valueIterator: false,
iteratorNextv: false,
Expand All @@ -144,13 +147,12 @@ SubDown.prototype.type = 'subleveldown'

// TODO: remove _open() once abstract-leveldown supports deferredOpen,
// because that means we can always do operations on this.leveldown.
// Alternatively have the sublevel follow the open state of this.db.
SubDown.prototype._open = function (opts, cb) {
// TODO: make _isOpening public in levelup or add a method like
// ready(cb) which waits for - but does not initiate - a state change.
// TODO: start using db.status (added to levelup recently) in a next major.
const m = typeof this.db.isOpening === 'function' ? 'isOpening' : '_isOpening'

const onopen = () => {
// TODO: start using db.status (added to levelup recently) in a next major.
if (!this.db.isOpen()) return cb(new Error('Parent database is not open'))
if (this.leveldown.status !== 'open') return cb(new Error('Inner database is not open'))

Expand Down Expand Up @@ -181,6 +183,12 @@ SubDown.prototype._get = function (key, opts, cb) {
this.leveldown.get(concat(this.prefix, key), opts, cb)
}

SubDown.prototype._getMany = function (keys, opts, cb) {
// maybeError is not necessary here, abstract-leveldown does that
keys = keys.map(key => concat(this.prefix, key))
this.leveldown.getMany(keys, opts, cb)
}

SubDown.prototype._del = function (key, opts, cb) {
if (maybeError(this.leveldown, cb)) return
this.leveldown.del(concat(this.prefix, key), opts, cb)
Expand Down Expand Up @@ -235,7 +243,7 @@ function maybeError (leveldown, callback) {
if (leveldown.status !== 'open') {
// Same error message as levelup
// TODO: use require('level-errors').ReadError
nextTick(callback, new Error('Database is not open'))
;(leveldown._nextTick || nextTick)(callback, new Error('Database is not open'))
return true
}

Expand Down
10 changes: 7 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ function runSuite (factory) {
createIfMissing: false,
errorIfExists: false,

// Opt-in to new clear() tests
clear: true
// Opt-in to new tests
clear: true,
getMany: true
})
}

Expand Down Expand Up @@ -56,6 +57,7 @@ runSuite(function factory () {
down.supports.deferredOpen = true
down.isOpen = function () { return this.status === 'open' }
down.isOpening = function () { return this.status === 'opening' }
down._isOperational = () => this.status === 'opening'
down.once = emitter.once.bind(emitter)
down.open(function (err) {
if (err) throw err
Expand All @@ -78,10 +80,12 @@ suite({
createIfMissing: false,
errorIfExists: false,

// Opt-in to new clear() tests
// Opt-in to new tests
clear: true,
getMany: true,

// Adapt for levelup
deferredOpen: true,
promises: true,
status: false,
serialize: false,
Expand Down

0 comments on commit fb1c5a4

Please sign in to comment.