小组成员:姚凯文(kevinyao0901),姜嘉琪
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

118 řádky
3.3 KiB

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