liuis / leveldb

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

Is it possible to seek to the first key less than a given key #30

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to find the first key that is less than a given key. I can only 
think of two ways to achieve this goal and they both fail.

1. seek to key then go back one.  This fails because if the key you want is the 
last key, then you have an invalid iterator and you get a segfault.

2. seek to key, then check if iterator is invalid (because we are past the 
end), then set to seekToLast and do the read.  This fails because if a key 
comes in before the seekToLast, then you get the wrong value.

I have attached tests to try each one.  Is there an easier way I don't know 
about?

TEST(DBTest, IterPastEndThenPrev) { 
  ASSERT_OK(Put("a", "va"));
  Iterator* iter = db_->NewIterator(ReadOptions());

  iter->Seek("b");
  ASSERT_EQ(IterStatus(iter), "(invalid)");
  iter->Prev();
  ASSERT_EQ(IterStatus(iter), "a->va");
}

TEST(DBTest, IterSnapshotViewSeekToEnd) { 
  ASSERT_OK(Put("a", "va"));
  Iterator* iter = db_->NewIterator(ReadOptions());

  iter->Seek("b");
  ASSERT_EQ(IterStatus(iter), "(invalid)");
  ASSERT_OK(Put("c", "vc"));
  iter->SeekToLast();
  ASSERT_EQ(IterStatus(iter), "a->va");
}

Original issue reported on code.google.com by john.car...@gmail.com on 13 Aug 2011 at 6:34

GoogleCodeExporter commented 9 years ago
Actually I realized there is an iterator snapshot operation. This will work for 
me in this case.  Do you know the performance penalty of using a snapshot?

Original comment by john.car...@gmail.com on 13 Aug 2011 at 7:18

GoogleCodeExporter commented 9 years ago
Hi John, thanks a lot for your tests.

We'd like to patch these tests into LevelDB code's - do you mind filling out 
this CLA so we can patch it in?
http://code.google.com/legal/individual-cla-v1.0.html
Let me know when you've completed this.

Thanks for your help,

Gabor

Original comment by ga...@google.com on 15 Aug 2011 at 12:19

GoogleCodeExporter commented 9 years ago
"I would like to find the first key that is less than a given key. I can only 
think of two ways to achieve this goal and they both fail.

1. seek to key then go back one.  This fails because if the key you want is the 
last key, then you have an invalid iterator and you get a segfault."

I've the same use-case but I solved #1 by adding a definite last key as a 
marker.  This key is checked on database startup ... and initialized if 
database is new. 
Its possible if your keys are predictable (I used 0xFFFF as the exclusive last 
key).

Original comment by david.yu...@gmail.com on 15 Aug 2011 at 5:55

GoogleCodeExporter commented 9 years ago
This should be fixed in R48 - please verify!

Original comment by ga...@google.com on 22 Aug 2011 at 8:55