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 13 class AutoCompactTest { 14 public: 15 std::string dbname_; 16 Cache* tiny_cache_; 17 Options options_; 18 DB* db_; 19 20 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_)); 28 } 29 30 ~AutoCompactTest() { 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). 58 void AutoCompactTest::DoReads(int n) { 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 } 66 ASSERT_OK(dbi->TEST_CompactMemTable()); 67 68 // Delete everything 69 for (int i = 0; i < kCount; i++) { 70 ASSERT_OK(db_->Delete(WriteOptions(), Key(i))); 71 } 72 ASSERT_OK(dbi->TEST_CompactMemTable()); 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. 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) { 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 106 TEST(AutoCompactTest, ReadAll) { 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) { 117 return leveldb::test::RunAllTests(); 118 } 119