Changeset 16416
- Timestamp:
- 05/28/13 23:28:18 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/zfits.h
r16285 r16416 6 6 */ 7 7 8 #ifndef ZFITS_H_ 9 #define ZFITS_H_ 8 #ifndef MARS_ZFITS 9 #define MARS_ZFITS 10 11 #include <stdexcept> 10 12 11 13 #include "fits.h" 12 13 #include <stdexcept>14 14 #include "huffman.h" 15 15 16 #define COMPRESSED_FLAG 0x117 #define UNCOMPRESSED_FLAG 0x018 16 19 17 #ifndef __MARS__ 20 18 namespace std 21 19 { 22 #else23 using namespace std;24 20 #endif 25 21 … … 28 24 public: 29 25 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) 44 36 { 45 37 InitCompressionReading(); 46 38 } 47 39 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) 63 50 { 64 51 InitCompressionReading(); 65 52 } 66 53 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) 84 58 return fits::SkipNextRow(); 59 60 fRow++; 61 return true; 85 62 } 86 63 87 64 private: 88 /* 89 * Do what it takes to initialize the compressed structured 90 */ 65 66 // Do what it takes to initialize the compressed structured 91 67 void InitCompressionReading() 92 68 { 93 69 //The constructor may have failed 94 if (!good()) return; 70 if (!good()) 71 return; 95 72 96 73 //Get compressed specific keywords … … 104 81 ReadCatalog(); 105 82 } 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 { 115 90 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 { 125 102 fits::MoveColumnDataToUserSpace(dest, src, c); 126 } 103 return; 104 } 105 106 memcpy(dest, src, c.num*c.size); 107 } 108 127 109 vector<char> fBuffer; ///<store the uncompressed rows 128 110 vector<char> fTransposedBuffer; ///<intermediate buffer to transpose the rows 129 111 vector<char> fCompressedBuffer; ///<compressed rows 112 130 113 size_t fNumTiles; ///< Total number of tiles 131 114 size_t fNumRowsPerTile; ///< Number of rows per compressed tile 132 size_t fHeapPtr; ///< offset from the beginning of the file of the binary data133 115 size_t fCurrentRow; ///< current row in memory. 134 116 117 streamoff fHeapOff; ///< offset from the beginning of the file of the binary data 118 135 119 vector<vector<pair<int64_t, int64_t> > > fCatalog;///< Catalog, i.e. the main table that points to the compressed data. 136 120 … … 141 125 142 126 fBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile); 127 143 128 fTransposedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile); 144 129 fCompressedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile + 1024*1024); //use a bit more memory, in case the compression algorithms uses more 145 130 } 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 149 133 void ReadCatalog() 150 134 { … … 162 146 //swap the bytes 163 147 int64_t tempValues[2] = {0,0}; 164 revcpy<8>( (char*)(&tempValues[0]), readBuf, 2);148 revcpy<8>(reinterpret_cast<char*>(tempValues[0]), readBuf, 2); 165 149 166 150 //add catalog entry 167 fCatalog[i]. push_back(make_pair(tempValues[0], tempValues[1]));151 fCatalog[i].emplace_back(tempValues[0], tempValues[1]); 168 152 } 169 153 170 154 //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 177 159 virtual bool ReadBinaryRow(uint64_t rowNum, char* bufferToRead) 178 160 { 179 if (rowNum >= GetNumRows()) return false; 161 if (rowNum >= GetNumRows()) 162 return false; 180 163 181 164 const int requestedTile = rowNum/fNumRowsPerTile; 182 165 const int currentTile = fCurrentRow/fNumRowsPerTile; 166 183 167 fCurrentRow = rowNum; 184 168 … … 188 172 //read yet another chunk from the file 189 173 //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 190 176 int64_t sizeToRead = 0; 191 const uint32_t currentCatRow = fCurrentRow/fNumRowsPerTile;192 193 177 for (uint32_t i=0;i<fCatalog[currentCatRow].size();i++) 194 178 sizeToRead += fCatalog[currentCatRow][i].first; 195 179 196 180 //skip to the beginning of the tile 197 seekg(f HeapPtr+fCatalog[currentCatRow][0].second);181 seekg(fCatalog[currentCatRow][0].second, ios_base::cur); 198 182 read(fCompressedBuffer.data(), sizeToRead); 199 183 … … 203 187 //copy the tile and transpose it 204 188 uint32_t offset=0; 189 205 190 const uint32_t thisRoundNumRows = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile; 206 191 for (uint32_t i=0;i<fTable.sortedCols.size();i++) … … 208 193 switch (fTable.sortedCols[i].comp) 209 194 { 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++) 212 208 for (uint32_t k=0;k<thisRoundNumRows;k++) 213 { //regular, "semi-transposed" copy214 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; 216 212 } 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 copy222 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 }225 213 break; 226 214 }; 227 215 } 228 216 } 217 229 218 //Data loaded and uncompressed. Copy it to destination 230 219 memcpy(bufferToRead, fBuffer.data()+fTable.bytes_per_row*(fCurrentRow%fNumRowsPerTile), fTable.bytes_per_row); 231 220 return good(); 232 221 } 233 /* 234 * Read a bunch of uncompressed data 235 */ 222 223 // Read a bunch of uncompressed data 236 224 uint32_t UncompressUNCOMPRESSED(char* dest, 237 225 const char* src, … … 243 231 return numRows*sizeOfElems*numRowElems; 244 232 } 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 248 235 uint32_t UncompressHUFFMAN(char* dest, 249 236 const char* src, … … 257 244 return -1; 258 245 } 246 259 247 vector<uint16_t> uncompressed; 260 248 … … 268 256 { 269 257 Huffman::Decode(reinterpret_cast<const unsigned char*>(src), compressedSizes[j], uncompressed); 258 270 259 memcpy(dest, uncompressed.data(), uncompressed.size()*sizeof(uint16_t)); 260 271 261 sizeWritten += uncompressed.size()*sizeof(uint16_t); 272 262 dest += uncompressed.size()*sizeof(uint16_t); … … 275 265 return sizeWritten; 276 266 } 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 280 269 uint32_t UncompressSMOOTHMAN(int16_t* dest, 281 270 const char* src, … … 285 274 { 286 275 //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); 288 277 289 278 //un-do the integer smoothing … … 293 282 return sizeWritten; 294 283 } 295 /* 296 * Data has been read from disk. Uncompress it ! 297 */ 284 285 // Data has been read from disk. Uncompress it ! 298 286 void UncompressBuffer() 299 287 { 300 uint32_t offset = 0;301 int64_t compressedOffset = 0;302 288 const uint32_t catalogCurrentRow = fCurrentRow/fNumRowsPerTile; 303 289 const uint32_t thisRoundNumRows = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile; 304 290 291 uint32_t offset = 0; 292 int64_t compressedOffset = 0; 293 305 294 //uncompress column by column 306 295 for (uint32_t i=0;i<fTable.sortedCols.size();i++) 307 296 { 308 compressedOffset 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; 312 301 313 302 //get the compression flag 314 303 const char compressedFlag = fCompressedBuffer[compressedOffset++]; 315 304 305 //#define COMPRESSED_FLAG 0x1 306 //#define UNCOMPRESSED_FLAG 0x0 307 316 308 //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) 318 311 compression = UNCOMPRESSED; 319 312 … … 322 315 case UNCOMPRESSED: 323 316 offset += UncompressUNCOMPRESSED(fTransposedBuffer.data()+offset, fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num); 324 break; 317 break; 318 325 319 case SMOOTHMAN: 326 320 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 328 323 default: 329 324 ; … … 338 333 #endif 339 334 340 #endif /* ZFITS_H_ */335 #endif
Note:
See TracChangeset
for help on using the changeset viewer.