Changeset 16416 for trunk/Mars/mcore


Ignore:
Timestamp:
05/28/13 23:28:18 (11 years ago)
Author:
tbretz
Message:
removed the obsolete mightFail from the constructor; some little cosmetics; changed type of fHeapPtr to streamoff and make it seek relatively
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/zfits.h

    r16285 r16416  
    66 */
    77
    8 #ifndef ZFITS_H_
    9 #define ZFITS_H_
     8#ifndef MARS_ZFITS
     9#define MARS_ZFITS
     10
     11#include <stdexcept>
    1012
    1113#include "fits.h"
    12 
    13 #include <stdexcept>
    1414#include "huffman.h"
    1515
    16 #define COMPRESSED_FLAG 0x1
    17 #define UNCOMPRESSED_FLAG 0x0
    1816
    1917#ifndef __MARS__
    2018namespace std
    2119{
    22 #else
    23 using namespace std;
    2420#endif
    2521
     
    2824public:
    2925
    30     /*
    31      *   Basic constructor
    32      */
    33     zfits(const string& fname,
    34           const string& tableName="",
    35           bool          force=false,
    36           bool          mightFail=false) : fits(fname, tableName, force, mightFail),
    37                                            fBuffer(0),
    38                                            fTransposedBuffer(0),
    39                                            fCompressedBuffer(0),
    40                                            fNumTiles(0),
    41                                            fNumRowsPerTile(0),
    42                                            fHeapPtr(0),
    43                                            fCurrentRow(-1)
     26    // Basic constructor
     27    zfits(const string& fname, const string& tableName="",
     28          bool force=false) : fits(fname, tableName, force),
     29        fBuffer(0),
     30        fTransposedBuffer(0),
     31        fCompressedBuffer(0),
     32        fNumTiles(0),
     33        fNumRowsPerTile(0),
     34        fCurrentRow(-1),
     35        fHeapOff(0)
    4436    {
    4537        InitCompressionReading();
    4638    }
    4739
    48     /*
    49      *   Alternative contstructor
    50      */
    51     zfits(const string& fname,
    52           const string& fout,
    53           const string& tableName="",
    54           bool          force=false,
    55           bool          mightFail=false): fits(fname, fout, tableName, force, mightFail),
    56                                           fBuffer(0),
    57                                           fTransposedBuffer(0),
    58                                           fCompressedBuffer(0),
    59                                           fNumTiles(0),
    60                                           fNumRowsPerTile(0),
    61                                           fHeapPtr(0),
    62                                           fCurrentRow(-1)
     40    // Alternative contstructor
     41    zfits(const string& fname, const string& fout, const string& tableName,
     42          bool force=false) : fits(fname, fout, tableName, force),
     43              fBuffer(0),
     44              fTransposedBuffer(0),
     45              fCompressedBuffer(0),
     46              fNumTiles(0),
     47              fNumRowsPerTile(0),
     48              fCurrentRow(-1),
     49              fHeapOff(0)
    6350    {
    6451        InitCompressionReading();
    6552    }
    6653
    67     /*
    68      *  Default destructor
    69      */
    70     ~zfits()
    71     {}
    72 
    73     /*
    74      *  Skip the next row
    75      */
    76     virtual bool SkipNextRow()
    77     {
    78         if (fTable.isCompressed)
    79         {
    80             fRow++;
    81             return true;
    82         }
    83         else
     54    //  Skip the next row
     55    bool SkipNextRow()
     56    {
     57        if (!fTable.isCompressed)
    8458            return fits::SkipNextRow();
     59
     60        fRow++;
     61        return true;
    8562    }
    8663
    8764private:
    88     /*
    89      * Do what it takes to initialize the compressed structured
    90      */
     65
     66    // Do what it takes to initialize the compressed structured
    9167    void InitCompressionReading()
    9268    {
    9369        //The constructor may have failed
    94         if (!good()) return;
     70        if (!good())
     71            return;
    9572
    9673        //Get compressed specific keywords
     
    10481        ReadCatalog();
    10582    }
    106     /*
    107      *  Stage the requested row to internal buffer
    108      *  Does NOT return data to users
    109      */
    110     virtual void StageRow(size_t row, char* dest)
    111     {
    112         if (fTable.isCompressed)
    113             ReadBinaryRow(row, dest);
    114         else
     83
     84    //  Stage the requested row to internal buffer
     85    //  Does NOT return data to users
     86    void StageRow(size_t row, char* dest)
     87    {
     88        if (!fTable.isCompressed)
     89        {
    11590            fits::StageRow(row, dest);
    116     }
    117     /*
    118      *  Copy decompressed data to location requested by user
    119      */
    120     virtual void MoveColumnDataToUserSpace(char* dest, const char* src, const Table::Column& c)
    121     {
    122         if (fTable.isCompressed)
    123             memcpy(dest, src, c.num*c.size);
    124         else
     91            return;
     92        }
     93
     94        ReadBinaryRow(row, dest);
     95    }
     96
     97    // Copy decompressed data to location requested by user
     98    void MoveColumnDataToUserSpace(char* dest, const char* src, const Table::Column& c)
     99    {
     100        if (!fTable.isCompressed)
     101        {
    125102            fits::MoveColumnDataToUserSpace(dest, src, c);
    126     }
     103            return;
     104        }
     105
     106        memcpy(dest, src, c.num*c.size);
     107    }
     108
    127109    vector<char> fBuffer;           ///<store the uncompressed rows
    128110    vector<char> fTransposedBuffer; ///<intermediate buffer to transpose the rows
    129111    vector<char> fCompressedBuffer; ///<compressed rows
     112
    130113    size_t fNumTiles;       ///< Total number of tiles
    131114    size_t fNumRowsPerTile; ///< Number of rows per compressed tile
    132     size_t fHeapPtr;        ///< offset from the beginning of the file of the binary data
    133115    size_t fCurrentRow;     ///< current row in memory.
    134116
     117    streamoff fHeapOff;     ///< offset from the beginning of the file of the binary data
     118
    135119    vector<vector<pair<int64_t, int64_t> > > fCatalog;///< Catalog, i.e. the main table that points to the compressed data.
    136120
     
    141125
    142126        fBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile);
     127
    143128        fTransposedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile);
    144129        fCompressedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile + 1024*1024); //use a bit more memory, in case the compression algorithms uses more
    145130    }
    146     /*
    147      *  Read catalog data. I.e. the address of the compressed data inside the heap
    148      */
     131
     132    // Read catalog data. I.e. the address of the compressed data inside the heap
    149133    void ReadCatalog()
    150134    {
     
    162146                //swap the bytes
    163147                int64_t tempValues[2] = {0,0};
    164                 revcpy<8>((char*)(&tempValues[0]), readBuf, 2);
     148                revcpy<8>(reinterpret_cast<char*>(tempValues[0]), readBuf, 2);
    165149
    166150                //add catalog entry
    167                 fCatalog[i].push_back(make_pair(tempValues[0], tempValues[1]));
     151                fCatalog[i].emplace_back(tempValues[0], tempValues[1]);
    168152            }
    169153
    170154        //see if there is a gap before heap data
    171         const off_t heapShift = ComputeGapFromDataToHeap(fTable);
    172         fHeapPtr = tellg() + heapShift;
    173     }
    174     /*
    175      *  Compressed versin of the read row
    176      */
     155        fHeapOff = fTable.GetHeapShift();
     156    }
     157
     158    // Compressed versin of the read row
    177159    virtual bool ReadBinaryRow(uint64_t rowNum, char* bufferToRead)
    178160    {
    179         if (rowNum >= GetNumRows()) return false;
     161        if (rowNum >= GetNumRows())
     162            return false;
    180163
    181164        const int requestedTile = rowNum/fNumRowsPerTile;
    182165        const int currentTile   = fCurrentRow/fNumRowsPerTile;
     166
    183167        fCurrentRow = rowNum;
    184168
     
    188172            //read yet another chunk from the file
    189173            //the size that we should read is in the catalog. we should sum up the sizes of all columns
     174            const uint32_t currentCatRow = fCurrentRow/fNumRowsPerTile;
     175
    190176            int64_t sizeToRead  = 0;
    191             const uint32_t currentCatRow = fCurrentRow/fNumRowsPerTile;
    192 
    193177            for (uint32_t i=0;i<fCatalog[currentCatRow].size();i++)
    194178                sizeToRead += fCatalog[currentCatRow][i].first;
    195179
    196180            //skip to the beginning of the tile
    197             seekg(fHeapPtr+fCatalog[currentCatRow][0].second);
     181            seekg(fCatalog[currentCatRow][0].second, ios_base::cur);
    198182            read(fCompressedBuffer.data(), sizeToRead);
    199183
     
    203187            //copy the tile and transpose it
    204188            uint32_t offset=0;
     189
    205190            const uint32_t thisRoundNumRows = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
    206191            for (uint32_t i=0;i<fTable.sortedCols.size();i++)
     
    208193                switch (fTable.sortedCols[i].comp)
    209194                {
    210                     case UNCOMPRESSED:
    211                     case SMOOTHMAN:
     195                case UNCOMPRESSED:
     196                case SMOOTHMAN:
     197                    //regular, "semi-transposed" copy
     198                    for (uint32_t k=0;k<thisRoundNumRows;k++)
     199                    {
     200                        memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size*fTable.sortedCols[i].num);
     201                        offset += fTable.sortedCols[i].size*fTable.sortedCols[i].num;
     202                    }
     203                    break;
     204
     205                default:
     206                    //transposed copy
     207                    for (uint32_t j=0;j<fTable.sortedCols[i].num;j++)
    212208                        for (uint32_t k=0;k<thisRoundNumRows;k++)
    213                         {//regular, "semi-transposed" copy
    214                             memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size*fTable.sortedCols[i].num);
    215                             offset += fTable.sortedCols[i].size*fTable.sortedCols[i].num;
     209                        {
     210                            memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset + fTable.sortedCols[i].size*j, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size);
     211                            offset += fTable.sortedCols[i].size;
    216212                        }
    217                     break;
    218                     default :
    219                         for (uint32_t j=0;j<fTable.sortedCols[i].num;j++)
    220                             for (uint32_t k=0;k<thisRoundNumRows;k++)
    221                             {//transposed copy
    222                                 memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset + fTable.sortedCols[i].size*j, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size);
    223                                 offset += fTable.sortedCols[i].size;
    224                             }
    225213                    break;
    226214                };
    227215            }
    228216        }
     217
    229218        //Data loaded and uncompressed. Copy it to destination
    230219        memcpy(bufferToRead, fBuffer.data()+fTable.bytes_per_row*(fCurrentRow%fNumRowsPerTile), fTable.bytes_per_row);
    231220        return good();
    232221    }
    233     /*
    234      *  Read a bunch of uncompressed data
    235      */
     222
     223    // Read a bunch of uncompressed data
    236224    uint32_t UncompressUNCOMPRESSED(char*       dest,
    237225                                    const char* src,
     
    243231        return numRows*sizeOfElems*numRowElems;
    244232    }
    245     /*
    246      *  Read a bunch of data compressed with the Huffman algorithm
    247      */
     233
     234    // Read a bunch of data compressed with the Huffman algorithm
    248235    uint32_t UncompressHUFFMAN(char*       dest,
    249236                               const char* src,
     
    257244            return -1;
    258245        }
     246
    259247        vector<uint16_t> uncompressed;
    260248
     
    268256        {
    269257            Huffman::Decode(reinterpret_cast<const unsigned char*>(src), compressedSizes[j], uncompressed);
     258
    270259            memcpy(dest, uncompressed.data(), uncompressed.size()*sizeof(uint16_t));
     260
    271261            sizeWritten += uncompressed.size()*sizeof(uint16_t);
    272262            dest        += uncompressed.size()*sizeof(uint16_t);
     
    275265        return sizeWritten;
    276266    }
    277     /*
    278      *  Read a bunch of data compressed with the smoothman algorithm
    279      */
     267
     268    //Read a bunch of data compressed with the smoothman algorithm
    280269    uint32_t UncompressSMOOTHMAN(int16_t*   dest,
    281270                                 const char* src,
     
    285274    {
    286275        //call huffman transposed
    287         uint32_t sizeWritten = UncompressHUFFMAN(reinterpret_cast<char*>(dest), src, numRowElems, sizeOfElems, numRows);
     276        const uint32_t sizeWritten = UncompressHUFFMAN(reinterpret_cast<char*>(dest), src, numRowElems, sizeOfElems, numRows);
    288277
    289278        //un-do the integer smoothing
     
    293282        return sizeWritten;
    294283    }
    295     /*
    296      *  Data has been read from disk. Uncompress it !
    297      */
     284
     285    // Data has been read from disk. Uncompress it !
    298286    void UncompressBuffer()
    299287    {
    300         uint32_t offset            = 0;
    301         int64_t  compressedOffset  = 0;
    302288        const uint32_t catalogCurrentRow = fCurrentRow/fNumRowsPerTile;
    303289        const uint32_t thisRoundNumRows  = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
    304290
     291        uint32_t offset = 0;
     292        int64_t  compressedOffset  = 0;
     293 
    305294        //uncompress column by column
    306295        for (uint32_t i=0;i<fTable.sortedCols.size();i++)
    307296        {
    308             compressedOffset     = fCatalog[catalogCurrentRow][i].second - fCatalog[catalogCurrentRow][0].second;
    309             uint32_t compression = fTable.sortedCols[i].comp;
    310 
    311             if (fTable.sortedCols[i].num == 0) continue;
     297            compressedOffset = fCatalog[catalogCurrentRow][i].second - fCatalog[catalogCurrentRow][0].second;
     298
     299            if (fTable.sortedCols[i].num == 0)
     300                continue;
    312301
    313302            //get the compression flag
    314303            const char compressedFlag = fCompressedBuffer[compressedOffset++];
    315304
     305            //#define COMPRESSED_FLAG 0x1
     306            //#define UNCOMPRESSED_FLAG 0x0
     307
    316308            //if this bunch of data is not compressed, modify the compression flag
    317             if (compressedFlag == UNCOMPRESSED_FLAG)
     309            uint32_t compression = fTable.sortedCols[i].comp;
     310            if (compressedFlag == 0)
    318311                compression = UNCOMPRESSED;
    319312
     
    322315                case UNCOMPRESSED:
    323316                    offset += UncompressUNCOMPRESSED(fTransposedBuffer.data()+offset, fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num);
    324                 break;
     317                    break;
     318
    325319                case SMOOTHMAN:
    326320                    offset += UncompressSMOOTHMAN(reinterpret_cast<int16_t*>(fTransposedBuffer.data()+offset), fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num);
    327                 break;
     321                    break;
     322
    328323                default:
    329324                    ;
     
    338333#endif
    339334
    340 #endif /* ZFITS_H_ */
     335#endif
Note: See TracChangeset for help on using the changeset viewer.