liuis / leveldb

Automatically exported from code.google.com/p/leveldb
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Seek() semantics asymmetry #47

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
As it is possible to:

Seek at first key in the source that at or past target

it should be possibile to:

Seek at first key in the source that at or before target

This would enable easier backward iterations.

I let you judge on the two options that I see:

1. adding a back option to Seek
2. providing SeekBack method.

Thanks!

Original issue reported on code.google.com by Paolo.Losi on 19 Oct 2011 at 9:39

GoogleCodeExporter commented 9 years ago
We tried having something like this in the iterator interface and it 
complicated the  implementation a fair amount.  Given that you can accomplish 
what you want with a fairly small amount of code at the application level, we 
didn't feel it was necessary and opted to keep the iterator interface narrower. 
 Something like this should do what you are asking:

  void SeekBack(leveldb::Iterator* iter,
                leveldb::Comparator* comparator, 
                const leveldb::Slice& target) {
    iter->Seek(target);
    if (!iter->Valid()) {
      iter->SeekToFirst();
    } else if (comparator->Compare(iter->key(), target) > 0) {
      iter->Prev();
    }
  }

Original comment by j...@google.com on 19 Oct 2011 at 7:44

GoogleCodeExporter commented 9 years ago
That's fair. Thank you for the quick answer.

Original comment by Paolo.Losi on 19 Oct 2011 at 7:57

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Is it something wrong for this function? if iterator is invalid, all the value 
are greater than target. so, we need to SeekToLast, right?

 void SeekBack(leveldb::Iterator* iter,
                leveldb::Comparator* comparator, 
                const leveldb::Slice& target) {
    iter->Seek(target);
    if (!iter->Valid()) {
      iter->SeekToLast();
    } else if (comparator->Compare(iter->key(), target) > 0) {
      iter->Prev();
    }
  }

Original comment by liudaok...@gmail.com on 23 Dec 2011 at 7:40

GoogleCodeExporter commented 9 years ago
You are right.  If iter is not Valid, the next step should be to call 
SeekToLast(), not SeekToFirst().

Original comment by san...@google.com on 16 Oct 2012 at 11:34