-
-
Notifications
You must be signed in to change notification settings - Fork 53
feat: map method with default _map fallback #376
Conversation
this needs more tests to confirm _map is working properly |
seems not all tests are ran within abstract-down's test script - I've added a test into self.js now to check the default loop-get fallback. Not sure how to make sure the |
@@ -427,6 +435,14 @@ Delete an entry. There are no default options but `options` will always be an ob | |||
|
|||
The default `_del()` invokes `callback` on a next tick. It must be overridden. | |||
|
|||
### `db._map(keys, options, callback)` | |||
|
|||
Map each `key` in the `keys` array to a `value`. The `options` object will always have the following properties: `asBuffer`. If _any_ key in `keys` does not exist, call the `callback` function with a `new Error('NotFound')`. Otherwise call `callback` with `null` as the first argument and the values array as the second. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any key in
keys
does not exist, call thecallback
function with anew Error('NotFound')
Not sure this is desirable. You wouldn't know which key wasn't found. With parallel writes, it's not unlikely that keys
will contain deleted keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the default fallback - but how would you want to deal with this? if it fails on the n'th item, then return an array with n-1 items and a non-null error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that we could yield an undefined
value. For example, given keys ['a', 'b', 'c']
and key b
does not exist, the result would be [value, undefined, value]
.
throw new Error('map() requires a callback argument') | ||
} | ||
|
||
if (Array.isArray(keys) === false || keys === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Array.isArray(keys) === false || keys === null) { | |
if (!Array.isArray(keys)) { |
The null-check is redundant.
|
||
const _keys = new Array(keys.length) | ||
|
||
for (const index in keys) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const result = new Array(count) | ||
let index = 0 | ||
|
||
if (count === 0) return this._nextTick(callback, null, []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do this in map()
instead?
Superseded by #381 |
As per Level/community#100
Adds new
map
method to serve as a "multi-get" / "batch-get" method for implementing 'downs.Adds protected overload-able
_map
method with default fallback to looping over each key and using_get
. i.e. non-breaking for non-implementing 'downs.