Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add db.getMany(keys) #102

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
80 changes: 80 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@

const test = require('tape')
const encdown = require('..')
const suite = require('abstract-leveldown/test')
const memdown = require('memdown')
const Buffer = require('buffer').Buffer
const hasOwnProperty = Object.prototype.hasOwnProperty
const noop = function () {}

const testCommon = suite.common({
test: test,
factory: function () {
return encdown(memdown())
},

encodings: true,

// Unsupported features
createIfMissing: false,
errorIfExists: false,

// Opt-in to new tests
clear: true,
getMany: true
})

// Test abstract-leveldown compliance
suite(testCommon)

// Custom tests
test('opens and closes the underlying db', function (t) {
const _db = {
open: function (opts, cb) {
Expand Down Expand Up @@ -176,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)

Expand Down