fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
405 lines
12 KiB
405 lines
12 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
11 years ago
|
// This source code is licensed under the BSD-style license found in the
|
||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||
|
//
|
||
14 years ago
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style license that can be
|
||
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||
8 years ago
|
|
||
8 years ago
|
#include "table/merging_iterator.h"
|
||
8 years ago
|
#include <string>
|
||
11 years ago
|
#include <vector>
|
||
9 years ago
|
#include "db/pinned_iterators_manager.h"
|
||
11 years ago
|
#include "rocksdb/comparator.h"
|
||
|
#include "rocksdb/iterator.h"
|
||
11 years ago
|
#include "rocksdb/options.h"
|
||
9 years ago
|
#include "table/internal_iterator.h"
|
||
12 years ago
|
#include "table/iter_heap.h"
|
||
14 years ago
|
#include "table/iterator_wrapper.h"
|
||
11 years ago
|
#include "util/arena.h"
|
||
9 years ago
|
#include "util/autovector.h"
|
||
10 years ago
|
#include "util/heap.h"
|
||
9 years ago
|
#include "util/perf_context_imp.h"
|
||
11 years ago
|
#include "util/stop_watch.h"
|
||
9 years ago
|
#include "util/sync_point.h"
|
||
14 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
10 years ago
|
// Without anonymous namespace here, we fail the warning -Wmissing-prototypes
|
||
|
namespace {
|
||
10 years ago
|
typedef BinaryHeap<IteratorWrapper*, MaxIteratorComparator> MergerMaxIterHeap;
|
||
|
typedef BinaryHeap<IteratorWrapper*, MinIteratorComparator> MergerMinIterHeap;
|
||
10 years ago
|
} // namespace
|
||
11 years ago
|
|
||
11 years ago
|
const size_t kNumIterReserve = 4;
|
||
|
|
||
9 years ago
|
class MergingIterator : public InternalIterator {
|
||
14 years ago
|
public:
|
||
9 years ago
|
MergingIterator(const Comparator* comparator, InternalIterator** children,
|
||
8 years ago
|
int n, bool is_arena_mode, bool prefix_seek_mode)
|
||
9 years ago
|
: is_arena_mode_(is_arena_mode),
|
||
11 years ago
|
comparator_(comparator),
|
||
12 years ago
|
current_(nullptr),
|
||
12 years ago
|
direction_(kForward),
|
||
9 years ago
|
minHeap_(comparator_),
|
||
8 years ago
|
prefix_seek_mode_(prefix_seek_mode),
|
||
9 years ago
|
pinned_iters_mgr_(nullptr) {
|
||
11 years ago
|
children_.resize(n);
|
||
14 years ago
|
for (int i = 0; i < n; i++) {
|
||
|
children_[i].Set(children[i]);
|
||
|
}
|
||
11 years ago
|
for (auto& child : children_) {
|
||
|
if (child.Valid()) {
|
||
|
minHeap_.push(&child);
|
||
12 years ago
|
}
|
||
|
}
|
||
10 years ago
|
current_ = CurrentForward();
|
||
14 years ago
|
}
|
||
|
|
||
9 years ago
|
virtual void AddIterator(InternalIterator* iter) {
|
||
11 years ago
|
assert(direction_ == kForward);
|
||
|
children_.emplace_back(iter);
|
||
9 years ago
|
if (pinned_iters_mgr_) {
|
||
|
iter->SetPinnedItersMgr(pinned_iters_mgr_);
|
||
9 years ago
|
}
|
||
11 years ago
|
auto new_wrapper = children_.back();
|
||
|
if (new_wrapper.Valid()) {
|
||
|
minHeap_.push(&new_wrapper);
|
||
10 years ago
|
current_ = CurrentForward();
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
virtual ~MergingIterator() {
|
||
|
for (auto& child : children_) {
|
||
|
child.DeleteIter(is_arena_mode_);
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
10 years ago
|
virtual bool Valid() const override { return (current_ != nullptr); }
|
||
14 years ago
|
|
||
10 years ago
|
virtual void SeekToFirst() override {
|
||
12 years ago
|
ClearHeaps();
|
||
11 years ago
|
for (auto& child : children_) {
|
||
|
child.SeekToFirst();
|
||
|
if (child.Valid()) {
|
||
|
minHeap_.push(&child);
|
||
12 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
direction_ = kForward;
|
||
10 years ago
|
current_ = CurrentForward();
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
virtual void SeekToLast() override {
|
||
12 years ago
|
ClearHeaps();
|
||
10 years ago
|
InitMaxHeap();
|
||
11 years ago
|
for (auto& child : children_) {
|
||
|
child.SeekToLast();
|
||
|
if (child.Valid()) {
|
||
10 years ago
|
maxHeap_->push(&child);
|
||
12 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
direction_ = kReverse;
|
||
10 years ago
|
current_ = CurrentReverse();
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
virtual void Seek(const Slice& target) override {
|
||
10 years ago
|
ClearHeaps();
|
||
11 years ago
|
for (auto& child : children_) {
|
||
10 years ago
|
{
|
||
|
PERF_TIMER_GUARD(seek_child_seek_time);
|
||
|
child.Seek(target);
|
||
|
}
|
||
8 years ago
|
PERF_COUNTER_ADD(seek_child_seek_count, 1);
|
||
8 years ago
|
|
||
11 years ago
|
if (child.Valid()) {
|
||
10 years ago
|
PERF_TIMER_GUARD(seek_min_heap_time);
|
||
|
minHeap_.push(&child);
|
||
12 years ago
|
}
|
||
14 years ago
|
}
|
||
10 years ago
|
direction_ = kForward;
|
||
|
{
|
||
10 years ago
|
PERF_TIMER_GUARD(seek_min_heap_time);
|
||
10 years ago
|
current_ = CurrentForward();
|
||
11 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
8 years ago
|
virtual void SeekForPrev(const Slice& target) override {
|
||
|
ClearHeaps();
|
||
|
InitMaxHeap();
|
||
|
|
||
|
for (auto& child : children_) {
|
||
|
{
|
||
|
PERF_TIMER_GUARD(seek_child_seek_time);
|
||
|
child.SeekForPrev(target);
|
||
|
}
|
||
|
PERF_COUNTER_ADD(seek_child_seek_count, 1);
|
||
|
|
||
|
if (child.Valid()) {
|
||
|
PERF_TIMER_GUARD(seek_max_heap_time);
|
||
|
maxHeap_->push(&child);
|
||
|
}
|
||
|
}
|
||
|
direction_ = kReverse;
|
||
|
{
|
||
|
PERF_TIMER_GUARD(seek_max_heap_time);
|
||
|
current_ = CurrentReverse();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual void Next() override {
|
||
14 years ago
|
assert(Valid());
|
||
8 years ago
|
|
||
14 years ago
|
// Ensure that all children are positioned after key().
|
||
|
// If we are moving in the forward direction, it is already
|
||
10 years ago
|
// true for all of the non-current children since current_ is
|
||
|
// the smallest child and key() == current_->key().
|
||
14 years ago
|
if (direction_ != kForward) {
|
||
10 years ago
|
// Otherwise, advance the non-current children. We advance current_
|
||
|
// just after the if-block.
|
||
12 years ago
|
ClearHeaps();
|
||
11 years ago
|
for (auto& child : children_) {
|
||
|
if (&child != current_) {
|
||
8 years ago
|
child.Seek(key());
|
||
8 years ago
|
if (child.Valid() && comparator_->Equal(key(), child.key())) {
|
||
11 years ago
|
child.Next();
|
||
14 years ago
|
}
|
||
10 years ago
|
}
|
||
|
if (child.Valid()) {
|
||
8 years ago
|
minHeap_.push(&child);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
direction_ = kForward;
|
||
10 years ago
|
// The loop advanced all non-current children to be > key() so current_
|
||
|
// should still be strictly the smallest key.
|
||
|
assert(current_ == CurrentForward());
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
// For the heap modifications below to be correct, current_ must be the
|
||
|
// current top of the heap.
|
||
|
assert(current_ == CurrentForward());
|
||
|
|
||
12 years ago
|
// as the current points to the current record. move the iterator forward.
|
||
14 years ago
|
current_->Next();
|
||
10 years ago
|
if (current_->Valid()) {
|
||
|
// current is still valid after the Next() call above. Call
|
||
|
// replace_top() to restore the heap property. When the same child
|
||
|
// iterator yields a sequence of keys, this is cheap.
|
||
|
minHeap_.replace_top(current_);
|
||
|
} else {
|
||
|
// current stopped being valid, remove it from the heap.
|
||
|
minHeap_.pop();
|
||
12 years ago
|
}
|
||
10 years ago
|
current_ = CurrentForward();
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
virtual void Prev() override {
|
||
14 years ago
|
assert(Valid());
|
||
14 years ago
|
// Ensure that all children are positioned before key().
|
||
|
// If we are moving in the reverse direction, it is already
|
||
10 years ago
|
// true for all of the non-current children since current_ is
|
||
|
// the largest child and key() == current_->key().
|
||
14 years ago
|
if (direction_ != kReverse) {
|
||
10 years ago
|
// Otherwise, retreat the non-current children. We retreat current_
|
||
|
// just after the if-block.
|
||
12 years ago
|
ClearHeaps();
|
||
10 years ago
|
InitMaxHeap();
|
||
11 years ago
|
for (auto& child : children_) {
|
||
|
if (&child != current_) {
|
||
8 years ago
|
if (!prefix_seek_mode_) {
|
||
|
child.Seek(key());
|
||
|
if (child.Valid()) {
|
||
|
// Child is at first entry >= key(). Step back one to be < key()
|
||
|
TEST_SYNC_POINT_CALLBACK("MergeIterator::Prev:BeforePrev",
|
||
|
&child);
|
||
|
child.Prev();
|
||
|
} else {
|
||
|
// Child has no entries >= key(). Position at last entry.
|
||
|
TEST_SYNC_POINT("MergeIterator::Prev:BeforeSeekToLast");
|
||
|
child.SeekToLast();
|
||
|
}
|
||
|
} else {
|
||
|
child.SeekForPrev(key());
|
||
|
if (child.Valid() && comparator_->Equal(key(), child.key())) {
|
||
|
child.Prev();
|
||
|
}
|
||
14 years ago
|
}
|
||
10 years ago
|
}
|
||
|
if (child.Valid()) {
|
||
8 years ago
|
maxHeap_->push(&child);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
direction_ = kReverse;
|
||
8 years ago
|
if (!prefix_seek_mode_) {
|
||
|
// Note that we don't do assert(current_ == CurrentReverse()) here
|
||
|
// because it is possible to have some keys larger than the seek-key
|
||
|
// inserted between Seek() and SeekToLast(), which makes current_ not
|
||
|
// equal to CurrentReverse().
|
||
|
current_ = CurrentReverse();
|
||
|
}
|
||
8 years ago
|
// The loop advanced all non-current children to be < key() so current_
|
||
|
// should still be strictly the smallest key.
|
||
|
assert(current_ == CurrentReverse());
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
// For the heap modifications below to be correct, current_ must be the
|
||
|
// current top of the heap.
|
||
|
assert(current_ == CurrentReverse());
|
||
|
|
||
14 years ago
|
current_->Prev();
|
||
12 years ago
|
if (current_->Valid()) {
|
||
10 years ago
|
// current is still valid after the Prev() call above. Call
|
||
|
// replace_top() to restore the heap property. When the same child
|
||
|
// iterator yields a sequence of keys, this is cheap.
|
||
|
maxHeap_->replace_top(current_);
|
||
|
} else {
|
||
|
// current stopped being valid, remove it from the heap.
|
||
|
maxHeap_->pop();
|
||
12 years ago
|
}
|
||
10 years ago
|
current_ = CurrentReverse();
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
virtual Slice key() const override {
|
||
14 years ago
|
assert(Valid());
|
||
|
return current_->key();
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual Slice value() const override {
|
||
14 years ago
|
assert(Valid());
|
||
|
return current_->value();
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual Status status() const override {
|
||
10 years ago
|
Status s;
|
||
11 years ago
|
for (auto& child : children_) {
|
||
10 years ago
|
s = child.status();
|
||
|
if (!s.ok()) {
|
||
14 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
return s;
|
||
14 years ago
|
}
|
||
|
|
||
9 years ago
|
virtual void SetPinnedItersMgr(
|
||
|
PinnedIteratorsManager* pinned_iters_mgr) override {
|
||
|
pinned_iters_mgr_ = pinned_iters_mgr;
|
||
9 years ago
|
for (auto& child : children_) {
|
||
9 years ago
|
child.SetPinnedItersMgr(pinned_iters_mgr);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
virtual bool IsKeyPinned() const override {
|
||
|
assert(Valid());
|
||
9 years ago
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
||
|
current_->IsKeyPinned();
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
virtual bool IsValuePinned() const override {
|
||
|
assert(Valid());
|
||
|
return pinned_iters_mgr_ && pinned_iters_mgr_->PinningEnabled() &&
|
||
|
current_->IsValuePinned();
|
||
|
}
|
||
|
|
||
14 years ago
|
private:
|
||
10 years ago
|
// Clears heaps for both directions, used when changing direction or seeking
|
||
12 years ago
|
void ClearHeaps();
|
||
10 years ago
|
// Ensures that maxHeap_ is initialized when starting to go in the reverse
|
||
|
// direction
|
||
|
void InitMaxHeap();
|
||
14 years ago
|
|
||
11 years ago
|
bool is_arena_mode_;
|
||
14 years ago
|
const Comparator* comparator_;
|
||
11 years ago
|
autovector<IteratorWrapper, kNumIterReserve> children_;
|
||
10 years ago
|
|
||
|
// Cached pointer to child iterator with the current key, or nullptr if no
|
||
|
// child iterators are valid. This is the top of minHeap_ or maxHeap_
|
||
|
// depending on the direction.
|
||
14 years ago
|
IteratorWrapper* current_;
|
||
14 years ago
|
// Which direction is the iterator moving?
|
||
|
enum Direction {
|
||
|
kForward,
|
||
|
kReverse
|
||
|
};
|
||
|
Direction direction_;
|
||
10 years ago
|
MergerMinIterHeap minHeap_;
|
||
8 years ago
|
bool prefix_seek_mode_;
|
||
|
|
||
10 years ago
|
// Max heap is used for reverse iteration, which is way less common than
|
||
|
// forward. Lazily initialize it to save memory.
|
||
|
std::unique_ptr<MergerMaxIterHeap> maxHeap_;
|
||
9 years ago
|
PinnedIteratorsManager* pinned_iters_mgr_;
|
||
12 years ago
|
|
||
10 years ago
|
IteratorWrapper* CurrentForward() const {
|
||
|
assert(direction_ == kForward);
|
||
|
return !minHeap_.empty() ? minHeap_.top() : nullptr;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
IteratorWrapper* CurrentReverse() const {
|
||
|
assert(direction_ == kReverse);
|
||
|
assert(maxHeap_);
|
||
|
return !maxHeap_->empty() ? maxHeap_->top() : nullptr;
|
||
10 years ago
|
}
|
||
10 years ago
|
};
|
||
14 years ago
|
|
||
10 years ago
|
void MergingIterator::ClearHeaps() {
|
||
10 years ago
|
minHeap_.clear();
|
||
|
if (maxHeap_) {
|
||
|
maxHeap_->clear();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void MergingIterator::InitMaxHeap() {
|
||
|
if (!maxHeap_) {
|
||
|
maxHeap_.reset(new MergerMaxIterHeap(comparator_));
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
InternalIterator* NewMergingIterator(const Comparator* cmp,
|
||
|
InternalIterator** list, int n,
|
||
8 years ago
|
Arena* arena, bool prefix_seek_mode) {
|
||
14 years ago
|
assert(n >= 0);
|
||
|
if (n == 0) {
|
||
9 years ago
|
return NewEmptyInternalIterator(arena);
|
||
14 years ago
|
} else if (n == 1) {
|
||
|
return list[0];
|
||
|
} else {
|
||
11 years ago
|
if (arena == nullptr) {
|
||
8 years ago
|
return new MergingIterator(cmp, list, n, false, prefix_seek_mode);
|
||
11 years ago
|
} else {
|
||
|
auto mem = arena->AllocateAligned(sizeof(MergingIterator));
|
||
8 years ago
|
return new (mem) MergingIterator(cmp, list, n, true, prefix_seek_mode);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
MergeIteratorBuilder::MergeIteratorBuilder(const Comparator* comparator,
|
||
8 years ago
|
Arena* a, bool prefix_seek_mode)
|
||
11 years ago
|
: first_iter(nullptr), use_merging_iter(false), arena(a) {
|
||
|
auto mem = arena->AllocateAligned(sizeof(MergingIterator));
|
||
8 years ago
|
merge_iter =
|
||
|
new (mem) MergingIterator(comparator, nullptr, 0, true, prefix_seek_mode);
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
void MergeIteratorBuilder::AddIterator(InternalIterator* iter) {
|
||
11 years ago
|
if (!use_merging_iter && first_iter != nullptr) {
|
||
|
merge_iter->AddIterator(first_iter);
|
||
|
use_merging_iter = true;
|
||
|
}
|
||
|
if (use_merging_iter) {
|
||
|
merge_iter->AddIterator(iter);
|
||
|
} else {
|
||
|
first_iter = iter;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
InternalIterator* MergeIteratorBuilder::Finish() {
|
||
11 years ago
|
if (!use_merging_iter) {
|
||
|
return first_iter;
|
||
|
} else {
|
||
|
auto ret = merge_iter;
|
||
|
merge_iter = nullptr;
|
||
|
return ret;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|