Badger.iterator needs Last() and Prev()

Moved from GitHub badger/786

Posted by Lawliet-Chan:

We recently write a blockchain project by using badger. But we need some functions in badger.Iterator as follows:

func Last() : move the iterator to the last KV pair.

func Prev() : move the iterator to the previous KV pair.

I also saw these functions in go-leveldb and boltdb. But I just insist on Badger when I was at tech-meeting with my colleagues and leader.
Would you mind add these funtions into badger?

I like the badger more than other embedded DBs :stuck_out_tongue:
Thanks a lot!

asdine commented :

I also agree that Last and Prev are more useful than creating an iterator with the Reverse = true option.
The current implementation only allows one way Iterators whereas having First, Last, Next, Prev and Seek allows to go back and forth in constant time using the same iterator.
I’m interested in providing an implementation but I suppose that would mean removing the Reverse option.
Would you be interested in this change?

1 Like

campoy commented :

Adding this to the list of API enhancements we could consider for v2.

@jarifibrahim, could you confirm that this is something that can’t be easily done with the existing API?

jarifibrahim commented :

Adding Last() is fairly straight forward but adding Prev() might be quite complicated.
Right now we store a min heap of iterators which helps in determining the next item to fetch from the DB.

Adding a Prev() would mean we need another heap which stores the same keys sorted in the opposite order and this heap would have to be built while we remove entries from the min heap. This would be fairly complex.

stale commented :

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I would also prefer to get rid of the Reverse solution and go for First, Last, Next, Prev and Seek.

Any chance this can be considered for v3?

But please do not get rid of the ‘Reverse=true’ option itself