2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// 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.
|
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "table/merger.h"
|
|
|
|
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2012-12-26 20:51:36 +01:00
|
|
|
#include "table/iter_heap.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "table/iterator_wrapper.h"
|
|
|
|
|
2013-08-21 07:58:16 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
namespace {
|
2012-12-26 20:51:36 +01:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
class MergingIterator : public Iterator {
|
|
|
|
public:
|
|
|
|
MergingIterator(const Comparator* comparator, Iterator** children, int n)
|
|
|
|
: comparator_(comparator),
|
2013-08-21 07:58:16 +02:00
|
|
|
children_(n),
|
2013-03-01 03:04:58 +01:00
|
|
|
current_(nullptr),
|
2012-12-26 20:51:36 +01:00
|
|
|
direction_(kForward),
|
|
|
|
maxHeap_(NewMaxIterHeap(comparator_)),
|
|
|
|
minHeap_ (NewMinIterHeap(comparator_)) {
|
2011-03-18 23:37:00 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
children_[i].Set(children[i]);
|
|
|
|
}
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
if (child.Valid()) {
|
|
|
|
minHeap_.push(&child);
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2013-08-21 07:58:16 +02:00
|
|
|
virtual ~MergingIterator() { }
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
virtual bool Valid() const {
|
2013-03-01 03:04:58 +01:00
|
|
|
return (current_ != nullptr);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SeekToFirst() {
|
2012-12-26 20:51:36 +01:00
|
|
|
ClearHeaps();
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
child.SeekToFirst();
|
|
|
|
if (child.Valid()) {
|
|
|
|
minHeap_.push(&child);
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
FindSmallest();
|
2011-03-21 20:40:57 +01:00
|
|
|
direction_ = kForward;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SeekToLast() {
|
2012-12-26 20:51:36 +01:00
|
|
|
ClearHeaps();
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
child.SeekToLast();
|
|
|
|
if (child.Valid()) {
|
|
|
|
maxHeap_.push(&child);
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
FindLargest();
|
2011-03-21 20:40:57 +01:00
|
|
|
direction_ = kReverse;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Seek(const Slice& target) {
|
2012-12-26 20:51:36 +01:00
|
|
|
ClearHeaps();
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
child.Seek(target);
|
|
|
|
if (child.Valid()) {
|
|
|
|
minHeap_.push(&child);
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
FindSmallest();
|
2011-03-21 20:40:57 +01:00
|
|
|
direction_ = kForward;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Next() {
|
|
|
|
assert(Valid());
|
2011-03-21 20:40:57 +01:00
|
|
|
|
|
|
|
// Ensure that all children are positioned after key().
|
|
|
|
// If we are moving in the forward direction, it is already
|
|
|
|
// true for all of the non-current_ children since current_ is
|
|
|
|
// the smallest child and key() == current_->key(). Otherwise,
|
|
|
|
// we explicitly position the non-current_ children.
|
|
|
|
if (direction_ != kForward) {
|
2012-12-26 20:51:36 +01:00
|
|
|
ClearHeaps();
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
if (&child != current_) {
|
|
|
|
child.Seek(key());
|
|
|
|
if (child.Valid() &&
|
|
|
|
comparator_->Compare(key(), child.key()) == 0) {
|
|
|
|
child.Next();
|
2011-03-21 20:40:57 +01:00
|
|
|
}
|
2013-08-21 07:58:16 +02:00
|
|
|
if (child.Valid()) {
|
|
|
|
minHeap_.push(&child);
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
2011-03-21 20:40:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
direction_ = kForward;
|
|
|
|
}
|
|
|
|
|
2012-12-26 20:51:36 +01:00
|
|
|
// as the current points to the current record. move the iterator forward.
|
|
|
|
// and if it is valid add it to the heap.
|
2011-03-18 23:37:00 +01:00
|
|
|
current_->Next();
|
2012-12-26 20:51:36 +01:00
|
|
|
if (current_->Valid()){
|
|
|
|
minHeap_.push(current_);
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
FindSmallest();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Prev() {
|
|
|
|
assert(Valid());
|
2011-03-21 20:40:57 +01:00
|
|
|
// Ensure that all children are positioned before key().
|
|
|
|
// If we are moving in the reverse direction, it is already
|
|
|
|
// true for all of the non-current_ children since current_ is
|
|
|
|
// the largest child and key() == current_->key(). Otherwise,
|
|
|
|
// we explicitly position the non-current_ children.
|
|
|
|
if (direction_ != kReverse) {
|
2012-12-26 20:51:36 +01:00
|
|
|
ClearHeaps();
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
if (&child != current_) {
|
|
|
|
child.Seek(key());
|
|
|
|
if (child.Valid()) {
|
2011-03-21 20:40:57 +01:00
|
|
|
// Child is at first entry >= key(). Step back one to be < key()
|
2013-08-21 07:58:16 +02:00
|
|
|
child.Prev();
|
2011-03-21 20:40:57 +01:00
|
|
|
} else {
|
|
|
|
// Child has no entries >= key(). Position at last entry.
|
2013-08-21 07:58:16 +02:00
|
|
|
child.SeekToLast();
|
2011-03-21 20:40:57 +01:00
|
|
|
}
|
2013-08-21 07:58:16 +02:00
|
|
|
if (child.Valid()) {
|
|
|
|
maxHeap_.push(&child);
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
2011-03-21 20:40:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
direction_ = kReverse;
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
current_->Prev();
|
2012-12-26 20:51:36 +01:00
|
|
|
if (current_->Valid()) {
|
|
|
|
maxHeap_.push(current_);
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
FindLargest();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Slice key() const {
|
|
|
|
assert(Valid());
|
|
|
|
return current_->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Slice value() const {
|
|
|
|
assert(Valid());
|
|
|
|
return current_->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status status() const {
|
|
|
|
Status status;
|
2013-08-21 07:58:16 +02:00
|
|
|
for (auto& child : children_) {
|
|
|
|
status = child.status();
|
2011-03-18 23:37:00 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void FindSmallest();
|
|
|
|
void FindLargest();
|
2012-12-26 20:51:36 +01:00
|
|
|
void ClearHeaps();
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
const Comparator* comparator_;
|
2013-08-21 07:58:16 +02:00
|
|
|
std::vector<IteratorWrapper> children_;
|
2011-03-18 23:37:00 +01:00
|
|
|
IteratorWrapper* current_;
|
2011-03-21 20:40:57 +01:00
|
|
|
// Which direction is the iterator moving?
|
|
|
|
enum Direction {
|
|
|
|
kForward,
|
|
|
|
kReverse
|
|
|
|
};
|
|
|
|
Direction direction_;
|
2012-12-26 20:51:36 +01:00
|
|
|
MaxIterHeap maxHeap_;
|
|
|
|
MinIterHeap minHeap_;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void MergingIterator::FindSmallest() {
|
2012-12-26 20:51:36 +01:00
|
|
|
if (minHeap_.empty()) {
|
2013-03-01 03:04:58 +01:00
|
|
|
current_ = nullptr;
|
2012-12-26 20:51:36 +01:00
|
|
|
} else {
|
|
|
|
current_ = minHeap_.top();
|
|
|
|
assert(current_->Valid());
|
|
|
|
minHeap_.pop();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergingIterator::FindLargest() {
|
2012-12-26 20:51:36 +01:00
|
|
|
if (maxHeap_.empty()) {
|
2013-03-01 03:04:58 +01:00
|
|
|
current_ = nullptr;
|
2012-12-26 20:51:36 +01:00
|
|
|
} else {
|
|
|
|
current_ = maxHeap_.top();
|
|
|
|
assert(current_->Valid());
|
|
|
|
maxHeap_.pop();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2012-12-26 20:51:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MergingIterator::ClearHeaps() {
|
|
|
|
maxHeap_ = NewMaxIterHeap(comparator_);
|
|
|
|
minHeap_ = NewMinIterHeap(comparator_);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2011-10-31 18:22:06 +01:00
|
|
|
} // namespace
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
Iterator* NewMergingIterator(const Comparator* cmp, Iterator** list, int n) {
|
|
|
|
assert(n >= 0);
|
|
|
|
if (n == 0) {
|
|
|
|
return NewEmptyIterator();
|
|
|
|
} else if (n == 1) {
|
|
|
|
return list[0];
|
|
|
|
} else {
|
|
|
|
return new MergingIterator(cmp, list, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|