Skip to content

Commit

Permalink
filter deletion in compaction filter
Browse files Browse the repository at this point in the history
Signed-off-by: tabokie <[email protected]>
  • Loading branch information
tabokie committed Aug 1, 2023
1 parent 5b9cef9 commit 66dcaa3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions db/compaction/compaction_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ void CompactionIterator::Next() {

bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
Slice* skip_until) {
// Note: Filtering kTypeDeletion is UB. Only `UnsafeFilter` accepts it.
if (!compaction_filter_ ||
(ikey_.type != kTypeValue && ikey_.type != kTypeBlobIndex)) {
(ikey_.type != kTypeValue && ikey_.type != kTypeBlobIndex &&
ikey_.type != kTypeDeletion)) {
return true;
}
bool error = false;
Expand All @@ -210,9 +212,12 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
CompactionFilter::Decision filter = CompactionFilter::Decision::kUndetermined;
compaction_filter_value_.clear();
compaction_filter_skip_until_.Clear();
CompactionFilter::ValueType value_type =
ikey_.type == kTypeValue ? CompactionFilter::ValueType::kValue
: CompactionFilter::ValueType::kBlobIndex;
CompactionFilter::ValueType value_type = CompactionFilter::ValueType::kValue;
if (ikey_.type == kTypeBlobIndex) {
value_type = CompactionFilter::ValueType::kBlobIndex;
} else if (ikey_.type == kTypeDeletion) {
value_type = CompactionFilter::ValueType::kDeletion;
}
// Hack: pass internal key to BlobIndexCompactionFilter since it needs
// to get sequence number.
assert(compaction_filter_);
Expand Down Expand Up @@ -273,7 +278,7 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
}
}
if (CompactionFilter::Decision::kUndetermined == filter) {
filter = compaction_filter_->FilterV3(
filter = compaction_filter_->UnsafeFilter(
level_, filter_key, ikey_.sequence, value_type,
blob_value_.empty() ? value_ : blob_value_, &compaction_filter_value_,
compaction_filter_skip_until_.rep());
Expand Down
16 changes: 16 additions & 0 deletions include/rocksdb/compaction_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CompactionFilter : public Customizable {
kValue,
kMergeOperand,
kBlobIndex, // used internally by BlobDB.
kDeletion, // used only by TiKV's region range filter.
};

enum class Decision {
Expand Down Expand Up @@ -212,6 +213,21 @@ class CompactionFilter : public Customizable {
skip_until);
}

// This interface is reserved for TiKV's region range filter. Only this
// interface can accept `value_type=kTypeDeletion`.
virtual Decision UnsafeFilter(int level, const Slice& key,
SequenceNumber seqno, ValueType value_type,
const Slice& existing_value,
std::string* new_value,
std::string* skip_until) const {
if (value_type != ValueType::kTypeDeletion) {
return FilterV3(level, key, seqno, value_type, existing_value, new_value,
skip_until);
} else {
return Decision::kKeep;
}
}

// Internal (BlobDB) use only. Do not override in application code.
virtual BlobDecision PrepareBlobOutput(const Slice& /* key */,
const Slice& /* existing_value */,
Expand Down

0 comments on commit 66dcaa3

Please sign in to comment.