作者: 韩晨旭 10225101440 李畅 10225102463
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

159 líneas
4.0 KiB

hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
hace 3 semanas
  1. #include "gtest/gtest.h"
  2. #include "leveldb/env.h"
  3. #include "leveldb/db.h"
  4. using namespace leveldb;
  5. constexpr int value_size = 2048;
  6. constexpr int data_size = 128 << 20;
  7. Status OpenDB(std::string dbName, DB **db) {
  8. Options options;
  9. options.create_if_missing = true;
  10. return DB::Open(options, dbName, db);
  11. }
  12. void InsertData(DB *db, uint64_t ttl/* second */) {
  13. WriteOptions writeOptions;
  14. int key_num = data_size / value_size;
  15. srand(0);
  16. for (int i = 0; i < key_num; i++) {
  17. int key_ = rand() % key_num+1;
  18. std::string key = std::to_string(key_);
  19. std::string value(value_size, 'a');
  20. db->Put(writeOptions, key, value, ttl);
  21. }
  22. }
  23. void GetData(DB *db, int size = (1 << 30)) {
  24. ReadOptions readOptions;
  25. int key_num = data_size / value_size;
  26. // 点查
  27. srand(0);
  28. for (int i = 0; i < 100; i++) {
  29. int key_ = rand() % key_num+1;
  30. std::string key = std::to_string(key_);
  31. std::string value;
  32. db->Get(readOptions, key, &value);
  33. }
  34. }
  35. TEST(TestTTL, ReadTTL) {
  36. DB *db;
  37. if(OpenDB("testdb", &db).ok() == false) {
  38. std::cerr << "open db failed" << std::endl;
  39. abort();
  40. }
  41. uint64_t ttl = 20;
  42. InsertData(db, ttl);
  43. ReadOptions readOptions;
  44. Status status;
  45. int key_num = data_size / value_size;
  46. srand(0);
  47. for (int i = 0; i < 100; i++) {
  48. int key_ = rand() % key_num+1;
  49. std::string key = std::to_string(key_);
  50. std::string value;
  51. status = db->Get(readOptions, key, &value);
  52. ASSERT_TRUE(status.ok());
  53. }
  54. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  55. for (int i = 0; i < 100; i++) {
  56. int key_ = rand() % key_num+1;
  57. std::string key = std::to_string(key_);
  58. std::string value;
  59. status = db->Get(readOptions, key, &value);
  60. ASSERT_FALSE(status.ok());
  61. }
  62. delete db;
  63. }
  64. TEST(TestTTL, CompactionTTL) {
  65. DB *db;
  66. if(OpenDB("testdb", &db).ok() == false) {
  67. std::cerr << "open db failed" << std::endl;
  68. abort();
  69. }
  70. uint64_t ttl = 20;
  71. InsertData(db, ttl);
  72. leveldb::Range ranges[1];
  73. ranges[0] = leveldb::Range("-", "A");
  74. uint64_t sizes[1];
  75. db->GetApproximateSizes(ranges, 1, sizes);
  76. ASSERT_GT(sizes[0], 0);
  77. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  78. db->CompactRange(nullptr, nullptr);
  79. leveldb::Range ranges1[1];
  80. ranges1[0] = leveldb::Range("-", "A");
  81. uint64_t sizes1[1];
  82. db->GetApproximateSizes(ranges1, 1, sizes1);
  83. ASSERT_EQ(sizes1[0], 0);
  84. delete db;
  85. }
  86. // Time-consuming when kNumLevels is 7, so run this test when kNumLevels is set to 3,
  87. // the codes on branch light_ver is a version with kNumLevels set to 3.
  88. // In this case level 2 is the last level with files.
  89. TEST(TestTTL, LastLevelCompaction) {
  90. DB *db;
  91. if(OpenDB("testdb", &db).ok() == false) {
  92. std::cerr << "open db failed" << std::endl;
  93. abort();
  94. }
  95. uint64_t ttl = 20;
  96. InsertData(db, ttl);
  97. leveldb::Range ranges[1];
  98. ranges[0] = leveldb::Range("-", "A");
  99. uint64_t sizes[1];
  100. db->GetApproximateSizes(ranges, 1, sizes);
  101. ASSERT_GT(sizes[0], 0);
  102. int last_level_file_num;
  103. std::string file_num;
  104. std::string last_level = "2";
  105. for (int level = 0; level < 7; level++){
  106. db->GetProperty("leveldb.num-files-at-level" + std::to_string(level), &file_num);
  107. if (std::atoi(file_num.c_str()) > 0){
  108. last_level = std::to_string(level);
  109. last_level_file_num = std::atoi(file_num.c_str());
  110. }
  111. else{
  112. break;
  113. }
  114. }
  115. std::cout << "File nums in last level: " << last_level_file_num << std::endl;
  116. ASSERT_GT(last_level_file_num, 0);
  117. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  118. db->CompactRange(nullptr, nullptr);
  119. leveldb::Range ranges1[1];
  120. ranges1[0] = leveldb::Range("-", "A");
  121. uint64_t sizes1[1];
  122. db->GetApproximateSizes(ranges1, 1, sizes1);
  123. ASSERT_EQ(sizes1[0], 0);
  124. delete db;
  125. }
  126. int main(int argc, char** argv) {
  127. // All tests currently run with the same read-only file limits.
  128. testing::InitGoogleTest(&argc, argv);
  129. return RUN_ALL_TESTS();
  130. }