1 // Copyright (c) 2013 The LevelDB Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 5 // Test for issue 178: a manual compaction causes deleted data to reappear. 6 #include <iostream> 7 #include <sstream> 8 #include <cstdlib> 9 10 #include "leveldb/db.h" 11 #include "leveldb/write_batch.h" 12 #include "util/testharness.h" 13 14 namespace { 15 16 const int kNumKeys = 1100000; 17 18 std::string Key1(int i) { 19 char buf[100]; 20 snprintf(buf, sizeof(buf), "my_key_%d", i); 21 return buf; 22 } 23 24 std::string Key2(int i) { 25 return Key1(i) + "_xxx"; 26 } 27 28 class Issue178 { }; 29 30 TEST(Issue178, Test) { 31 // Get rid of any state from an old run. 32 std::string dbpath = leveldb::test::TmpDir() + "/leveldb_cbug_test"; 33 DestroyDB(dbpath, leveldb::Options()); 34 35 // Open database. Disable compression since it affects the creation 36 // of layers and the code below is trying to test against a very 37 // specific scenario. 38 leveldb::DB* db; 39 leveldb::Options db_options; 40 db_options.create_if_missing = true; 41 db_options.compression = leveldb::kNoCompression; 42 ASSERT_OK(leveldb::DB::Open(db_options, dbpath, &db)); 43 44 // create first key range 45 leveldb::WriteBatch batch; 46 for (size_t i = 0; i < kNumKeys; i++) { 47 batch.Put(Key1(i), "value for range 1 key"); 48 } 49 ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch)); 50 51 // create second key range 52 batch.Clear(); 53 for (size_t i = 0; i < kNumKeys; i++) { 54 batch.Put(Key2(i), "value for range 2 key"); 55 } 56 ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch)); 57 58 // delete second key range 59 batch.Clear(); 60 for (size_t i = 0; i < kNumKeys; i++) { 61 batch.Delete(Key2(i)); 62 } 63 ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch)); 64 65 // compact database 66 std::string start_key = Key1(0); 67 std::string end_key = Key1(kNumKeys - 1); 68 leveldb::Slice least(start_key.data(), start_key.size()); 69 leveldb::Slice greatest(end_key.data(), end_key.size()); 70 71 // commenting out the line below causes the example to work correctly 72 db->CompactRange(&least, &greatest); 73 74 // count the keys 75 leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions()); 76 size_t num_keys = 0; 77 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { 78 num_keys++; 79 } 80 delete iter; 81 ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys"; 82 83 // close database 84 delete db; 85 DestroyDB(dbpath, leveldb::Options()); 86 } 87 88 } // anonymous namespace 89 90 int main(int argc, char** argv) { 91 return leveldb::test::RunAllTests(); 92 } 93