diff --git a/index.js b/index.js index 1d244bd..42a859e 100644 --- a/index.js +++ b/index.js @@ -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') @@ -22,7 +23,8 @@ function Level (location, opts) { bufferKeys: support.bufferKeys(indexedDB), snapshots: true, permanence: true, - clear: true + clear: true, + getMany: true }) opts = opts || {} @@ -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 diff --git a/package.json b/package.json index cbcf27e..8a06a90 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.js b/test/index.js index 230e25d..d8d9516 100644 --- a/test/index.js +++ b/test/index.js @@ -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