00001 #ifndef Impala_Core_Table_ReadTable_h
00002 #define Impala_Core_Table_ReadTable_h
00003
00004 #include "Core/Table/TableTem.h"
00005 #include "Core/Column/Read.h"
00006 #include "Util/StringParser.h"
00007 #include "Persistency/File.h"
00008
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Table
00014 {
00015
00016
00017 template<class TableT>
00018 inline void
00019 ReadOldHeader(TableT* table, Util::IOBuffer* buffer)
00020 {
00021 ILOG_VAR(Impala.Core.Table.Read);
00022 if (!table)
00023 {
00024 ILOG_ERROR("No table");
00025 return;
00026 }
00027 table->ReadColNames(buffer);
00028 Column::Read(table->GetColumn1(), buffer);
00029 int nr = table->GetColumn1()->Capacity();
00030 Column::Read(table->GetColumn2(), buffer);
00031 if (table->GetColumn2()->Valid() && (table->GetColumn2()->Capacity() != nr))
00032 ILOG_ERROR("col2 has different size than col1");
00033 Column::Read(table->GetColumn3(), buffer);
00034 if (table->GetColumn3()->Valid() && (table->GetColumn3()->Capacity() != nr))
00035 ILOG_ERROR("col3 has different size than col1");
00036 Column::Read(table->GetColumn4(), buffer);
00037 if (table->GetColumn4()->Valid() && (table->GetColumn4()->Capacity() != nr))
00038 ILOG_ERROR("col4 has different size than col1");
00039 Column::Read(table->GetColumn5(), buffer);
00040 if (table->GetColumn5()->Valid() && (table->GetColumn5()->Capacity() != nr))
00041 ILOG_ERROR("col5 has different size than col1");
00042 Column::Read(table->GetColumn6(), buffer);
00043 if (table->GetColumn6()->Valid() && (table->GetColumn6()->Capacity() != nr))
00044 ILOG_ERROR("col6 has different size than col1");
00045 Column::Read(table->GetColumn7(), buffer);
00046 if (table->GetColumn7()->Valid() && (table->GetColumn7()->Capacity() != nr))
00047 ILOG_ERROR("col7 has different size than col1");
00048 Column::Read(table->GetColumn8(), buffer);
00049 if (table->GetColumn8()->Valid() && (table->GetColumn8()->Capacity() != nr))
00050 ILOG_ERROR("col8 has different size than col1");
00051 Column::Read(table->GetColumn9(), buffer);
00052 if (table->GetColumn9()->Valid() && (table->GetColumn9()->Capacity() != nr))
00053 ILOG_ERROR("col9 has different size than col1");
00054 table->SetSize(nr);
00055 }
00056
00057 template<class TableT>
00058 inline void
00059 Read(TableT* table, Util::IOBuffer* buffer)
00060 {
00061 ILOG_VAR(Impala.Core.Table.Read);
00062 if (!table)
00063 {
00064 ILOG_ERROR("No table");
00065 return;
00066 }
00067
00068 String formatStr = "impalatableversion: 2, columns: %d, size: %d";
00069 const int TABLEHEADERSIZE = 1024;
00070
00071 int columns;
00072 int nr;
00073 char buf[TABLEHEADERSIZE+1];
00074 for (int i=0 ; i<TABLEHEADERSIZE+1 ; i++)
00075 buf[i] = 0;
00076
00077 Util::IOBuffer::PositionType startPos = buffer->GetPosition();
00078 buffer->Read(buf, TABLEHEADERSIZE);
00079
00080
00081 if((buf[0] == 'I') && (buf[1] == 'm'))
00082 {
00083 buffer->Seek(startPos, SEEK_SET);
00084 String marker = buffer->ReadLine();
00085 if(marker.substr(0, 13) == "Impala::Table")
00086 {
00087 table->SetInfo(buffer->ReadLine());
00088 ReadOldHeader(table, buffer);
00089 return;
00090 }
00091 buffer->Seek(startPos + TABLEHEADERSIZE, SEEK_SET);
00092 }
00093
00094 int res = sscanf(buf, formatStr.c_str(),
00095 &columns, &nr);
00096 if (res != 2)
00097 {
00098 ILOG_ERROR("Header is not compatible: res==" << res <<
00099 " (should be 2), size==" << nr <<
00100 ", columns==" << columns << ", startPos==" << startPos);
00101 return;
00102 }
00103
00104
00105 String buf2(buf);
00106 Util::StringParser sp(buf2);
00107 while(!(sp.TheEnd()))
00108 {
00109 String key = sp.GetString(':', false);
00110 if(sp.TheEnd()) break;
00111 String value = sp.GetString('\n', true);
00112 if(key == "column1") table->SetColName(1, value);
00113 if(key == "column2") table->SetColName(2, value);
00114 if(key == "column3") table->SetColName(3, value);
00115 if(key == "column4") table->SetColName(4, value);
00116 if(key == "column5") table->SetColName(5, value);
00117 if(key == "column6") table->SetColName(6, value);
00118 if(key == "column7") table->SetColName(7, value);
00119 if(key == "column8") table->SetColName(8, value);
00120 if(key == "column9") table->SetColName(9, value);
00121 if(key == "info") table->SetInfo(value);
00122 }
00123
00124
00125 buffer->SetPosition(startPos + TABLEHEADERSIZE + sizeof(Int64) * 16);
00126
00127 Column::Read(table->GetColumn1(), buffer);
00128 if (table->GetColumn1()->Capacity() != nr)
00129 ILOG_ERROR("col1 has different size than header");
00130 Column::Read(table->GetColumn2(), buffer);
00131 if (table->GetColumn2()->Valid() && (table->GetColumn2()->Capacity() != nr))
00132 ILOG_ERROR("col2 has different size than col1");
00133 Column::Read(table->GetColumn3(), buffer);
00134 if (table->GetColumn3()->Valid() && (table->GetColumn3()->Capacity() != nr))
00135 ILOG_ERROR("col3 has different size than col1");
00136 Column::Read(table->GetColumn4(), buffer);
00137 if (table->GetColumn4()->Valid() && (table->GetColumn4()->Capacity() != nr))
00138 ILOG_ERROR("col4 has different size than col1");
00139 Column::Read(table->GetColumn5(), buffer);
00140 if (table->GetColumn5()->Valid() && (table->GetColumn5()->Capacity() != nr))
00141 ILOG_ERROR("col5 has different size than col1");
00142 Column::Read(table->GetColumn6(), buffer);
00143 if (table->GetColumn6()->Valid() && (table->GetColumn6()->Capacity() != nr))
00144 ILOG_ERROR("col6 has different size than col1");
00145 Column::Read(table->GetColumn7(), buffer);
00146 if (table->GetColumn7()->Valid() && (table->GetColumn7()->Capacity() != nr))
00147 ILOG_ERROR("col7 has different size than col1");
00148 Column::Read(table->GetColumn8(), buffer);
00149 if (table->GetColumn8()->Valid() && (table->GetColumn8()->Capacity() != nr))
00150 ILOG_ERROR("col8 has different size than col1");
00151 Column::Read(table->GetColumn9(), buffer);
00152 if (table->GetColumn9()->Valid() && (table->GetColumn9()->Capacity() != nr))
00153 ILOG_ERROR("col9 has different size than col1");
00154 table->SetSize(nr);
00155 }
00156
00157
00158 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00159 template<class TableT>
00160 inline bool
00161 Read(TableT* table, String fileName, Util::Database* db)
00162 {
00163 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "", 0, true);
00164 if (buf)
00165 {
00166 Read(table, buf);
00167 delete buf;
00168 return true;
00169 }
00170 return false;
00171 }
00172 #endif // REPOSITORY_USED
00173
00174 template<class TableT>
00175 inline bool
00176 Read(TableT* table, Persistency::File file)
00177 {
00178 Util::IOBuffer* buf = file.GetReadBuffer();
00179 if (buf)
00180 {
00181 Read(table, buf);
00182 delete buf;
00183 return true;
00184 }
00185 return false;
00186 }
00187
00188 }
00189 }
00190 }
00191
00192 #endif