diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ab3b42..a0d8e15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,7 @@ ENDIF ( "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" ) #CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) IF ( HAVE_STASIS ) - ADD_LIBRARY(logstore logserver.cpp logstore.cpp diskTreeComponent.cpp logiterators.cpp datapage.cpp merger.cpp tuplemerger.cpp) + ADD_LIBRARY(logstore logserver.cpp logstore.cpp diskTreeComponent.cpp memTreeComponent.cpp logiterators.cpp datapage.cpp merger.cpp tuplemerger.cpp) CREATE_EXECUTABLE(server) ENDIF ( HAVE_STASIS ) ADD_LIBRARY(logstore_client tcpclient.cpp) diff --git a/logiterators.cpp b/logiterators.cpp index e4036cb..8e1d9c8 100644 --- a/logiterators.cpp +++ b/logiterators.cpp @@ -1,6 +1,6 @@ #include "logstore.h" #include "logiterators.h" - +#include "memTreeComponent.h" ///////////////////////////////////////////////////////////////////// // tree iterator implementation ///////////////////////////////////////////////////////////////////// @@ -144,4 +144,4 @@ TUPLE * diskTreeIterator::getnext() } template class diskTreeIterator; -template class changingMemTreeIterator; +template class changingMemTreeIterator::rbtree_t, datatuple>; diff --git a/logiterators.h b/logiterators.h index 0945580..e9a0eca 100644 --- a/logiterators.h +++ b/logiterators.h @@ -72,70 +72,6 @@ private: -////////////////////////////////////////////////////////////// -// memTreeIterator -///////////////////////////////////////////////////////////// - -template -class memTreeIterator -{ -private: - typedef typename MEMTREE::const_iterator MTITER; - -public: - memTreeIterator( MEMTREE *s ) - : first_(true), - done_(s == NULL) { - init_iterators(s, NULL, NULL); - } - - memTreeIterator( MEMTREE *s, TUPLE *&key ) - : first_(true), done_(s == NULL) { - init_iterators(s, key, NULL); - } - - ~memTreeIterator() { - delete it_; - delete itend_; - } - - TUPLE* getnext() { - if(done_) { return NULL; } - if(first_) { first_ = 0;} else { (*it_)++; } - if(*it_==*itend_) { done_= true; return NULL; } - - return (*(*it_))->create_copy(); - } - - -private: - void init_iterators(MEMTREE * s, TUPLE * key1, TUPLE * key2) { - if(s) { - it_ = key1 ? new MTITER(s->find(key1)) : new MTITER(s->begin()); - itend_ = key2 ? new MTITER(s->find(key2)) : new MTITER(s->end()); - if(*it_ == *itend_) { done_ = true; } - if(key1) { - if(done_) { - DEBUG("memtree opened at eot\n"); - } else { - DEBUG("memtree opened key = %s\n", (**it_)->key()); - } - } - } else { - it_ = NULL; - itend_ = NULL; - } - } - explicit memTreeIterator() { abort(); } - void operator=(memTreeIterator & t) { abort(); } - int operator-(memTreeIterator & t) { abort(); } -private: - bool first_; - bool done_; - MTITER *it_; - MTITER *itend_; -}; - ///////////////////////////////////////////////////////////////// template diff --git a/logstore.cpp b/logstore.cpp index 7010e48..af9f438 100644 --- a/logstore.cpp +++ b/logstore.cpp @@ -48,26 +48,6 @@ logtable::logtable() epoch = 0; } -void logtable::tearDownTree(rbtree_ptr_t tree) { - datatuple * t = 0; - rbtree_t::iterator old; - for(rbtree_t::iterator delitr = tree->begin(); - delitr != tree->end(); - delitr++) { - if(t) { - tree->erase(old); - datatuple::freetuple(t); - t = 0; - } - t = *delitr; - old = delitr; - } - if(t) { - tree->erase(old); - datatuple::freetuple(t); - } - delete tree; -} logtable::~logtable() { @@ -78,7 +58,7 @@ logtable::~logtable() if(tree_c0 != NULL) { - tearDownTree(tree_c0); + memTreeComponent::tearDownTree(tree_c0); } deletelock(header_lock); @@ -173,7 +153,7 @@ void logtable::flushTable() pthread_cond_signal(mergedata->input_ready_cond); merge_count ++; - set_tree_c0(new rbtree_t); + set_tree_c0(new memTreeComponent::rbtree_t); tsize = 0; tree_bytes = 0; @@ -205,7 +185,7 @@ datatuple * logtable::findTuple(int xid, const datatuple::key_t key, size_t keyS datatuple *ret_tuple=0; //step 1: look in tree_c0 - rbtree_t::iterator rbitr = get_tree_c0()->find(search_tuple); + memTreeComponent::rbtree_t::iterator rbitr = get_tree_c0()->find(search_tuple); if(rbitr != get_tree_c0()->end()) { DEBUG("tree_c0 size %d\n", get_tree_c0()->size()); @@ -350,7 +330,7 @@ datatuple * logtable::findTuple_first(int xid, datatuple::key_t key, size_t keyS datatuple *ret_tuple=0; //step 1: look in tree_c0 - rbtree_t::iterator rbitr = get_tree_c0()->find(search_tuple); + memTreeComponent::rbtree_t::iterator rbitr = get_tree_c0()->find(search_tuple); if(rbitr != get_tree_c0()->end()) { DEBUG("tree_c0 size %d\n", tree_c0->size()); @@ -417,7 +397,7 @@ void logtable::insertTuple(datatuple *tuple) readlock(header_lock,0); pthread_mutex_lock(mergedata->rbtree_mut); //find the previous tuple with same key in the memtree if exists - rbtree_t::iterator rbitr = tree_c0->find(tuple); + memTreeComponent::rbtree_t::iterator rbitr = tree_c0->find(tuple); if(rbitr != tree_c0->end()) { datatuple *pre_t = *rbitr; diff --git a/logstore.h b/logstore.h index fb07b5e..49de4d4 100644 --- a/logstore.h +++ b/logstore.h @@ -5,7 +5,7 @@ #undef begin #include -#include +//#include #include #include #include @@ -31,16 +31,13 @@ #include #include "diskTreeComponent.h" - +#include "memTreeComponent.h" #include "datapage.h" #include "tuplemerger.h" #include "datatuple.h" #include "logiterators.h" -typedef std::set rbtree_t; -typedef rbtree_t* rbtree_ptr_t; - #include "merger.h" template @@ -64,8 +61,6 @@ public: void openTable(int xid, recordid rid); void flushTable(); - static void tearDownTree(rbtree_ptr_t t); - DataPage* insertTuple(int xid, datatuple *tuple,diskTreeComponent *ltree); datatuple * findTuple(int xid, const datatuple::key_t key, size_t keySize, diskTreeComponent *ltree); @@ -86,10 +81,10 @@ public: inline void set_tree_c1_mergeable(diskTreeComponent *t){tree_c1_mergeable=t; bump_epoch(); } inline void set_tree_c2(diskTreeComponent *t){tree_c2=t; bump_epoch(); } - inline rbtree_ptr_t get_tree_c0(){return tree_c0;} - inline rbtree_ptr_t get_tree_c0_mergeable(){return tree_c0_mergeable;} - void set_tree_c0(rbtree_ptr_t newtree){tree_c0 = newtree; bump_epoch(); } - void set_tree_c0_mergeable(rbtree_ptr_t newtree){tree_c0_mergeable = newtree; bump_epoch(); } + inline memTreeComponent::rbtree_ptr_t get_tree_c0(){return tree_c0;} + inline memTreeComponent::rbtree_ptr_t get_tree_c0_mergeable(){return tree_c0_mergeable;} + void set_tree_c0(memTreeComponent::rbtree_ptr_t newtree){tree_c0 = newtree; bump_epoch(); } + void set_tree_c0_mergeable(memTreeComponent::rbtree_ptr_t newtree){tree_c0_mergeable = newtree; bump_epoch(); } void update_persistent_header(int xid); @@ -132,8 +127,8 @@ private: diskTreeComponent *tree_c2; //big tree diskTreeComponent *tree_c1; //small tree diskTreeComponent *tree_c1_mergeable; //small tree: ready to be merged with c2 - rbtree_ptr_t tree_c0; // in-mem red black tree - rbtree_ptr_t tree_c0_mergeable; // in-mem red black tree: ready to be merged with c1. + memTreeComponent::rbtree_ptr_t tree_c0; // in-mem red black tree + memTreeComponent::rbtree_ptr_t tree_c0_mergeable; // in-mem red black tree: ready to be merged with c1. int tsize; //number of tuples int64_t tree_bytes; //number of bytes @@ -334,8 +329,10 @@ private: static const int C2 = 2; logtable * ltable; uint64_t epoch; - typedef mergeManyIterator, memTreeIterator, TUPLE> inner_merge_it_t; -// typedef mergeManyIterator, diskTreeIterator, TUPLE> merge_it_t; + typedef mergeManyIterator< + changingMemTreeIterator::rbtree_t, TUPLE>, + typename memTreeComponent::memTreeIterator, TUPLE> inner_merge_it_t; +// typedef mergeManyIterator, diskTreeIterator, TUPLE> merge_it_t; typedef mergeManyIterator, TUPLE> merge_it_t; merge_it_t* merge_it_; @@ -353,25 +350,25 @@ private: void validate() { - changingMemTreeIterator * c0_it; - memTreeIterator * c0_mergeable_it[1]; + changingMemTreeIterator::rbtree_t, TUPLE> * c0_it; + typename memTreeComponent::memTreeIterator *c0_mergeable_it[1]; diskTreeIterator * disk_it[3]; epoch = ltable->get_epoch(); if(last_returned) { - c0_it = new changingMemTreeIterator(ltable->get_tree_c0(), ltable->getMergeData()->rbtree_mut, last_returned); - c0_mergeable_it[0] = new memTreeIterator (ltable->get_tree_c0_mergeable(), last_returned); + c0_it = new changingMemTreeIterator::rbtree_t, TUPLE>(ltable->get_tree_c0(), ltable->getMergeData()->rbtree_mut, last_returned); + c0_mergeable_it[0] = new typename memTreeComponent::memTreeIterator (ltable->get_tree_c0_mergeable(), last_returned); disk_it[0] = new diskTreeIterator (ltable->get_tree_c1(), *last_returned); disk_it[1] = new diskTreeIterator (ltable->get_tree_c1_mergeable(), *last_returned); disk_it[2] = new diskTreeIterator (ltable->get_tree_c2(), *last_returned); } else if(key) { - c0_it = new changingMemTreeIterator(ltable->get_tree_c0(), ltable->getMergeData()->rbtree_mut, key); - c0_mergeable_it[0] = new memTreeIterator (ltable->get_tree_c0_mergeable(), key); + c0_it = new changingMemTreeIterator::rbtree_t, TUPLE>(ltable->get_tree_c0(), ltable->getMergeData()->rbtree_mut, key); + c0_mergeable_it[0] = new typename memTreeComponent::memTreeIterator(ltable->get_tree_c0_mergeable(), key); disk_it[0] = new diskTreeIterator (ltable->get_tree_c1(), *key); disk_it[1] = new diskTreeIterator (ltable->get_tree_c1_mergeable(), *key); disk_it[2] = new diskTreeIterator (ltable->get_tree_c2(), *key); } else { - c0_it = new changingMemTreeIterator(ltable->get_tree_c0(), ltable->getMergeData()->rbtree_mut ); - c0_mergeable_it[0] = new memTreeIterator (ltable->get_tree_c0_mergeable() ); + c0_it = new changingMemTreeIterator::rbtree_t, TUPLE>(ltable->get_tree_c0(), ltable->getMergeData()->rbtree_mut ); + c0_mergeable_it[0] = new typename memTreeComponent::memTreeIterator (ltable->get_tree_c0_mergeable() ); disk_it[0] = new diskTreeIterator (ltable->get_tree_c1() ); disk_it[1] = new diskTreeIterator (ltable->get_tree_c1_mergeable() ); disk_it[2] = new diskTreeIterator (ltable->get_tree_c2() ); diff --git a/memTreeComponent.cpp b/memTreeComponent.cpp new file mode 100644 index 0000000..b59666d --- /dev/null +++ b/memTreeComponent.cpp @@ -0,0 +1,24 @@ +#include "memTreeComponent.h" +template +void memTreeComponent::tearDownTree(rbtree_ptr_t tree) { + TUPLE * t = 0; + typename rbtree_t::iterator old; + for(typename rbtree_t::iterator delitr = tree->begin(); + delitr != tree->end(); + delitr++) { + if(t) { + tree->erase(old); + TUPLE::freetuple(t); + t = 0; + } + t = *delitr; + old = delitr; + } + if(t) { + tree->erase(old); + TUPLE::freetuple(t); + } + delete tree; +} + +template class memTreeComponent; diff --git a/memTreeComponent.h b/memTreeComponent.h new file mode 100644 index 0000000..490c849 --- /dev/null +++ b/memTreeComponent.h @@ -0,0 +1,79 @@ +#ifndef _MEMTREECOMPONENT_H_ +#define _MEMTREECOMPONENT_H_ +#include +#include "datatuple.h" +template +class memTreeComponent { +public: + typedef std::set rbtree_t; + typedef rbtree_t* rbtree_ptr_t; + + static void tearDownTree(rbtree_ptr_t t); + +////////////////////////////////////////////////////////////// +// memTreeIterator +///////////////////////////////////////////////////////////// + + class memTreeIterator + { + private: + typedef typename rbtree_t::const_iterator MTITER; + + public: + memTreeIterator( rbtree_t *s ) + : first_(true), + done_(s == NULL) { + init_iterators(s, NULL, NULL); + } + + memTreeIterator( rbtree_t *s, TUPLE *&key ) + : first_(true), done_(s == NULL) { + init_iterators(s, key, NULL); + } + + ~memTreeIterator() { + delete it_; + delete itend_; + } + + TUPLE* getnext() { + if(done_) { return NULL; } + if(first_) { first_ = 0;} else { (*it_)++; } + if(*it_==*itend_) { done_= true; return NULL; } + + return (*(*it_))->create_copy(); + } + + + private: + void init_iterators(rbtree_t * s, TUPLE * key1, TUPLE * key2) { + if(s) { + it_ = key1 ? new MTITER(s->find(key1)) : new MTITER(s->begin()); + itend_ = key2 ? new MTITER(s->find(key2)) : new MTITER(s->end()); + if(*it_ == *itend_) { done_ = true; } + if(key1) { + if(done_) { + // DEBUG("memtree opened at eot\n"); + } else { + // DEBUG("memtree opened key = %s\n", (**it_)->key()); + } + } + } else { + it_ = NULL; + itend_ = NULL; + } + } + explicit memTreeIterator() { abort(); } + void operator=(memTreeIterator & t) { abort(); } + int operator-(memTreeIterator & t) { abort(); } + private: + bool first_; + bool done_; + MTITER *it_; + MTITER *itend_; + }; + +///////////////////////////////////////////////////////////////// +}; + +#endif //_MEMTREECOMPONENT_H_ diff --git a/merger.cpp b/merger.cpp index ee2c35e..3e1f9db 100644 --- a/merger.cpp +++ b/merger.cpp @@ -187,7 +187,7 @@ void merge_scheduler::startlogtable(int index, int64_t MAX_C0_SIZE) *block1_size = FUDGE * ((int)R) * (*(mdata->input_size)); //initialize rb-tree - ltable->set_tree_c0(new rbtree_t); + ltable->set_tree_c0(new memTreeComponent::rbtree_t); //disk merger args @@ -335,8 +335,8 @@ void* memMergeThread(void*arg) //create the iterators diskTreeIterator *itrA = new diskTreeIterator(ltable->get_tree_c1()->get_root_rec()); // XXX don't want get_root_rec() to be here. - memTreeIterator *itrB = - new memTreeIterator(ltable->get_tree_c0_mergeable()); + memTreeComponent::memTreeIterator *itrB = + new memTreeComponent::memTreeIterator(ltable->get_tree_c0_mergeable()); //create a new tree @@ -396,7 +396,7 @@ void* memMergeThread(void*arg) delete ltable->get_tree_c1(); // 11.5: delete old c0_mergeable - logtable::tearDownTree(ltable->get_tree_c0_mergeable()); + memTreeComponent::tearDownTree(ltable->get_tree_c0_mergeable()); // 11: c0_mergeable = NULL ltable->set_tree_c0_mergeable(NULL); diff --git a/test/check_rbtree.cpp b/test/check_rbtree.cpp index 40a52eb..43be575 100644 --- a/test/check_rbtree.cpp +++ b/test/check_rbtree.cpp @@ -39,7 +39,7 @@ void insertProbeIter(size_t NUM_ENTRIES) if(data_arr.size() > NUM_ENTRIES) data_arr.erase(data_arr.begin()+NUM_ENTRIES, data_arr.end()); - rbtree_t rbtree; + memTreeComponent::rbtree_t rbtree; int64_t datasize = 0; std::vector dsp; @@ -70,7 +70,7 @@ void insertProbeIter(size_t NUM_ENTRIES) //step 1: look in tree_c0 - rbtree_t::iterator rbitr = rbtree.find(search_tuple); + memTreeComponent::rbtree_t::iterator rbitr = rbtree.find(search_tuple); if(rbitr != rbtree.end()) { datatuple *tuple = *rbitr;