From 1117ec5bab95b0f95243c76afdea2edbb947fa84 Mon Sep 17 00:00:00 2001 From: zhangguangchao <1614439+zgcbj@users.noreply.github.com> Date: Thu, 29 Sep 2022 02:45:21 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Remove=20ExtractUserKey=20of=20start?= =?UTF-8?q?=E3=80=81end=20in=20CreateCompactionFilter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangguangchao <1614439+zgcbj@users.noreply.github.com> --- db/compaction/compaction.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/db/compaction/compaction.cc b/db/compaction/compaction.cc index 181df406d2c..4667ab1379a 100644 --- a/db/compaction/compaction.cc +++ b/db/compaction/compaction.cc @@ -545,10 +545,8 @@ std::unique_ptr Compaction::CreateCompactionFilter( context.is_full_compaction = is_full_compaction_; context.is_manual_compaction = is_manual_compaction_; context.is_bottommost_level = bottommost_level_; - context.start_key = - (start == nullptr) ? GetSmallestUserKey() : ExtractUserKey(*start); - context.end_key = - (end == nullptr) ? GetLargestUserKey() : ExtractUserKey(*end); + context.start_key = (start == nullptr) ? GetSmallestUserKey() : *start; + context.end_key = (end == nullptr) ? GetLargestUserKey() : *end; context.is_end_key_inclusive = (end == nullptr); for (auto l = inputs_.begin(); l != inputs_.end(); ++l) { for (auto f = l->files.begin(); f != l->files.end(); ++f) { From bfb5207a353fd677a78dad492bb2dc151f5befab Mon Sep 17 00:00:00 2001 From: zhangguangchao <1614439+zgcbj@users.noreply.github.com> Date: Tue, 18 Oct 2022 07:38:14 +0000 Subject: [PATCH 2/2] add unittest SubCompactionWithFilterFactory Signed-off-by: zhangguangchao <1614439+zgcbj@users.noreply.github.com> --- db/compaction/compaction_service_test.cc | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/db/compaction/compaction_service_test.cc b/db/compaction/compaction_service_test.cc index 5a4ff7799c5..b99bdaaaeb7 100644 --- a/db/compaction/compaction_service_test.cc +++ b/db/compaction/compaction_service_test.cc @@ -546,6 +546,59 @@ TEST_P(CompactionServiceTest, CompactionFilter) { ASSERT_GE(my_cs->GetCompactionNum(), 1); } +class EmptyFilter : public CompactionFilter { + public: + bool Filter(int /*level*/, const Slice& /*key*/, const Slice& /*value*/, + std::string* /*new_value*/, + bool* /*value_changed*/) const override { + return false; + } + + const char* Name() const override { return "EmptyFilter"; } +}; + +class EmptyFilterFactory : public CompactionFilterFactory { + public: + explicit EmptyFilterFactory() : compaction_filter_created_(false) {} + + std::unique_ptr CreateCompactionFilter( + const CompactionFilter::Context&) override { + compaction_filter_created_ = true; + return std::unique_ptr(new EmptyFilter()); + } + + bool compaction_filter_created() const { return compaction_filter_created_; } + + const char* Name() const override { return "EmptyFilterFactory"; } + bool compaction_filter_created_; +}; + +TEST_P(CompactionServiceTest, SubCompactionWithFilterFactory) { + EmptyFilterFactory* filter = new EmptyFilterFactory(); + Options options = CurrentOptions(); + options.compaction_filter_factory.reset(filter); + options.max_subcompactions = 10; + options.target_file_size_base = 1 << 10; // 1KB + options.disable_auto_compactions = true; + ReopenWithCompactionService(&options); + + GenerateTestData(); + VerifyTestData(); + + auto my_cs = GetCompactionService(); + int compaction_num_before = my_cs->GetCompactionNum(); + + auto cro = CompactRangeOptions(); + cro.max_subcompactions = 10; + Status s = db_->CompactRange(cro, nullptr, nullptr); + ASSERT_OK(s); + VerifyTestData(); + int compaction_num = my_cs->GetCompactionNum() - compaction_num_before; + // make sure there's sub-compaction by checking the compaction number + ASSERT_GE(compaction_num, 2); + ASSERT_TRUE(filter->compaction_filter_created()); +} + TEST_P(CompactionServiceTest, Snapshot) { Options options = CurrentOptions(); ReopenWithCompactionService(&options);