Skip to content

Commit

Permalink
Add db.getMany(keys) (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Sep 28, 2021
1 parent 2ccda5d commit f5a3ca3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
30 changes: 29 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = Level

const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const inherits = require('inherits')
const parallel = require('run-parallel-limit')
const Iterator = require('./iterator')
const serialize = require('./util/serialize')
const deserialize = require('./util/deserialize')
Expand All @@ -22,7 +23,8 @@ function Level (location, opts) {
bufferKeys: support.bufferKeys(indexedDB),
snapshots: true,
permanence: true,
clear: true
clear: true,
getMany: true
})

opts = opts || {}
Expand Down Expand Up @@ -102,6 +104,32 @@ Level.prototype._get = function (key, options, callback) {
})
}

Level.prototype._getMany = function (keys, options, callback) {
const asBuffer = options.asBuffer
const store = this.store('readonly')
const tasks = keys.map((key) => (next) => {
let request

try {
request = store.get(key)
} catch (err) {
return next(err)
}

request.onsuccess = () => {
const value = request.result
next(null, value === undefined ? value : deserialize(value, asBuffer))
}

request.onerror = (ev) => {
ev.stopPropagation()
next(request.error)
}
})

parallel(tasks, 16, callback)
}

Level.prototype._del = function (key, options, callback) {
const store = this.store('readwrite')
let req
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"sauce-labs.svg"
],
"dependencies": {
"abstract-leveldown": "^7.0.0",
"abstract-leveldown": "^7.2.0",
"buffer": "^6.0.3",
"inherits": "^2.0.3",
"ltgt": "^2.1.2"
"ltgt": "^2.1.2",
"run-parallel-limit": "^1.1.0"
},
"devDependencies": {
"airtap": "^4.0.1",
Expand Down
5 changes: 3 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const testCommon = suite.common({
// Support of buffer keys depends on environment
bufferKeys: leveljs(uuid()).supports.bufferKeys,

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

// Test abstract-leveldown compliance
Expand Down

0 comments on commit f5a3ca3

Please sign in to comment.