Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
issue178_test.cc
Go to the documentation of this file.
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;
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
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) {
92 }
virtual void CompactRange(const Slice *begin, const Slice *end)=0
virtual Status Write(const WriteOptions &options, WriteBatch *updates)=0
bool create_if_missing
Definition: options.h:45
std::string TmpDir()
Definition: testharness.cc:60
#define ASSERT_OK(s)
Definition: testharness.h:106
int RunAllTests()
Definition: testharness.cc:36
virtual Iterator * NewIterator(const ReadOptions &options)=0
void Delete(const Slice &key)
Definition: write_batch.cc:105
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
virtual void Next()=0
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1467
Definition: db.h:44
static Status Open(const Options &options, const std::string &name, DB **dbptr)
Definition: db_impl.cc:1430
int main(int argc, char **argv)
virtual void SeekToFirst()=0
CompressionType compression
Definition: options.h:129
virtual bool Valid() const =0
#define TEST(base, name)
Definition: testharness.h:117
void Put(const Slice &key, const Slice &value)
Definition: write_batch.cc:98