27 #include "../common/PlatformUtils.h"
28 #include "../common/Translator.h"
29 #include "../raster/Band.h"
30 #include "../raster/BandProperty.h"
77 const unsigned int maxNumberRAMBlocks,
78 const std::vector< unsigned int>& numbersOfBlocksX,
79 const std::vector< unsigned int>& numbersOfBlocksY,
80 const std::vector< unsigned int>& blocksSizesBytes,
81 const unsigned long int maxDiskFilesSize )
83 if( numbersOfBlocksX.size() == 0 )
return false;
84 if( numbersOfBlocksX.size() != numbersOfBlocksY.size() )
return false;
85 if( numbersOfBlocksY.size() != blocksSizesBytes.size() )
return false;
95 unsigned int maxNumbersOfBlocksX = 0;
96 unsigned int maxNumbersOfBlocksY = 0;
97 unsigned int totalRequiredBlocks = 0;
100 for(
unsigned int blockBIdx = 0 ; blockBIdx < numbersOfBlocksX.size() ;
103 if( blocksSizesBytes[ blockBIdx ] == 0 )
return false;
108 if( numbersOfBlocksX[ blockBIdx ] == 0 )
return false;
110 if( maxNumbersOfBlocksX < numbersOfBlocksX[ blockBIdx ] )
111 maxNumbersOfBlocksX = numbersOfBlocksX[ blockBIdx ];
113 if( numbersOfBlocksY[ blockBIdx ] == 0 )
return false;
115 if( maxNumbersOfBlocksY < numbersOfBlocksY[ blockBIdx ] )
116 maxNumbersOfBlocksY = numbersOfBlocksY[ blockBIdx ];
118 totalRequiredBlocks += ( numbersOfBlocksX[ blockBIdx ] *
119 numbersOfBlocksY[ blockBIdx ] );
127 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
133 for(
unsigned int blockBIdx = 0 ; blockBIdx < numbersOfBlocksX.size() ;
138 for(
unsigned int blockYIdx = 0 ; blockYIdx < numbersOfBlocksY[ blockBIdx ] ;
143 for(
unsigned int blockXIdx = 0 ; blockXIdx < numbersOfBlocksX[ blockBIdx ] ; ++blockXIdx )
152 newBlockHandler.get();
158 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( blockBIdx,
159 blockYIdx, blockXIdx ) );
185 for(
unsigned int blockBIdx = 0 ; blockBIdx < numbersOfBlocksX.size() ;
190 for(
unsigned int blockYIdx = 0 ; blockYIdx < numbersOfBlocksY[ blockBIdx ] ;
229 unsigned int x,
unsigned int y )
256 assert( swapIndex.m_dim2Index <
m_activeDiskBlocksInfo[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ].size() );
258 swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ];
264 (
long)( inDiskInfo.
m_fileOff ), SEEK_SET ) )
266 throw Exception(
TR_MEMORY(
"File seek error") );
273 throw Exception(
TR_MEMORY(
"File read error") );
279 (
long)( inDiskInfo.
m_fileOff ), SEEK_SET ) )
281 throw Exception(
TR_MEMORY(
"File seek error") );
286 assert( swapIndex.m_dim2Index <
m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ].size() );
287 assert(
m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ] != 0 );
288 if( 1 != fwrite( (
void*)
m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ],
292 throw Exception(
TR_MEMORY(
"File write error") );
303 m_ramBlocksPointers[ swapIndex.m_dim0Index ][ swapIndex.m_dim1Index ][ swapIndex.m_dim2Index ] = 0;
305 swapIndex.m_dim0Index = band;
306 swapIndex.m_dim1Index = y;
307 swapIndex.m_dim2Index = x;
309 outDiskInfo = inDiskInfo;
324 const unsigned int& expansionSize,
const unsigned int& band,
325 std::vector< BlockIndex3D >& addedBlocksCoords )
330 addedBlocksCoords.clear();
336 const unsigned int numberOfBlocksX = (
unsigned int)
341 std::vector< BlockelementPtrT >( numberOfBlocksX, 0 ) );
345 std::vector< DiskBlockInfo >( numberOfBlocksX ) );
347 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
349 for(
unsigned int blockYIdx = 0 ; blockYIdx < expansionSize ; ++blockYIdx )
351 for(
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
360 newBlockHandler.get();
366 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
369 addedBlocksCoords.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
380 const unsigned int& expansionSize,
const unsigned int& band,
381 std::vector< BlockIndex3D >& addedBlocksCoords )
386 addedBlocksCoords.clear();
390 const unsigned int numberOfBlocksX = (
unsigned int)
395 std::vector< BlockelementPtrT >( numberOfBlocksX, 0 ) );
399 std::vector< DiskBlockInfo >( numberOfBlocksX ) );
401 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
407 for(
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
416 newBlockHandler.get();
422 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
425 addedBlocksCoords.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
436 const unsigned int& expansionSize,
const unsigned int& band,
437 std::vector< BlockIndex3D >& addedBlocksCoords )
442 addedBlocksCoords.clear();
448 const unsigned int numberOfBlocksY = (
unsigned int)
451 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
453 for(
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
463 for(
unsigned int blockXIdx = 0 ; blockXIdx < expansionSize ; ++blockXIdx )
472 newBlockHandler.get();
478 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
481 addedBlocksCoords.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
492 const unsigned int& expansionSize,
const unsigned int& band,
493 std::vector< BlockIndex3D >& addedBlocksCoords )
498 addedBlocksCoords.clear();
502 const unsigned int numberOfBlocksY = (
unsigned int)
505 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
507 for(
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
518 - expansionSize ; blockXIdx <
528 newBlockHandler.get();
534 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
537 addedBlocksCoords.push_back(
BlockIndex3D( band, blockYIdx, blockXIdx ) );
548 const unsigned int& expansionSize,
549 std::vector< BlockIndex3D >& addedBlocksCoords )
553 addedBlocksCoords.clear();
559 const unsigned int numberOfBlocksY = (
unsigned int)
561 const unsigned int numberOfBlocksX = (
unsigned int)
566 std::vector< std::vector< BlockelementPtrT > >( numberOfBlocksY ) );
570 std::vector< std::vector< DiskBlockInfo > >( numberOfBlocksY ) );
572 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
574 for(
unsigned int expansionIdx = 0 ; expansionIdx < expansionSize ;
577 for(
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
582 for(
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
591 newBlockHandler.get();
597 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) );
600 addedBlocksCoords.push_back(
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) );
612 std::vector< BlockIndex3D >& addedBlocksCoords )
616 addedBlocksCoords.clear();
620 const unsigned int numberOfBlocksY = (
unsigned int)
622 const unsigned int numberOfBlocksX = (
unsigned int)
627 std::vector< std::vector< BlockelementPtrT > >( numberOfBlocksY ) );
631 std::vector< std::vector< DiskBlockInfo > >( numberOfBlocksY ) );
633 std::vector< BlockIndex3D > diskBlocksToAllocateIndexes;
638 for(
unsigned int blockYIdx = 0 ; blockYIdx < numberOfBlocksY ; ++blockYIdx )
643 for(
unsigned int blockXIdx = 0 ; blockXIdx < numberOfBlocksX ; ++blockXIdx )
652 newBlockHandler.get();
658 diskBlocksToAllocateIndexes.push_back(
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) );
661 addedBlocksCoords.push_back(
BlockIndex3D( expansionIdx, blockYIdx, blockXIdx ) );
673 const unsigned int blocksNumber,
674 std::vector< DiskBlockInfo >& diskBlocksInfos,
681 diskBlocksInfos.clear();
682 diskFilesHandler.clear();
686 unsigned int remainnigBlocksNumber = blocksNumber;
688 while( remainnigBlocksNumber )
690 const unsigned int fileBlocksNumber = std::min( maxBlocksPerFile, remainnigBlocksNumber );
691 const unsigned long int fileSizeBytes = ((
unsigned long int)fileBlocksNumber) *
700 diskFilesHandler.push_back( newFileHandlerPtr );
702 for(
unsigned int fileBlockIdx = 0 ; fileBlockIdx < fileBlocksNumber ;
706 diskBlocksInfos.back().m_filePtr = newFileHandlerPtr->m_filePtr;
707 diskBlocksInfos.back().m_fileOff = ((
unsigned long int)fileBlockIdx) *
708 ((
unsigned long int)m_maxBlockSizeBytes);
711 remainnigBlocksNumber -= fileBlocksNumber;
718 const std::vector< BlockIndex3D >& blocksIndxes )
720 if( ! blocksIndxes.empty() )
724 std::vector< DiskBlockInfo > newDiskBlocksInfos;
727 newDiskFilesHandler ) )
729 assert( newDiskBlocksInfos.size() == blocksIndxes.size() );
732 newDiskFilesHandler.begin(), newDiskFilesHandler.end() );
734 unsigned int blocksIndxesIdx = 0;
735 while( blocksIndxesIdx < blocksIndxes.size() )
737 const BlockIndex3D& bIdx = blocksIndxes[ blocksIndxesIdx ];
744 assert( blocksIndxesIdx < newDiskBlocksInfos.size() );
746 bIdx.
m_dim2Index ] = newDiskBlocksInfos[ blocksIndxesIdx ];
758 FILE** fileptr )
const
761 (*fileptr) = tmpfile();
762 if( (*fileptr) == 0 )
765 long seekoff = (long)( size - 1 );
767 if( 0 != fseek( (*fileptr), seekoff, SEEK_SET ) )
769 fclose( (*fileptr) );
773 unsigned char c =
'\0';
774 if( 1 != fwrite( &c, 1, 1, (*fileptr) ) )
776 fclose( (*fileptr) );
SwapFifoT::size_type m_nextFIFOPositionOverSwapFifo
The next position where a block swap will occur over m_swapFifo;.
std::list< OpenDiskFileHandlerPtrT > OpenDiskFilesHandlerT
Open dis files handler type.
bool createNewDiskFile(unsigned long int size, FILE **fileptr) const
Create a new disk file.
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).
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.
bool allocateAndActivateDiskBlocks(const std::vector< BlockIndex3D > &blocksIndxes)
Allocate and activate disk blocks.
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.
#define TR_MEMORY(message)
An exception class for the TerraLib In-Memory Data Access driver.
~ExpansibleBandBlocksManager()
void initState()
Initialize this instance to an initial state.
void free()
Free all allocated internal resources and go back to the initial state.
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_dim0Index
Block Z (band) index.
unsigned int m_maxNumberRAMBlocks
The maximum number of RAM blocks;.
void * getBlockPointer(unsigned int band, unsigned int x, unsigned int y)
Returns a pointer to the required data block.
BlockElementT * BlockelementPtrT
Block element pointer type.
boost::shared_array< BlockElementT > RAMBlockHandlerT
RAM Block handler type;.
ExpansibleBandBlocksManager()
unsigned long int m_fileOff
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.
bool m_isInitialized
Is this instance initialized ?
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.
bool allocateDiskBlocks(const unsigned int blocksNumber, std::vector< DiskBlockInfo > &diskBlocksInfos, OpenDiskFilesHandlerT &diskFilesHandler) const
Allocate disk blocks.
OpenDiskFilesHandlerT m_diskFilesHandler
The disk files handler;.
RAMBlocksPointersContainerT m_ramBlocksPointers
3D Matrix of active RAM blocks pointers indexed in the form [band][blockYIndex][blockXIndex].
RAMBlocksHandlerT m_activeRAMBlocksHandler
The active RAM blocks handler.
CoordDataType m_dim2Index
Block X index.
unsigned long int m_maxBlockSizeBytes
The maximum global used block size in bytes.
RAM cached and tiled raster band blocks manager.
unsigned char * m_currSwapBlockPtr
A pointer to the current block where disk data swap will be done.
RAMBlockHandlerT m_swapBlockHandler
An extra block for disk swap purposes.
unsigned long int m_maxDiskFilesSize
The maximum temporary disk file size (bytes).
unsigned char * m_getBlockPointer_returnValue
unsigned char BlockElementT
Block element type.
ActiveDiskBlocksInfoT m_activeDiskBlocksInfo
3D Matrix of active disk block info indexed as [band][blockYIndex][blockXIndex].
CoordDataType m_dim1Index
Block Y index.
SwapFifoT m_swapFifo
Disk swapping FIFO.
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).
void shiftDim03DCoords(ContainerType &inputContainer, const unsigned int dim0index, const int dim1Shift, const int dim2Shift) const
Shift coords given a fixed dimention 0 index.
void shift3DCoords(ContainerType &inputContainer, const int &dim0Shift, const int &dim1Shift, const int &dim2Shift) const
Shift 3D coords.