27 #include "../common/PlatformUtils.h" 
   28 #include "../common/Translator.h" 
   29 #include "../raster/Band.h" 
   30 #include "../raster/BandProperty.h" 
   40 #include <boost/filesystem.hpp> 
   53     remove( m_fullFileName.c_str() );
 
   82   const unsigned int maxNumberRAMBlocks,
 
   83   const std::vector< unsigned int>& numbersOfBlocksX,
 
   84   const std::vector< unsigned int>& numbersOfBlocksY,
 
   85   const std::vector< unsigned int>& blocksSizesBytes,
 
   86   const unsigned long int maxDiskFilesSize )
 
   88   if( numbersOfBlocksX.size() == 0 ) 
return false;
 
   89   if( numbersOfBlocksX.size() != numbersOfBlocksY.size() ) 
return false;
 
   90   if( numbersOfBlocksY.size() != blocksSizesBytes.size() ) 
return false;
 
  100   unsigned int maxNumbersOfBlocksX = 0;
 
  101   unsigned int maxNumbersOfBlocksY = 0;
 
  102   unsigned int totalRequiredBlocks = 0;
 
  105     for( 
unsigned int blockBIdx = 0 ; blockBIdx < numbersOfBlocksX.size() ;  
 
  108       if( blocksSizesBytes[ blockBIdx ] == 0 ) 
return false;
 
  113       if( numbersOfBlocksX[ blockBIdx ] == 0 ) 
return false;
 
  115       if( maxNumbersOfBlocksX < numbersOfBlocksX[ blockBIdx ] )
 
  116         maxNumbersOfBlocksX = numbersOfBlocksX[ blockBIdx ];
 
  118       if( numbersOfBlocksY[ blockBIdx ] == 0 ) 
return false;
 
  120       if( maxNumbersOfBlocksY < numbersOfBlocksY[ blockBIdx ] )
 
  121         maxNumbersOfBlocksY = numbersOfBlocksY[ blockBIdx ];  
 
  123       totalRequiredBlocks += ( numbersOfBlocksX[ blockBIdx ] *
 
  124         numbersOfBlocksY[ blockBIdx ] );
 
  132   std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  138     for( 
unsigned int blockBIdx = 0 ; blockBIdx < numbersOfBlocksX.size() ;  
 
  143       for( 
unsigned int blockYIdx = 0 ; blockYIdx < numbersOfBlocksY[ blockBIdx ] ; 
 
  148         for( 
unsigned int blockXIdx = 0 ; blockXIdx < numbersOfBlocksX[ blockBIdx ] ; ++blockXIdx )
 
  157               newBlockHandler.get();
 
  163             diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( blockBIdx,
 
  164               blockYIdx, blockXIdx ) );
 
  190     for( 
unsigned int blockBIdx = 0 ; blockBIdx < numbersOfBlocksX.size() ;  
 
  195       for( 
unsigned int blockYIdx = 0 ; blockYIdx < numbersOfBlocksY[ blockBIdx ] ; 
 
  234   unsigned int x, 
unsigned int y )
 
  261     assert( swapIndex.m_dim2Index <  
m_activeDiskBlocksInfo[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ].size() );    
 
  263       swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ]; 
 
  269       (
long)( inDiskInfo.
m_fileOff ), SEEK_SET ) )
 
  271       throw Exception(
TE_TR(
"File seek error") );
 
  278       throw Exception(
TE_TR(
"File read error") );
 
  284       (
long)( inDiskInfo.
m_fileOff ), SEEK_SET ) )
 
  286       throw Exception(
TE_TR(
"File seek error") );
 
  291     assert( swapIndex.m_dim2Index <  
m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ].size() );      
 
  292     assert( 
m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ] != 0 );
 
  293     if( 1 != fwrite( (
void*)
m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ], 
 
  297       throw Exception(
TE_TR(
"File write error") );
 
  308     m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ] = 0;
 
  310     swapIndex.m_dim0Index = band;
 
  311     swapIndex.m_dim1Index = y;
 
  312     swapIndex.m_dim2Index = x;
 
  314     outDiskInfo = inDiskInfo;
 
  329   const unsigned int& expansionSize, 
