Fix crash caused by opening an empty DB in readonly mode

Summary:
This diff fixes a crash found when an empty database is opened in readonly mode.
We now check the number of levels before we open the DB as a compacted DB.

Test Plan: DBTest.EmptyCompactedDB

Reviewers: igor, sdong

Reviewed By: sdong

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D36327
main
Venkatesh Radhakrishnan 10 years ago
parent 51c8133a72
commit d0695f3e26
  1. 11
      db/db_test.cc
  2. 3
      utilities/compacted_db/compacted_db_impl.cc

@ -12270,6 +12270,17 @@ TEST_F(DBTest, TestLogCleanup) {
} }
} }
TEST_F(DBTest, EmptyCompactedDB) {
Options options;
options.max_open_files = -1;
options = CurrentOptions(options);
Close();
ASSERT_OK(ReadOnlyReopen(options));
Status s = Put("new", "value");
ASSERT_TRUE(s.IsNotSupported());
Close();
}
} // namespace rocksdb } // namespace rocksdb
int main(int argc, char** argv) { int main(int argc, char** argv) {

@ -107,6 +107,9 @@ Status CompactedDBImpl::Init(const Options& options) {
version_ = cfd_->GetSuperVersion()->current; version_ = cfd_->GetSuperVersion()->current;
user_comparator_ = cfd_->user_comparator(); user_comparator_ = cfd_->user_comparator();
auto* vstorage = version_->storage_info(); auto* vstorage = version_->storage_info();
if (vstorage->num_non_empty_levels() == 0) {
return Status::NotSupported("no file exists");
}
const LevelFilesBrief& l0 = vstorage->LevelFilesBrief(0); const LevelFilesBrief& l0 = vstorage->LevelFilesBrief(0);
// L0 should not have files // L0 should not have files
if (l0.num_files > 1) { if (l0.num_files > 1) {

Loading…
Cancel
Save