Skip to content

Commit bf3872b

Browse files
committed
Properly handle an empty array of keys for prefetch/getMany, #151
1 parent 1c16aea commit bf3872b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "lmdb",
33
"author": "Kris Zyp",
4-
"version": "2.2.5",
4+
"version": "2.2.6",
55
"description": "Simple, efficient, scalable, high-performance LMDB interface",
66
"license": "MIT",
77
"repository": {

read.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,15 @@ export function addReadMethods(LMDBStore, {
417417
// allowing a separate to absorb the potentially largest cost: hard page faults (and disk I/O).
418418
// And then we just do standard sync gets (to deserialized data) to fulfil the callback/promise
419419
// once the prefetch occurs
420+
let promise = callback ? undefined : new Promise(resolve => callback = (error, results) => resolve(results));
420421
this.prefetch(keys, () => {
421422
let results = new Array(keys.length);
422423
for (let i = 0, l = keys.length; i < l; i++) {
423424
results[i] = get.call(this, keys[i]);
424425
}
425426
callback(null, results);
426427
});
427-
return callback ? undefined : new Promise(resolve => callback = (error, results) => resolve(results));
428+
return promise;
428429
},
429430
getSharedBufferForGet(id) {
430431
let txn = (env.writeTxn || (readTxnRenewed ? readTxn : renewReadTxn()));
@@ -451,6 +452,15 @@ export function addReadMethods(LMDBStore, {
451452
return buffer.slice(lastOffset, lastOffset + this.lastSize);/*Uint8ArraySlice.call(buffer, lastOffset, lastOffset + this.lastSize)*/
452453
},
453454
prefetch(keys, callback) {
455+
if (!keys)
456+
throw new Error('An array of keys must be provided');
457+
if (!keys.length) {
458+
if (callback) {
459+
callback(null);
460+
return;
461+
} else
462+
return Promise.resolve();
463+
}
454464
let buffers = [];
455465
let startPosition;
456466
let bufferHolder = {};

test/index.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,8 @@ describe('lmdb-js', function() {
668668
let values = await db.getMany(keys);
669669
should.equal(values.length, 20);
670670
should.equal(values[3], value);
671+
values = await db.getMany([]);
672+
should.equal(values.length, 0);
671673
});
672674

673675
it('invalid key', async function() {

0 commit comments

Comments
 (0)