Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db.getMany(keys) #107

Merged
merged 1 commit into from
Oct 1, 2021
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
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"test": "test"
},
"dependencies": {
"abstract-leveldown": "^7.1.0",
"encoding-down": "^7.0.0",
"abstract-leveldown": "^7.2.0",
"encoding-down": "^7.1.0",
"inherits": "^2.0.3",
"level-option-wrap": "^1.1.0",
"levelup": "^5.0.1",
"levelup": "^5.1.0",
"reachdown": "^1.1.0"
},
"devDependencies": {
Expand All @@ -33,7 +33,7 @@
"hallmark": "^3.1.0",
"level-community": "^3.0.0",
"level-concat-iterator": "^3.0.0",
"memdown": "^6.0.0",
"memdown": "^6.1.0",
"nyc": "^15.1.0",
"standard": "^16.0.3",
"tape": "^5.0.1"
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