-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Proposal: Add db.has(key)
and db.hasMany(keys)
#106
Comments
I think currently the most performant way is this: let exists = false
const it = db.iterator({
values: false,
gte: key,
lte: key,
limit: 1,
})
for await (const _ of it) {
exists = true
} It's not a nice way though. |
@vweevers might be able to tell you more about any ongoing work on this |
Until more folks express the need for this feature (give it a thumbs up if you do) this can be implemented in userland. Similar to It might also help to set the If your values are small (and thus fast to copy from the underlying store) I reckon that For |
Thanks for this alternative idea. I'd think, though, that in most cases this would also fetch the value from the underlying store, because the const it = db.iterator({
values: false,
gte: key,
lte: key,
limit: 1,
})
return !it.next().done; so even though we don't read |
With values: false it should only read keys. You are right that the iteration mechanics can be changed |
Ah yes, sorry, I oversaw the |
Re: keys vs values, see also Level/abstract-leveldown#380 which proposes key-only and value-only iterators. |
Just looked at https://github.com/juliangruber/level-exists and realize that it really is my
|
I can answer question 1: in the past, iterators weren't directly exposed (and async iterators weren't a thing) - level was all about streams |
@vweevers I understand that Anyway, looking forward to other community members' feedback. Maybe I'm not alone in need of this feature :) |
Yes, it would undoubtedly perform better. Needs upvotes because new features come with a maintenance cost. Other than that I have no objections; your proposal aligns well with existing features (I like the symmetry with |
db.Has(key)
and db.HasMany(keys)
db.has(key)
and db.hasMany(keys)
Not at this time but pull requests welcome 👍 Feel free to open an issue there |
I love working with the Level stack. However, I miss one feature: a way to check any db whether it has a value for a key, without actually retrieving the value. Right now, as a workaround, I extend
LevelUp
instances withhas
andhasMany
keys like so (TypeScript):However, this is not as efficient as it could be, since the underlying storage can probably implement
has
much more efficient than first actually retrieving the value, just for it to be abandoned in this workaround.This could even be implemented in
levelup
first usind above code, iflevelup
itself cannot find functionshas
orhasMany
on theabstract-leveldown
passed to it. This way, leveldown implementations can one by one addhas
/hasMany
whilelevelup
can already provide the functionality in a backwards-compatible fashion.The text was updated successfully, but these errors were encountered: