小组成员: 曹可心-10223903406 朴祉燕-10224602413
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.

131 lines
3.3 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. #include "gtest/gtest.h"
  2. #include "leveldb/env.h"
  3. #include "leveldb/db.h"
  4. using namespace leveldb;
  5. // 定义了数据的大小,用于插入大数据集,燕
  6. constexpr int value_size = 2048;
  7. constexpr int data_size = 128 << 20;
  8. Status OpenDB(std::string dbName, DB **db) {
  9. Options options;
  10. options.create_if_missing = true;
  11. return DB::Open(options, dbName, db);
  12. }
  13. void InsertData(DB *db, uint64_t ttl/* second */) {
  14. WriteOptions writeOptions;
  15. int key_num = data_size / value_size;
  16. srand(0);
  17. for (int i = 0; i < key_num; i++) {
  18. //int key_ = rand() % key_num+1;
  19. int key_ = i % key_num+1;
  20. std::string key = std::to_string(key_);
  21. std::string value(value_size, 'a');
  22. db->Put(writeOptions, key, value, ttl);
  23. if (i<100) printf("%d ", key_);
  24. }
  25. }
  26. void GetData(DB *db, int size = (1 << 30)) {
  27. ReadOptions readOptions;
  28. int key_num = data_size / value_size;
  29. // 点查
  30. srand(0);
  31. for (int i = 0; i < 100; i++) {
  32. int key_ = rand() % key_num+1;
  33. //int key_ = i % key_num+1;
  34. std::string key = std::to_string(key_);
  35. std::string value;
  36. db->Get(readOptions, key, &value);
  37. }
  38. }
  39. TEST(TestTTL, ReadTTL) {
  40. DB *db;
  41. if(OpenDB("testdb", &db).ok() == false) {
  42. std::cerr << "open db failed" << std::endl;
  43. abort();
  44. }
  45. uint64_t ttl = 40;
  46. InsertData(db, ttl);
  47. printf("\n-------------\n");
  48. ReadOptions readOptions;
  49. Status status;
  50. int key_num = data_size / value_size;
  51. srand(0);
  52. for (int i = 0; i < 100; i++) {
  53. int key_ = rand() % key_num+1;
  54. printf("%d ", key_);
  55. std::string key = std::to_string(key_);
  56. std::string value;
  57. status = db->Get(readOptions, key, &value);
  58. if (!status.ok())
  59. {
  60. status = db->Get(readOptions, key, &value);
  61. printf("\n%d\n", key_);
  62. }
  63. // ASSERT_TRUE(status.ok());
  64. }
  65. // Env::Default()->SleepForMicroseconds(ttl * 1000000);
  66. // srand(0);
  67. // for (int i = 0; i < 100; i++) {
  68. // int key_ = rand() % key_num+1;
  69. // std::string key = std::to_string(key_);
  70. // std::string value;
  71. // status = db->Get(readOptions, key, &value);
  72. // if (!status.ok())
  73. // {
  74. // status = db->Get(readOptions, key, &value);
  75. // printf("\n%d\n", key_);
  76. // }
  77. // // ASSERT_FALSE(status.ok()); // 经过超长时间之后所有的键值对应该都过期了,心
  78. // }
  79. delete db;
  80. }
  81. TEST(TestTTL, CompactionTTL) {
  82. DB *db;
  83. if(OpenDB("testdb", &db).ok() == false) {
  84. std::cerr << "open db failed" << std::endl;
  85. abort();
  86. }
  87. uint64_t ttl = 20;
  88. InsertData(db, ttl);
  89. leveldb::Range ranges[1];
  90. ranges[0] = leveldb::Range("-", "A");
  91. uint64_t sizes[1];
  92. db->GetApproximateSizes(ranges, 1, sizes);
  93. ASSERT_GT(sizes[0], 0);
  94. Env::Default()->SleepForMicroseconds(100 * 1000000);
  95. db->CompactRange(nullptr, nullptr);
  96. // leveldb::Range ranges[1]; // 这里为什么要重复定义?心
  97. ranges[0] = leveldb::Range("-", "A");
  98. // uint64_t sizes[1]; // 心
  99. db->GetApproximateSizes(ranges, 1, sizes);
  100. ASSERT_EQ(sizes[0], 0);
  101. delete db;
  102. }
  103. int main(int argc, char** argv) {
  104. // All tests currently run with the same read-only file limits.
  105. testing::InitGoogleTest(&argc, argv);
  106. return RUN_ALL_TESTS();
  107. }