Column-oriented GPU-accelerated Database Management System
CoGaDB
/home/sebastian/gpudbms/trunk/cogadb/include/core/lookup_array.hpp
Go to the documentation of this file.
00001 
00002 #pragma once
00003 
00004 #include <core/column_base_typed.hpp>
00005 #include <core/column.hpp>
00006 #include <lookup_table/lookup_column.hpp>
00007 #include <core/base_table.hpp>
00008 
00009 namespace CoGaDB{
00010 
00023 template<class T>
00024 class LookupArray : public ColumnBaseTyped<T>{
00025         public:
00026         /***************** constructors and destructor *****************/
00027         LookupArray(const std::string& name, AttributeType db_type, ColumnPtr column, PositionListPtr tids);
00028         virtual ~LookupArray();
00029 
00030         virtual bool insert(const boost::any& new_Value);
00031         virtual bool insert(const T& new_Value);
00032         virtual bool update(TID tid, const boost::any& new_value);
00033         virtual bool update(PositionListPtr tid, const boost::any& new_value);  
00034         
00035         virtual bool remove(TID tid);
00036         //assumes tid list is sorted ascending
00037         virtual bool remove(PositionListPtr tid);
00038         virtual bool clearContent();
00039         
00040         virtual const boost::any get(TID tid);
00041         //virtual const boost::any* const getRawData()=0;
00042         virtual void print() const throw();
00043         virtual size_t size() const throw();
00044         virtual unsigned int getSizeinBytes() const throw();
00045         PositionListPtr getPositionList();
00046 
00047         virtual const ColumnPtr copy() const;
00048         virtual const ColumnPtr gather(PositionListPtr tid_list);
00049 
00050         virtual bool store(const std::string& path);
00051         virtual bool load(const std::string& path);
00052         virtual bool isMaterialized() const  throw();
00053         virtual bool isCompressed() const  throw();     
00054         virtual const ColumnPtr materialize() throw();
00055 
00056         
00058         virtual T& operator[](const int index);
00059         //inline T& operator[](const int index) __attribute__((always_inline));
00060 
00061         private:
00062         shared_pointer_namespace::shared_ptr<ColumnBaseTyped<T> > column_;
00063         PositionListPtr tids_;
00064 };
00065 
00066 inline ColumnPtr createLookupArrayForColumn(ColumnPtr col, PositionListPtr tids_)
00067 {
00068     assert(col!=NULL);
00069     
00070     AttributeType type=col->getType();   
00071     std::string column_name=col->getName();
00072     
00073     if(type==INT){
00074             return ColumnPtr(new LookupArray<int>(column_name,INT,col,tids_));
00075     }else if(type==FLOAT){
00076             return ColumnPtr(new LookupArray<float>(column_name,FLOAT,col,tids_));
00077     }else if(type==VARCHAR){
00078             return ColumnPtr(new LookupArray<std::string>(column_name,VARCHAR,col,tids_));
00079     }else if(type==BOOLEAN){
00080             std::cout << "Fatal Error! invalid AttributeType: " << type << " for Column: " << column_name 
00081                       << " Note: bool is currently not supported, will be added again in the future!"<< std::endl;
00082             return ColumnPtr();
00083     }else{
00084             std::cout << "Fatal Error! invalid AttributeType: " << type << " for Column: " << column_name << std::endl;
00085             return ColumnPtr();
00086     }
00087 }
00088 
00089 inline PositionListPtr getPositonListfromLookupArray(ColumnPtr col){
00090     if(!col) return PositionListPtr(); 
00091     //is LookupArray?
00092     if(col->isMaterialized() || col->isCompressed()){
00093         //input is not LookupArray, so no Positionlist can be retrieved
00094         return PositionListPtr(); 
00095     }
00096     
00097     AttributeType type=col->getType();   
00098 
00099     
00100     if(type==INT){
00101         shared_pointer_namespace::shared_ptr<LookupArray<int> > lookup_array = shared_pointer_namespace::dynamic_pointer_cast<LookupArray<int> > (col);
00102         return lookup_array->getPositionList();           
00103     }else if(type==FLOAT){
00104         shared_pointer_namespace::shared_ptr<LookupArray<float> > lookup_array = shared_pointer_namespace::dynamic_pointer_cast<LookupArray<float> > (col);
00105         return lookup_array->getPositionList();              
00106     }else if(type==VARCHAR){
00107         shared_pointer_namespace::shared_ptr<LookupArray<std::string> > lookup_array = shared_pointer_namespace::dynamic_pointer_cast<LookupArray<std::string> > (col);
00108         return lookup_array->getPositionList();               
00109     }else if(type==BOOLEAN){
00110             std::cout << "Fatal Error! invalid AttributeType: " << type << " for Column: " << col->getName() 
00111                       << " Note: bool is currently not supported, will be added again in the future!"<< std::endl;
00112             return PositionListPtr();
00113     }else{
00114             std::cout << "Fatal Error! invalid AttributeType: " << type << " for Column: " <<  col->getName() << std::endl;
00115             return PositionListPtr();
00116     }
00117     
00118 
00119 }
00120 
00121 
00122         //typedef shared_pointer_namespace::shared_ptr<LookupArray> LookupArrayPtr;
00123 
00124 /***************** Start of Implementation Section ******************/
00125 
00126         
00127         template<class T>
00128         LookupArray<T>::LookupArray(const std::string& name, AttributeType db_type, ColumnPtr column, PositionListPtr tids) 
00129                                                 : ColumnBaseTyped<T>(name, db_type),
00130                                                   column_( shared_pointer_namespace::static_pointer_cast<ColumnBaseTyped<T> > (column) ),
00131                                                   tids_(tids) {
00132         
00133                 assert(column_!=NULL);
00134                 assert(tids_!=NULL);
00135                 assert(db_type==column->getType());
00136 
00137         }
00138 
00139         template<class T>
00140         LookupArray<T>::~LookupArray(){
00141 
00142         }
00143 
00144         template<class T>
00145         bool LookupArray<T>::insert(const boost::any& ){
00146                 return false;
00147         }
00148         template<class T>
00149         bool LookupArray<T>::insert(const T& new_Value){
00150                 return false;
00151         } 
00152         template<class T>
00153         bool LookupArray<T>::update(TID , const boost::any& ){
00154                 return  false;
00155         }
00156 
00157         template<class T>
00158         bool LookupArray<T>::update(PositionListPtr, const boost::any&){
00159                 return  false;                  
00160         }
00161         
00162         template<class T>
00163         bool LookupArray<T>::remove(TID ){
00164 
00165                 return false;
00166         }
00167         
00168         //assumes tid list is sorted ascending
00169         template<class T>
00170         bool LookupArray<T>::remove(PositionListPtr){
00171                 return false;
00172         }
00173         
00174         template<class T>
00175         bool LookupArray<T>::clearContent(){
00176             return false;
00177         }
00178         
00179         template<class T>
00180         const boost::any LookupArray<T>::get(TID tid){
00181                 return boost::any((*this)[tid]);
00182         }
00183 
00184         template<class T>
00185         void LookupArray<T>::print() const throw(){
00186                 
00187                 const shared_pointer_namespace::shared_ptr<ColumnBaseTyped<T> > column = column_;
00188                 const PositionListPtr tids = tids_;
00189 
00190                 std::cout << "Lookup Array for Column " << column_->getName() << " ";
00191                 if(column_->isMaterialized()){
00192                         std::cout << "which is a materialized Column" << std::endl;
00193                 }else{
00194                         std::cout << "which is a LookupArray of a Lookup column" << std::endl;
00195                 }
00196                 std::cout << "| values | Translatetion TIDS | Index in Lookup Table |" << std::endl;
00197                 for(unsigned int i=0;i<tids->size();i++){
00198                         std::cout << "| " << (*column_)[(*tids_)[i]] << " | " << (*tids_)[i] << " | " << i << " |" << std::endl;
00199                 }
00200 
00201 
00202         }
00203         template<class T>
00204         size_t LookupArray<T>::size() const throw(){
00205 
00206                 return tids_->size();
00207         }
00208 
00209         template<class T>
00210         const ColumnPtr LookupArray<T>::materialize() throw(){
00211 
00212             Column<T>* result = new Column<T>(this->name_,this->db_type_);
00213             std::vector<T>& data = result->getContent();
00214             data.resize(this->size());
00215             for(unsigned int i=0;i<this->size();i++){
00216                 data[i]=(*this)[i];
00217             }
00218             return ColumnPtr(result); 
00219         }
00220 
00221         template<class T>
00222         const ColumnPtr LookupArray<T>::copy() const{
00223                 PositionListPtr new_tids (new PositionList(*tids_));
00224                 return ColumnPtr(new LookupArray<T>(this->name_,this->db_type_,this->column_,new_tids));
00225         }
00226         
00227         template<class T>
00228         const ColumnPtr LookupArray<T>::gather(PositionListPtr tid_list){
00229             Column<T>* result = new Column<T>(this->name_,this->db_type_);
00230             std::vector<T>& data = result->getContent();
00231             data.resize(tid_list->size());
00232             for(unsigned int i=0;i<tid_list->size();i++){              
00233                 data[i]=(*this)[(*tid_list)[i]];
00234             }
00235             return ColumnPtr(result); 
00236         }    
00237         
00238         /***************** relational operations on LookupArrays which return lookup tables *****************/
00239 //      template<class T>
00240 //      const std::vector<TID> LookupArray<T>::sort(const ComputeDevice comp_dev) const {
00241 
00242 //              return std::vector<TID>();
00243 //      }
00244 
00245 //      template<class T> 
00246 //      const std::vector<TID> LookupArray<T>::selection(const boost::any& value_for_comparison, const ValueComparator comp, const ComputeDevice comp_dev) const {
00247 //              return std::vector<TID>();
00248 //      }
00249 //      //join algorithms
00250 //      template<class T>
00251 //      const std::vector<TID_Pair> LookupArray<T>::sort_merge_join(ColumnPtr join_LookupArray, const ComputeDevice comp_dev) const{
00252 //              return std::vector<TID_Pair>();
00253 //      }
00254 //      template<class T>
00255 //      const std::vector<TID_Pair> LookupArray<T>::nested_loop_join(ColumnPtr join_LookupArray, const ComputeDevice comp_dev) const{
00256 //              return false;
00257 //      }
00258         template<class T>
00259         bool LookupArray<T>::store(const std::string&){
00260                 return false;
00261         }
00262         template<class T>
00263         bool LookupArray<T>::load(const std::string&){
00264                 return false;
00265         }
00266         template<class T>
00267         bool LookupArray<T>::isMaterialized() const  throw(){
00268                 return false;
00269         }
00270         template<class T>
00271         bool LookupArray<T>::isCompressed() const  throw(){
00272                 return false;
00273         }
00274         template<class T>
00275         T& LookupArray<T>::operator[](const int index){
00276                 return (*column_)[(*tids_)[index]];
00277         }
00278 
00279         template<class T>
00280         unsigned int LookupArray<T>::getSizeinBytes() const throw(){
00281                 return tids_->capacity()*sizeof(typename PositionList::value_type);
00282         }
00283 
00284         template<class T>
00285         PositionListPtr LookupArray<T>::getPositionList(){
00286             return this->tids_;
00287         }
00288         
00289 /***************** End of Implementation Section ******************/
00290 
00291 
00292 }; //end namespace CogaDB
00293 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines