mirror of
https://github.com/facebook/rocksdb.git
synced 2025-05-14 08:53:08 +08:00
Summary: Delete range logic is moved from `DBIter` to `MergingIterator`, and `MergingIterator` will seek to the end of a range deletion if possible instead of scanning through each key and check with `RangeDelAggregator`. With the invariant that a key in level L (consider memtable as the first level, each immutable and L0 as a separate level) has a larger sequence number than all keys in any level >L, a range tombstone `[start, end)` from level L covers all keys in its range in any level >L. This property motivates optimizations in iterator: - in `Seek(target)`, if level L has a range tombstone `[start, end)` that covers `target.UserKey`, then for all levels > L, we can do Seek() on `end` instead of `target` to skip some range tombstone covered keys. - in `Next()/Prev()`, if the current key is covered by a range tombstone `[start, end)` from level L, we can do `Seek` to `end` for all levels > L. This PR implements the above optimizations in `MergingIterator`. As all range tombstone covered keys are now skipped in `MergingIterator`, the range tombstone logic is removed from `DBIter`. The idea in this PR is similar to https://github.com/facebook/rocksdb/issues/7317, but this PR leaves `InternalIterator` interface mostly unchanged. **Credit**: the cascading seek optimization and the sentinel key (discussed below) are inspired by [Pebble](https://github.com/cockroachdb/pebble/blob/master/merging_iter.go) and suggested by ajkr in https://github.com/facebook/rocksdb/issues/7317. The two optimizations are mostly implemented in `SeekImpl()/SeekForPrevImpl()` and `IsNextDeleted()/IsPrevDeleted()` in `merging_iterator.cc`. See comments for each method for more detail. One notable change is that the minHeap/maxHeap used by `MergingIterator` now contains range tombstone end keys besides point key iterators. This helps to reduce the number of key comparisons. For example, for a range tombstone `[start, end)`, a `start` and an `end` `HeapItem` are inserted into the heap. When a `HeapItem` for range tombstone start key is popped from the minHeap, we know this range tombstone becomes "active" in the sense that, before the range tombstone's end key is popped from the minHeap, all the keys popped from this heap is covered by the range tombstone's internal key range `[start, end)`. Another major change, *delete range sentinel key*, is made to `LevelIterator`. Before this PR, when all point keys in an SST file are iterated through in `MergingIterator`, a level iterator would advance to the next SST file in its level. In the case when an SST file has a range tombstone that covers keys beyond the SST file's last point key, advancing to the next SST file would lose this range tombstone. Consequently, `MergingIterator` could return keys that should have been deleted by some range tombstone. We prevent this by pretending that file boundaries in each SST file are sentinel keys. A `LevelIterator` now only advance the file iterator once the sentinel key is processed. Pull Request resolved: https://github.com/facebook/rocksdb/pull/10449 Test Plan: - Added many unit tests in db_range_del_test - Stress test: `./db_stress --readpercent=5 --prefixpercent=19 --writepercent=20 -delpercent=10 --iterpercent=44 --delrangepercent=2` - Additional iterator stress test is added to verify against iterators against expected state: https://github.com/facebook/rocksdb/issues/10538. This is based on ajkr's previous attempt https://github.com/facebook/rocksdb/pull/5506#issuecomment-506021913. ``` python3 ./tools/db_crashtest.py blackbox --simple --write_buffer_size=524288 --target_file_size_base=524288 --max_bytes_for_level_base=2097152 --compression_type=none --max_background_compactions=8 --value_size_mult=33 --max_key=5000000 --interval=10 --duration=7200 --delrangepercent=3 --delpercent=9 --iterpercent=25 --writepercent=60 --readpercent=3 --prefixpercent=0 --num_iterations=1000 --range_deletion_width=100 --verify_iterator_with_expected_state_one_in=1 ``` - Performance benchmark: I used a similar setup as in the blog [post](http://rocksdb.org/blog/2018/11/21/delete-range.html) that introduced DeleteRange, "a database with 5 million data keys, and 10000 range tombstones (ignoring those dropped during compaction) that were written in regular intervals after 4.5 million data keys were written". As expected, the performance with this PR depends on the range tombstone width. ``` # Setup: TEST_TMPDIR=/dev/shm ./db_bench_main --benchmarks=fillrandom --writes=4500000 --num=5000000 TEST_TMPDIR=/dev/shm ./db_bench_main --benchmarks=overwrite --writes=500000 --num=5000000 --use_existing_db=true --writes_per_range_tombstone=50 # Scan entire DB TEST_TMPDIR=/dev/shm ./db_bench_main --benchmarks=readseq[-X5] --use_existing_db=true --num=5000000 --disable_auto_compactions=true # Short range scan (10 Next()) TEST_TMPDIR=/dev/shm/width-100/ ./db_bench_main --benchmarks=seekrandom[-X5] --use_existing_db=true --num=500000 --reads=100000 --seek_nexts=10 --disable_auto_compactions=true # Long range scan(1000 Next()) TEST_TMPDIR=/dev/shm/width-100/ ./db_bench_main --benchmarks=seekrandom[-X5] --use_existing_db=true --num=500000 --reads=2500 --seek_nexts=1000 --disable_auto_compactions=true ``` Avg over of 10 runs (some slower tests had fews runs): For the first column (tombstone), 0 means no range tombstone, 100-10000 means width of the 10k range tombstones, and 1 means there is a single range tombstone in the entire DB (width is 1000). The 1 tombstone case is to test regression when there's very few range tombstones in the DB, as no range tombstone is likely to take a different code path than with range tombstones. - Scan entire DB | tombstone width | Pre-PR ops/sec | Post-PR ops/sec | ±% | | ------------- | ------------- | ------------- | ------------- | | 0 range tombstone |2525600 (± 43564) |2486917 (± 33698) |-1.53% | | 100 |1853835 (± 24736) |2073884 (± 32176) |+11.87% | | 1000 |422415 (± 7466) |1115801 (± 22781) |+164.15% | | 10000 |22384 (± 227) |227919 (± 6647) |+918.22% | | 1 range tombstone |2176540 (± 39050) |2434954 (± 24563) |+11.87% | - Short range scan | tombstone width | Pre-PR ops/sec | Post-PR ops/sec | ±% | | ------------- | ------------- | ------------- | ------------- | | 0 range tombstone |35398 (± 533) |35338 (± 569) |-0.17% | | 100 |28276 (± 664) |31684 (± 331) |+12.05% | | 1000 |7637 (± 77) |25422 (± 277) |+232.88% | | 10000 |1367 |28667 |+1997.07% | | 1 range tombstone |32618 (± 581) |32748 (± 506) |+0.4% | - Long range scan | tombstone width | Pre-PR ops/sec | Post-PR ops/sec | ±% | | ------------- | ------------- | ------------- | ------------- | | 0 range tombstone |2262 (± 33) |2353 (± 20) |+4.02% | | 100 |1696 (± 26) |1926 (± 18) |+13.56% | | 1000 |410 (± 6) |1255 (± 29) |+206.1% | | 10000 |25 |414 |+1556.0% | | 1 range tombstone |1957 (± 30) |2185 (± 44) |+11.65% | - Microbench does not show significant regression: https://gist.github.com/cbi42/59f280f85a59b678e7e5d8561e693b61 Reviewed By: ajkr Differential Revision: D38450331 Pulled By: cbi42 fbshipit-source-id: b5ef12e8d8c289ed2e163ccdf277f5039b511fca
482 lines
17 KiB
C++
482 lines
17 KiB
C++
// Copyright (c) 2018-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#include "db/range_del_aggregator.h"
|
|
|
|
#include "db/compaction/compaction_iteration_stats.h"
|
|
#include "db/dbformat.h"
|
|
#include "db/pinned_iterators_manager.h"
|
|
#include "db/range_del_aggregator.h"
|
|
#include "db/range_tombstone_fragmenter.h"
|
|
#include "db/version_edit.h"
|
|
#include "rocksdb/comparator.h"
|
|
#include "rocksdb/types.h"
|
|
#include "table/internal_iterator.h"
|
|
#include "table/scoped_arena_iterator.h"
|
|
#include "table/table_builder.h"
|
|
#include "util/heap.h"
|
|
#include "util/kv_map.h"
|
|
#include "util/vector_iterator.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
TruncatedRangeDelIterator::TruncatedRangeDelIterator(
|
|
std::unique_ptr<FragmentedRangeTombstoneIterator> iter,
|
|
const InternalKeyComparator* icmp, const InternalKey* smallest,
|
|
const InternalKey* largest)
|
|
: iter_(std::move(iter)),
|
|
icmp_(icmp),
|
|
smallest_ikey_(smallest),
|
|
largest_ikey_(largest) {
|
|
if (smallest != nullptr) {
|
|
pinned_bounds_.emplace_back();
|
|
auto& parsed_smallest = pinned_bounds_.back();
|
|
Status pik_status = ParseInternalKey(smallest->Encode(), &parsed_smallest,
|
|
false /* log_err_key */); // TODO
|
|
pik_status.PermitUncheckedError();
|
|
assert(pik_status.ok());
|
|
smallest_ = &parsed_smallest;
|
|
}
|
|
if (largest != nullptr) {
|
|
pinned_bounds_.emplace_back();
|
|
auto& parsed_largest = pinned_bounds_.back();
|
|
|
|
Status pik_status = ParseInternalKey(largest->Encode(), &parsed_largest,
|
|
false /* log_err_key */); // TODO
|
|
pik_status.PermitUncheckedError();
|
|
assert(pik_status.ok());
|
|
|
|
if (parsed_largest.type == kTypeRangeDeletion &&
|
|
parsed_largest.sequence == kMaxSequenceNumber) {
|
|
// The file boundary has been artificially extended by a range tombstone.
|
|
// We do not need to adjust largest to properly truncate range
|
|
// tombstones that extend past the boundary.
|
|
} else if (parsed_largest.sequence == 0) {
|
|
// The largest key in the sstable has a sequence number of 0. Since we
|
|
// guarantee that no internal keys with the same user key and sequence
|
|
// number can exist in a DB, we know that the largest key in this sstable
|
|
// cannot exist as the smallest key in the next sstable. This further
|
|
// implies that no range tombstone in this sstable covers largest;
|
|
// otherwise, the file boundary would have been artificially extended.
|
|
//
|
|
// Therefore, we will never truncate a range tombstone at largest, so we
|
|
// can leave it unchanged.
|
|
} else {
|
|
// The same user key may straddle two sstable boundaries. To ensure that
|
|
// the truncated end key can cover the largest key in this sstable, reduce
|
|
// its sequence number by 1.
|
|
parsed_largest.sequence -= 1;
|
|
// This line is not needed for correctness, but it ensures that the
|
|
// truncated end key is not covering keys from the next SST file.
|
|
parsed_largest.type = kValueTypeForSeek;
|
|
}
|
|
largest_ = &parsed_largest;
|
|
}
|
|
}
|
|
|
|
bool TruncatedRangeDelIterator::Valid() const {
|
|
assert(iter_ != nullptr);
|
|
return iter_->Valid() &&
|
|
(smallest_ == nullptr ||
|
|
icmp_->Compare(*smallest_, iter_->parsed_end_key()) < 0) &&
|
|
(largest_ == nullptr ||
|
|
icmp_->Compare(iter_->parsed_start_key(), *largest_) < 0);
|
|
}
|
|
|
|
// NOTE: target is a user key
|
|
void TruncatedRangeDelIterator::Seek(const Slice& target) {
|
|
if (largest_ != nullptr &&
|
|
icmp_->Compare(*largest_, ParsedInternalKey(target, kMaxSequenceNumber,
|
|
kTypeRangeDeletion)) <= 0) {
|
|
iter_->Invalidate();
|
|
return;
|
|
}
|
|
if (smallest_ != nullptr &&
|
|
icmp_->user_comparator()->Compare(target, smallest_->user_key) < 0) {
|
|
iter_->Seek(smallest_->user_key);
|
|
return;
|
|
}
|
|
iter_->Seek(target);
|
|
}
|
|
|
|
// NOTE: target is a user key
|
|
void TruncatedRangeDelIterator::SeekForPrev(const Slice& target) {
|
|
if (smallest_ != nullptr &&
|
|
icmp_->Compare(ParsedInternalKey(target, 0, kTypeRangeDeletion),
|
|
*smallest_) < 0) {
|
|
iter_->Invalidate();
|
|
return;
|
|
}
|
|
if (largest_ != nullptr &&
|
|
icmp_->user_comparator()->Compare(largest_->user_key, target) < 0) {
|
|
iter_->SeekForPrev(largest_->user_key);
|
|
return;
|
|
}
|
|
iter_->SeekForPrev(target);
|
|
}
|
|
|
|
void TruncatedRangeDelIterator::SeekToFirst() {
|
|
if (smallest_ != nullptr) {
|
|
iter_->Seek(smallest_->user_key);
|
|
return;
|
|
}
|
|
iter_->SeekToTopFirst();
|
|
}
|
|
|
|
void TruncatedRangeDelIterator::SeekToLast() {
|
|
if (largest_ != nullptr) {
|
|
iter_->SeekForPrev(largest_->user_key);
|
|
return;
|
|
}
|
|
iter_->SeekToTopLast();
|
|
}
|
|
|
|
std::map<SequenceNumber, std::unique_ptr<TruncatedRangeDelIterator>>
|
|
TruncatedRangeDelIterator::SplitBySnapshot(
|
|
const std::vector<SequenceNumber>& snapshots) {
|
|
using FragmentedIterPair =
|
|
std::pair<const SequenceNumber,
|
|
std::unique_ptr<FragmentedRangeTombstoneIterator>>;
|
|
|
|
auto split_untruncated_iters = iter_->SplitBySnapshot(snapshots);
|
|
std::map<SequenceNumber, std::unique_ptr<TruncatedRangeDelIterator>>
|
|
split_truncated_iters;
|
|
std::for_each(
|
|
split_untruncated_iters.begin(), split_untruncated_iters.end(),
|
|
[&](FragmentedIterPair& iter_pair) {
|
|
auto truncated_iter = std::make_unique<TruncatedRangeDelIterator>(
|
|
std::move(iter_pair.second), icmp_, smallest_ikey_, largest_ikey_);
|
|
split_truncated_iters.emplace(iter_pair.first,
|
|
std::move(truncated_iter));
|
|
});
|
|
return split_truncated_iters;
|
|
}
|
|
|
|
ForwardRangeDelIterator::ForwardRangeDelIterator(
|
|
const InternalKeyComparator* icmp)
|
|
: icmp_(icmp),
|
|
unused_idx_(0),
|
|
active_seqnums_(SeqMaxComparator()),
|
|
active_iters_(EndKeyMinComparator(icmp)),
|
|
inactive_iters_(StartKeyMinComparator(icmp)) {}
|
|
|
|
bool ForwardRangeDelIterator::ShouldDelete(const ParsedInternalKey& parsed) {
|
|
// Move active iterators that end before parsed.
|
|
while (!active_iters_.empty() &&
|
|
icmp_->Compare((*active_iters_.top())->end_key(), parsed) <= 0) {
|
|
TruncatedRangeDelIterator* iter = PopActiveIter();
|
|
do {
|
|
iter->Next();
|
|
} while (iter->Valid() && icmp_->Compare(iter->end_key(), parsed) <= 0);
|
|
PushIter(iter, parsed);
|
|
assert(active_iters_.size() == active_seqnums_.size());
|
|
}
|
|
|
|
// Move inactive iterators that start before parsed.
|
|
while (!inactive_iters_.empty() &&
|
|
icmp_->Compare(inactive_iters_.top()->start_key(), parsed) <= 0) {
|
|
TruncatedRangeDelIterator* iter = PopInactiveIter();
|
|
while (iter->Valid() && icmp_->Compare(iter->end_key(), parsed) <= 0) {
|
|
iter->Next();
|
|
}
|
|
PushIter(iter, parsed);
|
|
assert(active_iters_.size() == active_seqnums_.size());
|
|
}
|
|
|
|
return active_seqnums_.empty()
|
|
? false
|
|
: (*active_seqnums_.begin())->seq() > parsed.sequence;
|
|
}
|
|
|
|
void ForwardRangeDelIterator::Invalidate() {
|
|
unused_idx_ = 0;
|
|
active_iters_.clear();
|
|
active_seqnums_.clear();
|
|
inactive_iters_.clear();
|
|
}
|
|
|
|
ReverseRangeDelIterator::ReverseRangeDelIterator(
|
|
const InternalKeyComparator* icmp)
|
|
: icmp_(icmp),
|
|
unused_idx_(0),
|
|
active_seqnums_(SeqMaxComparator()),
|
|
active_iters_(StartKeyMaxComparator(icmp)),
|
|
inactive_iters_(EndKeyMaxComparator(icmp)) {}
|
|
|
|
bool ReverseRangeDelIterator::ShouldDelete(const ParsedInternalKey& parsed) {
|
|
// Move active iterators that start after parsed.
|
|
while (!active_iters_.empty() &&
|
|
icmp_->Compare(parsed, (*active_iters_.top())->start_key()) < 0) {
|
|
TruncatedRangeDelIterator* iter = PopActiveIter();
|
|
do {
|
|
iter->Prev();
|
|
} while (iter->Valid() && icmp_->Compare(parsed, iter->start_key()) < 0);
|
|
PushIter(iter, parsed);
|
|
assert(active_iters_.size() == active_seqnums_.size());
|
|
}
|
|
|
|
// Move inactive iterators that end after parsed.
|
|
while (!inactive_iters_.empty() &&
|
|
icmp_->Compare(parsed, inactive_iters_.top()->end_key()) < 0) {
|
|
TruncatedRangeDelIterator* iter = PopInactiveIter();
|
|
while (iter->Valid() && icmp_->Compare(parsed, iter->start_key()) < 0) {
|
|
iter->Prev();
|
|
}
|
|
PushIter(iter, parsed);
|
|
assert(active_iters_.size() == active_seqnums_.size());
|
|
}
|
|
|
|
return active_seqnums_.empty()
|
|
? false
|
|
: (*active_seqnums_.begin())->seq() > parsed.sequence;
|
|
}
|
|
|
|
void ReverseRangeDelIterator::Invalidate() {
|
|
unused_idx_ = 0;
|
|
active_iters_.clear();
|
|
active_seqnums_.clear();
|
|
inactive_iters_.clear();
|
|
}
|
|
|
|
bool RangeDelAggregator::StripeRep::ShouldDelete(
|
|
const ParsedInternalKey& parsed, RangeDelPositioningMode mode) {
|
|
if (!InStripe(parsed.sequence) || IsEmpty()) {
|
|
return false;
|
|
}
|
|
switch (mode) {
|
|
case RangeDelPositioningMode::kForwardTraversal:
|
|
InvalidateReverseIter();
|
|
|
|
// Pick up previously unseen iterators.
|
|
for (auto it = std::next(iters_.begin(), forward_iter_.UnusedIdx());
|
|
it != iters_.end(); ++it, forward_iter_.IncUnusedIdx()) {
|
|
auto& iter = *it;
|
|
forward_iter_.AddNewIter(iter.get(), parsed);
|
|
}
|
|
|
|
return forward_iter_.ShouldDelete(parsed);
|
|
case RangeDelPositioningMode::kBackwardTraversal:
|
|
InvalidateForwardIter();
|
|
|
|
// Pick up previously unseen iterators.
|
|
for (auto it = std::next(iters_.begin(), reverse_iter_.UnusedIdx());
|
|
it != iters_.end(); ++it, reverse_iter_.IncUnusedIdx()) {
|
|
auto& iter = *it;
|
|
reverse_iter_.AddNewIter(iter.get(), parsed);
|
|
}
|
|
|
|
return reverse_iter_.ShouldDelete(parsed);
|
|
default:
|
|
assert(false);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool RangeDelAggregator::StripeRep::IsRangeOverlapped(const Slice& start,
|
|
const Slice& end) {
|
|
Invalidate();
|
|
|
|
// Set the internal start/end keys so that:
|
|
// - if start_ikey has the same user key and sequence number as the
|
|
// current end key, start_ikey will be considered greater; and
|
|
// - if end_ikey has the same user key and sequence number as the current
|
|
// start key, end_ikey will be considered greater.
|
|
ParsedInternalKey start_ikey(start, kMaxSequenceNumber,
|
|
static_cast<ValueType>(0));
|
|
ParsedInternalKey end_ikey(end, 0, static_cast<ValueType>(0));
|
|
for (auto& iter : iters_) {
|
|
bool checked_candidate_tombstones = false;
|
|
for (iter->SeekForPrev(start);
|
|
iter->Valid() && icmp_->Compare(iter->start_key(), end_ikey) <= 0;
|
|
iter->Next()) {
|
|
checked_candidate_tombstones = true;
|
|
if (icmp_->Compare(start_ikey, iter->end_key()) < 0 &&
|
|
icmp_->Compare(iter->start_key(), end_ikey) <= 0) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (!checked_candidate_tombstones) {
|
|
// Do an additional check for when the end of the range is the begin
|
|
// key of a tombstone, which we missed earlier since SeekForPrev'ing
|
|
// to the start was invalid.
|
|
iter->SeekForPrev(end);
|
|
if (iter->Valid() && icmp_->Compare(start_ikey, iter->end_key()) < 0 &&
|
|
icmp_->Compare(iter->start_key(), end_ikey) <= 0) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ReadRangeDelAggregator::AddTombstones(
|
|
std::unique_ptr<FragmentedRangeTombstoneIterator> input_iter,
|
|
const InternalKey* smallest, const InternalKey* largest) {
|
|
if (input_iter == nullptr || input_iter->empty()) {
|
|
return;
|
|
}
|
|
rep_.AddTombstones(std::make_unique<TruncatedRangeDelIterator>(
|
|
std::move(input_iter), icmp_, smallest, largest));
|
|
}
|
|
|
|
bool ReadRangeDelAggregator::ShouldDeleteImpl(const ParsedInternalKey& parsed,
|
|
RangeDelPositioningMode mode) {
|
|
return rep_.ShouldDelete(parsed, mode);
|
|
}
|
|
|
|
bool ReadRangeDelAggregator::IsRangeOverlapped(const Slice& start,
|
|
const Slice& end) {
|
|
InvalidateRangeDelMapPositions();
|
|
return rep_.IsRangeOverlapped(start, end);
|
|
}
|
|
|
|
void CompactionRangeDelAggregator::AddTombstones(
|
|
std::unique_ptr<FragmentedRangeTombstoneIterator> input_iter,
|
|
const InternalKey* smallest, const InternalKey* largest) {
|
|
if (input_iter == nullptr || input_iter->empty()) {
|
|
return;
|
|
}
|
|
assert(input_iter->lower_bound() == 0);
|
|
assert(input_iter->upper_bound() == kMaxSequenceNumber);
|
|
parent_iters_.emplace_back(new TruncatedRangeDelIterator(
|
|
std::move(input_iter), icmp_, smallest, largest));
|
|
|
|
auto split_iters = parent_iters_.back()->SplitBySnapshot(*snapshots_);
|
|
for (auto& split_iter : split_iters) {
|
|
auto it = reps_.find(split_iter.first);
|
|
if (it == reps_.end()) {
|
|
bool inserted;
|
|
SequenceNumber upper_bound = split_iter.second->upper_bound();
|
|
SequenceNumber lower_bound = split_iter.second->lower_bound();
|
|
std::tie(it, inserted) = reps_.emplace(
|
|
split_iter.first, StripeRep(icmp_, upper_bound, lower_bound));
|
|
assert(inserted);
|
|
}
|
|
assert(it != reps_.end());
|
|
it->second.AddTombstones(std::move(split_iter.second));
|
|
}
|
|
}
|
|
|
|
bool CompactionRangeDelAggregator::ShouldDelete(const ParsedInternalKey& parsed,
|
|
RangeDelPositioningMode mode) {
|
|
auto it = reps_.lower_bound(parsed.sequence);
|
|
if (it == reps_.end()) {
|
|
return false;
|
|
}
|
|
return it->second.ShouldDelete(parsed, mode);
|
|
}
|
|
|
|
namespace {
|
|
|
|
class TruncatedRangeDelMergingIter : public InternalIterator {
|
|
public:
|
|
TruncatedRangeDelMergingIter(
|
|
const InternalKeyComparator* icmp, const Slice* lower_bound,
|
|
const Slice* upper_bound, bool upper_bound_inclusive,
|
|
const std::vector<std::unique_ptr<TruncatedRangeDelIterator>>& children)
|
|
: icmp_(icmp),
|
|
lower_bound_(lower_bound),
|
|
upper_bound_(upper_bound),
|
|
upper_bound_inclusive_(upper_bound_inclusive),
|
|
heap_(StartKeyMinComparator(icmp)) {
|
|
for (auto& child : children) {
|
|
if (child != nullptr) {
|
|
assert(child->lower_bound() == 0);
|
|
assert(child->upper_bound() == kMaxSequenceNumber);
|
|
children_.push_back(child.get());
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Valid() const override {
|
|
return !heap_.empty() && BeforeEndKey(heap_.top());
|
|
}
|
|
Status status() const override { return Status::OK(); }
|
|
|
|
void SeekToFirst() override {
|
|
heap_.clear();
|
|
for (auto& child : children_) {
|
|
if (lower_bound_ != nullptr) {
|
|
child->Seek(*lower_bound_);
|
|
} else {
|
|
child->SeekToFirst();
|
|
}
|
|
if (child->Valid()) {
|
|
heap_.push(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Next() override {
|
|
auto* top = heap_.top();
|
|
top->InternalNext();
|
|
if (top->Valid()) {
|
|
heap_.replace_top(top);
|
|
} else {
|
|
heap_.pop();
|
|
}
|
|
}
|
|
|
|
Slice key() const override {
|
|
auto* top = heap_.top();
|
|
cur_start_key_.Set(top->start_key().user_key, top->seq(),
|
|
kTypeRangeDeletion);
|
|
return cur_start_key_.Encode();
|
|
}
|
|
|
|
Slice value() const override {
|
|
auto* top = heap_.top();
|
|
assert(top->end_key().sequence == kMaxSequenceNumber);
|
|
return top->end_key().user_key;
|
|
}
|
|
|
|
// Unused InternalIterator methods
|
|
void Prev() override { assert(false); }
|
|
void Seek(const Slice& /* target */) override { assert(false); }
|
|
void SeekForPrev(const Slice& /* target */) override { assert(false); }
|
|
void SeekToLast() override { assert(false); }
|
|
|
|
private:
|
|
bool BeforeEndKey(const TruncatedRangeDelIterator* iter) const {
|
|
if (upper_bound_ == nullptr) {
|
|
return true;
|
|
}
|
|
int cmp = icmp_->user_comparator()->Compare(iter->start_key().user_key,
|
|
*upper_bound_);
|
|
return upper_bound_inclusive_ ? cmp <= 0 : cmp < 0;
|
|
}
|
|
|
|
const InternalKeyComparator* icmp_;
|
|
const Slice* lower_bound_;
|
|
const Slice* upper_bound_;
|
|
bool upper_bound_inclusive_;
|
|
BinaryHeap<TruncatedRangeDelIterator*, StartKeyMinComparator> heap_;
|
|
std::vector<TruncatedRangeDelIterator*> children_;
|
|
|
|
mutable InternalKey cur_start_key_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
std::unique_ptr<FragmentedRangeTombstoneIterator>
|
|
CompactionRangeDelAggregator::NewIterator(const Slice* lower_bound,
|
|
const Slice* upper_bound,
|
|
bool upper_bound_inclusive) {
|
|
InvalidateRangeDelMapPositions();
|
|
auto merging_iter = std::make_unique<TruncatedRangeDelMergingIter>(
|
|
icmp_, lower_bound, upper_bound, upper_bound_inclusive, parent_iters_);
|
|
|
|
auto fragmented_tombstone_list =
|
|
std::make_shared<FragmentedRangeTombstoneList>(
|
|
std::move(merging_iter), *icmp_, true /* for_compaction */,
|
|
*snapshots_);
|
|
|
|
return std::make_unique<FragmentedRangeTombstoneIterator>(
|
|
fragmented_tombstone_list, *icmp_, kMaxSequenceNumber /* upper_bound */);
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|