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 #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"
13 class AutoCompactTest
{
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_
));
32 DestroyDB(dbname_
, Options());
36 std::string
Key(int i
) {
38 snprintf(buf
, sizeof(buf
), "key%06d", i
);
39 return std::string(buf
);
42 uint64_t Size(const Slice
& start
, const Slice
& limit
) {
43 Range
r(start
, limit
);
45 db_
->GetApproximateSizes(&r
, 1, &size
);
52 static const int kValueSize
= 200 * 1024;
53 static const int kTotalSize
= 100 * 1024 * 1024;
54 static const int kCount
= kTotalSize
/ kValueSize
;
56 // Read through the first n keys repeatedly and check that they get
57 // compacted (verified by checking the size of the key space).
58 void AutoCompactTest::DoReads(int n
) {
59 std::string
value(kValueSize
, 'x');
60 DBImpl
* dbi
= reinterpret_cast<DBImpl
*>(db_
);
63 for (int i
= 0; i
< kCount
; i
++) {
64 ASSERT_OK(db_
->Put(WriteOptions(), Key(i
), value
));
66 ASSERT_OK(dbi
->TEST_CompactMemTable());
69 for (int i
= 0; i
< kCount
; i
++) {
70 ASSERT_OK(db_
->Delete(WriteOptions(), Key(i
)));
72 ASSERT_OK(dbi
->TEST_CompactMemTable());
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
));
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
;
89 // Wait a little bit to allow any triggered compactions to complete.
90 Env::Default()->SleepForMicroseconds(1000000);
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) {
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);
106 TEST(AutoCompactTest
, ReadAll
) {
110 TEST(AutoCompactTest
, ReadHalf
) {
114 } // namespace leveldb
116 int main(int argc
, char** argv
) {
117 return leveldb::test::RunAllTests();