Column-oriented GPU-accelerated Database Management System
CoGaDB
/home/sebastian/gpudbms/trunk/cogadb/include/core/row_value_column.hpp
Go to the documentation of this file.
00001 #pragma once
00002 #include <core/column_base_typed.hpp>
00003 #include <core/column.hpp>
00004 #include <core/row_table.hpp>
00005 
00006 using namespace std;
00007 namespace CoGaDB
00008 {       
00011 template <typename T>
00012 class RowValueColumn : public ColumnBaseTyped<T>
00013 {
00014 private:
00016         RowTablePtr _row_table_ptr;
00018         unsigned int _column_index;
00020         AttributeType _type;
00022         char* getValue(const int index, unsigned short& stringLength) const;
00023 public:    
00024         RowValueColumn(RowTablePtr, string&, const AttributeType&);
00025         virtual ~RowValueColumn();
00026         
00027         bool isCompressed() const throw();      
00028         bool update(PositionListPtr tid, const boost::any& new_value);
00029         bool remove(PositionListPtr tid);
00030 
00032         bool insert(const boost::any& new_Value);
00034         bool update(TID tid, const boost::any& new_Value);
00036         bool remove(TID tid);
00037 
00038         virtual const boost::any get(TID tid);  
00039         virtual void print() const throw();
00041         size_t size() const throw();
00043         unsigned int getSizeinBytes() const throw();
00044         
00046         const ColumnPtr copy() const;
00047     
00049         bool store(const string& path);
00051         bool load(const string& path);
00053         bool isMaterialized() const  throw();
00055         const type_info& type() const throw();
00056     
00058         T& operator[](const int index); 
00059         
00060         ColumnPtr toColumn();
00061 };
00062 
00063 template <typename T>
00064 RowValueColumn<T>::RowValueColumn(RowTablePtr row_table_ptr, string& name, const AttributeType& type) : ColumnBaseTyped<T>(name, type), _row_table_ptr(), _column_index(0), _type()
00065 {       
00066         _row_table_ptr = row_table_ptr; 
00067         TableSchema schema = _row_table_ptr->getSchema();       
00068         TableSchema::const_iterator it;          /* Schema iterator */                  
00069         
00070         /* Iterate throw all columns of the schema and get the relative pages from BufferManager */
00071         for(it=schema.begin();it!=schema.end();it++)
00072         {               
00073                 _column_index++;                
00074                 if(it->second == name)                  
00075                 {
00076                         _type = it->first;
00077                         break;
00078                 }               
00079         }
00080 }
00081 
00082 template <typename T>
00083 RowValueColumn<T>::~RowValueColumn()
00084 {       
00085 }
00086 
00087 template <typename T>
00088 bool RowValueColumn<T>::insert(const boost::any& new_Value)
00089 {
00090         if(new_Value.empty())
00091                 return false;
00092                 
00093         return false;   
00094 }
00095 
00096 template <typename T>
00097 bool RowValueColumn<T>::update(TID tid, const boost::any& new_Value)
00098 {
00099         cout << tid << new_Value.empty() << endl;
00100         return false;
00101 }
00102 
00103 template <typename T>
00104 bool RowValueColumn<T>::remove(TID tid)
00105 {
00106         cout << tid;
00107         return false;
00108 }
00109 
00110 template <typename T>
00111 const boost::any RowValueColumn<T>::get(TID tid)
00112 {       
00113         unsigned short* stringLength = new unsigned short(0);   
00114         
00115         char* res = getValue(tid, *stringLength);
00116         string s;
00117         int i;
00118         float f;
00119         bool b;
00120                 
00121         switch(_type)
00122         {
00123            case INT: 
00124                     i = *reinterpret_cast<int*>(res);
00125                return boost::any(i);
00126            case FLOAT: 
00127                f = *reinterpret_cast<float*>(res);
00128                return boost::any(f);
00129            case BOOLEAN: 
00130                b = *reinterpret_cast<bool*>(res);
00131                return boost::any(b);
00132            case VARCHAR:
00133               for(unsigned short index = 0; index < *stringLength; index++)
00134                    s.push_back(res[index]);
00135            
00136                    return boost::any(s);                
00137                 }       
00138         
00139         return boost::any();
00140 }
00141 
00142 template <typename T>   
00143 void RowValueColumn<T>::print() const throw()
00144 {       
00145         unsigned short* stringLength = new unsigned short(0);   
00146         for(unsigned int index = 0; index < _row_table_ptr->getNumberofRows(); index++)
00147         {
00148                 char* res = getValue(index, *stringLength);             
00149                 string s;
00150         
00151             switch(_type)
00152                 {
00153                    case INT: cout << *reinterpret_cast<int*>(res) << endl; break;
00154                    case FLOAT: cout << *reinterpret_cast<float*>(res) << endl; break;
00155                    case BOOLEAN: cout << *reinterpret_cast<bool*>(res) << endl; break;
00156                    case VARCHAR:
00157                    for(unsigned short index = 0; index < *stringLength; index++)
00158                            s.push_back(res[index]);
00159                    
00160                    cout << s << endl;
00161                    break;
00162                 
00163                 }
00164         }
00165 }
00166 
00167 template <typename T>
00168 size_t RowValueColumn<T>::size() const throw()
00169 {
00170         /* Not implemented */
00171         return 0;
00172 }
00173 
00174 template <typename T>
00175 unsigned int RowValueColumn<T>::getSizeinBytes() const throw()
00176 {
00177         /* Not implemented */
00178         return 0;
00179 }
00180 
00181 template <typename T>
00182 const ColumnPtr RowValueColumn<T>::copy() const
00183 {
00184         /* Not implemented */
00185         return ColumnPtr();
00186 }
00187 
00188 template <typename T>
00189 bool RowValueColumn<T>::store(const string& path)
00190 {
00191         /* Not implemented */
00192         cout << path << endl;
00193         return false;
00194 }
00195 
00196 template <typename T>
00197 bool RowValueColumn<T>::load(const string& path)
00198 {
00199         /* Not implemented */
00200         cout << path << endl;
00201         return false;
00202 }
00203 
00204 template <typename T>
00205 bool RowValueColumn<T>::isMaterialized() const  throw()
00206 {
00207         /* Always false */
00208         return false;
00209 }
00210         
00211 
00212 template <typename T>
00213 const std::type_info& RowValueColumn<T>::type() const throw()
00214 {
00215         return typeid(T);       
00216 }
00217 
00218 template <>
00219 inline int& RowValueColumn<int>::operator[](const int index)
00220 {               
00221         unsigned short* stringLength = new unsigned short(0);   
00222         
00223         char* res = getValue(index, *stringLength);
00224         
00225         return *reinterpret_cast<int*>(res);    
00226 }
00227 
00228 template<>
00229 inline string& RowValueColumn<string>::operator[](const int index)
00230 {               
00231         unsigned short* stringLength = new unsigned short(0);   
00232         
00233         char* res = getValue(index, *stringLength);     
00234         string* s = new string();
00235                 
00236         for(unsigned short index = 0; index < *stringLength; index++)
00237              s->push_back(res[index]);
00238                 
00239         return *s;              
00240 }
00241 
00242 template<>
00243 inline bool& RowValueColumn<bool>::operator[](const int index)
00244 {       
00245         
00246         unsigned short* stringLength = new unsigned short(0);   
00247         
00248         char* res = getValue(index, *stringLength);     
00249         
00250         return *reinterpret_cast<bool*>(res);           
00251 }
00252 
00253 template<>
00254 inline float& RowValueColumn<float>::operator[](const int index)
00255 {       
00256         unsigned short* stringLength = new unsigned short(0);   
00257         
00258         char* res = getValue(index, *stringLength);     
00259         
00260         return *reinterpret_cast<float*>(res);          
00261 }
00262 
00263 template <typename T>
00264 char* RowValueColumn<T>::getValue(const int index, unsigned short& stringLength) const
00265 {
00266         unsigned int* prefix = new unsigned int(0);
00267         RowPagePtr rpp = _row_table_ptr->getPageByIndex(index, *prefix);        
00268                 
00269         /* Call getValue() from RowPage */
00270     return rpp->getValue(index - (*prefix), _column_index, stringLength);
00271 }
00272 
00273 template <typename T>
00274 bool RowValueColumn<T>::isCompressed() const throw(){ return false; }   
00275 
00276 template <typename T>
00277 bool RowValueColumn<T>::update(PositionListPtr tid, const boost::any& new_value)
00278 { 
00279         cout << tid << boost::any_cast<string>(new_value);
00280         return false;
00281 }
00282 
00283 template <typename T>
00284 bool RowValueColumn<T>::remove(PositionListPtr tid) 
00285 {
00286         cout << tid;
00287         return false;
00288 }
00289 
00290 template <typename T>
00291 ColumnPtr RowValueColumn<T>::toColumn() 
00292 {
00293         string name = this->getName();  
00294         ColumnPtr col = createColumn(this->getType(), this->getName());
00295         int count = _row_table_ptr->getNumberofRows();
00296         
00297         for(int index = 0; index < count; index++)
00298         {
00299                 col->insert((this)->operator [](index));
00300         }
00301         
00302         return col;
00303 }
00304 
00305 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines