FACT++  1.0
bool zfits::ReadBinaryRow ( const size_t &  rowNum,
char *  bufferToRead 
)
inlineprivate

Definition at line 274 of file zfits.h.

References ___err___, Checksum::add(), buffer, fits::Table::bytes_per_row, clear(), fits::fChkData, fits::fCopy, fHeapFromDataStart, fHeapOff, fNumRowsPerTile, fShrinkFactor, fits::fTable, GetNumRows(), gLog, i, InitCompressionReading(), FITS::kOrderByCol, FITS::kOrderByRow, fits::Table::num_cols, size, FITS::TileHeader::size, fits::Table::sorted_cols, str, TileHeader(), and UncompressBuffer().

Referenced by StageRow().

275  {
276  if (rowNum >= GetNumRows())
277  return false;
278 
279  if (!fCatalogInitialized)
281 
282  // Book keeping, where are we?
283  const int64_t requestedTile = rowNum / fNumRowsPerTile;
284  const int64_t currentTile = fCurrentRow / fNumRowsPerTile;
285 
286  const int64_t requestedSuperTile = requestedTile / fShrinkFactor;
287  //const int64_t currentSuperTile = currentTile / fShrinkFactor;
288 
289  const int64_t requestedSubTile = requestedTile % fShrinkFactor;
290  //const int64_t currentSubTile = currentTile % fShrinkFactor;
291 
292  // Is this the first tile we read at all?
293  const bool isFirstTile = fCurrentRow<0;
294 
295  // Is this just the next tile in the sequence?
296  const bool isNextTile = requestedTile==currentTile+1 || isFirstTile;
297 
298  fCurrentRow = rowNum;
299 
300  // Do we have to read a new tile from disk?
301  if (requestedTile!=currentTile || isFirstTile)
302  {
303  //skip to the beginning of the tile
304  const int64_t superTileStart = fCatalog[requestedSuperTile][0].second - sizeof(FITS::TileHeader);
305 
306  std::vector<size_t> offsets = fTileOffsets[requestedSuperTile];
307 
308  // If this is a sub tile we might have to step forward a bit and
309  // seek for the sub tile. If we were just reading the previous one
310  // we can skip that.
311  if (!isNextTile || isFirstTile)
312  {
313  // step to the beginnig of the super tile
314  seekg(fHeapOff+superTileStart);
315 
316  // If there are sub tiles we might have to seek through the super tile
317  for (uint32_t k=0; k<requestedSubTile; k++)
318  {
319  // Read header
320  FITS::TileHeader header;
321  read((char*)&header, sizeof(FITS::TileHeader));
322 
323  // Skip to the next header
324  seekg(header.size-sizeof(FITS::TileHeader), cur);
325  }
326  }
327 
328  // this is now the beginning of the sub-tile we want to read
329  const int64_t subTileStart = tellg() - fHeapOff;
330  // calculate the 32 bits offset of the current tile.
331  const uint32_t offset = (subTileStart + fHeapFromDataStart)%4;
332 
333  // start of destination buffer (padding comes later)
334  char *destBuffer = fCompressedBuffer.data()+offset;
335 
336  // Store the current tile size once known
337  size_t currentTileSize = 0;
338 
339  // If this is a request for a sub tile which is not cataloged
340  // recalculate the offsets from the buffer, once read
341  if (requestedSubTile>0)
342  {
343  // Read header
344  read(destBuffer, sizeof(FITS::TileHeader));
345 
346  // Get size of tile
347  currentTileSize = reinterpret_cast<FITS::TileHeader*>(destBuffer)->size;
348 
349  // now read the remaining bytes of this tile
350  read(destBuffer+sizeof(FITS::TileHeader), currentTileSize-sizeof(FITS::TileHeader));
351 
352  // Calculate the offsets recursively
353  offsets[0] = 0;
354 
355  //skip through the columns
356  for (size_t i=0; i<fTable.num_cols-1; i++)
357  {
358  //zero sized column do not have headers. Skip it
359  if (fTable.sorted_cols[i].num == 0)
360  {
361  offsets[i+1] = offsets[i];
362  continue;
363  }
364 
365  const char *pos = destBuffer + offsets[i] + sizeof(FITS::TileHeader);
366  offsets[i+1] = offsets[i] + reinterpret_cast<const FITS::BlockHeader*>(pos)->size;
367  }
368  }
369  else
370  {
371  // If we are reading the first tile of a super tile, all information
372  // is already available.
373  currentTileSize = fTileSize[requestedSuperTile] + sizeof(FITS::TileHeader);
374  read(destBuffer, currentTileSize);
375  }
376 
377 
378  // If we are reading sequentially, calcualte checksum
379  if (isNextTile)
380  {
381  // Padding for checksum calculation
382  memset(fCompressedBuffer.data(), 0, offset);
383  memset(destBuffer+currentTileSize, 0, fCompressedBuffer.size()-currentTileSize-offset);
385  }
386 
387  // Check if we are writing a copy of the file
388  if (isNextTile && fCopy.is_open() && fCopy.good())
389  {
390  fCopy.write(fCompressedBuffer.data()+offset, currentTileSize);
391  if (!fCopy)
392  clear(rdstate()|std::ios::badbit);
393  }
394  else
395  if (fCopy.is_open())
396  clear(rdstate()|std::ios::badbit);
397 
398 
399  // uncompress the buffer
400  const uint32_t thisRoundNumRows = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
401  if (!UncompressBuffer(offsets, thisRoundNumRows, offset+sizeof(FITS::TileHeader)))
402  return false;
403 
404  // pointer to column (source buffer)
405  const char *src = fTransposedBuffer.data();
406 
407  uint32_t i=0;
408  for (auto it=fTable.sorted_cols.cbegin(); it!=fTable.sorted_cols.cend(); it++, i++)
409  {
410  char *buffer = fBuffer.data() + it->offset; // pointer to column (destination buffer)
411 
412  switch (fColumnOrdering[i])
413  {
414  case FITS::kOrderByRow:
415  // regular, "semi-transposed" copy
416  for (char *dest=buffer; dest<buffer+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row
417  {
418  memcpy(dest, src, it->bytes);
419  src += it->bytes; // next column
420  }
421  break;
422 
423  case FITS::kOrderByCol:
424  // transposed copy
425  for (char *elem=buffer; elem<buffer+it->bytes; elem+=it->size) // element-by-element (arrays)
426  {
427  for (char *dest=elem; dest<elem+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row
428  {
429  memcpy(dest, src, it->size);
430  src += it->size; // next element
431  }
432  }
433  break;
434 
435  default:
436  clear(rdstate()|std::ios::badbit);
437 
438  std::ostringstream str;
439  str << "Unkown column ordering scheme found (i=" << i << ", " << fColumnOrdering[i] << ")";
440 #ifdef __EXCEPTIONS
441  throw std::runtime_error(str.str());
442 #else
443  gLog << ___err___ << "ERROR - " << str.str() << std::endl;
444  return false;
445 #endif
446  };
447  }
448  }
449 
450  //Data loaded and uncompressed. Copy it to destination
452  return good();
453  }
size_t fNumRowsPerTile
Number of rows per compressed tile.
Definition: zfits.h:143
size_t bytes_per_row
Definition: fits.h:96
std::vector< char > fTransposedBuffer
intermediate buffer to transpose the rows
Definition: zfits.h:138
streamoff fHeapOff
offset from the beginning of the file of the binary data
Definition: zfits.h:147
bool fCatalogInitialized
Definition: zfits.h:135
int i
Definition: db_dim_client.c:21
char str[80]
Definition: test_client.c:7
size_t num_cols
Definition: fits.h:98
size_t GetNumRows() const
Definition: zfits.h:57
std::ofstream fCopy
Definition: fits.h:499
SortedColumns sorted_cols
Definition: fits.h:117
#define gLog
Definition: fits.h:36
void InitCompressionReading()
Definition: zfits.h:84
Checksum fChkData
Definition: fits.h:517
#define ___err___
Definition: fits.h:37
Table fTable
Definition: fits.h:502
std::vector< char > fColumnOrdering
ordering of the column&#39;s rows. Can change from tile to tile.
Definition: zfits.h:140
std::vector< char > fCompressedBuffer
compressed rows
Definition: zfits.h:139
std::vector< std::vector< std::pair< int64_t, int64_t > > > fCatalog
Catalog, i.e. the main table that points to the compressed data.
Definition: zfits.h:150
std::vector< size_t > fTileSize
size in bytes of each compressed tile
Definition: zfits.h:151
bool UncompressBuffer(const std::vector< size_t > &offsets, const uint32_t &thisRoundNumRows, const uint32_t offset)
Definition: zfits.h:503
std::vector< std::vector< size_t > > fTileOffsets
offset from start of tile of a given compressed column
Definition: zfits.h:152
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
int size
Definition: db_dim_server.c:17
void clear()
Definition: HeadersFTM.h:216
int64_t fCurrentRow
current row in memory signed because we need -1
Definition: zfits.h:144
streamoff fHeapFromDataStart
offset from the beginning of the data table
Definition: zfits.h:148
bool add(const char *buf, size_t len, bool big_endian=true)
Definition: checksum.h:49
size_t fShrinkFactor
shrink factor
Definition: zfits.h:145
std::vector< char > fBuffer
store the uncompressed rows
Definition: zfits.h:137
uint64_t size
Definition: FITS.h:116
TileHeader()
Definition: FITS.h:75

+ Here is the call graph for this function:

+ Here is the caller graph for this function: