LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

193 rader
4.6 KiB

1 månad sedan
1 månad sedan
1 månad sedan
  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. #include "gtest/gtest.h"
  11. #include "leveldb/env.h"
  12. #include "leveldb/db.h"
  13. using namespace leveldb;
  14. constexpr int value_size = 2048;
  15. constexpr int data_size = 128 << 20;
  16. Status OpenDB(std::string dbName, DB **db) {
  17. Options options;
  18. options.create_if_missing = true;
  19. return DB::Open(options, dbName, db);
  20. }
  21. void InsertData(DB *db, uint64_t ttl/* second */) {
  22. WriteOptions writeOptions;
  23. int key_num = data_size / value_size;
  24. srand(static_cast<unsigned int>(time(0)));
  25. for (int i = 0; i < key_num; i++) {
  26. int key_ = rand() % key_num+1;
  27. std::string key = std::to_string(key_);
  28. std::string value(value_size, 'a');
  29. db->Put(writeOptions, key, value, ttl);
  30. }
  31. }
  32. void GetData(DB *db, int size = (1 << 30)){
  33. ReadOptions readOptions;
  34. int key_num = data_size / value_size;
  35. // 点查
  36. srand(static_cast<unsigned int>(time(0)));
  37. for (int i = 0; i < 100; i++) {
  38. int key_ = rand() % key_num+1;
  39. std::string key = std::to_string(key_);
  40. std::string value;
  41. db->Get(readOptions, key, &value);
  42. }
  43. }
  44. TEST(TestTTL, ReadTTL) {
  45. DB *db;
  46. if(OpenDB("testdb", &db).ok() == false) {
  47. std::cerr << "open db failed" << std::endl;
  48. abort();
  49. }
  50. uint64_t ttl = 20;
  51. InsertData(db, ttl);
  52. ReadOptions readOptions;
  53. Status status;
  54. int key_num = data_size / value_size;
  55. srand(static_cast<unsigned int>(time(0)));
  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_TRUE(status.ok());
  62. }
  63. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  64. for (int i = 0; i < 100; i++) {
  65. int key_ = rand() % key_num+1;
  66. std::string key = std::to_string(key_);
  67. std::string value;
  68. status = db->Get(readOptions, key, &value);
  69. ASSERT_FALSE(status.ok());
  70. }
  71. }
  72. return DB::Open(options, dbName, db);
  73. }
  74. void InsertData(DB *db, uint64_t ttl/* second */) {
  75. WriteOptions writeOptions;
  76. int key_num = data_size / value_size;
  77. srand(static_cast<unsigned int>(time(0)));
  78. for (int i = 0; i < key_num; i++) {
  79. int key_ = rand() % key_num+1;
  80. std::string key = std::to_string(key_);
  81. std::string value(value_size, 'a');
  82. db->Put(writeOptions, key, value, ttl);
  83. }
  84. }
  85. void GetData(DB *db, int size = (1 << 30)) {
  86. ReadOptions readOptions;
  87. int key_num = data_size / value_size;
  88. // 点查
  89. srand(static_cast<unsigned int>(time(0)));
  90. for (int i = 0; i < 100; i++) {
  91. int key_ = rand() % key_num+1;
  92. std::string key = std::to_string(key_);
  93. std::string value;
  94. db->Get(readOptions, key, &value);
  95. }
  96. }
  97. TEST(TestTTL, ReadTTL) {
  98. DB *db;
  99. if(OpenDB("testdb", &db).ok() == false) {
  100. std::cerr << "open db failed" << std::endl;
  101. abort();
  102. }
  103. uint64_t ttl = 20;
  104. InsertData(db, ttl);
  105. ReadOptions readOptions;
  106. Status status;
  107. int key_num = data_size / value_size;
  108. srand(static_cast<unsigned int>(time(0)));
  109. for (int i = 0; i < 100; i++) {
  110. int key_ = rand() % key_num+1;
  111. std::string key = std::to_string(key_);
  112. std::string value;
  113. status = db->Get(readOptions, key, &value);
  114. ASSERT_TRUE(status.ok());
  115. }
  116. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  117. for (int i = 0; i < 100; i++) {
  118. int key_ = rand() % key_num+1;
  119. std::string key = std::to_string(key_);
  120. std::string value;
  121. status = db->Get(readOptions, key, &value);
  122. ASSERT_FALSE(status.ok());
  123. }
  124. }
  125. TEST(TestTTL, CompactionTTL) {
  126. DB *db;
  127. if(OpenDB("testdb", &db).ok() == false) {
  128. std::cerr << "open db failed" << std::endl;
  129. abort();
  130. }
  131. uint64_t ttl = 20;
  132. InsertData(db, ttl);
  133. leveldb::Range ranges[1];
  134. ranges[0] = leveldb::Range("-", "A");
  135. uint64_t sizes[1];
  136. db->GetApproximateSizes(ranges, 1, sizes);
  137. ASSERT_GT(sizes[0], 0);
  138. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  139. db->CompactRange(nullptr, nullptr);
  140. leveldb::Range ranges[1];
  141. ranges[0] = leveldb::Range("-", "A");
  142. uint64_t sizes[1];
  143. db->GetApproximateSizes(ranges, 1, sizes);
  144. ASSERT_EQ(sizes[0], 0);
  145. }
  146. int main(int argc, char** argv) {
  147. // All tests currently run with the same read-only file limits.
  148. testing::InitGoogleTest(&argc, argv);
  149. return RUN_ALL_TESTS();
  150. }