00001 #ifndef Impala_Core_Array_WriteRaw_h
00002 #define Impala_Core_Array_WriteRaw_h
00003
00004 #include <vector>
00005 #include "Persistency/File.h"
00006 #include "Util/DatabaseWriteNative.h"
00007 #include "Core/Array/Element/TypeString.h"
00008 #include "Core/Array/ArraySet.h"
00009 #include "Core/Array/Endian.h"
00010
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Array
00016 {
00017
00018
00019 template<class ArrayT>
00020 inline void
00021 WriteRaw(ArrayT* src, Util::IOBuffer* buffer, bool binary)
00022 {
00023 typedef typename ArrayT::StorType StorT;
00024
00025 String typeStr = Element::TypeString<StorT>(0);
00026 String formatStr = "version: 1, binary: %d, type: " + typeStr +
00027 ", elemSize: %lld, width: %lld, height: %lld, nr: %lld";
00028 Int64 width = src->CW();
00029 Int64 height = src->CH();
00030 Int64 elemSize = ArrayT::ElemSize();
00031 Int64 nrA = 1;
00032 char buf[200];
00033 memset(buf, 0, 200);
00034 sprintf(buf, formatStr.c_str(), binary, elemSize, width, height, nrA);
00035
00036 if (binary)
00037 {
00038 Endian(src, src);
00039 buffer->Write(buf, 200);
00040 if ((src->BW() == 0) && (src->BH() == 0))
00041 {
00042 StorT* srcPtr = src->CPB(0, 0);
00043 buffer->Write(srcPtr, sizeof(StorT) * width * height * elemSize);
00044 }
00045 else
00046 {
00047 for (Int64 y=0 ; y<height ; y++)
00048 {
00049 StorT* srcPtr = src->CPB(0, y);
00050 buffer->Write(srcPtr, sizeof(StorT) * width * elemSize);
00051 }
00052 }
00053 }
00054 else
00055 {
00056 buffer->Puts(buf);
00057 for (Int64 n=0 ; n<nrA ; n++)
00058 {
00059 for(Int64 i=0 ; i<height ; i++)
00060 {
00061 StorT* srcPtr = src->CPB(0, i);
00062 for(Int64 j=0 ; j<width ; j++)
00063 for (Int64 k=0 ; k<elemSize ; k++)
00064 buffer->NativeTypeWrite(*srcPtr++);
00065 buffer->Puts("");
00066 }
00067 }
00068 }
00069 }
00070
00071 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00072 template<class ArrayT>
00073 inline void
00074 WriteRaw(ArrayT* src, String fileName, Util::Database* db, bool binary)
00075 {
00076
00077 Util::IOBuffer* buf = db->GetIOBuffer(fileName, false, false, "tmp", 0, true);
00078 if (buf)
00079 {
00080 WriteRaw(src, buf, binary);
00081 delete buf;
00082 }
00083 }
00084 #endif // REPOSITORY_USED
00085
00086 template<class ArrayT>
00087 inline void
00088 WriteRaw(ArrayT* src, Persistency::File file, bool binary)
00089 {
00090 Util::IOBuffer* buf = file.GetWriteBuffer();
00091 if (buf)
00092 {
00093 WriteRaw(src, buf, binary);
00094 delete buf;
00095 }
00096 }
00097
00098 template<class ArrayT>
00099 inline void
00100 WriteRawList(std::vector<ArrayT*> list, Util::IOBuffer* buffer, bool binary)
00101 {
00102 typedef typename ArrayT::StorType StorT;
00103
00104 String typeStr = Element::TypeString<StorT>(0);
00105 String formatStr = "version: 1, binary: %d, type: " + typeStr +
00106 ", elemSize: %lld, width: %lld, height: %lld, nr: %lld";
00107 Int64 width = list[0]->CW();
00108 Int64 height = list[0]->CH();
00109 Int64 elemSize = ArrayT::ElemSize();
00110 Int64 nrA = list.size();
00111 char buf[200];
00112 memset(buf, 0, 200);
00113 sprintf(buf, formatStr.c_str(), binary, elemSize, width, height, nrA);
00114
00115 if (binary)
00116 {
00117 buffer->Write(buf, 200);
00118 for (Int64 n=0 ; n<nrA ; n++)
00119 {
00120 Endian(list[n], list[n]);
00121 for (Int64 y=0 ; y<height ; y++)
00122 {
00123 StorT* srcPtr = list[n]->CPB(0, y);
00124 buffer->Write(srcPtr, sizeof(StorT) * width * elemSize);
00125 }
00126 }
00127 }
00128 else
00129 {
00130 buffer->Puts(buf);
00131 for (Int64 n=0 ; n<nrA ; n++)
00132 {
00133 for(Int64 i=0 ; i<height ; i++)
00134 {
00135 StorT* srcPtr = list[n]->CPB(0, i);
00136 for(Int64 j=0 ; j<width ; j++)
00137 for (Int64 k=0 ; k<elemSize ; k++)
00138 buffer->NativeTypeWrite(*srcPtr++);
00139 buffer->Puts("");
00140 }
00141 }
00142 }
00143 }
00144
00145 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00146 template<class ArrayT>
00147 inline void
00148 WriteRawList(std::vector<ArrayT*> list, String fileName, Util::Database* db,
00149 bool binary)
00150 {
00151
00152 Util::IOBuffer* buf = db->GetIOBuffer(fileName, false, false, "tmp", 0, true);
00153 if (buf)
00154 {
00155 WriteRawList(list, buf, binary);
00156 delete buf;
00157 }
00158 }
00159 #endif // REPOSITORY_USED
00160
00161 template<class ArrayT>
00162 inline void
00163 WriteRawList(std::vector<ArrayT*> list, Persistency::File file, bool binary)
00164 {
00165 Util::IOBuffer* buf = file.GetWriteBuffer();
00166 if (buf)
00167 {
00168 WriteRawList(list, buf, binary);
00169 delete buf;
00170 }
00171 }
00172
00173 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00174
00175 template<class ArrayT>
00176 inline void
00177 WriteRawList(ArraySet<ArrayT>& aSet, String fileName, bool binary)
00178 {
00179 std::vector<ArrayT*> list;
00180 for (Int64 i=0 ; i<aSet.Size() ; i++)
00181 list.push_back(aSet.Array(i));
00182 WriteRawList(list, fileName, binary);
00183 }
00184 #endif // REPOSITORY_USED
00185
00186 template<class ArrayT>
00187 inline void
00188 WriteRawList(ArraySet<ArrayT>& aSet, Persistency::File file, bool binary)
00189 {
00190 std::vector<ArrayT*> list;
00191 for (Int64 i=0 ; i<aSet.Size() ; i++)
00192 list.push_back(aSet.Array(i));
00193 WriteRawList(list, file, binary);
00194 }
00195
00196 template<class ArrayT>
00197 inline void
00198 WriteRawListVar(std::vector<ArrayT*> list, Util::IOBuffer* buffer, bool binary)
00199 {
00200 typedef typename ArrayT::StorType StorT;
00201
00202 String typeStr = Element::TypeString<StorT>(0);
00203 String formatStr = "version: 2, binary: %d, type: " + typeStr +
00204 ", elemSize: %lld, nr: %lld";
00205 Int64 elemSize = ArrayT::ElemSize();
00206 Int64 nrA = list.size();
00207 char buf[200];
00208 memset(buf, 0, 200);
00209 sprintf(buf, formatStr.c_str(), binary, elemSize, nrA);
00210
00211 if (binary)
00212 {
00213 buffer->Write(buf, 200);
00214 for (Int64 n=0 ; n<nrA ; n++)
00215 {
00216 Endian(list[n], list[n]);
00217 Int64 width = list[n]->CW();
00218 Int64 height = list[n]->CH();
00219 memset(buf, 0, 200);
00220 sprintf(buf, "%lld %lld", width, height);
00221 buffer->Write(buf, 20);
00222 for (Int64 y=0 ; y<height ; y++)
00223 {
00224 StorT* srcPtr = list[n]->CPB(0, y);
00225 buffer->Write(srcPtr, sizeof(StorT) * width * elemSize);
00226 }
00227 }
00228 }
00229 else
00230 {
00231 buffer->Puts(buf);
00232 for (Int64 n=0 ; n<nrA ; n++)
00233 {
00234 Int64 width = list[n]->CW();
00235 Int64 height = list[n]->CH();
00236 buffer->NativeTypeWrite(width);
00237 buffer->NativeTypeWrite(height);
00238 for(Int64 i=0 ; i<height ; i++)
00239 {
00240 StorT* srcPtr = list[n]->CPB(0, i);
00241 for(Int64 j=0 ; j<width ; j++)
00242 for (Int64 k=0 ; k<elemSize ; k++)
00243 buffer->NativeTypeWrite(*srcPtr++);
00244 buffer->Puts("");
00245 }
00246 }
00247 }
00248 }
00249
00250 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00251 template<class ArrayT>
00252 inline void
00253 WriteRawListVar(std::vector<ArrayT*> list, String fileName, Util::Database* db,
00254 bool writeIndex, bool binary)
00255 {
00256 ILOG_VAR(Impala.Core.Array.WriteRawListVar);
00257
00258 Util::IOBuffer* buf = db->GetIOBuffer(fileName, false, false, "tmp", 0, true);
00259 if (!buf)
00260 return;
00261
00262 WriteRawListVar(list, buf, binary);
00263 delete buf;
00264
00265 if (!writeIndex)
00266 return;
00267 if (!binary)
00268 {
00269 ILOG_ERROR("Cannot write index for non-binary files");
00270 return;
00271 }
00272
00273 size_t storSize = sizeof(typename ArrayT::StorType);
00274 Int64 elemSize = ArrayT::ElemSize();
00275 std::vector<Int64> v;
00276 Int64 pos = 200;
00277 for (Int64 i=0 ; i<list.size() ; i++)
00278 {
00279 v.push_back(pos);
00280 pos += 20 + storSize * list[i]->CW() * list[i]->CH() * elemSize;
00281 }
00282 Util::DatabaseWriteNative(fileName + ".idx", db, v.begin(), v.end());
00283 }
00284 #endif // REPOSITORY_USED
00285
00286 template<class ArrayT>
00287 inline void
00288 WriteRawListVar(std::vector<ArrayT*> list, Persistency::File file,
00289 bool writeIndex, bool binary)
00290 {
00291 ILOG_VAR(Impala.Core.Array.WriteRawListVar);
00292 Util::IOBuffer* buf = file.GetWriteBuffer();
00293 if (!buf)
00294 return;
00295
00296 WriteRawListVar(list, buf, binary);
00297 delete buf;
00298
00299 if (!writeIndex)
00300 return;
00301 if (!binary)
00302 {
00303 ILOG_ERROR("Cannot write index for non-binary files");
00304 return;
00305 }
00306
00307 size_t storSize = sizeof(typename ArrayT::StorType);
00308 Int64 elemSize = ArrayT::ElemSize();
00309 std::vector<Int64> v;
00310 Int64 pos = 200;
00311 for (Int64 i=0 ; i<list.size() ; i++)
00312 {
00313 v.push_back(pos);
00314 pos += 20 + storSize * list[i]->CW() * list[i]->CH() * elemSize;
00315 }
00316 Persistency::File idxFile(file.GetPath() + ".idx", file.GetFileSystem());
00317 idxFile.WriteNative(v.begin(), v.end());
00318 }
00319
00320 }
00321 }
00322 }
00323
00324 #endif