[RocksDB] Minor iterator cleanup

Summary: Was going through the iterator related code, did some cleanup along the way. Basically replaced array with vector and adopted range based loop where applicable.

Test Plan: make check; make valgrind_check

Reviewers: dhruba, emayanke

Reviewed By: emayanke

CC: leveldb

Differential Revision: https://reviews.facebook.net/D12435
main
Haobo Xu 11 years ago
parent 404d63ac3e
commit f9e2decf7c
  1. 4
      db/version_set.cc
  2. 80
      table/merger.cc

@ -210,10 +210,10 @@ void Version::AddIterators(const ReadOptions& options,
const EnvOptions& soptions, const EnvOptions& soptions,
std::vector<Iterator*>* iters) { std::vector<Iterator*>* iters) {
// Merge all level zero files together since they may overlap // Merge all level zero files together since they may overlap
for (size_t i = 0; i < files_[0].size(); i++) { for (const FileMetaData* file : files_[0]) {
iters->push_back( iters->push_back(
vset_->table_cache_->NewIterator( vset_->table_cache_->NewIterator(
options, soptions, files_[0][i]->number, files_[0][i]->file_size)); options, soptions, file->number, file->file_size));
} }
// For levels > 0, we can use a concatenating iterator that sequentially // For levels > 0, we can use a concatenating iterator that sequentially

@ -9,6 +9,8 @@
#include "table/iter_heap.h" #include "table/iter_heap.h"
#include "table/iterator_wrapper.h" #include "table/iterator_wrapper.h"
#include <vector>
namespace leveldb { namespace leveldb {
namespace { namespace {
@ -17,8 +19,7 @@ class MergingIterator : public Iterator {
public: public:
MergingIterator(const Comparator* comparator, Iterator** children, int n) MergingIterator(const Comparator* comparator, Iterator** children, int n)
: comparator_(comparator), : comparator_(comparator),
children_(new IteratorWrapper[n]), children_(n),
n_(n),
current_(nullptr), current_(nullptr),
direction_(kForward), direction_(kForward),
maxHeap_(NewMaxIterHeap(comparator_)), maxHeap_(NewMaxIterHeap(comparator_)),
@ -26,16 +27,14 @@ class MergingIterator : public Iterator {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
children_[i].Set(children[i]); children_[i].Set(children[i]);
} }
for (int i = 0; i < n; ++i) { for (auto& child : children_) {
if (children_[i].Valid()) { if (child.Valid()) {
minHeap_.push(&children_[i]); minHeap_.push(&child);
} }
} }
} }
virtual ~MergingIterator() { virtual ~MergingIterator() { }
delete[] children_;
}
virtual bool Valid() const { virtual bool Valid() const {
return (current_ != nullptr); return (current_ != nullptr);
@ -43,10 +42,10 @@ class MergingIterator : public Iterator {
virtual void SeekToFirst() { virtual void SeekToFirst() {
ClearHeaps(); ClearHeaps();
for (int i = 0; i < n_; i++) { for (auto& child : children_) {
children_[i].SeekToFirst(); child.SeekToFirst();
if (children_[i].Valid()) { if (child.Valid()) {
minHeap_.push(&children_[i]); minHeap_.push(&child);
} }
} }
FindSmallest(); FindSmallest();
@ -55,10 +54,10 @@ class MergingIterator : public Iterator {
virtual void SeekToLast() { virtual void SeekToLast() {
ClearHeaps(); ClearHeaps();
for (int i = 0; i < n_; i++) { for (auto& child : children_) {
children_[i].SeekToLast(); child.SeekToLast();
if (children_[i].Valid()) { if (child.Valid()) {
maxHeap_.push(&children_[i]); maxHeap_.push(&child);
} }
} }
FindLargest(); FindLargest();
@ -67,10 +66,10 @@ class MergingIterator : public Iterator {
virtual void Seek(const Slice& target) { virtual void Seek(const Slice& target) {
ClearHeaps(); ClearHeaps();
for (int i = 0; i < n_; i++) { for (auto& child : children_) {
children_[i].Seek(target); child.Seek(target);
if (children_[i].Valid()) { if (child.Valid()) {
minHeap_.push(&children_[i]); minHeap_.push(&child);
} }
} }
FindSmallest(); FindSmallest();
@ -87,16 +86,15 @@ class MergingIterator : public Iterator {
// we explicitly position the non-current_ children. // we explicitly position the non-current_ children.
if (direction_ != kForward) { if (direction_ != kForward) {
ClearHeaps(); ClearHeaps();
for (int i = 0; i < n_; i++) { for (auto& child : children_) {
IteratorWrapper* child = &children_[i]; if (&child != current_) {
if (child != current_) { child.Seek(key());
child->Seek(key()); if (child.Valid() &&
if (child->Valid() && comparator_->Compare(key(), child.key()) == 0) {
comparator_->Compare(key(), child->key()) == 0) { child.Next();
child->Next();
} }
if (child->Valid()) { if (child.Valid()) {
minHeap_.push(child); minHeap_.push(&child);
} }
} }
} }
@ -121,19 +119,18 @@ class MergingIterator : public Iterator {
// we explicitly position the non-current_ children. // we explicitly position the non-current_ children.
if (direction_ != kReverse) { if (direction_ != kReverse) {
ClearHeaps(); ClearHeaps();
for (int i = 0; i < n_; i++) { for (auto& child : children_) {
IteratorWrapper* child = &children_[i]; if (&child != current_) {
if (child != current_) { child.Seek(key());
child->Seek(key()); if (child.Valid()) {
if (child->Valid()) {
// Child is at first entry >= key(). Step back one to be < key() // Child is at first entry >= key(). Step back one to be < key()
child->Prev(); child.Prev();
} else { } else {
// Child has no entries >= key(). Position at last entry. // Child has no entries >= key(). Position at last entry.
child->SeekToLast(); child.SeekToLast();
} }
if (child->Valid()) { if (child.Valid()) {
maxHeap_.push(child); maxHeap_.push(&child);
} }
} }
} }
@ -159,8 +156,8 @@ class MergingIterator : public Iterator {
virtual Status status() const { virtual Status status() const {
Status status; Status status;
for (int i = 0; i < n_; i++) { for (auto& child : children_) {
status = children_[i].status(); status = child.status();
if (!status.ok()) { if (!status.ok()) {
break; break;
} }
@ -174,8 +171,7 @@ class MergingIterator : public Iterator {
void ClearHeaps(); void ClearHeaps();
const Comparator* comparator_; const Comparator* comparator_;
IteratorWrapper* children_; std::vector<IteratorWrapper> children_;
int n_;
IteratorWrapper* current_; IteratorWrapper* current_;
// Which direction is the iterator moving? // Which direction is the iterator moving?
enum Direction { enum Direction {

Loading…
Cancel
Save