LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

111 lines
2.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  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. //
  36. TEST(TestTTL, ReadTTL) {
  37. DB *db;
  38. if(OpenDB("testdb", &db).ok() == false) {
  39. std::cerr << "open db failed" << std::endl;
  40. abort();
  41. }
  42. // 如果出现open db fail,请酌情提高这里
  43. uint64_t ttl = 20;
  44. InsertData(db, ttl);
  45. ReadOptions readOptions;
  46. Status status;
  47. int key_num = data_size / value_size;
  48. srand(0);
  49. for (int i = 0; i < 100; i++) {
  50. int key_ = rand() % key_num+1;
  51. std::string key = std::to_string(key_);
  52. std::string value;
  53. status = db->Get(readOptions, key, &value);
  54. ASSERT_TRUE(status.ok());
  55. }
  56. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  57. for (int i = 0; i < 100; i++) {
  58. int key_ = rand() % key_num+1;
  59. std::string key = std::to_string(key_);
  60. std::string value;
  61. status = db->Get(readOptions, key, &value);
  62. ASSERT_FALSE(status.ok());
  63. }
  64. delete db;
  65. Env::Default()->SleepForMicroseconds( 1000);
  66. }
  67. TEST(TestTTL, CompactionTTL) {
  68. DestroyDB("testdb", Options());
  69. DB *db;
  70. if(OpenDB("testdb", &db).ok() == false) {
  71. std::cerr << "open db failed" << std::endl;
  72. abort();
  73. }
  74. uint64_t ttl = 20;
  75. InsertData(db, ttl);
  76. leveldb::Range ranges[1];
  77. ranges[0] = leveldb::Range("-", "A");
  78. uint64_t sizes[1];
  79. db->GetApproximateSizes(ranges, 1, sizes);
  80. ASSERT_GT(sizes[0], 0);
  81. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  82. db->CompactRange(nullptr, nullptr);
  83. leveldb::Range ranges2[1];
  84. ranges2[0] = leveldb::Range("-", "A");
  85. uint64_t sizes2[1];
  86. db->GetApproximateSizes(ranges2, 1, sizes2);
  87. ASSERT_EQ(sizes2[0], 0);
  88. }
  89. int main(int argc, char** argv) {
  90. // All tests currently run with the same read-only file limits.
  91. testing::InitGoogleTest(&argc, argv);
  92. return RUN_ALL_TESTS();
  93. }