leveldb
autocompact_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 #include "leveldb/db.h"
6 #include "db/db_impl.h"
7 #include "leveldb/cache.h"
8 #include "util/testharness.h"
9 #include "util/testutil.h"
10 
11 namespace leveldb {
12 
14  public:
15  std::string dbname_;
18  DB* db_;
19 
21  dbname_ = test::TmpDir() + "/autocompact_test";
22  tiny_cache_ = NewLRUCache(100);
23  options_.block_cache = tiny_cache_;
24  DestroyDB(dbname_, options_);
25  options_.create_if_missing = true;
26  options_.compression = kNoCompression;
27  ASSERT_OK(DB::Open(options_, dbname_, &db_));
28  }
29 
31  delete db_;
32  DestroyDB(dbname_, Options());
33  delete tiny_cache_;
34  }
35 
36  std::string Key(int i) {
37  char buf[100];
38  snprintf(buf, sizeof(buf), "key%06d", i);
39  return std::string(buf);
40  }
41 
42  uint64_t Size(const Slice& start, const Slice& limit) {
43  Range r(start, limit);
44  uint64_t size;
45  db_->GetApproximateSizes(&r, 1, &size);
46  return size;
47  }
48 
49  void DoReads(int n);
50 };
51 
52 static const int kValueSize = 200 * 1024;
53 static const int kTotalSize = 100 * 1024 * 1024;
54 static const int kCount = kTotalSize / kValueSize;
55 
56 // Read through the first n keys repeatedly and check that they get
57 // compacted (verified by checking the size of the key space).
59  std::string value(kValueSize, 'x');
60  DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
61 
62  // Fill database
63  for (int i = 0; i < kCount; i++) {
64  ASSERT_OK(db_->Put(WriteOptions(), Key(i), value));
65  }
67 
68  // Delete everything
69  for (int i = 0; i < kCount; i++) {
71  }
73 
74  // Get initial measurement of the space we will be reading.
75  const int64_t initial_size = Size(Key(0), Key(n));
76  const int64_t initial_other_size = Size(Key(n), Key(kCount));
77 
78  // Read until size drops significantly.
79  std::string limit_key = Key(n);
80  for (int read = 0; true; read++) {
81  ASSERT_LT(read, 100) << "Taking too long to compact";
82  Iterator* iter = db_->NewIterator(ReadOptions());
83  for (iter->SeekToFirst();
84  iter->Valid() && iter->key().ToString() < limit_key;
85  iter->Next()) {
86  // Drop data
87  }
88  delete iter;
89  // Wait a little bit to allow any triggered compactions to complete.
91  uint64_t size = Size(Key(0), Key(n));
92  fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n",
93  read+1, size/1048576.0, Size(Key(n), Key(kCount))/1048576.0);
94  if (size <= initial_size/10) {
95  break;
96  }
97  }
98 
99  // Verify that the size of the key space not touched by the reads
100  // is pretty much unchanged.
101  const int64_t final_other_size = Size(Key(n), Key(kCount));
102  ASSERT_LE(final_other_size, initial_other_size + 1048576);
103  ASSERT_GE(final_other_size, initial_other_size/5 - 1048576);
104 }
105 
107  DoReads(kCount);
108 }
109 
110 TEST(AutoCompactTest, ReadHalf) {
111  DoReads(kCount/2);
112 }
113 
114 } // namespace leveldb
115 
116 int main(int argc, char** argv) {
118 }
static const int kTotalSize
uint64_t Size(const Slice &start, const Slice &limit)
int main(int argc, char **argv)
std::string TmpDir()
Definition: testharness.cc:60
virtual Slice key() const =0
#define ASSERT_LE(a, b)
Definition: testharness.h:111
#define ASSERT_OK(s)
Definition: testharness.h:106
virtual void SleepForMicroseconds(int micros)=0
virtual void SeekToFirst()=0
int RunAllTests()
Definition: testharness.cc:36
std::string ToString() const
Definition: slice.h:66
static const int kValueSize
virtual void Next()=0
virtual Status Put(const WriteOptions &options, const Slice &key, const Slice &value)=0
Definition: db_impl.cc:1476
Status DestroyDB(const std::string &dbname, const Options &options)
Definition: db_impl.cc:1537
std::string Key(int i)
virtual Status Delete(const WriteOptions &options, const Slice &key)=0
Definition: db_impl.cc:1482
static Status Open(const Options &options, const std::string &name, DB **dbptr)
Definition: db_impl.cc:1490
#define ASSERT_LT(a, b)
Definition: testharness.h:112
Status TEST_CompactMemTable()
Definition: db_impl.cc:621
TEST(AutoCompactTest, ReadAll)
virtual void GetApproximateSizes(const Range *range, int n, uint64_t *sizes)=0
bool create_if_missing
Definition: options.h:45
#define ASSERT_GE(a, b)
Definition: testharness.h:109
Cache * block_cache
Definition: options.h:98
Cache * NewLRUCache(size_t capacity)
Definition: cache.cc:401
virtual Iterator * NewIterator(const ReadOptions &options)=0
static Env * Default()
Definition: env_posix.cc:613
Definition: db.h:44
static const int kCount
virtual bool Valid() const =0
CompressionType compression
Definition: options.h:141