10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

210 lines
5.4 KiB

3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks 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 */, int vsize = 0) {
  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 + vsize, 'a');
  20. db->Put(writeOptions, std::to_string(i+1), value, ttl);
  21. // db->Put(writeOptions, key, value, ttl);
  22. }
  23. }
  24. void GetData(DB *db, int size = (1 << 30)) {
  25. ReadOptions readOptions;
  26. int key_num = data_size / value_size;
  27. // 点查
  28. srand(0);
  29. for (int i = 0; i < 100; i++) {
  30. int key_ = rand() % key_num+1;
  31. std::string key = std::to_string(key_);
  32. std::string value;
  33. db->Get(readOptions, key, &value);
  34. }
  35. }
  36. TEST(TestTTL,ReadWithoutTTL) {
  37. DestroyDB("testdb",Options());
  38. DB *db;
  39. if(OpenDB("testdb", &db).ok() == false) {
  40. std::cerr << "open db failed" << std::endl;
  41. abort();
  42. }
  43. uint64_t ttl1 = 15;
  44. uint64_t ttl2 = 0;
  45. uint64_t extra_size = 1;
  46. InsertData(db, ttl2);
  47. // sleep(1);
  48. InsertData(db, ttl1, extra_size); //后一个数据长度变化一下
  49. //都没过期先找到后插的
  50. Env::Default()->SleepForMicroseconds(1 * 1000000);
  51. int key_num = data_size / value_size;
  52. ReadOptions readOptions;
  53. Status status;
  54. srand(0);
  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_TRUE(status.ok());
  61. ASSERT_EQ(value.size(), value_size + extra_size);
  62. }
  63. //再找到前一次
  64. Env::Default()->SleepForMicroseconds(ttl1 * 1000000);
  65. // db->CompactRange(nullptr,nullptr);
  66. for (int i = 0; i < 100; i++) {
  67. int key_ = rand() % key_num+1;
  68. std::string key = std::to_string(key_);
  69. std::string value;
  70. status = db->Get(readOptions, key, &value);
  71. ASSERT_TRUE(status.ok());
  72. ASSERT_EQ(value.size(), value_size);
  73. }
  74. delete db;
  75. }
  76. TEST(TestTTL, ReadTTL) {
  77. DestroyDB("testdb",Options());
  78. DB *db;
  79. if(OpenDB("testdb", &db).ok() == false) {
  80. std::cerr << "open db failed" << std::endl;
  81. abort();
  82. }
  83. uint64_t ttl = 15;
  84. InsertData(db, ttl);
  85. ReadOptions readOptions;
  86. Status status;
  87. int key_num = data_size / value_size;
  88. srand(0);
  89. for (int i = 0; i < 100; i++) {
  90. int key_ = rand() % key_num+1;
  91. std::string key = std::to_string(key_);
  92. std::string value;
  93. status = db->Get(readOptions, key, &value);
  94. std::cout<<key<<" "<<value[0]<<std::endl;
  95. ASSERT_TRUE(status.ok());
  96. }
  97. Env::Default()->SleepForMicroseconds((ttl+1) * 1000000);
  98. for (int i = 0; i < 100; i++) {
  99. int key_ = rand() % key_num+1;
  100. std::string key = std::to_string(key_);
  101. std::string value;
  102. status = db->Get(readOptions, key, &value);
  103. std::cout<<key<<" "<<value<<std::endl;
  104. ASSERT_FALSE(status.ok());
  105. }
  106. delete db;
  107. }
  108. TEST(TestTTL, GetEarlierData) {
  109. DestroyDB("testdb",Options());
  110. DB *db;
  111. if(OpenDB("testdb", &db).ok() == false) {
  112. std::cerr << "open db failed" << std::endl;
  113. abort();
  114. }
  115. uint64_t ttl1 = 15;
  116. uint64_t ttl2 = 115;
  117. uint64_t extra_size = 1;
  118. InsertData(db, ttl2);
  119. // sleep(1);
  120. InsertData(db, ttl1, extra_size); //后一个数据长度变化一下
  121. //都没过期先找到后插的
  122. Env::Default()->SleepForMicroseconds(1 * 1000000);
  123. int key_num = data_size / value_size;
  124. ReadOptions readOptions;
  125. Status status;
  126. srand(0);
  127. for (int i = 0; i < 100; i++) {
  128. int key_ = rand() % key_num+1;
  129. std::string key = std::to_string(key_);
  130. std::string value;
  131. status = db->Get(readOptions, key, &value);
  132. ASSERT_TRUE(status.ok());
  133. ASSERT_EQ(value.size(), value_size + extra_size);
  134. }
  135. //再找到前一次
  136. Env::Default()->SleepForMicroseconds(ttl1 * 1000000);
  137. // db->CompactRange(nullptr,nullptr);
  138. for (int i = 0; i < 100; i++) {
  139. int key_ = rand() % key_num+1;
  140. std::string key = std::to_string(key_);
  141. std::string value;
  142. status = db->Get(readOptions, key, &value);
  143. ASSERT_TRUE(status.ok());
  144. ASSERT_EQ(value.size(), value_size);
  145. }
  146. delete db;
  147. }
  148. TEST(TestTTL, CompactionTTL) {
  149. DestroyDB("testdb",Options());
  150. DB *db;
  151. if(OpenDB("testdb", &db).ok() == false) {
  152. std::cerr << "open db failed" << std::endl;
  153. abort();
  154. }
  155. uint64_t ttl = 10;
  156. InsertData(db, ttl);
  157. leveldb::Range ranges[1];
  158. ranges[0] = leveldb::Range("-", "A");
  159. uint64_t sizes[1];
  160. db->GetApproximateSizes(ranges, 1, sizes);
  161. ASSERT_GT(sizes[0], 0);
  162. Env::Default()->SleepForMicroseconds((ttl+1) * 1000000);
  163. // Env::Default()->SleepForMicroseconds(ttl * 1000000);
  164. db->CompactRange(nullptr, nullptr);
  165. // leveldb::Range ranges[1];
  166. ranges[0] = leveldb::Range("-", "A");
  167. // uint64_t sizes[1];
  168. db->GetApproximateSizes(ranges, 1, sizes);
  169. ASSERT_EQ(sizes[0], 0);
  170. delete db;
  171. }
  172. int main(int argc, char** argv) {
  173. // All tests currently run with the same read-only file limits.
  174. testing::InitGoogleTest(&argc, argv);
  175. return RUN_ALL_TESTS();
  176. }