LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

135 lignes
3.2 KiB

  1. #include "leveldb/db.h"
  2. #include "leveldb/filter_policy.h"
  3. #include "leveldb/env.h"
  4. #include "leveldb/db.h"
  5. using namespace leveldb;
  6. constexpr int value_size = 2048;
  7. constexpr int data_size = 128 << 20;
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <ctime>
  11. using namespace leveldb;
  12. // 3. 数据管理(Manifest/创建/恢复数据库)
  13. Status OpenDB(std::string dbName, DB **db) {
  14. Options options;
  15. options.create_if_missing = true;
  16. options.filter_policy = NewBloomFilterPolicy(10);
  17. return DB::Open(options, dbName, db);
  18. }
  19. // 1. 存储(数据结构与写入)
  20. // 4. 数据合并(Compaction)
  21. //void InsertData(DB *db) {
  22. // WriteOptions writeOptions;
  23. // int key_num = data_size / value_size;
  24. // srand(static_cast<unsigned int>(time(0)));
  25. //
  26. // for (int i = 0; i < key_num; i++) {
  27. // int key_ = rand() % key_num+1;
  28. // std::string key = std::to_string(key_);
  29. // std::string value(value_size, 'a');
  30. // db->Put(writeOptions, key, value);
  31. // }
  32. //}
  33. //void InsertData(DB *db, uint64_t ttl/* second */) {
  34. // WriteOptions writeOptions;
  35. // int key_num = data_size / value_size;
  36. // srand(static_cast<unsigned int>(time(0)));
  37. // for (int i = 0; i < key_num; i++) {
  38. // int key_ = rand() % key_num+1;
  39. // key_ = 1;
  40. // std::string key = std::to_string(key_);
  41. // std::string value(value_size, 'a');
  42. // db->Put(writeOptions, key, value, ttl);
  43. // std::cout << "time to alive" << ttl << std::endl;
  44. // break;
  45. // }
  46. //}
  47. void InsertData(DB *db, uint64_t ttl/* second */) {
  48. WriteOptions writeOptions;
  49. int key_num = data_size / value_size;
  50. srand(0);
  51. for (int i = 0; i < key_num; i++) {
  52. int key_ = rand() % key_num+1;
  53. std::string key = std::to_string(key_);
  54. std::string value(value_size, 'a');
  55. db->Put(writeOptions, key, value, ttl);
  56. //break;
  57. }
  58. }
  59. //int main(){
  60. // DB *db;
  61. //
  62. // if(OpenDB("testdb", &db).ok() == false) {
  63. // std::cerr << "open db failed" << std::endl;
  64. // abort();
  65. // }
  66. //
  67. // uint64_t ttl = 20;
  68. // InsertData(db, ttl);
  69. //
  70. // leveldb::Range ranges[1];
  71. // ranges[0] = leveldb::Range("-", "A");
  72. // uint64_t sizes[1];
  73. // db->GetApproximateSizes(ranges, 1, sizes);
  74. // //ASSERT_GT(sizes[0], 0);
  75. // assert(sizes[0] > 0);
  76. // Env::Default()->SleepForMicroseconds(ttl * 1000000);
  77. //
  78. // db->CompactRange(nullptr, nullptr);
  79. //
  80. // leveldb::Range ranges2[1];
  81. // ranges2[0] = leveldb::Range("-", "A");
  82. // uint64_t sizes2[1];
  83. // db->GetApproximateSizes(ranges2, 1, sizes2);
  84. // assert(sizes2[0] == 0);
  85. //}
  86. int main() {
  87. DB *db;
  88. if(OpenDB("testdb", &db).ok() == false) {
  89. std::cerr << "open db failed" << std::endl;
  90. abort();
  91. }
  92. uint64_t ttl = 20;
  93. // InsertData(db, ttl);
  94. WriteOptions writeOptions;
  95. int key_ = 1;
  96. std::string key = std::to_string(key_);
  97. std::string value(1, 'a');
  98. db->Put(writeOptions, key, value, ttl);
  99. std::cout << "time to alive" << ttl << std::endl;
  100. ReadOptions readOptions;
  101. std::string value_read;
  102. Status status = db->Get(readOptions, key, &value);
  103. //std::cout<<"start sleep for " <<
  104. Env::Default()->SleepForMicroseconds(ttl * 1000000);
  105. key_ = 1;
  106. key = std::to_string(key_);
  107. std::string value_read_second;
  108. status = db->Get(readOptions, key, &value_read_second);
  109. assert(status.ok() != true);
  110. return 0;
  111. }