-
Notifications
You must be signed in to change notification settings - Fork 59
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 Database::get_or_put
: insert if value does not exist, otherwise return previous value
#252
Conversation
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 like what you've done. I am going to accept your work (mostly) as is.
However, I am wondering if I don't want to write a system similar to the HashMap::entry
method, at some point 🤔 What do you think?
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.
So style issues but I prefer this way, like you convert before matching 😄
This could probably be done using LMDB's cursor API. Looking at the source, there is an optimization in case a cursor is already positioned on the correct data item for an insert. Thus, an mdb_cursor_get(&mut cursor, &mut key, &mut data, MDB_SET);
if data.is_none() {
// ... encode value into `data` ...
mdb_cursor_put(&mut cursor, &mut key, &mut data);
} |
You are absolutely right, and I already worked on a cursor wrapper in the cursor.rs file, It can maybe be improved 🤔 |
Pull Request
Related issue
N/A
Based on #251
What does this PR do?
Database::get_or_put
method which uses theMDB_NOOVERWRITE
flag to simultaneously attempt to insert a key/value pair, or get the previous value if it already exists.which can now be written as (with one fewer lookups):
One drawback of the above is that it would serialize/allocate the
new_value
even if it would not end up being inserted. This can be avoided using theget_or_put_reserved*
methods, mirroring theput_reserved*
method.PR checklist
Please check if your PR fulfills the following requirements:
Thank you so much for contributing to Meilisearch!