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"
|
|
|
|
|
2011-03-30 20:35:40 +02:00
|
|
|
#include "leveldb/comparator.h"
|
|
|
|
#include "leveldb/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"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
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),
|
|
|
|
children_(new IteratorWrapper[n]),
|
|
|
|
n_(n),
|
2011-03-21 20:40:57 +01:00
|
|
|
current_(NULL),
|
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]);
|
|
|
|
}
|
2012-12-26 20:51:36 +01:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
if (children_[i].Valid()) {
|
|
|
|
minHeap_.push(&children_[i]);
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MergingIterator() {
|
|
|
|
delete[] children_;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Valid() const {
|
|
|
|
return (current_ != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SeekToFirst() {
|
2012-12-26 20:51:36 +01:00
|
|
|
ClearHeaps();
|
2011-03-18 23:37:00 +01:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
children_[i].SeekToFirst();
|
2012-12-26 20:51:36 +01:00
|
|
|
if (children_[i].Valid()) {
|
|
|
|
minHeap_.push(&children_[i]);
|
|
|
|
}
|
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();
|
2011-03-18 23:37:00 +01:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
children_[i].SeekToLast();
|
2012-12-26 20:51:36 +01:00
|
|
|
if (children_[i].Valid()) {
|
|
|
|
maxHeap_.push(&children_[i]);
|
|
|
|
}
|
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();
|
2011-03-18 23:37:00 +01:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
children_[i].Seek(target);
|
2012-12-26 20:51:36 +01:00
|
|
|
if (children_[i].Valid()) {
|
|
|
|
minHeap_.push(&children_[i]);
|
|
|
|
}
|
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();
|
2011-03-21 20:40:57 +01:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
IteratorWrapper* child = &children_[i];
|
|
|
|
if (child != current_) {
|
|
|
|
child->Seek(key());
|
|
|
|
if (child->Valid() &&
|
|
|
|
comparator_->Compare(key(), child->key()) == 0) {
|
|
|
|
child->Next();
|
|
|
|
}
|
2012-12-26 20:51:36 +01:00
|
|
|
if (child->Valid()) {
|
|
|
|
minHeap_.push(child);
|
|
|
|
}
|
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();
|
2011-03-21 20:40:57 +01:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
IteratorWrapper* child = &children_[i];
|
|
|
|
if (child != current_) {
|
|
|
|
child->Seek(key());
|
|
|
|
if (child->Valid()) {
|
|
|
|
// Child is at first entry >= key(). Step back one to be < key()
|
|
|
|
child->Prev();
|
|
|
|
} else {
|
|
|
|
// Child has no entries >= key(). Position at last entry.
|
|
|
|
child->SeekToLast();
|
|
|
|
}
|
2012-12-26 20:51:36 +01:00
|
|
|
if (child->Valid()) {
|
|
|
|
maxHeap_.push(child);
|
|
|
|
}
|
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;
|
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
status = children_[i].status();
|
|
|
|
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_;
|
|
|
|
IteratorWrapper* children_;
|
|
|
|
int n_;
|
|
|
|
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()) {
|
|
|
|
current_ = NULL;
|
|
|
|
} 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()) {
|
|
|
|
current_ = NULL;
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 18:22:06 +01:00
|
|
|
} // namespace leveldb
|