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.
5 // Test for issue 178: a manual compaction causes deleted data to reappear.
10 #include "leveldb/db.h"
11 #include "leveldb/write_batch.h"
12 #include "util/testharness.h"
16 const int kNumKeys = 1100000;
18 std::string Key1(int i) {
20 snprintf(buf, sizeof(buf), "my_key_%d", i);
24 std::string Key2(int i) {
25 return Key1(i) + "_xxx";
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());
35 // Open database. Disable compression since it affects the creation
36 // of layers and the code below is trying to test against a very
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));
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");
49 ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
51 // create second key range
53 for (size_t i = 0; i < kNumKeys; i++) {
54 batch.Put(Key2(i), "value for range 2 key");
56 ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
58 // delete second key range
60 for (size_t i = 0; i < kNumKeys; i++) {
61 batch.Delete(Key2(i));
63 ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
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());
71 // commenting out the line below causes the example to work correctly
72 db->CompactRange(&least, &greatest);
75 leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions());
77 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
81 ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
85 DestroyDB(dbpath, leveldb::Options());
88 } // anonymous namespace
90 int main(int argc, char** argv) {
91 return leveldb::test::RunAllTests();