小组成员: 曹可心-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.

121 lines
3.1 KiB

  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. }
  24. }
  25. void GetData(DB *db, int size = (1 << 30)) {
  26. ReadOptions readOptions;
  27. int key_num = data_size / value_size;
  28. // 使用随机种子生成随机键进行点查(单次查询),燕
  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 = 50;
  46. InsertData(db, ttl);
  47. ReadOptions readOptions;
  48. Status status;
  49. int key_num = data_size / value_size;
  50. srand(0);
  51. for (int i = 0; i < 100; i++) {
  52. int key_ = rand() % key_num+1;
  53. // int key_ = i % key_num+1;
  54. std::string key = std::to_string(key_);
  55. std::string value;
  56. status = db->Get(readOptions, key, &value);
  57. ASSERT_TRUE(status.ok());
  58. }
  59. // 等待TTL过期,使插入的数据变为“过期”状态,燕
  60. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  61. for (int i = 0; i < 100; i++) {
  62. int key_ = rand() % key_num+1;
  63. // int key_ = i % key_num+1;
  64. std::string key = std::to_string(key_);
  65. std::string value;
  66. status = db->Get(readOptions, key, &value);
  67. ASSERT_FALSE(status.ok());
  68. }
  69. delete db;
  70. }
  71. TEST(TestTTL, CompactionTTL) {
  72. DB *db;
  73. if(OpenDB("testdb", &db).ok() == false) {
  74. std::cerr << "open db failed" << std::endl;
  75. abort();
  76. }
  77. uint64_t ttl = 50;
  78. InsertData(db, ttl);
  79. leveldb::Range ranges[1];
  80. ranges[0] = leveldb::Range("-", "A");
  81. uint64_t sizes[1];
  82. db->GetApproximateSizes(ranges, 1, sizes);
  83. ASSERT_GT(sizes[0], 0);
  84. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  85. db->CompactRange(nullptr, nullptr);
  86. // leveldb::Range ranges[1]; // 这里为什么要重复定义?心
  87. ranges[0] = leveldb::Range("-", "A");
  88. // uint64_t sizes[1]; // 心
  89. db->GetApproximateSizes(ranges, 1, sizes);
  90. ASSERT_EQ(sizes[0], 0);
  91. }
  92. int main(int argc, char** argv) {
  93. // All tests currently run with the same read-only file limits.
  94. testing::InitGoogleTest(&argc, argv);
  95. return RUN_ALL_TESTS();
  96. }