作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

173 rindas
4.3 KiB

pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
pirms 3 nedēļām
pirms 1 mēnesi
  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. 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(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+2) * 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. delete db;
  63. }
  64. TEST(TestTTL, CompactionTTL) {
  65. DB *db;
  66. if(OpenDB("testdb", &db).ok() == false) {
  67. std::cerr << "open db failed" << std::endl;
  68. abort();
  69. }
  70. uint64_t ttl = 20;
  71. InsertData(db, ttl);
  72. leveldb::Range ranges[1];
  73. ranges[0] = leveldb::Range("-", "A");
  74. uint64_t sizes[1];
  75. db->GetApproximateSizes(ranges, 1, sizes);
  76. ASSERT_GT(sizes[0], 0);
  77. Env::Default()->SleepForMicroseconds((ttl+1) * 1000000);
  78. db->CompactRange(nullptr, nullptr);
  79. leveldb::Range ranges_1[1];
  80. ranges[0] = leveldb::Range("-", "A");
  81. uint64_t sizes_1[1];
  82. db->GetApproximateSizes(ranges_1, 1, sizes_1);
  83. ASSERT_EQ(sizes_1[0], 0);
  84. delete db;
  85. }
  86. TEST(TestTTL, OurTTL) {
  87. DB *db;
  88. WriteOptions writeOptions;
  89. ReadOptions readOptions;
  90. if(OpenDB("testdb_for_XOY", &db).ok() == false) {
  91. std::cerr << "open db failed" << std::endl;
  92. abort();
  93. }
  94. for (int i = 0; i < 10000; i++) {
  95. std::string key = std::to_string(i);
  96. std::string value = std::to_string(i);
  97. db->Put(writeOptions, key, value);
  98. }
  99. for (int i = 0; i < 10000; i++) {
  100. std::string key = std::to_string(i);
  101. std::string value = std::to_string(i*2);
  102. db->Put(writeOptions, key, value, 30);
  103. }
  104. for (int i = 0; i < 10000; i++) {
  105. std::string key = std::to_string(i);
  106. std::string value = std::to_string(i*3);
  107. db->Put(writeOptions, key, value, 15);
  108. }
  109. for (int i = 0; i < 10000; i++) {
  110. std::string key = std::to_string(i);
  111. std::string value;
  112. Status status = db->Get(readOptions, key, &value);
  113. ASSERT_TRUE(status.ok());
  114. ASSERT_TRUE(value==std::to_string(i*3));
  115. }
  116. Env::Default()->SleepForMicroseconds((15+1) * 1000000);
  117. for (int i = 0; i < 10000; i++) {
  118. std::string key = std::to_string(i);
  119. std::string value;
  120. Status status = db->Get(readOptions, key, &value);
  121. ASSERT_TRUE(status.ok());
  122. ASSERT_TRUE(value==std::to_string(i*2));
  123. }
  124. Env::Default()->SleepForMicroseconds((15+1) * 1000000);
  125. for (int i = 0; i < 10000; i++) {
  126. std::string key = std::to_string(i);
  127. std::string value;
  128. Status status = db->Get(readOptions, key, &value);
  129. ASSERT_TRUE(status.ok());
  130. ASSERT_TRUE(value==std::to_string(i));
  131. }
  132. delete db;
  133. }
  134. int main(int argc, char** argv) {
  135. // All tests currently run with the same read-only file limits.
  136. testing::InitGoogleTest(&argc, argv);
  137. return RUN_ALL_TESTS();
  138. }