小组成员:姚凯文(kevinyao0901),姜嘉琪
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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