From f5a3ca31061491d767ec270e9e55a5765df25f8b Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Tue, 28 Sep 2021 23:35:15 +0200 Subject: [PATCH] Add `db.getMany(keys)` (#214) Ref https://github.com/Level/community/issues/101 --- index.js | 30 +++++++++++++++++++++++++++++- package.json | 5 +++-- test/index.js | 5 +++-- 3 files changed, 35 insertions(+), 5 deletions(-) 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 5b48867..d0bf9fe 100644 --- a/package.json +++ b/package.json @@ -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", 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