LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

126 lines
3.5 KiB

2 months ago
  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 "gtest/gtest.h"
  11. #include "leveldb/db.h"
  12. #include "leveldb/write_batch.h"
  13. #include "util/testutil.h"
  14. namespace leveldb {
  15. namespace {
  16. // Creates a random number in the range of [0, max).
  17. int GenerateRandomNumber(int max) { return std::rand() % max; }
  18. std::string CreateRandomString(int32_t index) {
  19. static const size_t len = 1024;
  20. char bytes[len];
  21. size_t i = 0;
  22. while (i < 8) {
  23. bytes[i] = 'a' + ((index >> (4 * i)) & 0xf);
  24. ++i;
  25. }
  26. while (i < sizeof(bytes)) {
  27. bytes[i] = 'a' + GenerateRandomNumber(26);
  28. ++i;
  29. }
  30. return std::string(bytes, sizeof(bytes));
  31. }
  32. } // namespace
  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 = testing::TempDir() + "leveldb_issue320_test";
  44. ASSERT_LEVELDB_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_LEVELDB_OK(
  65. db->Get(readOptions, test_map[index]->first, &old_value));
  66. if (old_value != test_map[index]->second) {
  67. std::cout << "ERROR incorrect value returned by Get" << std::endl;
  68. std::cout << " count=" << count << std::endl;
  69. std::cout << " old value=" << old_value << std::endl;
  70. std::cout << " test_map[index]->second=" << test_map[index]->second
  71. << std::endl;
  72. std::cout << " test_map[index]->first=" << test_map[index]->first
  73. << std::endl;
  74. std::cout << " index=" << index << std::endl;
  75. ASSERT_EQ(old_value, test_map[index]->second);
  76. }
  77. if (num_items >= target_size && GenerateRandomNumber(100) > 30) {
  78. batch.Delete(test_map[index]->first);
  79. test_map[index] = nullptr;
  80. --num_items;
  81. } else {
  82. test_map[index]->second = CreateRandomString(index);
  83. if (delete_before_put) batch.Delete(test_map[index]->first);
  84. batch.Put(test_map[index]->first, test_map[index]->second);
  85. }
  86. }
  87. ASSERT_LEVELDB_OK(db->Write(writeOptions, &batch));
  88. if (keep_snapshots && GenerateRandomNumber(10) == 0) {
  89. int i = GenerateRandomNumber(snapshots.size());
  90. if (snapshots[i] != nullptr) {
  91. db->ReleaseSnapshot(snapshots[i]);
  92. }
  93. snapshots[i] = db->GetSnapshot();
  94. }
  95. }
  96. for (Snapshot const* snapshot : snapshots) {
  97. if (snapshot) {
  98. db->ReleaseSnapshot(snapshot);
  99. }
  100. }
  101. delete db;
  102. DestroyDB(dbpath, options);
  103. }
  104. } // namespace leveldb