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

Fix reverse iterator #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

numion
Copy link

@numion numion commented Feb 12, 2014

Mirroring the implementation of real_next() in real_prev() resolves an issue with reverse iteration.

@wbolster
Copy link
Owner

Hi, sorry for the late reply.

I'm not sure what the exact problem is. Can you provide a minimal example that illustrates the problem you describe?

@numion
Copy link
Author

numion commented Apr 13, 2014

here is an example:

>>> import plyvel
>>> db = plyvel.DB('/tmp/testdb', create_if_missing=True)
>>> db.put(b'1', b'')
>>> db.put(b'2', b'')

forward iteration works as expected

>>> list(db.iterator(start=b'1', stop=b'2', include_value=False))
['1']

reverse iteration should give the same result

>>> list(db.iterator(start=b'1', stop=b'2', include_value=False, reverse=True))
[]

same here

>>> list(db.iterator(start=b'1', stop=b'1', include_stop=True, include_value=False))
['1']
>>> list(db.iterator(start=b'1', stop=b'1', include_stop=True, include_value=False, reverse=True))
[]

@ichernev
Copy link

ichernev commented Dec 9, 2014

I have a very similar problem, here's the gist:

import plyvel

db = plyvel.DB('/tmp/testdb/', create_if_missing=True)
db.put(b'key', b'value')
db.put(b'key2', b'value')

print list(db.iterator(start=b'key', include_start=True,
                       stop=b'key1', include_stop=True,
                       reverse=True))
// should print [('key', 'value')], instead prints []

The fix for this particular problem is fixed by:

diff --git a/plyvel/_plyvel.pyx b/plyvel/_plyvel.pyx
index 984a12a..f97cd71 100644
--- a/plyvel/_plyvel.pyx
+++ b/plyvel/_plyvel.pyx
@@ -910,7 +910,8 @@ cdef class Iterator(BaseIterator):
             # After all the stepping back, we might even have ended up
             # *before* the start key. In this case the iterator does not
             # yield any items.
-            if self.start is not None and self.comparator.Compare(self.start_slice, self._iter.key()) >= 0:
+            n = 1 if self.include_start else 0
+            if self.start is not None and self.comparator.Compare(self.start_slice, self._iter.key()) >= n:
                 raise StopIteration

             raise_for_status(self._iter.status())

But making sure going forward and going backward do the same thing makes more sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants