merger.cc 5.2 KB
Newer Older
J
jorlow@chromium.org 已提交
1 2 3 4 5 6
// 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"

7 8
#include "rocksdb/comparator.h"
#include "rocksdb/iterator.h"
9
#include "table/iter_heap.h"
J
jorlow@chromium.org 已提交
10 11
#include "table/iterator_wrapper.h"

H
Haobo Xu 已提交
12 13
#include <vector>

14
namespace rocksdb {
J
jorlow@chromium.org 已提交
15 16

namespace {
17

J
jorlow@chromium.org 已提交
18 19 20 21
class MergingIterator : public Iterator {
 public:
  MergingIterator(const Comparator* comparator, Iterator** children, int n)
      : comparator_(comparator),
H
Haobo Xu 已提交
22
        children_(n),
A
Abhishek Kona 已提交
23
        current_(nullptr),
24 25 26
        direction_(kForward),
        maxHeap_(NewMaxIterHeap(comparator_)),
        minHeap_ (NewMinIterHeap(comparator_)) {
J
jorlow@chromium.org 已提交
27 28 29
    for (int i = 0; i < n; i++) {
      children_[i].Set(children[i]);
    }
H
Haobo Xu 已提交
30 31 32
    for (auto& child : children_) {
      if (child.Valid()) {
        minHeap_.push(&child);
33 34
      }
    }
J
jorlow@chromium.org 已提交
35 36
  }

H
Haobo Xu 已提交
37
  virtual ~MergingIterator() { }
J
jorlow@chromium.org 已提交
38 39

  virtual bool Valid() const {
A
Abhishek Kona 已提交
40
    return (current_ != nullptr);
J
jorlow@chromium.org 已提交
41 42 43
  }

  virtual void SeekToFirst() {
44
    ClearHeaps();
H
Haobo Xu 已提交
45 46 47 48
    for (auto& child : children_) {
      child.SeekToFirst();
      if (child.Valid()) {
        minHeap_.push(&child);
49
      }
J
jorlow@chromium.org 已提交
50 51
    }
    FindSmallest();
J
jorlow@chromium.org 已提交
52
    direction_ = kForward;
J
jorlow@chromium.org 已提交
53 54 55
  }

  virtual void SeekToLast() {
56
    ClearHeaps();
H
Haobo Xu 已提交
57 58 59 60
    for (auto& child : children_) {
      child.SeekToLast();
      if (child.Valid()) {
        maxHeap_.push(&child);
61
      }
J
jorlow@chromium.org 已提交
62 63
    }
    FindLargest();
J
jorlow@chromium.org 已提交
64
    direction_ = kReverse;
J
jorlow@chromium.org 已提交
65 66 67
  }

  virtual void Seek(const Slice& target) {
68
    ClearHeaps();
H
Haobo Xu 已提交
69 70 71 72
    for (auto& child : children_) {
      child.Seek(target);
      if (child.Valid()) {
        minHeap_.push(&child);
73
      }
J
jorlow@chromium.org 已提交
74 75
    }
    FindSmallest();
J
jorlow@chromium.org 已提交
76
    direction_ = kForward;
J
jorlow@chromium.org 已提交
77 78 79 80
  }

  virtual void Next() {
    assert(Valid());
J
jorlow@chromium.org 已提交
81 82 83 84 85 86 87

    // 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) {
88
      ClearHeaps();
H
Haobo Xu 已提交
89 90 91 92 93 94
      for (auto& child : children_) {
        if (&child != current_) {
          child.Seek(key());
          if (child.Valid() &&
              comparator_->Compare(key(), child.key()) == 0) {
            child.Next();
J
jorlow@chromium.org 已提交
95
          }
H
Haobo Xu 已提交
96 97
          if (child.Valid()) {
            minHeap_.push(&child);
98
          }
J
jorlow@chromium.org 已提交
99 100 101 102 103
        }
      }
      direction_ = kForward;
    }

104 105
    // as the current points to the current record. move the iterator forward.
    // and if it is valid add it to the heap.
J
jorlow@chromium.org 已提交
106
    current_->Next();
107 108 109
    if (current_->Valid()){
      minHeap_.push(current_);
    }
J
jorlow@chromium.org 已提交
110 111 112 113 114
    FindSmallest();
  }

  virtual void Prev() {
    assert(Valid());
J
jorlow@chromium.org 已提交
115 116 117 118 119 120
    // 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) {
121
      ClearHeaps();
H
Haobo Xu 已提交
122 123 124 125
      for (auto& child : children_) {
        if (&child != current_) {
          child.Seek(key());
          if (child.Valid()) {
J
jorlow@chromium.org 已提交
126
            // Child is at first entry >= key().  Step back one to be < key()
H
Haobo Xu 已提交
127
            child.Prev();
J
jorlow@chromium.org 已提交
128 129
          } else {
            // Child has no entries >= key().  Position at last entry.
H
Haobo Xu 已提交
130
            child.SeekToLast();
J
jorlow@chromium.org 已提交
131
          }
H
Haobo Xu 已提交
132 133
          if (child.Valid()) {
            maxHeap_.push(&child);
134
          }
J
jorlow@chromium.org 已提交
135 136 137 138 139
        }
      }
      direction_ = kReverse;
    }

J
jorlow@chromium.org 已提交
140
    current_->Prev();
141 142 143
    if (current_->Valid()) {
      maxHeap_.push(current_);
    }
J
jorlow@chromium.org 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    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;
H
Haobo Xu 已提交
159 160
    for (auto& child : children_) {
      status = child.status();
J
jorlow@chromium.org 已提交
161 162 163 164 165 166 167 168 169 170
      if (!status.ok()) {
        break;
      }
    }
    return status;
  }

 private:
  void FindSmallest();
  void FindLargest();
171
  void ClearHeaps();
J
jorlow@chromium.org 已提交
172 173

  const Comparator* comparator_;
H
Haobo Xu 已提交
174
  std::vector<IteratorWrapper> children_;
J
jorlow@chromium.org 已提交
175
  IteratorWrapper* current_;
J
jorlow@chromium.org 已提交
176 177 178 179 180 181
  // Which direction is the iterator moving?
  enum Direction {
    kForward,
    kReverse
  };
  Direction direction_;
182 183
  MaxIterHeap maxHeap_;
  MinIterHeap minHeap_;
J
jorlow@chromium.org 已提交
184 185 186
};

void MergingIterator::FindSmallest() {
187
  if (minHeap_.empty()) {
A
Abhishek Kona 已提交
188
    current_ = nullptr;
189 190 191 192
  } else {
    current_ = minHeap_.top();
    assert(current_->Valid());
    minHeap_.pop();
J
jorlow@chromium.org 已提交
193 194 195 196
  }
}

void MergingIterator::FindLargest() {
197
  if (maxHeap_.empty()) {
A
Abhishek Kona 已提交
198
    current_ = nullptr;
199 200 201 202
  } else {
    current_ = maxHeap_.top();
    assert(current_->Valid());
    maxHeap_.pop();
J
jorlow@chromium.org 已提交
203
  }
204 205 206 207 208
}

void MergingIterator::ClearHeaps() {
  maxHeap_ = NewMaxIterHeap(comparator_);
  minHeap_ = NewMinIterHeap(comparator_);
J
jorlow@chromium.org 已提交
209
}
H
Hans Wennborg 已提交
210
}  // namespace
J
jorlow@chromium.org 已提交
211 212 213 214 215 216 217 218 219 220 221 222

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);
  }
}

223
}  // namespace rocksdb