Column-oriented GPU-accelerated Database Management System
CoGaDB
/home/sebastian/gpudbms/trunk/cogadb/include/compression/tmp/db2/core/column.hpp
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <core/column_base_typed.hpp>
00004 #include <iostream>
00005 #include <fstream>
00006 
00007 namespace CoGaDB {
00008 
00009 template<typename T>
00010 class Column : public ColumnBaseTyped<T> {
00011         public:
00012         /***************** constructors and destructor *****************/
00013         Column(const std::string& name, AttributeType db_type);
00014         //Column(const Column& column);
00015         virtual ~Column();
00016 
00017         virtual bool insert(const boost::any& new_value);
00018         bool insert(const T& new_value);
00019         template <typename InputIterator>
00020         bool insert(InputIterator first, InputIterator last);
00021 
00022         virtual bool update(TID tid, const boost::any& new_value);
00023         virtual bool update(PositionListPtr tid, const boost::any& new_value);  
00024         
00025         virtual bool remove(TID tid);
00026         //assumes tid list is sorted ascending
00027         virtual bool remove(PositionListPtr tid);
00028         virtual bool clearContent();
00029 
00030         virtual const boost::any get(TID tid);
00031         //virtual const boost::any* const getRawData();
00032         virtual void print() const throw();
00033         virtual size_t size() const throw();
00034         virtual unsigned int getSizeinBytes() const throw();
00035 
00036         virtual const ColumnPtr copy() const;
00037 
00038         virtual bool store(const std::string& path);
00039         virtual bool load(const std::string& path);
00040         virtual bool isMaterialized() const  throw();
00041         virtual bool isCompressed() const  throw();     
00042         
00043         virtual T& operator[](const int index);
00044 
00045         std::vector<T>& getContent();
00046 
00047         private:
00048 
00049                 struct Type_TID_Comparator {
00050                         inline bool operator() (std::pair<T,TID> i, std::pair<T,TID> j) { return (i.first<j.first);}
00051                 } type_tid_comparator;
00052 
00053         
00055         std::vector<T> values_;
00056 };
00057 
00058 
00059 
00060 /***************** Start of Implementation Section ******************/
00061 
00062         
00063         template<class T>
00064         Column<T>::Column(const std::string& name, AttributeType db_type) : ColumnBaseTyped<T>(name,db_type), type_tid_comparator(), values_(){
00065 
00066         }
00067 
00068 //      template<class T>
00069 //      Column<T>::Column(const Column& column) : this->db_type_(column->db_type_), this->values_(column->values_){
00070 //              
00071 //      }
00072 
00073         template<class T>
00074         Column<T>::~Column(){
00075 
00076         }
00077 
00078         template<class T>
00079         std::vector<T>& Column<T>::getContent(){
00080                 return values_;
00081         }
00082 
00083 
00084         template<class T>
00085         bool Column<T>::insert(const boost::any& new_value){
00086                 if(new_value.empty()) return false;
00087                 if(typeid(T)==new_value.type()){
00088                          T value = boost::any_cast<T>(new_value);
00089                          values_.push_back(value);
00090                          return true;
00091                 }
00092                 return false;
00093         }
00094 
00095         template<class T>
00096         bool Column<T>::insert(const T& new_value){
00097                 values_.push_back(new_value);
00098                 return true;
00099         }
00100 
00101 
00102         template <typename T> 
00103         template <typename InputIterator>
00104         bool Column<T>::insert(InputIterator first, InputIterator last){
00105                 this->values_.insert(this->values_.end(),first,last);
00106                 return true;
00107         }
00108 
00109         template<class T>
00110         bool Column<T>::update(TID tid, const boost::any& new_value){
00111                 if(new_value.empty()) return false;
00112                 if(typeid(T)==new_value.type()){
00113                          T value = boost::any_cast<T>(new_value);
00114                          values_[tid]=value;
00115                          return true;
00116                 }else{
00117                         std::cout << "Fatal Error!!! Typemismatch for column " << this->name_ << std::endl; 
00118                 }
00119                 return false;
00120         }
00121 
00122         template<class T>
00123         bool Column<T>::update(PositionListPtr tids, const boost::any& new_value){
00124                 if(!tids)
00125                         return false;
00126         if(new_value.empty()) return false;
00127                 if(typeid(T)==new_value.type()){
00128                          T value = boost::any_cast<T>(new_value);
00129                          for(unsigned int i=0;i<tids->size();i++){
00130                                 TID tid=(*tids)[i];
00131                                 values_[tid]=value;
00132                          }
00133                          return true;
00134                 }else{
00135                         std::cout << "Fatal Error!!! Typemismatch for column " << this->name_ << std::endl; 
00136                 }
00137                 return false;           
00138         }
00139         
00140 
00141 
00142 
00143         template<class T>
00144         bool Column<T>::remove(TID tid){
00145                 values_.erase(values_.begin()+tid);
00146                 return true;
00147         }
00148         
00149         template<class T>
00150         bool Column<T>::remove(PositionListPtr tids){
00151                 if(!tids)
00152                         return false;
00153                 //test whether tid list has at least one element, if not, return with error
00154                 if(tids->empty())
00155                         return false;   
00156 
00157                 //assert();
00158 
00159                 typename PositionList::reverse_iterator rit;
00160 
00161                 for (rit = tids->rbegin(); rit!=tids->rend(); ++rit)
00162                         values_.erase(values_.begin()+(*rit));
00163 
00164                 /*
00165                 //delete tuples in reverse order, otherwise the first deletion would invalidate all other tids
00166                 unsigned int i=tids->size()-1;
00167                 while(true)     
00168                         TID = (*tids)[i];
00169                         values_.erase(values_.begin()+tid);             
00170                         if(i==0) break;
00171                 }*/
00172                 
00173                 
00174                 return true;                    
00175         }
00176 
00177         template<class T>
00178         bool Column<T>::clearContent(){
00179                 values_.clear();
00180                 return true;
00181         }
00182 
00183         template<class T>
00184         const boost::any Column<T>::get(TID tid){
00185                 if(tid<values_.size())
00186                         return boost::any(values_[tid]);
00187                 else{
00188                         std::cout << "fatal Error!!! Invalid TID!!! Attribute: " << this->name_ << " TID: " << tid  << std::endl;
00189                 }
00190                 return boost::any();
00191         }
00192 
00193         template<class T>
00194         void Column<T>::print() const throw(){
00195                 std::cout << "| " << this->name_ << " |" << std::endl;
00196                 std::cout << "________________________" << std::endl;
00197                 for(unsigned int i=0;i<values_.size();i++){
00198                         std::cout << "| " << values_[i] << " |" << std::endl;
00199                 }
00200         }
00201         template<class T>
00202         size_t Column<T>::size() const throw(){
00203                 return values_.size();
00204         }
00205         template<class T>
00206         const ColumnPtr Column<T>::copy() const{
00207                 return ColumnPtr(new Column<T>(*this));
00208         }
00209         /***************** relational operations on Columns which return lookup tables *****************/
00210 //      template<class T>
00211 //      const std::vector<TID> Column<T>::sort(const ComputeDevice comp_dev) const {
00212 
00213 //              return std::vector<TID>();
00214 //      }
00215 
00216 //      template<class T> 
00217 //      const std::vector<TID> Column<T>::selection(const boost::any& value_for_comparison, const ValueComparator comp, const ComputeDevice comp_dev) const {
00218 
00219 //              return std::vector<TID>();
00220 //      }
00221 //      //join algorithms
00222 //      template<class T>
00223 //      const std::vector<TID_Pair> Column<T>::sort_merge_join(ColumnPtr join_Column, const ComputeDevice comp_dev) const{
00224 
00225 //              return std::vector<TID_Pair>();
00226 //      }
00227 //      template<class T>
00228 //      const std::vector<TID_Pair> Column<T>::nested_loop_join(ColumnPtr join_Column, const ComputeDevice comp_dev) const{
00229 
00230 //              return std::vector<TID_Pair>();
00231 //      }
00232         template<class T>
00233         bool Column<T>::store(const std::string& path_){
00234                 //string path("data/");
00235                 std::string path(path_);
00236                 path += "/";
00237                 path += this->name_;
00238                 //std::cout << "Writing Column " << this->getName() << " to File " << path << std::endl;
00239                 std::ofstream outfile (path.c_str(),std::ios_base::binary | std::ios_base::out);
00240                 boost::archive::binary_oarchive oa(outfile);
00241 
00242                 oa << values_;
00243 
00244                 outfile.flush();
00245                 outfile.close();
00246                 return true;
00247         }
00248         template<class T>
00249         bool Column<T>::load(const std::string& path_){
00250                 std::string path(path_);
00251                 //std::cout << "Loading column '" << this->name_ << "' from path '" << path << "'..." << std::endl;
00252                 //string path("data/");
00253                 path += "/";
00254                 path += this->name_;
00255                 
00256                 //std::cout << "Opening File '" << path << "'..." << std::endl;
00257                 std::ifstream infile (path.c_str(),std::ios_base::binary | std::ios_base::in);
00258                 boost::archive::binary_iarchive ia(infile);
00259                 ia >> values_;
00260                 infile.close();
00261 
00262 
00263                 return true;
00264         }
00265         template<class T>
00266         bool Column<T>::isMaterialized() const  throw(){
00267                 return true;
00268         }
00269         
00270         template<class T>
00271         bool Column<T>::isCompressed() const  throw(){
00272                 return false;
00273         }
00274 
00275         template<class T>
00276         T& Column<T>::operator[](const int index){
00277                 return values_[index];
00278         }
00279 
00280         template<class T>
00281         unsigned int Column<T>::getSizeinBytes() const throw(){
00282                 return sizeof(values_) + values_.capacity() * sizeof(T);
00283         }
00284 
00285         //total template specialization
00286         template<>
00287         inline unsigned int Column<std::string>::getSizeinBytes() const throw(){
00288                 unsigned int size_in_bytes = sizeof(values_);
00289                 for(unsigned int i = 0; i < values_.size(); ++i){
00290                         size_in_bytes += values_[i].capacity();
00291                 }
00292                 //return values_.size()*sizeof(T);
00293                 return size_in_bytes;
00294         }
00295 
00296 /***************** End of Implementation Section ******************/
00297 
00298 
00299 
00300 
00301 }; //end namespace CogaDB
00302 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines