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

12 13
#include "rocksdb/comparator.h"
#include "rocksdb/iterator.h"
14
#include "rocksdb/options.h"
15
#include "table/iter_heap.h"
J
jorlow@chromium.org 已提交
16
#include "table/iterator_wrapper.h"
17 18
#include "util/stop_watch.h"
#include "util/perf_context_imp.h"
J
jorlow@chromium.org 已提交
19

H
Haobo Xu 已提交
20 21
#include <vector>

22
namespace rocksdb {
J
jorlow@chromium.org 已提交
23 24

namespace {
25

J
jorlow@chromium.org 已提交
26 27
class MergingIterator : public Iterator {
 public:
I
Igor Canadi 已提交
28
  MergingIterator(const Comparator* comparator, Iterator** children, int n)
J
jorlow@chromium.org 已提交
29
      : comparator_(comparator),
H
Haobo Xu 已提交
30
        children_(n),
A
Abhishek Kona 已提交
31
        current_(nullptr),
32
        use_heap_(true),
33 34
        direction_(kForward),
        maxHeap_(NewMaxIterHeap(comparator_)),
I
Igor Canadi 已提交
35
        minHeap_(NewMinIterHeap(comparator_)) {
J
jorlow@chromium.org 已提交
36 37 38
    for (int i = 0; i < n; i++) {
      children_[i].Set(children[i]);
    }
H
Haobo Xu 已提交
39 40 41
    for (auto& child : children_) {
      if (child.Valid()) {
        minHeap_.push(&child);
42 43
      }
    }
J
jorlow@chromium.org 已提交
44 45
  }

H
Haobo Xu 已提交
46
  virtual ~MergingIterator() { }
J
jorlow@chromium.org 已提交
47 48

  virtual bool Valid() const {
A
Abhishek Kona 已提交
49
    return (current_ != nullptr);
J
jorlow@chromium.org 已提交
50 51 52
  }

  virtual void SeekToFirst() {
53
    ClearHeaps();
H
Haobo Xu 已提交
54 55 56 57
    for (auto& child : children_) {
      child.SeekToFirst();
      if (child.Valid()) {
        minHeap_.push(&child);
58
      }
J
jorlow@chromium.org 已提交
59 60
    }
    FindSmallest();
J
jorlow@chromium.org 已提交
61
    direction_ = kForward;
J
jorlow@chromium.org 已提交
62 63 64
  }

  virtual void SeekToLast() {
65
    ClearHeaps();
H
Haobo Xu 已提交
66 67 68 69
    for (auto& child : children_) {
      child.SeekToLast();
      if (child.Valid()) {
        maxHeap_.push(&child);
70
      }
J
jorlow@chromium.org 已提交
71 72
    }
    FindLargest();
J
jorlow@chromium.org 已提交
73
    direction_ = kReverse;
J
jorlow@chromium.org 已提交
74 75 76
  }

  virtual void Seek(const Slice& target) {
77 78 79
    // Invalidate the heap.
    use_heap_ = false;
    IteratorWrapper* first_child = nullptr;
L
Lei Jin 已提交
80 81
    PERF_TIMER_DECLARE();

H
Haobo Xu 已提交
82
    for (auto& child : children_) {
L
Lei Jin 已提交
83
      PERF_TIMER_START(seek_child_seek_time);
H
Haobo Xu 已提交
84
      child.Seek(target);
L
Lei Jin 已提交
85 86
      PERF_TIMER_STOP(seek_child_seek_time);
      PERF_COUNTER_ADD(seek_child_seek_count, 1);
87

H
Haobo Xu 已提交
88
      if (child.Valid()) {
89 90 91 92 93 94 95 96 97
        // This child has valid key
        if (!use_heap_) {
          if (first_child == nullptr) {
            // It's the first child has valid key. Only put it int
            // current_. Now the values in the heap should be invalid.
            first_child = &child;
          } else {
            // We have more than one children with valid keys. Initialize
            // the heap and put the first child into the heap.
L
Lei Jin 已提交
98
            PERF_TIMER_START(seek_min_heap_time);
99 100
            ClearHeaps();
            minHeap_.push(first_child);
L
Lei Jin 已提交
101
            PERF_TIMER_STOP(seek_min_heap_time);
102 103 104
          }
        }
        if (use_heap_) {
L
Lei Jin 已提交
105
          PERF_TIMER_START(seek_min_heap_time);
106
          minHeap_.push(&child);
L
Lei Jin 已提交
107
          PERF_TIMER_STOP(seek_min_heap_time);
108
        }
109
      }
J
jorlow@chromium.org 已提交
110
    }
111 112
    if (use_heap_) {
      // If heap is valid, need to put the smallest key to curent_.
L
Lei Jin 已提交
113
      PERF_TIMER_START(seek_min_heap_time);
114
      FindSmallest();
L
Lei Jin 已提交
115
      PERF_TIMER_STOP(seek_min_heap_time);
116 117 118 119 120
    } else {
      // The heap is not valid, then the current_ iterator is the first
      // one, or null if there is no first child.
      current_ = first_child;
    }
121
    direction_ = kForward;
J
jorlow@chromium.org 已提交
122 123 124 125
  }

