00001 #ifndef Impala_Core_Array_ReadRaw_h
00002 #define Impala_Core_Array_ReadRaw_h
00003
00004 #include <vector>
00005 #include "Persistency/File.h"
00006 #include "Util/Database.h"
00007 #include "Core/Array/Element/TypeString.h"
00008 #include "Basis/NativeTypeRead.h"
00009 #include "Core/Array/ArrayType.h"
00010 #include "Core/Array/Arrays.h"
00011 #include "Core/Array/Endian.h"
00012
00013 namespace Impala
00014 {
00015 namespace Core
00016 {
00017 namespace Array
00018 {
00019
00020
00021 inline ArrayType
00022 ReadRawArrayType(Util::IOBuffer* buffer)
00023 {
00024 ILOG_VAR(Impala.Core.Array.ReadRawArrayType);
00025 char buf[200];
00026 buffer->Read(buf, 200);
00027 String line(buf);
00028
00029
00030 String::size_type p = line.find("type:");
00031 if (p == String::npos) {
00032 ILOG_ERROR("type not found");
00033 return ARRAY2DUNKNOWN;
00034 }
00035 p += 6;
00036 String::size_type p2 = line.find(',', p);
00037 String type = line.substr(p, p2-p);
00038
00039
00040 p = line.find("elemSize:");
00041 if (p == String::npos) {
00042 ILOG_ERROR("elemSize not found");
00043 return ARRAY2DUNKNOWN;
00044 }
00045 p += 10;
00046 p2 = line.find(',', p);
00047 String elemSizeStr = line.substr(p, p2-p);
00048 Int64 elemSize = atoi(elemSizeStr);
00049
00050
00051 switch (elemSize) {
00052 case 1:
00053 if (type == "uint8")
00054 return ARRAY2DSCALARUINT8;
00055 if (type == "int32")
00056 return ARRAY2DSCALARINT32;
00057 if (type == "uint64")
00058 return ARRAY2DSCALARUINT64;
00059 if (type == "real64")
00060 return ARRAY2DSCALARREAL64;
00061 break;
00062 case 2:
00063 if (type == "real64")
00064 return ARRAY2DVEC2REAL64;
00065 break;
00066 case 3:
00067 if (type == "uint8")
00068 return ARRAY2DVEC3UINT8;
00069 if (type == "real64")
00070 return ARRAY2DVEC3REAL64;
00071 break;
00072 }
00073 ILOG_ERROR("unknown type " << type << " elemSize " << elemSize);
00074 return ARRAY2DUNKNOWN;
00075 }
00076
00077 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00078 inline ArrayType
00079 ReadRawArrayType(String fileName, Util::Database* db)
00080 {
00081 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "", 0, true);
00082 ArrayType res = ReadRawArrayType(buf);
00083 delete buf;
00084 return res;
00085 }
00086 #endif // REPOSITORY_USED
00087
00088 inline ArrayType
00089 ReadRawArrayType(Persistency::File file)
00090 {
00091 Util::IOBuffer* buf = file.GetReadBuffer();
00092 ArrayType res = ReadRawArrayType(buf);
00093 delete buf;
00094 return res;
00095 }
00096
00097 template<class ArrayT>
00098 inline void
00099 ReadRaw(ArrayT*& dst, Util::IOBuffer* buffer)
00100 {
00101 typedef typename ArrayT::StorType StorT;
00102
00103 ILOG_VAR(Impala.Core.Array.ReadRaw);
00104 String typeStr = Element::TypeString<StorT>(0);
00105 String formatStr = "version: 1, binary: %lld, type: " + typeStr +
00106 ", elemSize: %lld, width: %lld, height: %lld, nr: %lld";
00107
00108 Int64 binary;
00109 Int64 elemSize;
00110 Int64 width;
00111 Int64 height;
00112 Int64 nrA;
00113 char buf[200];
00114 for (int i=0 ; i<200 ; i++)
00115 buf[i] = 0;
00116 Util::IOBuffer::PositionType startPos = buffer->GetPosition();
00117 buffer->Gets(buf, 200);
00118 int res = sscanf(buf, formatStr.c_str(),
00119 &binary, &elemSize, &width, &height, &nrA);
00120 if ((res != 5) || (elemSize != ArrayT::ElemSize()) || (nrA != 1))
00121 {
00122 ILOG_ERROR("Header is not compatible: res==" << res <<
00123 " (should be 5), elemSize==" << elemSize << " (should be "
00124 << ArrayT::ElemSize() << "), nrA==" << nrA <<
00125 " (should be 1), buf==[" << buf << "]");
00126 return;
00127 }
00128 size_t blockSize = width*height*elemSize;
00129 if (dst == 0)
00130 dst = ArrayCreate<ArrayT>(width, height);
00131 StorT* dstPtr = dst->CPB(0, 0);
00132 if (binary)
00133 {
00134 buffer->SetPosition(startPos + 200);
00135 for (Int64 n=0 ; n<nrA ; n++)
00136 {
00137 size_t nrRead = buffer->Read(dstPtr, sizeof(StorT) * blockSize);
00138 if (nrRead != sizeof(StorT) * blockSize)
00139 ILOG_ERROR("read " << nrRead << " bytes instead of "
00140 << sizeof(StorT) * blockSize);
00141 dstPtr += blockSize;
00142 }
00143 Endian(dst, dst);
00144 }
00145 else
00146 {
00147 for (Int64 n=0 ; n<nrA ; n++)
00148 {
00149 for(Int64 i=0 ; i<height ; i++)
00150 {
00151 for(Int64 j=0 ; j<width ; j++)
00152 {
00153 for (Int64 k=0 ; k<elemSize ; k++)
00154 {
00155 buffer->NativeTypeRead(dstPtr);
00156 dstPtr++;
00157 }
00158 }
00159 }
00160 }
00161 buffer->ReadLine();
00162 }
00163 }
00164
00165 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00166 template<class ArrayT>
00167 inline void
00168 ReadRaw(ArrayT*& dst, String fileName, Util::Database* db)
00169 {
00170
00171 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "", 0, true);
00172 if (buf)
00173 {
00174 ReadRaw(dst, buf);
00175 delete buf;
00176 }
00177 }
00178 #endif // REPOSITORY_USED
00179
00180 template<class ArrayT>
00181 inline void
00182 ReadRaw(ArrayT*& dst, Persistency::File file)
00183 {
00184 Util::IOBuffer* buf = file.GetReadBuffer();
00185 if (buf)
00186 {
00187 ReadRaw(dst, buf);
00188 delete buf;
00189 }
00190 }
00191
00192 template<class ArrayT>
00193 inline void
00194 ReadRawList(std::vector<ArrayT*>& list, Util::IOBuffer* buffer)
00195 {
00196 typedef typename ArrayT::StorType StorT;
00197
00198 ILOG_VAR(Impala.Core.Array.ReadRawList);
00199 String typeStr = Element::TypeString<StorT>(0);
00200 String formatStr = "version: 1, binary: %lld, type: " + typeStr +
00201 ", elemSize: %lld, width: %lld, height: %lld, nr: %lld";
00202
00203 Int64 binary;
00204 Int64 elemSize;
00205 Int64 width;
00206 Int64 height;
00207 Int64 nrA;
00208 char buf[200];
00209 Util::IOBuffer::PositionType startPos = buffer->GetPosition();
00210 buffer->Gets(buf, 200);
00211 int res = sscanf(buf, formatStr.c_str(),
00212 &binary, &elemSize, &width, &height, &nrA);
00213 if ((res != 5) || (elemSize != ArrayT::ElemSize()))
00214 {
00215 ILOG_ERROR("Header is not compatible");
00216 return;
00217 }
00218 size_t blockSize = width*height*elemSize;
00219 Int64 n;
00220 for (n=0 ; n<nrA ; n++)
00221 if (n+1 > list.size())
00222 list.push_back(ArrayCreate<ArrayT>(width, height));
00223 if (binary)
00224 {
00225 buffer->SetPosition(startPos + 200);
00226 for (Int64 n=0 ; n<nrA ; n++)
00227 {
00228 StorT* dstPtr = list[n]->CPB(0, 0);
00229 size_t nrRead = buffer->Read(dstPtr, sizeof(StorT) * blockSize);
00230 if (nrRead != sizeof(StorT) * blockSize)
00231 ILOG_ERROR("read " << nrRead << " bytes instead of "
00232 << sizeof(StorT) * blockSize);
00233 Endian(list[n], list[n]);
00234 }
00235 }
00236 else
00237 {
00238 for (Int64 n=0 ; n<nrA ; n++)
00239 {
00240 for(Int64 i=0 ; i<height ; i++)
00241 {
00242 StorT* dstPtr = list[n]->CPB(0, i);
00243 for(Int64 j=0 ; j<width ; j++)
00244 {
00245 for (Int64 k=0 ; k<elemSize ; k++)
00246 {
00247 buffer->NativeTypeRead(dstPtr);
00248 dstPtr++;
00249 }
00250 }
00251 }
00252 }
00253 }
00254 }
00255
00256 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00257 template<class ArrayT>
00258 inline void
00259 ReadRawList(std::vector<ArrayT*>& list, String fileName, Util::Database* db)
00260 {
00261
00262 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "", 0, true);
00263 if (buf)
00264 {
00265 ReadRawList(list, buf);
00266 delete buf;
00267 }
00268 }
00269 #endif // REPOSITORY_USED
00270
00271 template<class ArrayT>
00272 inline void
00273 ReadRawList(std::vector<ArrayT*>& list, Persistency::File file)
00274 {
00275 Util::IOBuffer* buf = file.GetReadBuffer();
00276 if (buf)
00277 {
00278 ReadRawList(list, buf);
00279 delete buf;
00280 }
00281 }
00282
00283 template<class ArrayT>
00284 inline void
00285 ReadRawListVar(std::vector<ArrayT*>& list, Util::IOBuffer* buffer)
00286 {
00287 typedef typename ArrayT::StorType StorT;
00288
00289 ILOG_VAR(Impala.Core.Array.ReadRawListVar);
00290 String typeStr = Element::TypeString<StorT>(0);
00291 String formatStr = "version: 2, binary: %lld, type: " + typeStr +
00292 ", elemSize: %lld, nr: %lld";
00293
00294 Int64 binary;
00295 Int64 elemSize;
00296 Int64 nrA;
00297 char buf[200];
00298 Util::IOBuffer::PositionType startPos = buffer->GetPosition();
00299 buffer->Gets(buf, 200);
00300 int res = sscanf(buf, formatStr.c_str(), &binary, &elemSize, &nrA);
00301 if ((res != 3) || (elemSize != ArrayT::ElemSize()))
00302 {
00303 ILOG_ERROR("Header is not compatible");
00304 return;
00305 }
00306 if (binary)
00307 {
00308 buffer->SetPosition(startPos + 200);
00309 for (Int64 n=0 ; n<nrA ; n++)
00310 {
00311 Int64 width;
00312 Int64 height;
00313 buffer->Read(buf, 20);
00314 sscanf(buf, "%lld %lld ", &width, &height);
00315 size_t blockSize = width*height*elemSize;
00316 ArrayT* a = ArrayCreate<ArrayT>(width, height);
00317 StorT* dstPtr = a->CPB(0, 0);
00318 size_t nrRead = buffer->Read(dstPtr, sizeof(StorT) * blockSize);
00319 if (nrRead != sizeof(StorT) * blockSize)
00320 ILOG_ERROR("read " << nrRead << " bytes instead of "
00321 << sizeof(StorT) * blockSize);
00322 Endian(a, a);
00323 list.push_back(a);
00324 }
00325 }
00326 else
00327 {
00328 for (Int64 n=0 ; n<nrA ; n++)
00329 {
00330 Int64 width;
00331 Int64 height;
00332 buffer->NativeTypeRead(&width);
00333 buffer->NativeTypeRead(&height);
00334 Int64 blockSize = width*height*elemSize;
00335 ArrayT* a = ArrayCreate<ArrayT>(width, height);
00336 for(Int64 i=0 ; i<height ; i++)
00337 {
00338 StorT* dstPtr = a->CPB(0, i);
00339 for(Int64 j=0 ; j<width ; j++)
00340 {
00341 for (Int64 k=0 ; k<elemSize ; k++)
00342 {
00343 buffer->NativeTypeRead(dstPtr);
00344 dstPtr++;
00345 }
00346 }
00347 }
00348 list.push_back(a);
00349 }
00350 }
00351 }
00352
00353 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00354 template<class ArrayT>
00355 inline void
00356 ReadRawListVar(std::vector<ArrayT*>& list, String fileName, Util::Database* db)
00357 {
00358
00359 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "", 0, true);
00360 if (buf)
00361 {
00362 ReadRawListVar(list, buf);
00363 delete buf;
00364 }
00365 }
00366 #endif // REPOSITORY_USED
00367
00368 template<class ArrayT>
00369 inline void
00370 ReadRawListVar(std::vector<ArrayT*>& list, Persistency::File file)
00371 {
00372 Util::IOBuffer* buf = file.GetReadBuffer();
00373 if (buf)
00374 {
00375 ReadRawListVar(list, buf);
00376 delete buf;
00377 }
00378 }
00379
00380
00381 template<class ArrayT>
00382 inline void
00383 ReadRawListVarIndex(std::vector<Int64>& list, Util::IOBuffer* buffer)
00384 {
00385 typedef typename ArrayT::StorType StorT;
00386
00387 ILOG_VAR(Impala.Core.Array.ReadRawListVarIndex);
00388 String typeStr = Element::TypeString<StorT>(0);
00389 String formatStr = "version: 2, binary: %lld, type: " + typeStr +
00390 ", elemSize: %lld, nr: %lld";
00391
00392 Int64 binary;
00393 Int64 elemSize;
00394 Int64 nrA;
00395 char buf[200];
00396 Util::IOBuffer::PositionType startPos = buffer->GetPosition();
00397 buffer->Gets(buf, 200);
00398 int res = sscanf(buf, formatStr.c_str(), &binary, &elemSize, &nrA);
00399 if ((res != 3) || (elemSize != ArrayT::ElemSize()))
00400 {
00401 ILOG_ERROR("Header is not compatible");
00402 return;
00403 }
00404 if (binary)
00405 {
00406 buffer->SetPosition(startPos + 200);
00407 for (Int64 n=0 ; n<nrA ; n++)
00408 {
00409 Int64 curPos = buffer->GetPosition();
00410 list.push_back(curPos);
00411 Int64 width;
00412 Int64 height;
00413 buffer->Read(buf, 20);
00414 sscanf(buf, "%lld %lld ", &width, &height);
00415 size_t blockSize = width*height*elemSize;
00416 buffer->SetPosition(buffer->GetPosition() + blockSize);
00417 }
00418 }
00419 else
00420 {
00421 ILOG_ERROR("mode not implemented");
00422 }
00423 }
00424
00425 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00426 template<class ArrayT>
00427 inline void
00428 ReadRawListVarIndex(std::vector<Int64>& list, String fileName, Util::Database* db)
00429 {
00430
00431 Util::IOBuffer* buf = db->GetIOBuffer(fileName, true, false, "", 0, true);
00432 if (buf)
00433 {
00434 ReadRawListVarIndex<ArrayT>(list, buf);
00435 delete buf;
00436 }
00437 }
00438 #endif // REPOSITORY_USED
00439
00440 template<class ArrayT>
00441 inline void
00442 ReadRawListVarIndex(std::vector<Int64>& list, Persistency::File file)
00443 {
00444 Util::IOBuffer* buf = file.GetReadBuffer();
00445 if (buf)
00446 {
00447 ReadRawListVarIndex<ArrayT>(list, buf);
00448 delete buf;
00449 }
00450 }
00451
00452 }
00453 }
00454 }
00455
00456 #endif