const unsigned int& band,
 
  330   std::vector< BlockIndex3D >& addedBlocksCoords )
 
  335   addedBlocksCoords.clear();
 
  341     const unsigned int numberOfBlocksX = (
unsigned int)
 
  346       std::vector< BlockelementPtrT >( numberOfBlocksX, 0 ) );
 
  350       std::vector< DiskBlockInfo >( numberOfBlocksX ) );   
 
  352     std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  354     for( 
unsigned int blockYIdx = 0 ; blockYIdx < expansionSize ; ++blockYIdx )
 
  356       for( 
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
 
  365             newBlockHandler.get();
 
  371           diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) );
 
  374         addedBlocksCoords.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) );
 
  385   const unsigned int& expansionSize, 
const unsigned int& band,
 
  386   std::vector< BlockIndex3D >& addedBlocksCoords )
 
  391   addedBlocksCoords.clear();
 
  395     const unsigned int numberOfBlocksX = (
unsigned int)
 
  400       std::vector< BlockelementPtrT >( numberOfBlocksX, 0 ) );
 
  404       std::vector< DiskBlockInfo >( numberOfBlocksX ) );    
 
  406     std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  412       for( 
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
 
  421             newBlockHandler.get();
 
  427           diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) ); 
 
  430         addedBlocksCoords.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) );
 
  441   const unsigned int& expansionSize, 
const unsigned int& band,
 
  442   std::vector< BlockIndex3D >& addedBlocksCoords )
 
  447   addedBlocksCoords.clear();
 
  453     const unsigned int numberOfBlocksY = (
unsigned int)
 
  456     std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  458     for( 
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
 
  468       for( 
unsigned int blockXIdx = 0 ; blockXIdx < expansionSize ; ++blockXIdx )
 
  477             newBlockHandler.get();
 
  483           diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) ); 
 
  486         addedBlocksCoords.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) );
 
  497   const unsigned int& expansionSize, 
const unsigned int& band,
 
  498   std::vector< BlockIndex3D >& addedBlocksCoords )
 
  503   addedBlocksCoords.clear();
 
  507     const unsigned int numberOfBlocksY = (
unsigned int)
 
  510     std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  512     for( 
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
 
  523         - expansionSize ; blockXIdx < 
 
  533             newBlockHandler.get();
 
  539           diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) ); 
 
  542         addedBlocksCoords.push_back( 
BlockIndex3D( band, blockYIdx, blockXIdx ) );
 
  553   const unsigned int& expansionSize,
 
  554   std::vector< BlockIndex3D >& addedBlocksCoords )
 
  558   addedBlocksCoords.clear();
 
  564     const unsigned int numberOfBlocksY = (
unsigned int)
 
  566     const unsigned int numberOfBlocksX = (
unsigned int)
 
  571       std::vector< std::vector< BlockelementPtrT > >( numberOfBlocksY ) );
 
  575       std::vector< std::vector< DiskBlockInfo > >( numberOfBlocksY ) );  
 
  577     std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  579     for( 
unsigned int expansionIdx = 0 ; expansionIdx < expansionSize ;
 
  582       for( 
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
 
  587         for( 
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
 
  596               newBlockHandler.get();
 
  602             diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) ); 
 
  605           addedBlocksCoords.push_back( 
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) ); 
 
  617   std::vector< BlockIndex3D >& addedBlocksCoords )
 
  621   addedBlocksCoords.clear();
 
  625     const unsigned int numberOfBlocksY = (
unsigned int)
 
  627     const unsigned int numberOfBlocksX = (
unsigned int)
 
  632       std::vector< std::vector< BlockelementPtrT > >( numberOfBlocksY ) );
 
  636       std::vector< std::vector< DiskBlockInfo > >( numberOfBlocksY ) );    
 
  638     std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
 
  643       for( 
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
 
  648         for( 
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
 
  657               newBlockHandler.get();
 
  663             diskBlocksToAllocateIndexes.push_back( 
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) ); 
 
  666           addedBlocksCoords.push_back( 
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) ); 
 
  678   const unsigned int blocksNumber,
 
  679   std::vector< DiskBlockInfo >& diskBlocksInfos,                       
 
  686   diskBlocksInfos.clear();
 
  687   diskFilesHandler.clear();
 
  691   unsigned int remainnigBlocksNumber = blocksNumber;
 
  693   while( remainnigBlocksNumber )
 
  695     const unsigned int fileBlocksNumber = std::min( maxBlocksPerFile, remainnigBlocksNumber );
 
  696     const unsigned long int fileSizeBytes = ((
unsigned long int)fileBlocksNumber) *
 
  702       newFileHandlerPtr->m_fullFileName ) )
 
  706     diskFilesHandler.push_back( newFileHandlerPtr );
 
  708     for( 
unsigned int fileBlockIdx = 0 ; fileBlockIdx < fileBlocksNumber ;
 
  712       diskBlocksInfos.back().m_filePtr = newFileHandlerPtr->m_filePtr;
 
  713       diskBlocksInfos.back().m_fileOff = ((
unsigned long int)fileBlockIdx) * 
 
  714         ((
unsigned long int)m_maxBlockSizeBytes);
 
  717     remainnigBlocksNumber -= fileBlocksNumber;
 
  724   const std::vector< BlockIndex3D >& blocksIndxes )
 
  726   if( ! blocksIndxes.empty() )
 
  730     std::vector< DiskBlockInfo > newDiskBlocksInfos;
 
  733       newDiskFilesHandler ) )
 
  735     assert( newDiskBlocksInfos.size() == blocksIndxes.size() );
 
  738       newDiskFilesHandler.begin(), newDiskFilesHandler.end() );
 
  740     unsigned int blocksIndxesIdx = 0;
 
  741     while( blocksIndxesIdx < blocksIndxes.size() )
 
  743       const BlockIndex3D& bIdx = blocksIndxes[ blocksIndxesIdx ];
 
  750       assert( blocksIndxesIdx < newDiskBlocksInfos.size() );
 
  752         bIdx.
m_dim2Index ] = newDiskBlocksInfos[ blocksIndxesIdx ];        
 
  764   FILE** fileptr, std::string& fullFileName )
 const 
  766   fullFileName = boost::filesystem::unique_path( 
 
  767     boost::filesystem::temp_directory_path() /= 
 
  768     boost::filesystem::path( 
"TerralibExpansibleBandBlocksManager_%%%%-%%%%-%%%%-%%%%" ) ).string();
 
  769   if( fullFileName.empty() )
 
  774   (*fileptr) = fopen( fullFileName.c_str(),  
"wb+"  );
 
  775   if( (*fileptr) == 0 )
 
  780   long seekoff = (long)( size - 1 );
 
  782   if( 0 != std::fseek( (*fileptr), seekoff, SEEK_SET ) )
 
  784     fclose( (*fileptr) );
 
  785     remove( fullFileName.c_str() );
 
  789   unsigned char c = 
'\0';
 
  790   if( 1 != std::fwrite( &c, 1, 1, (*fileptr) ) )
 
  792     fclose( (*fileptr) );
 
  793     remove( fullFileName.c_str() );
 
unsigned char * m_currSwapBlockPtr
A pointer to the current block where disk data swap will be done. 
 
unsigned char * m_getBlockPointer_returnValue
 
RAMBlocksPointersContainerT m_ramBlocksPointers
3D Matrix of active RAM blocks pointers indexed in the form [band][blockYIndex][blockXIndex]. 
 
bool addTopBands(const unsigned int &expansionSize, std::vector< BlockIndex3D > &addedBlocksCoords)
New bands will be added at the top of the raster (before the first band). 
 
void shift3DCoords(ContainerType &inputContainer, const int &dim0Shift, const int &dim1Shift, const int &dim2Shift) const 
Shift 3D coords. 
 
std::list< OpenDiskFileHandlerPtrT > OpenDiskFilesHandlerT
Open dis files handler type. 
 
