-
Notifications
You must be signed in to change notification settings - Fork 646
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
Fix cursor automatically moving to next position when delete and put are in the same TXN #611
Conversation
…n one txn Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
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.
The change looks good.
…tem from a node Refer to boltdb/bolt#357 Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
Actually the fix isn't enough. See the following two scenariosAssuming we already inserted the following key/value pairs in the same TXN.
The example below starts deleting from the third item (with index 2), so the number of remaining key/value pair should be 2, but actually it isn't. The reason is it skips one item each time after deleting one item.
The example below removes consecutive 3 key/value pair starting from key
Root cause and solutionJust as I mentioned above, the root cause is that the cursor is based on a mutable in-memory node. Each time when removing an item, the cursor automatically moves to next item. The solution should be maintain a immutable node copy for stable cursor. But it may have some impact on the performance. |
Good catch! I did pull your changes in my local and change the initPos with (c.First(), c.Next()) to test. But I didn't reproduce the issue 🥶 |
There is no cheap fix. In order to maintain an immutable node for stable cursor iteration, we need to copy the inodes for each node in the cursor iteration stack. Specifically we need to add field
Usually users won't delete key/value pairs either during iteration. So decided to just update doc to clarify the behavior. #614 |
Agh funny, I just opened #629, came to the same conclusions. |
Right, it isn't that simple. It doesn't deserve too much effort to fix it for now, unless lots of users drive it. Also it may either complicate the user interface or decrease the performance if we follow #611 (comment).
Let's update the doc (see #614) for now. |
No plan to continue to work on this for now. Note: the doc has already been updated in #614 |
Fix boltdb/bolt#357
cc @thatguystone @flowchartsman @benbjohnson
It's a bit funny that a bug isn't fixed until 8 years later.
Just copied the comment to explain the root cause,