@ -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 {