stasis-bLSM/logiterators.cpp
sears 094a0d41ee removed some more redundant code by unifying merge_iterators
git-svn-id: svn+ssh://svn.corp.yahoo.com/yahoo/yrl/labs/pnuts/code/logstore@532 8dad8b1f-cf64-0410-95b6-bcf113ffbcfe
2010-01-28 02:20:49 +00:00

112 lines
2.5 KiB
C++

#include "logstore.h"
#include "logiterators.h"
/////////////////////////////////////////////////////////////////////
// tree iterator implementation
/////////////////////////////////////////////////////////////////////
template <class TUPLE>
treeIterator<TUPLE>::treeIterator(recordid tree) :
tree_(tree),
lsmIterator_(logtreeIterator::open(-1,tree)),
curr_tuple(0)
{
init_helper();
}
template <class TUPLE>
treeIterator<TUPLE>::treeIterator(recordid tree, TUPLE& key) :
tree_(tree),
lsmIterator_(logtreeIterator::openAt(-1,tree,key.get_key()))
{
init_helper();
}
template <class TUPLE>
treeIterator<TUPLE>::~treeIterator()
{
if(lsmIterator_)
logtreeIterator::close(-1, lsmIterator_);
if(curr_tuple != NULL)
free(curr_tuple);
if(curr_page!=NULL)
{
delete curr_page;
curr_page = 0;
}
}
template <class TUPLE>
void treeIterator<TUPLE>::init_helper()
{
if(!lsmIterator_)
{
printf("treeIterator:\t__error__ init_helper():\tnull lsmIterator_");
curr_page = 0;
dp_itr = 0;
}
else
{
if(logtreeIterator::next(-1, lsmIterator_) == 0)
{
//printf("treeIterator:\t__error__ init_helper():\tlogtreeIteratr::next returned 0." );
curr_page = 0;
dp_itr = 0;
}
else
{
pageid_t * pid_tmp;
pageid_t ** hack = &pid_tmp;
logtreeIterator::value(-1,lsmIterator_,(byte**)hack);
curr_pageid = *pid_tmp;
curr_page = new DataPage<TUPLE>(-1, curr_pageid);
dp_itr = new DPITR_T(curr_page->begin());
}
}
}
template <class TUPLE>
TUPLE * treeIterator<TUPLE>::getnext()
{
assert(this->lsmIterator_);
if(dp_itr == 0)
return 0;
TUPLE* readTuple = dp_itr->getnext(-1);
if(!readTuple)
{
delete dp_itr;
dp_itr = 0;
delete curr_page;
curr_page = 0;
if(logtreeIterator::next(-1,lsmIterator_))
{
pageid_t *pid_tmp;
pageid_t **hack = &pid_tmp;
logtreeIterator::value(-1,lsmIterator_,(byte**)hack);
curr_pageid = *pid_tmp;
curr_page = new DataPage<TUPLE>(-1, curr_pageid);
dp_itr = new DPITR_T(curr_page->begin());
readTuple = dp_itr->getnext(-1);
assert(readTuple);
}
// else readTuple is null. We're done.
}
curr_tuple = readTuple;
return curr_tuple;
}