  virtual void Next() {
    assert(Valid());
J
jorlow@chromium.org 已提交
126 127 128 129 130 131 132

    // 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) {
133
      ClearHeaps();
H
Haobo Xu 已提交
134 135 136 137 138 139
      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 已提交
140
          }
H
Haobo Xu 已提交
141 142
          if (child.Valid()) {
            minHeap_.push(&child);
143
          }
J
jorlow@chromium.org 已提交
144 145 146 147 148
        }
      }
      direction_ = kForward;
    }

149 150
    // 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 已提交
151
    current_->Next();
152 153 154 155 156 157 158
    if (use_heap_) {
      if (current_->Valid()) {
        minHeap_.push(current_);
      }
      FindSmallest();
    } else if (!current_->Valid()) {
      current_ = nullptr;
159
    }
J
jorlow@chromium.org 已提交
160 161 162 163
  }

  virtual void Prev() {
    assert(Valid());
J
jorlow@chromium.org 已提交
164 165 166 167 168 169
    // 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) {
170
      ClearHeaps();
H
Haobo Xu 已提交
171 172 173 174
      for (auto& child : children_) {
        if (&child != current_) {
          child.Seek(key());
          if (child.Valid()) {
J
jorlow@chromium.org 已提交
175
            // Child is at first entry >= key().  Step back one to be < key()
H
Haobo Xu 已提交
176
            child.Prev();
J
jorlow@chromium.org 已提交
177 178
          } else {
            // Child has no entries >= key().  Position at last entry.
H
Haobo Xu 已提交
179
            child.SeekToLast();
J
jorlow@chromium.org 已提交
180
          }
H
Haobo Xu 已提交
181 182
          if (child.Valid()) {
            maxHeap_.push(&child);
183
          }
J
jorlow@chromium.org 已提交
184 185 186 187 188
        }
      }
      direction_ = kReverse;
    }

J
jorlow@chromium.org 已提交
189
    current_->Prev();
190 191 192
    if (current_->Valid()) {
      maxHeap_.push(current_);
    }
J
jorlow@chromium.org 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    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 已提交
208 209
    for (auto& child : children_) {
      status = child.status();
J
jorlow@chromium.org 已提交
210 211 212 213 214 215 216 217 218 219
      if (!status.ok()) {
        break;
      }
    }
    return status;
  }

 private:
  void FindSmallest();
  void FindLargest();
220
  void ClearHeaps();
J
jorlow@chromium.org 已提交
221 222

  const Comparator* comparator_;
H
Haobo Xu 已提交
223
  std::vector<IteratorWrapper> children_;
J
jorlow@chromium.org 已提交
224
  IteratorWrapper* current_;
225 226 227
  // If the value is true, both of iterators in the heap and current_
  // contain valid rows. If it is false, only current_ can possibly contain
  // valid rows.
228 229
  // This flag is always true for reverse direction, as we always use heap for
  // the reverse iterating case.
230
  bool use_heap_;
J
jorlow@chromium.org 已提交
231 232 233 234 235 236
  // Which direction is the iterator moving?
  enum Direction {
    kForward,
    kReverse
  };
  Direction direction_;
237 238
  MaxIterHeap maxHeap_;
  MinIterHeap minHeap_;
J
jorlow@chromium.org 已提交
239 240 241
};

void MergingIterator::FindSmallest() {
242
  assert(use_heap_);
243
  if (minHeap_.empty()) {
A
Abhishek Kona 已提交
244
    current_ = nullptr;
245 246 247 248
  } else {
    current_ = minHeap_.top();
    assert(current_->Valid());
    minHeap_.pop();
J
jorlow@chromium.org 已提交
249 250 251 252
  }
}

void MergingIterator::FindLargest() {
253
  assert(use_heap_);
254
  if (maxHeap_.empty()) {
A
Abhishek Kona 已提交
255
    current_ = nullptr;
256 257 258 259
  } else {
    current_ = maxHeap_.top();
    assert(current_->Valid());
    maxHeap_.pop();
J
jorlow@chromium.org 已提交
260
  }
261 262 263
}

void MergingIterator::ClearHeaps() {
264
  use_heap_ = true;
265 266
  maxHeap_ = NewMaxIterHeap(comparator_);
  minHeap_ = NewMinIterHeap(comparator_);
J
jorlow@chromium.org 已提交
267
}
H
Hans Wennborg 已提交
268
}  // namespace
J
jorlow@chromium.org 已提交
269

I
Igor Canadi 已提交
270
Iterator* NewMergingIterator(const Comparator* cmp, Iterator** list, int n) {
J
jorlow@chromium.org 已提交
271 272 273 274 275 276
  assert(n >= 0);
  if (n == 0) {
    return NewEmptyIterator();
  } else if (n == 1) {
    return list[0];
  } else {
I
Igor Canadi 已提交
277
    return new MergingIterator(cmp, list, n);
J
jorlow@chromium.org 已提交
278 279 280
  }
}

281
}  // namespace rocksdb