LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

110 lines
2.6 KiB

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