作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 line
3.6 KiB

  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. // Test handling last level with kNumLevels = 3
  87. TEST(TestTTL, LastLevelCompaction) {
  88. DB *db;
  89. if(OpenDB("testdb", &db).ok() == false) {
  90. std::cerr << "open db failed" << std::endl;
  91. abort();
  92. }
  93. uint64_t ttl = 20;
  94. InsertData(db, ttl);
  95. leveldb::Range ranges[1];
  96. ranges[0] = leveldb::Range("-", "A");
  97. uint64_t sizes[1];
  98. db->GetApproximateSizes(ranges, 1, sizes);
  99. ASSERT_GT(sizes[0], 0);
  100. std::string last_level_file_num;
  101. std::string last_level = "2";
  102. db->GetProperty("leveldb.num-files-at-level" + last_level, &last_level_file_num);
  103. std::cout << "File nums in last level: " << last_level_file_num << std::endl;
  104. ASSERT_GT(std::atoi(last_level_file_num.c_str()), 0);
  105. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  106. db->CompactRange(nullptr, nullptr);
  107. leveldb::Range ranges1[1];
  108. ranges1[0] = leveldb::Range("-", "A");
  109. uint64_t sizes1[1];
  110. db->GetApproximateSizes(ranges1, 1, sizes1);
  111. ASSERT_EQ(sizes1[0], 0);
  112. delete db;
  113. }
  114. int main(int argc, char** argv) {
  115. // All tests currently run with the same read-only file limits.
  116. testing::InitGoogleTest(&argc, argv);
  117. return RUN_ALL_TESTS();
  118. }