From 8eb552bf4d6dd88a773e1a9b77dac406923116bd Mon Sep 17 00:00:00 2001 From: Xing Jin Date: Sat, 28 Sep 2013 11:39:08 -0700 Subject: [PATCH] New unit test for iterator with snapshot Summary: I played with the reported bug about iterator with snapshot: https://code.google.com/p/leveldb/issues/detail?id=200. I turned the original test program (https://code.google.com/p/leveldb/issues/attachmentText?id=200&aid=2000000000&name=test.cc&token=7uOUQW-HFlbAFMUm7EqtaAEy7Tw%3A1378320724136) into a new unit test, but I cannot reproduce the problem. Notice lines 31-34 in above link. I have ran the new test with and without such Put() operations. Both succeed. So this diff simply adds the test, without changing any source codes. Test Plan: run new test. Reviewers: dhruba, haobo, emayanke Reviewed By: dhruba CC: leveldb Differential Revision: https://reviews.facebook.net/D12735 --- db/db_impl.cc | 1 - db/db_test.cc | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 5ca043c53..da4b3b4d3 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -3143,7 +3143,6 @@ Status DB::Open(const Options& options, const std::string& dbname, DB** dbptr) { impl->mutex_.Unlock(); if (options.compaction_style == kCompactionStyleUniversal) { - std::string property; int num_files; for (int i = 1; i < impl->NumberLevels(); i++) { num_files = impl->versions_->NumLevelFiles(i); diff --git a/db/db_test.cc b/db/db_test.cc index 1db529c44..dff6837eb 100644 --- a/db/db_test.cc +++ b/db/db_test.cc @@ -1283,6 +1283,44 @@ TEST(DBTest, IterMultiWithDelete) { } while (ChangeOptions()); } +TEST(DBTest, IterWithSnapshot) { + do { + ASSERT_OK(Put("key1", "val1")); + ASSERT_OK(Put("key2", "val2")); + ASSERT_OK(Put("key3", "val3")); + ASSERT_OK(Put("key4", "val4")); + ASSERT_OK(Put("key5", "val5")); + + const Snapshot *snapshot = db_->GetSnapshot(); + ReadOptions options; + options.snapshot = snapshot; + Iterator* iter = db_->NewIterator(options); + + // Put more values after the snapshot + ASSERT_OK(Put("key100", "val100")); + ASSERT_OK(Put("key101", "val101")); + + iter->Seek("key5"); + ASSERT_EQ(IterStatus(iter), "key5->val5"); + if (!CurrentOptions().merge_operator) { + // TODO: merge operator does not support backward iteration yet + iter->Prev(); + ASSERT_EQ(IterStatus(iter), "key4->val4"); + iter->Prev(); + ASSERT_EQ(IterStatus(iter), "key3->val3"); + + iter->Next(); + ASSERT_EQ(IterStatus(iter), "key4->val4"); + iter->Next(); + ASSERT_EQ(IterStatus(iter), "key5->val5"); + iter->Next(); + ASSERT_TRUE(!iter->Valid()); + } + db_->ReleaseSnapshot(snapshot); + delete iter; + } while (ChangeOptions()); +} + TEST(DBTest, Recover) { do { ASSERT_OK(Put("foo", "v1"));