db_clip_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "db/db_test_util.h"
  6. #include "port/port.h"
  7. #include "util/random.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. class DBClipTest : public DBTestBase {
  10. public:
  11. DBClipTest() : DBTestBase("db_clip_test", /*env_do_fsync=*/true) {}
  12. };
  13. TEST_F(DBClipTest, TestClipRange) {
  14. Options options = CurrentOptions();
  15. options.write_buffer_size = 10 * 1024 * 1024;
  16. options.max_bytes_for_level_multiplier = 2;
  17. options.num_levels = 3;
  18. options.max_background_compactions = 3;
  19. options.disable_auto_compactions = true;
  20. options.statistics = CreateDBStatistics();
  21. DestroyAndReopen(options);
  22. int32_t value_size = 10 * 1024; // 10 KB
  23. Random rnd(301);
  24. std::map<int32_t, std::string> values;
  25. // file [0 => 100), [100 => 200), ... [900, 1000)
  26. for (auto i = 0; i < 10; i++) {
  27. for (auto j = 0; j < 100; j++) {
  28. auto k = i * 100 + j;
  29. values[k] = rnd.RandomString(value_size);
  30. ASSERT_OK(Put(Key(k), values[k]));
  31. }
  32. ASSERT_OK(Flush());
  33. }
  34. ASSERT_EQ("10", FilesPerLevel(0));
  35. auto begin_key = Key(251), end_key = Key(751);
  36. ASSERT_OK(
  37. db_->ClipColumnFamily(db_->DefaultColumnFamily(), begin_key, end_key));
  38. for (auto i = 0; i < 251; i++) {
  39. ReadOptions ropts;
  40. std::string result;
  41. auto s = db_->Get(ropts, Key(i), &result);
  42. ASSERT_TRUE(s.IsNotFound());
  43. }
  44. for (auto i = 251; i < 751; i++) {
  45. ASSERT_EQ(Get(Key(i)), values[i]);
  46. }
  47. for (auto i = 751; i < 1000; i++) {
  48. ReadOptions ropts;
  49. std::string result;
  50. auto s = db_->Get(ropts, Key(i), &result);
  51. ASSERT_TRUE(s.IsNotFound());
  52. }
  53. std::vector<LiveFileMetaData> all_metadata;
  54. db_->GetLiveFilesMetaData(&all_metadata);
  55. for (auto& md : all_metadata) {
  56. // make sure clip_begin_key <= file_smallestkey <= file_largestkey <=
  57. // clip_end_key
  58. bool in_range = false;
  59. if (options.comparator->Compare(begin_key, md.smallestkey) <= 0 &&
  60. options.comparator->Compare(end_key, md.largestkey) > 0) {
  61. in_range = true;
  62. }
  63. ASSERT_TRUE(in_range);
  64. }
  65. CompactRangeOptions compact_options;
  66. compact_options.change_level = true;
  67. compact_options.target_level = 2;
  68. ASSERT_OK(db_->CompactRange(compact_options, nullptr, nullptr));
  69. ASSERT_EQ("0,0,3", FilesPerLevel(0));
  70. for (auto i = 0; i < 10; i += 2) {
  71. for (auto j = 0; j < 100; j++) {
  72. auto k = i * 100 + j;
  73. ASSERT_OK(Put(Key(k), values[k]));
  74. }
  75. ASSERT_OK(Flush());
  76. }
  77. ASSERT_EQ("5,0,3", FilesPerLevel(0));
  78. ASSERT_OK(dbfull()->TEST_CompactRange(0, nullptr, nullptr));
  79. ASSERT_EQ("0,5,3", FilesPerLevel(0));
  80. for (auto i = 1; i < 10; i += 2) {
  81. for (auto j = 0; j < 100; j++) {
  82. auto k = i * 100 + j;
  83. ASSERT_OK(Put(Key(k), values[k]));
  84. }
  85. ASSERT_OK(Flush());
  86. }
  87. ASSERT_EQ("5,5,3", FilesPerLevel(0));
  88. auto begin_key_2 = Key(222), end_key_2 = Key(888);
  89. ASSERT_OK(db_->ClipColumnFamily(db_->DefaultColumnFamily(), begin_key_2,
  90. end_key_2));
  91. for (auto i = 0; i < 222; i++) {
  92. ReadOptions ropts;
  93. std::string result;
  94. auto s = db_->Get(ropts, Key(i), &result);
  95. ASSERT_TRUE(s.IsNotFound());
  96. }
  97. for (auto i = 222; i < 888; i++) {
  98. ASSERT_EQ(Get(Key(i)), values[i]);
  99. }
  100. for (auto i = 888; i < 1000; i++) {
  101. ReadOptions ropts;
  102. std::string result;
  103. auto s = db_->Get(ropts, Key(i), &result);
  104. ASSERT_TRUE(s.IsNotFound());
  105. }
  106. std::vector<LiveFileMetaData> all_metadata_2;
  107. db_->GetLiveFilesMetaData(&all_metadata_2);
  108. for (auto& md : all_metadata_2) {
  109. // make sure clip_begin_key <= file_smallestkey <= file_largestkey <=
  110. // clip_end_key
  111. bool in_range = false;
  112. if (begin_key_2.compare(md.smallestkey) <= 0 &&
  113. end_key_2.compare(md.largestkey) > 0) {
  114. in_range = true;
  115. }
  116. ASSERT_TRUE(in_range);
  117. }
  118. }
  119. } // namespace ROCKSDB_NAMESPACE
  120. int main(int argc, char** argv) {
  121. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  122. ::testing::InitGoogleTest(&argc, argv);
  123. return RUN_ALL_TESTS();
  124. }