bool addBottomBlocks(const unsigned int &expansionSize, const unsigned int &band, std::vector< BlockIndex3D > &addedBlocksCoords)
New blocks will be added at the bottom of the raster. 
 
void * getBlockPointer(unsigned int band, unsigned int x, unsigned int y)
Returns a pointer to the required data block. 
 
bool initialize(const unsigned int maxNumberRAMBlocks, const std::vector< unsigned int > &numbersOfBlocksX, const std::vector< unsigned int > &numbersOfBlocksY, const std::vector< unsigned int > &blocksSizesBytes, const unsigned long int maxDiskFilesSize)
Initialize this instance to an initial state. 
 
ActiveDiskBlocksInfoT m_activeDiskBlocksInfo
3D Matrix of active disk block info indexed as [band][blockYIndex][blockXIndex]. 
 
#define TE_TR(message)
It marks a string in order to get translated. 
 
An exception class for the TerraLib In-Memory Data Access driver. 
 
ExpansibleBandBlocksManager()
 
unsigned long int m_maxBlockSizeBytes
The maximum global used block size in bytes. 
 
RAMBlocksHandlerT m_activeRAMBlocksHandler
The active RAM blocks handler. 
 
void shiftDim03DCoords(ContainerType &inputContainer, const unsigned int dim0index, const int dim1Shift, const int dim2Shift) const 
Shift coords given a fixed dimention 0 index. 
 
bool addBottomBands(const unsigned int &expansionSize, std::vector< BlockIndex3D > &addedBlocksCoords)
New bands will be added at the bottom of the raster (after de the last band). 
 
CoordDataType m_dim0Index
Block Z (band) index. 
 
bool addRightBlocks(const unsigned int &expansionSize, const unsigned int &band, std::vector< BlockIndex3D > &addedBlocksCoords)
New blocks will be added at the right of the raster. 
 
CoordDataType m_dim2Index
Block X index. 
 
bool allocateAndActivateDiskBlocks(const std::vector< BlockIndex3D > &blocksIndxes)
Allocate and activate disk blocks. 
 
unsigned long int m_maxDiskFilesSize
The maximum temporary disk file size (bytes). 
 
RAM cached and tiled raster band blocks manager. 
 
unsigned long int m_fileOff
 
SwapFifoT::size_type m_nextFIFOPositionOverSwapFifo
The next position where a block swap will occur over m_swapFifo;. 
 
~ExpansibleBandBlocksManager()
 
BlockElementT * BlockelementPtrT
Block element pointer type. 
 
bool createNewDiskFile(unsigned long int size, FILE **fileptr, std::string &fullFileName) const 
Create a new disk file. 
 
bool addTopBlocks(const unsigned int &expansionSize, const unsigned int &band, std::vector< BlockIndex3D > &addedBlocksCoords)
New blocks will be added at the top of the raster. 
 
void initState()
Initialize this instance to an initial state. 
 
boost::shared_array< BlockElementT > RAMBlockHandlerT
RAM Block handler type;. 
 
bool allocateDiskBlocks(const unsigned int blocksNumber, std::vector< DiskBlockInfo > &diskBlocksInfos, OpenDiskFilesHandlerT &diskFilesHandler) const 
Allocate disk blocks. 
 
bool m_isInitialized
Is this instance initialized ? 
 
OpenDiskFilesHandlerT m_diskFilesHandler
The disk files handler;. 
 
unsigned int m_maxNumberRAMBlocks
The maximum number of RAM blocks;. 
 
CoordDataType m_dim1Index
Block Y index. 
 
void free()
Free all allocated internal resources and go back to the initial state. 
 
bool addLeftBlocks(const unsigned int &expansionSize, const unsigned int &band, std::vector< BlockIndex3D > &addedBlocksCoords)
New blocks will be added at the left of the raster. 
 
SwapFifoT m_swapFifo
Disk swapping FIFO. 
 
unsigned char BlockElementT
Block element type. 
 
RAMBlockHandlerT m_swapBlockHandler
An extra block for disk swap purposes.