Skip to content

Commit

Permalink
Add db.getMany(keys)
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Sep 25, 2021
1 parent fe7ae36 commit d6664c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"abstract-leveldown": "github:Level/abstract-leveldown#get-many",
"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 d6664c2

Please sign in to comment.