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

120 line
3.0 KiB

4 週之前
4 週之前
4 週之前
4 週之前
4 週之前
4 週之前
4 週之前
  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 = 200;
  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. }
  70. TEST(TestTTL, CompactionTTL) {
  71. DB *db;
  72. if(OpenDB("testdb", &db).ok() == false) {
  73. std::cerr << "open db failed" << std::endl;
  74. abort();
  75. }
  76. uint64_t ttl = 20;
  77. InsertData(db, ttl);
  78. leveldb::Range ranges[1];
  79. ranges[0] = leveldb::Range("-", "A");
  80. uint64_t sizes[1];
  81. db->GetApproximateSizes(ranges, 1, sizes);
  82. ASSERT_GT(sizes[0], 0);
  83. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  84. db->CompactRange(nullptr, nullptr);
  85. // leveldb::Range ranges[1]; // 这里为什么要重复定义?心
  86. ranges[0] = leveldb::Range("-", "A");
  87. // uint64_t sizes[1]; // 心
  88. db->GetApproximateSizes(ranges, 1, sizes);
  89. ASSERT_EQ(sizes[0], 0);
  90. }
  91. int main(int argc, char** argv) {
  92. // All tests currently run with the same read-only file limits.
  93. testing::InitGoogleTest(&argc, argv);
  94. return RUN_ALL_TESTS();
  95. }