小组成员:陈予曈,朱陈媛
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.

119 lines
2.9 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(static_cast<unsigned int>(time(0)));
  16. //srand(0);
  17. for (int i = 0; i < key_num; i++) {
  18. //int key_ = rand() % key_num+1;
  19. int key_ = i + 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. srand(static_cast<unsigned int>(time(0)));
  30. //srand(0);
  31. for (int i = 0; i < 100; i++) {
  32. int key_ = rand() % key_num+1;
  33. std::string key = std::to_string(key_);
  34. std::string value;
  35. db->Get(readOptions, key, &value);
  36. }
  37. }
  38. // 暂时先注释掉写入Test-橙
  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 = 20;
  46. InsertData(db, ttl);
  47. ReadOptions readOptions;
  48. Status status;
  49. int key_num = data_size / value_size;
  50. srand(static_cast<unsigned int>(time(0)));
  51. for (int i = 0; i < 100; i++) {
  52. int key_ = rand() % key_num+1;
  53. std::string key = std::to_string(key_);
  54. std::string value;
  55. status = db->Get(readOptions, key, &value);
  56. ASSERT_TRUE(status.ok());
  57. }
  58. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  59. for (int i = 0; i < 100; i++) {
  60. int key_ = rand() % key_num+1;
  61. std::string key = std::to_string(key_);
  62. std::string value;
  63. status = db->Get(readOptions, key, &value);
  64. ASSERT_FALSE(status.ok());
  65. }
  66. delete db;
  67. }
  68. TEST(TestTTL, CompactionTTL) {
  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. //这里为什么要定义两个ranges1?-橙
  77. leveldb::Range ranges[1];
  78. ranges[0] = leveldb::Range("-", "A");
  79. uint64_t sizes[1];
  80. db->GetApproximateSizes(ranges, 1, sizes);
  81. // printf("part1\n");
  82. ASSERT_GT(sizes[0], 0);
  83. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  84. db->CompactRange(nullptr, nullptr);
  85. // 先注释掉重复定义的-橙
  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. }