From 7a9826c570e14599dfcb267d80333a7bef4eb7ed Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 25 Sep 2021 15:21:48 +0200 Subject: [PATCH] Add `db.getMany(keys)` Ref https://github.com/Level/community/issues/101 --- index.js | 26 ++++++++++++++++++++++ package.json | 4 ++-- test/index.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2c7824e..0c70184 100644 --- a/index.js +++ b/index.js @@ -84,6 +84,32 @@ DB.prototype._get = function (key, opts, cb) { }) } +DB.prototype._getMany = function (keys, opts, cb) { + keys = keys.map((key) => this.codec.encodeKey(key, opts)) + opts.asBuffer = this.codec.valueAsBuffer(opts) + + this.db.getMany(keys, opts, (err, values) => { + if (err) return cb(err) + + const decoded = new Array(values.length) + + for (let i = 0; i < values.length; i++) { + if (values[i] === undefined) { + decoded[i] = undefined + continue + } + + try { + decoded[i] = this.codec.decodeValue(values[i], opts) + } catch (err) { + return cb(new EncodingError(err)) + } + } + + cb(null, decoded) + }) +} + DB.prototype._del = function (key, opts, cb) { key = this.codec.encodeKey(key, opts) this.db.del(key, opts, cb) diff --git a/package.json b/package.json index 131cc1f..beb3ca4 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "UPGRADING.md" ], "dependencies": { - "abstract-leveldown": "^7.0.0", + "abstract-leveldown": "^7.2.0", "inherits": "^2.0.3", "level-codec": "^10.0.0", "level-errors": "^3.0.0" @@ -31,7 +31,7 @@ "dependency-check": "^3.3.0", "hallmark": "^3.1.0", "level-community": "^3.0.0", - "memdown": "^6.0.0", + "memdown": "^6.1.0", "nyc": "^15.1.0", "standard": "^16.0.3", "tape": "^5.0.1" diff --git a/test/index.js b/test/index.js index dc0b370..ad2e617 100644 --- a/test/index.js +++ b/test/index.js @@ -21,7 +21,8 @@ const testCommon = suite.common({ errorIfExists: false, // Opt-in to new tests - clear: true + clear: true, + getMany: true }) // Test abstract-leveldown compliance @@ -197,6 +198,64 @@ test('get() forwards error from underlying store', function (t) { }) }) +test('getMany() skips decoding not-found values', function (t) { + t.plan(6) + + const valueEncoding = { + encode: JSON.stringify, + decode (value) { + t.is(value, JSON.stringify(data)) + return JSON.parse(value) + }, + buffer: false, + type: 'test' + } + + const data = { beep: 'boop' } + const db = encdown(memdown(), { valueEncoding }) + + db.open(function (err) { + t.error(err, 'no open() error') + + db.put('foo', data, function (err) { + t.error(err, 'no put() error') + + db.getMany(['foo', 'bar'], function (err, values) { + t.error(err, 'no getMany() error') + t.same(values, [data, undefined]) + + db.close(t.error.bind(t)) + }) + }) + }) +}) + +test('getMany() forwards decode error', function (t) { + const valueEncoding = { + encode: (v) => v, + decode: (v) => { throw new Error('decode error') }, + buffer: false, + type: 'test' + } + + const db = encdown(memdown(), { valueEncoding }) + + db.open(function (err) { + t.error(err, 'no open() error') + + db.put('foo', 'bar', function (err) { + t.error(err, 'no put() error') + + db.getMany(['foo'], function (err, values) { + t.is(err && err.message, 'decode error') + t.is(values, undefined) + + db.close(t.end.bind(t)) + }) + }) + }) +}) + test('_del() encodes key', function (t) { t.plan(1)