2011-03-18 22:37:00 +00: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 18:35:40 +00:00
|
|
|
#include "leveldb/comparator.h"
|
|
|
|
#include "leveldb/iterator.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "table/iterator_wrapper.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MergingIterator : public Iterator {
|
|
|
|
public:
|
|
|
|
MergingIterator(const Comparator* comparator, Iterator** children, int n)
|
|
|
|
: comparator_(comparator),
|
|
|
|
children_(new IteratorWrapper[n]),
|
|
|
|
n_(n),
|
2018-04-10 16:18:06 -07:00
|
|
|
current_(nullptr),
|
2011-03-21 19:40:57 +00:00
|
|
|
direction_(kForward) {
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
children_[i].Set(children[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
~MergingIterator() override { delete[] children_; }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
bool Valid() const override { return (current_ != nullptr); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void SeekToFirst() override {
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
children_[i].SeekToFirst();
|
|
|
|
}
|
|
|
|
FindSmallest();
|
2011-03-21 19:40:57 +00:00
|
|
|
direction_ = kForward;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void SeekToLast() override {
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
children_[i].SeekToLast();
|
|
|
|
}
|
|
|
|
FindLargest();
|
2011-03-21 19:40:57 +00:00
|
|
|
direction_ = kReverse;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void Seek(const Slice& target) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
children_[i].Seek(target);
|
|
|
|
}
|
|
|
|
FindSmallest();
|
2011-03-21 19:40:57 +00:00
|
|
|
direction_ = kForward;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void Next() override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
2011-03-21 19:40:57 +00: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) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
direction_ = kForward;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
current_->Next();
|
|
|
|
FindSmallest();
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void Prev() override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
2011-03-21 19:40:57 +00: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) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
direction_ = kReverse;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
current_->Prev();
|
|
|
|
FindLargest();
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
Slice key() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
|
|
|
return current_->key();
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
Slice value() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(Valid());
|
|
|
|
return current_->value();
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
Status status() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status status;
|
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
status = children_[i].status();
|
|
|
|
if (!status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-03 09:31:18 -07:00
|
|
|
// Which direction is the iterator moving?
|
|
|
|
enum Direction { kForward, kReverse };
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
void FindSmallest();
|
|
|
|
void FindLargest();
|
|
|
|
|
|
|
|
// We might want to use a heap in case there are lots of children.
|
|
|
|
// For now we use a simple array since we expect a very small number
|
|
|
|
// of children in leveldb.
|
|
|
|
const Comparator* comparator_;
|
|
|
|
IteratorWrapper* children_;
|
|
|
|
int n_;
|
|
|
|
IteratorWrapper* current_;
|
2011-03-21 19:40:57 +00:00
|
|
|
Direction direction_;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void MergingIterator::FindSmallest() {
|
2018-04-10 16:18:06 -07:00
|
|
|
IteratorWrapper* smallest = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
for (int i = 0; i < n_; i++) {
|
|
|
|
IteratorWrapper* child = &children_[i];
|
|
|
|
if (child->Valid()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
if (smallest == nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
smallest = child;
|
|
|
|
} else if (comparator_->Compare(child->key(), smallest->key()) < 0) {
|
|
|
|
smallest = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current_ = smallest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergingIterator::FindLargest() {
|
2018-04-10 16:18:06 -07:00
|
|
|
IteratorWrapper* largest = nullptr;
|
2019-05-02 11:01:00 -07:00
|
|
|
for (int i = n_ - 1; i >= 0; i--) {
|
2011-03-18 22:37:00 +00:00
|
|
|
IteratorWrapper* child = &children_[i];
|
|
|
|
if (child->Valid()) {
|
2018-04-10 16:18:06 -07:00
|
|
|
if (largest == nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
largest = child;
|
|
|
|
} else if (comparator_->Compare(child->key(), largest->key()) > 0) {
|
|
|
|
largest = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current_ = largest;
|
|
|
|
}
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* NewMergingIterator(const Comparator* comparator, Iterator** children,
|
|
|
|
int n) {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(n >= 0);
|
|
|
|
if (n == 0) {
|
|
|
|
return NewEmptyIterator();
|
|
|
|
} else if (n == 1) {
|
2019-05-02 11:01:00 -07:00
|
|
|
return children[0];
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
2019-05-02 11:01:00 -07:00
|
|
|
return new MergingIterator(comparator, children, n);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|