00001 #ifndef Impala_Core_Table_Write_h
00002 #define Impala_Core_Table_Write_h
00003
00004 #include "Core/Table/TableTem.h"
00005 #include "Core/Column/Write.h"
00006 #include "Persistency/File.h"
00007 #include <ctime>
00008
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Table
00014 {
00015
00016
00017 template<class TableT>
00018 inline void
00019 Write(TableT* table, Util::IOBuffer* buffer, bool binary)
00020 {
00021 ILOG_VAR(Impala.Core.Table.Write);
00022 if (!table)
00023 {
00024 ILOG_ERROR("No table");
00025 return;
00026 }
00027
00028
00029 int columnCount = 0;
00030 if(table->GetColumn1()->Valid()) columnCount = 1;
00031 if(table->GetColumn2()->Valid()) columnCount = 2;
00032 if(table->GetColumn3()->Valid()) columnCount = 3;
00033 if(table->GetColumn4()->Valid()) columnCount = 4;
00034 if(table->GetColumn5()->Valid()) columnCount = 5;
00035 if(table->GetColumn6()->Valid()) columnCount = 6;
00036 if(table->GetColumn7()->Valid()) columnCount = 7;
00037 if(table->GetColumn8()->Valid()) columnCount = 8;
00038 if(table->GetColumn9()->Valid()) columnCount = 9;
00039
00040
00041 const int TABLEHEADERSIZE = 1024;
00042 std::ostringstream oss;
00043 oss << "impalatableversion: 2, columns: " << columnCount << ", "
00044 << "size: " << table->Size() << "\n";
00045 for(int i = 1; i <= columnCount; i++)
00046 oss << "column" << i << ": " << table->GetColName(i) << "\n";
00047
00048 oss << "info: " << table->GetInfo() << "\n";
00049
00050 std::time_t rawtime;
00051 std::time ( &rawtime );
00052 oss << "computedon: " << std::ctime (&rawtime);
00053 oss << "computedby: Impala " << IMPALA_VERSION_STR
00054 << " (svnversion " << SVN_VERSION_STR << ") compiled "
00055 << __DATE__ << " " << __TIME__ << "\n";
00056 while(oss.tellp() < TABLEHEADERSIZE)
00057 oss << ' ';
00058 oss.seekp(TABLEHEADERSIZE - 1, std::ios::beg);
00059 oss << '\n';
00060
00061
00062 buffer->Write(oss.str().c_str(), TABLEHEADERSIZE);
00063
00064
00065 Int64 footer[16];
00066 for(int i = 0; i < 16; i++)
00067 footer[i] = 0;
00068 Util::IOBuffer::PositionType footerStartPos = buffer->GetPosition();
00069 buffer->Write(footer, sizeof(Int64) * 16);
00070
00071
00072
00073 footer[0] = 0x6F66616C61706D69LL;
00074 footer[1] = 0x000000017265746FLL;
00075 footer[2] = columnCount;
00076 footer[3] = table->Size();
00077 footer[4] = buffer->GetPosition();
00078 Column::Write(table->GetColumn1(), table->Size(), buffer, binary);
00079 footer[5] = buffer->GetPosition();
00080 Column::Write(table->GetColumn2(), table->Size(), buffer, binary);
00081 footer[6] = buffer->GetPosition();
00082 Column::Write(table->GetColumn3(), table->Size(), buffer, binary);
00083 footer[7] = buffer->GetPosition();
00084 Column::Write(table->GetColumn4(), table->Size(), buffer, binary);
00085 footer[8] = buffer->GetPosition();
00086 Column::Write(table->GetColumn5(), table->Size(), buffer, binary);
00087 footer[9] = buffer->GetPosition();
00088 Column::Write(table->GetColumn6(), table->Size(), buffer, binary);
00089 footer[10] = buffer->GetPosition();
00090 Column::Write(table->GetColumn7(), table->Size(), buffer, binary);
00091 footer[11] = buffer->GetPosition();
00092 Column::Write(table->GetColumn8(), table->Size(), buffer, binary);
00093 footer[12] = buffer->GetPosition();
00094 Column::Write(table->GetColumn9(), table->Size(), buffer, binary);
00095 footer[13] = buffer->GetPosition();
00096 footer[14] = 0;
00097 footer[15] = 0;
00098
00099 Util::IOBuffer::PositionType eofPos = buffer->GetPosition();
00100
00101 buffer->Seek(footerStartPos, SEEK_SET);
00102 buffer->Write(footer, sizeof(Int64) * 16);
00103 buffer->Seek(eofPos, SEEK_SET);
00104 }
00105
00106 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00107 template<class TableT>
00108 inline bool
00109 Write(TableT* table, String fileName, Util::Database* db, bool binary)
00110 {
00111 Util::IOBuffer* buf = db->GetIOBuffer(fileName, false, false, "tmp", 0, true);
00112 if (buf)
00113 {
00114 Write(table, buf, binary);
00115 delete buf;
00116 return true;
00117 }
00118 return false;
00119 }
00120 #endif // REPOSITORY_USED
00121
00122 template<class TableT>
00123 inline bool
00124 Write(TableT* table, Persistency::File file)
00125 {
00126 Util::IOBuffer* buf = file.GetWriteBuffer();
00127 if (buf)
00128 {
00129 Write(table, buf, true);
00130 delete buf;
00131 return true;
00132 }
00133 return false;
00134 }
00135
00136 }
00137 }
00138 }
00139
00140 #endif