issue320_test.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2019 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include <cstdint>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "leveldb/db.h"
  11. #include "leveldb/write_batch.h"
  12. #include "util/testharness.h"
  13. namespace leveldb {
  14. namespace {
  15. // Creates a random number in the range of [0, max).
  16. int GenerateRandomNumber(int max) { return std::rand() % max; }
  17. std::string CreateRandomString(int32_t index) {
  18. static const size_t len = 1024;
  19. char bytes[len];
  20. size_t i = 0;
  21. while (i < 8) {
  22. bytes[i] = 'a' + ((index >> (4 * i)) & 0xf);
  23. ++i;
  24. }
  25. while (i < sizeof(bytes)) {
  26. bytes[i] = 'a' + GenerateRandomNumber(26);
  27. ++i;
  28. }
  29. return std::string(bytes, sizeof(bytes));
  30. }
  31. } // namespace
  32. class Issue320 {};
  33. TEST(Issue320, Test) {
  34. std::srand(0);
  35. bool delete_before_put = false;
  36. bool keep_snapshots = true;
  37. std::vector<std::unique_ptr<std::pair<std::string, std::string>>> test_map(
  38. 10000);
  39. std::vector<Snapshot const*> snapshots(100, nullptr);
  40. DB* db;
  41. Options options;
  42. options.create_if_missing = true;
  43. std::string dbpath = test::TmpDir() + "/leveldb_issue320_test";
  44. ASSERT_OK(DB::Open(options, dbpath, &db));
  45. uint32_t target_size = 10000;
  46. uint32_t num_items = 0;
  47. uint32_t count = 0;
  48. std::string key;
  49. std::string value, old_value;
  50. WriteOptions writeOptions;
  51. ReadOptions readOptions;
  52. while (count < 200000) {
  53. if ((++count % 1000) == 0) {
  54. std::cout << "count: " << count << std::endl;
  55. }
  56. int index = GenerateRandomNumber(test_map.size());
  57. WriteBatch batch;
  58. if (test_map[index] == nullptr) {
  59. num_items++;
  60. test_map[index].reset(new std::pair<std::string, std::string>(
  61. CreateRandomString(index), CreateRandomString(index)));
  62. batch.Put(test_map[index]->first, test_map[index]->second);
  63. } else {
  64. ASSERT_OK(db->Get(readOptions, test_map[index]->first, &old_value));
  65. if (old_value != test_map[index]->second) {
  66. std::cout << "ERROR incorrect value returned by Get" << std::endl;
  67. std::cout << " count=" << count << std::endl;
  68. std::cout << " old value=" << old_value << std::endl;
  69. std::cout << " test_map[index]->second=" << test_map[index]->second
  70. << std::endl;
  71. std::cout << " test_map[index]->first=" << test_map[index]->first
  72. << std::endl;
  73. std::cout << " index=" << index << std::endl;
  74. ASSERT_EQ(old_value, test_map[index]->second);
  75. }
  76. if (num_items >= target_size && GenerateRandomNumber(100) > 30) {
  77. batch.Delete(test_map[index]->first);
  78. test_map[index] = nullptr;
  79. --num_items;
  80. } else {
  81. test_map[index]->second = CreateRandomString(index);
  82. if (delete_before_put) batch.Delete(test_map[index]->first);
  83. batch.Put(test_map[index]->first, test_map[index]->second);
  84. }
  85. }
  86. ASSERT_OK(db->Write(writeOptions, &batch));
  87. if (keep_snapshots && GenerateRandomNumber(10) == 0) {
  88. int i = GenerateRandomNumber(snapshots.size());
  89. if (snapshots[i] != nullptr) {
  90. db->ReleaseSnapshot(snapshots[i]);
  91. }
  92. snapshots[i] = db->GetSnapshot();
  93. }
  94. }
  95. for (Snapshot const* snapshot : snapshots) {
  96. if (snapshot) {
  97. db->ReleaseSnapshot(snapshot);
  98. }
  99. }
  100. delete db;
  101. DestroyDB(dbpath, options);
  102. }
  103. } // namespace leveldb
  104. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }