Separate compaction filter for each compaction

Summary:
If we have same compaction filter for each compaction,
application cannot know about the different compaction processes.
Later on, we can put in more details in compaction filter for the
application to consume and use it according to its needs. For e.g. In
the universal compaction, we have a compaction process involving all the
files while others don't involve all the files. Applications may want to
collect some stats only when during full compaction.

Test Plan: run existing unit tests

Reviewers: haobo, dhruba

Reviewed By: dhruba

CC: xinyaohu, leveldb

Differential Revision: https://reviews.facebook.net/D12057
This commit is contained in:
sumeet
2013-08-13 10:56:20 -07:00
parent 9f6b8f0032
commit 3b81df34bd
8 changed files with 174 additions and 16 deletions

View File

@@ -158,6 +158,10 @@ Options SanitizeOptions(const std::string& dbname,
if (result.soft_rate_limit > result.hard_rate_limit) {
result.soft_rate_limit = result.hard_rate_limit;
}
if (result.compaction_filter &&
result.compaction_filter_factory->CreateCompactionFilter().get()) {
Log(result.info_log, "Both filter and factory specified. Using filter");
}
return result;
}
@@ -1784,6 +1788,13 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) {
MergeHelper merge(user_comparator(), options_.merge_operator,
options_.info_log.get(),
false /* internal key corruption is expected */);
auto compaction_filter = options_.compaction_filter;
std::unique_ptr<CompactionFilter> compaction_filter_from_factory = nullptr;
if (!compaction_filter) {
compaction_filter_from_factory = std::move(
options_.compaction_filter_factory->CreateCompactionFilter());
compaction_filter = compaction_filter_from_factory.get();
}
for (; input->Valid() && !shutting_down_.Acquire_Load(); ) {
// Prioritize immutable compaction work
if (imm_.imm_flush_needed.NoBarrier_Load() != nullptr) {
@@ -1830,7 +1841,7 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) {
visible_in_snapshot = kMaxSequenceNumber;
// apply the compaction filter to the first occurrence of the user key
if (options_.compaction_filter &&
if (compaction_filter &&
ikey.type == kTypeValue &&
(visible_at_tip || ikey.sequence > latest_snapshot)) {
// If the user has specified a compaction filter and the sequence
@@ -1841,7 +1852,7 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) {
bool value_changed = false;
compaction_filter_value.clear();
bool to_delete =
options_.compaction_filter->Filter(compact->compaction->level(),
compaction_filter->Filter(compact->compaction->level(),
ikey.user_key, value,
&compaction_filter_value,
&value_changed);

View File

@@ -1698,12 +1698,52 @@ class ChangeFilter : public CompactionFilter {
const int argv_;
};
class KeepFilterFactory : public CompactionFilterFactory {
public:
virtual std::unique_ptr<CompactionFilter>
CreateCompactionFilter() override {
return std::unique_ptr<CompactionFilter>(new KeepFilter());
}
virtual const char* Name() const override {
return "KeepFilterFactory";
}
};
class DeleteFilterFactory : public CompactionFilterFactory {
public:
virtual std::unique_ptr<CompactionFilter>
CreateCompactionFilter() override {
return std::unique_ptr<CompactionFilter>(new DeleteFilter());
}
virtual const char* Name() const override {
return "DeleteFilterFactory";
}
};
class ChangeFilterFactory : public CompactionFilterFactory {
public:
explicit ChangeFilterFactory(int argv) : argv_(argv) {}
virtual std::unique_ptr<CompactionFilter>
CreateCompactionFilter() override {
return std::unique_ptr<CompactionFilter>(new ChangeFilter(argv_));
}
virtual const char* Name() const override {
return "ChangeFilterFactory";
}
private:
const int argv_;
};
TEST(DBTest, CompactionFilter) {
Options options = CurrentOptions();
options.num_levels = 3;
options.max_mem_compaction_level = 0;
auto keep_filter = std::make_shared<KeepFilter>();
options.compaction_filter = keep_filter.get();
options.compaction_filter_factory = std::make_shared<KeepFilterFactory>();
Reopen(&options);
// Write 100K keys, these are written to a few files in L0.
@@ -1778,8 +1818,7 @@ TEST(DBTest, CompactionFilter) {
// create a new database with the compaction
// filter in such a way that it deletes all keys
auto delete_filter = std::make_shared<DeleteFilter>();
options.compaction_filter = delete_filter.get();
options.compaction_filter_factory = std::make_shared<DeleteFilterFactory>();
options.create_if_missing = true;
DestroyAndReopen(&options);
@@ -1843,8 +1882,8 @@ TEST(DBTest, CompactionFilterWithValueChange) {
Options options = CurrentOptions();
options.num_levels = 3;
options.max_mem_compaction_level = 0;
auto change_filter = std::make_shared<ChangeFilter>(100);
options.compaction_filter = change_filter.get();
options.compaction_filter_factory =
std::make_shared<ChangeFilterFactory>(100);
Reopen(&options);
// Write 100K+1 keys, these are written to a few files