Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 4038a30

Browse files
authored
Add db.getMany(keys) (#102)
Ref Level/community#101 Also adds the abstract-leveldown test suite. This works thanks to the `encodings` option that was added to the test suite (originally for `levelup` compatibility testing) and changes the expected outputs (from buffers by default to strings by default).
1 parent 48b3342 commit 4038a30

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

Diff for: index.js

+26
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ DB.prototype._get = function (key, opts, cb) {
8484
})
8585
}
8686

87+
DB.prototype._getMany = function (keys, opts, cb) {
88+
keys = keys.map((key) => this.codec.encodeKey(key, opts))
89+
opts.asBuffer = this.codec.valueAsBuffer(opts)
90+
91+
this.db.getMany(keys, opts, (err, values) => {
92+
if (err) return cb(err)
93+
94+
const decoded = new Array(values.length)
95+
96+
for (let i = 0; i < values.length; i++) {
97+
if (values[i] === undefined) {
98+
decoded[i] = undefined
99+
continue
100+
}
101+
102+
try {
103+
decoded[i] = this.codec.decodeValue(values[i], opts)
104+
} catch (err) {
105+
return cb(new EncodingError(err))
106+
}
107+
}
108+
109+
cb(null, decoded)
110+
})
111+
}
112+
87113
DB.prototype._del = function (key, opts, cb) {
88114
key = this.codec.encodeKey(key, opts)
89115
this.db.del(key, opts, cb)

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"UPGRADING.md"
2121
],
2222
"dependencies": {
23-
"abstract-leveldown": "^7.0.0",
23+
"abstract-leveldown": "^7.2.0",
2424
"inherits": "^2.0.3",
2525
"level-codec": "^10.0.0",
2626
"level-errors": "^3.0.0"
@@ -31,7 +31,7 @@
3131
"dependency-check": "^3.3.0",
3232
"hallmark": "^3.1.0",
3333
"level-community": "^3.0.0",
34-
"memdown": "^6.0.0",
34+
"memdown": "^6.1.0",
3535
"nyc": "^15.1.0",
3636
"standard": "^16.0.3",
3737
"tape": "^5.0.1"

Diff for: test/index.js

+80
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@
22

33
const test = require('tape')
44
const encdown = require('..')
5+
const suite = require('abstract-leveldown/test')
56
const memdown = require('memdown')
67
const Buffer = require('buffer').Buffer
78
const hasOwnProperty = Object.prototype.hasOwnProperty
89
const noop = function () {}
910

11+
const testCommon = suite.common({
12+
test: test,
13+
factory: function () {
14+
return encdown(memdown())
15+
},
16+
17+
encodings: true,
18+
19+
// Unsupported features
20+
createIfMissing: false,
21+
errorIfExists: false,
22+
23+
// Opt-in to new tests
24+
clear: true,
25+
getMany: true
26+
})
27+
28+
// Test abstract-leveldown compliance
29+
suite(testCommon)
30+
31+
// Custom tests
1032
test('opens and closes the underlying db', function (t) {
1133
const _db = {
1234
open: function (opts, cb) {
@@ -176,6 +198,64 @@ test('get() forwards error from underlying store', function (t) {
176198
})
177199
})
178200

201+
test('getMany() skips decoding not-found values', function (t) {
202+
t.plan(6)
203+
204+
const valueEncoding = {
205+
encode: JSON.stringify,
206+
decode (value) {
207+
t.is(value, JSON.stringify(data))
208+
return JSON.parse(value)
209+
},
210+
buffer: false,
211+
type: 'test'
212+
}
213+
214+
const data = { beep: 'boop' }
215+
const db = encdown(memdown(), { valueEncoding })
216+
217+
db.open(function (err) {
218+
t.error(err, 'no open() error')
219+
220+
db.put('foo', data, function (err) {
221+
t.error(err, 'no put() error')
222+
223+
db.getMany(['foo', 'bar'], function (err, values) {
224+
t.error(err, 'no getMany() error')
225+
t.same(values, [data, undefined])
226+
227+
db.close(t.error.bind(t))
228+
})
229+
})
230+
})
231+
})
232+
233+
test('getMany() forwards decode error', function (t) {
234+
const valueEncoding = {
235+
encode: (v) => v,
236+
decode: (v) => { throw new Error('decode error') },
237+
buffer: false,
238+
type: 'test'
239+
}
240+
241+
const db = encdown(memdown(), { valueEncoding })
242+
243+
db.open(function (err) {
244+
t.error(err, 'no open() error')
245+
246+
db.put('foo', 'bar', function (err) {
247+
t.error(err, 'no put() error')
248+
249+
db.getMany(['foo'], function (err, values) {
250+
t.is(err && err.message, 'decode error')
251+
t.is(values, undefined)
252+
253+
db.close(t.end.bind(t))
254+
})
255+
})
256+
})
257+
})
258+
179259
test('_del() encodes key', function (t) {
180260
t.plan(1)
181261

0 commit comments

Comments
 (0)