作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

101 lines
2.3 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. constexpr int key_num = 10000;
  8. Status OpenDB(std::string dbName, DB **db) {
  9. Options options;
  10. options.create_if_missing = true;
  11. return DB::Open(options, dbName, db);
  12. }
  13. void InsertData(DB *db, uint64_t ttl/* second */) {
  14. WriteOptions writeOptions;
  15. for (int i = 0; i < key_num; i++) {
  16. int key_ = i;
  17. std::string key = std::to_string(key_);
  18. std::string value(value_size, 'a');
  19. db->Put(writeOptions, key, value, ttl);
  20. }
  21. }
  22. TEST(TestTTL, ReadTTL) {
  23. DB *db;
  24. if(OpenDB("testdb", &db).ok() == false) {
  25. std::cerr << "open db failed" << std::endl;
  26. abort();
  27. }
  28. uint64_t ttl = 10;
  29. InsertData(db, ttl);
  30. ReadOptions readOptions;
  31. Status status;
  32. srand(static_cast<unsigned int>(time(0)));
  33. for (int i = 0; i < key_num; i++) {
  34. int key_ = i;
  35. std::string key = std::to_string(key_);
  36. std::string value;
  37. status = db->Get(readOptions, key, &value);
  38. ASSERT_TRUE(status.ok());
  39. }
  40. Env::Default()->SleepForMicroseconds((ttl+5) * 1000000);
  41. for (int i = 0; i < key_num; i++) {
  42. int key_ = i;
  43. std::string key = std::to_string(key_);
  44. std::string value;
  45. status = db->Get(readOptions, key, &value);
  46. if(status.ok()){
  47. std::cout<<"!!";
  48. }
  49. ASSERT_FALSE(status.ok());
  50. }
  51. }
  52. // TEST(TestTTL, CompactionTTL) {
  53. // DB *db;
  54. // if(OpenDB("testdb", &db).ok() == false) {
  55. // std::cerr << "open db failed" << std::endl;
  56. // abort();
  57. // }
  58. // uint64_t ttl = 20;
  59. // InsertData(db, ttl);
  60. // leveldb::Range ranges[1];
  61. // ranges[0] = leveldb::Range("-", "A");
  62. // uint64_t sizes[1];
  63. // db->GetApproximateSizes(ranges, 1, sizes);
  64. // ASSERT_GT(sizes[0], 0);
  65. // Env::Default()->SleepForMicroseconds(ttl * 1000000);
  66. // db->CompactRange(nullptr, nullptr);
  67. // leveldb::Range ranges[1];
  68. // ranges[0] = leveldb::Range("-", "A");
  69. // uint64_t sizes[1];
  70. // db->GetApproximateSizes(ranges, 1, sizes);
  71. // ASSERT_EQ(sizes[0], 0);
  72. // }
  73. int main(int argc, char** argv) {
  74. // All tests currently run with the same read-only file limits.
  75. testing::InitGoogleTest(&argc, argv);
  76. return RUN_ALL_TESTS();
  77. }