All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExpansibleBandBlocksManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/memory/ExpansibleBandBlocksManager.h
22 
23  \brief RAM cached and tiled raster band blocks manager.
24 */
25 
26 #ifndef __TERRALIB_MEMORY_INTERNAL_EXPANSIBLEBANDBLOCKSMANAGER_H
27 #define __TERRALIB_MEMORY_INTERNAL_EXPANSIBLEBANDBLOCKSMANAGER_H
28 
29 // TerraLib
30 #include "../raster/Raster.h"
31 #include "Config.h"
32 
33 // STL
34 #include <cstdio>
35 #include <list>
36 #include <vector>
37 
38 // Boost
39 #include <boost/noncopyable.hpp>
40 #include <boost/shared_ptr.hpp>
41 #include <boost/shared_array.hpp>
42 
43 namespace te
44 {
45  namespace mem
46  {
47  /*!
48  \class ExpansibleBandBlocksManager
49 
50  \brief RAM cached and tiled raster band blocks manager.
51  */
52  class TEMEMORYEXPORT ExpansibleBandBlocksManager : private boost::noncopyable
53  {
54  public:
55 
56  /*!
57  \brief 3D Block index.
58  */
60  {
61  public :
62 
63  typedef unsigned int CoordDataType; //!< Coords data type.
64 
65  CoordDataType m_dim0Index; //!< Block Z (band) index.
66 
67  CoordDataType m_dim1Index; //!< Block Y index.
68 
69  CoordDataType m_dim2Index; //!< Block X index.
70 
71  BlockIndex3D() : m_dim0Index( 0 ), m_dim1Index( 0 ), m_dim2Index( 0 ) {};
72 
73  BlockIndex3D( const CoordDataType& dim0Index, const CoordDataType& dim1Index,
74  const CoordDataType& dim2Index ) : m_dim0Index( dim0Index ),
75  m_dim1Index( dim1Index ), m_dim2Index( dim2Index ) {};
76 
78  };
79 
81 
83 
84  /*!
85  \brief Initialize this instance to an initial state.
86 
87  \param maxNumberRAMBlocks The maximum number of RAM blocks.
88 
89  \param numbersOfBlocksX The number of blocks along the X directon (for each band).
90 
91  \param numbersOfBlocksY The number of blocks along the Y directon (for each band).
92 
93  \param blocksSizesBytes The blocks sizes in bytes (for each band).
94 
95  \param maxDiskFilesSize The maximum temporary disk file size (if required).
96 
97  \return true if OK, false on errors.
98  */
99  bool initialize( const unsigned int maxNumberRAMBlocks,
100  const std::vector< unsigned int>& numbersOfBlocksX,
101  const std::vector< unsigned int>& numbersOfBlocksY,
102  const std::vector< unsigned int>& blocksSizesBytes,
103  const unsigned long int maxDiskFilesSize );
104 
105  /*!
106  \brief Returns true if this instance is initialized.
107 
108  \return true if this instance is initialized.
109  */
110  bool isInitialized() const
111  {
112  return m_isInitialized;
113  };
114 
115  /*!
116  \brief Returns the number of blocks along the X directon for the required band.
117 
118  \param band The required band.
119 
120  \return Returns the number of blocks along the X directon for the required band.
121  */
122  unsigned int getNumberOfBlocksX( const unsigned int band ) const
123  {
124  assert( m_ramBlocksPointers.size() > band );
125  assert( m_ramBlocksPointers[ band ].size() > 0 );
126  return (unsigned int)m_ramBlocksPointers[ band ][ 0 ].size();
127  };
128 
129  /*!
130  \brief Returns the number of blocks along the Y directon for the required band.
131 
132  \param band The required band.
133 
134  \return Returns the number of blocks along the Y directon for the required band.
135  */
136  unsigned int getNumberOfBlocksY(
137  const unsigned int band ) const
138  {
139  assert( m_ramBlocksPointers.size() > band );
140  return (unsigned int)m_ramBlocksPointers[ band ].size();
141  };
142 
143  /*!
144  \brief Returns the number of bands.
145 
146  \return Returns the number of bands.
147  */
148  unsigned int getNumberOfBands() const
149  {
150  return (unsigned int)m_ramBlocksPointers.size();
151  };
152 
153  /*!
154  \brief Returns the internal size( bytes ) used for all internal blocks.
155 
156  \return Returns the internal size( bytes ) used for all internal blocks
157  */
158  unsigned long int getBlockSizeBytes()
159  {
160  return m_maxBlockSizeBytes;
161  };
162 
163  /*! \brief Free all allocated internal resources and go back to the initial state. */
164  void free();
165 
166  /*!
167  \brief Returns a pointer to the required data block.
168 
169  \param band The band index.
170  \param x The block-id in x (or x-offset).
171  \param y The block-id in y (or y-offset).
172 
173  \return Pointer to the required data block.
174  */
175  void* getBlockPointer(unsigned int band, unsigned int x, unsigned int y );
176 
177  /*! \brief The maximum number of cache blocks. */
178  unsigned int getMaxNumberOfRAMBlocks() const
179  {
180  return m_maxNumberRAMBlocks;
181  };
182 
183  /*!
184  \brief New blocks will be added at the top of the raster.
185 
186  \param expansionSize The expansion size over the Y direction.
187 
188  \param band The band where the operation will be performed.
189 
190  \param addedBlocksCoords The added blocks coords.
191 
192  \return true if OK, false on errors.
193  */
194  bool addTopBlocks( const unsigned int& expansionSize,
195  const unsigned int& band,
196  std::vector< BlockIndex3D >& addedBlocksCoords );
197 
198  /*!
199  \brief New blocks will be added at the bottom of the raster.
200 
201  \param expansionSize The expansion size over the Y direction.
202 
203  \param band The band where the operation will be performed.
204 
205  \param addedBlocksCoords The added blocks coords.
206 
207  \return true if OK, false on errors.
208  */
209  bool addBottomBlocks( const unsigned int& expansionSize,
210  const unsigned int& band,
211  std::vector< BlockIndex3D >& addedBlocksCoords );
212 
213  /*!
214  \brief New blocks will be added at the left of the raster.
215 
216  \param expansionSize The expansion size over the X direction.
217  \param band The band where the operation will be performed.
218  \param addedBlocksCoords The added blocks coords.
219 
220  \return true if OK, false on errors.
221  */
222  bool addLeftBlocks( const unsigned int& expansionSize,
223  const unsigned int& band,
224  std::vector< BlockIndex3D >& addedBlocksCoords );
225 
226  /*!
227  \brief New blocks will be added at the right of the raster.
228 
229  \param expansionSize The expansion size over the X direction.
230  \param band The band where the operation will be performed.
231  \param addedBlocksCoords The added blocks coords.
232 
233  \return true if OK, false on errors.
234  */
235  bool addRightBlocks( const unsigned int& expansionSize, const unsigned int& band,
236  std::vector< BlockIndex3D >& addedBlocksCoords );
237 
238  /*!
239  \brief New bands will be added at the top of the raster (before the first band).
240 
241  \param expansionSize The number of bands to add.
242 
243  \param addedBlocksCoords The added blocks coords.
244 
245  \return true if OK, false on errors.
246  */
247  bool addTopBands( const unsigned int& expansionSize,
248  std::vector< BlockIndex3D >& addedBlocksCoords );
249 
250  /*!
251  \brief New bands will be added at the bottom of the raster (after de the last band).
252 
253  \param expansionSize The number of bands to add.
254  \param addedBlocksCoords The added blocks coords.
255 
256  \return true if OK, false on errors.
257  */
258  bool addBottomBands( const unsigned int& expansionSize,
259  std::vector< BlockIndex3D >& addedBlocksCoords );
260 
261  protected :
262 
263  /*!
264  \brief Disk block info.
265  */
267  {
268  public :
269 
270  FILE* m_filePtr;
271  unsigned long int m_fileOff;
272 
273  DiskBlockInfo() : m_filePtr( 0 ), m_fileOff( 0 ) {};
274 
276  };
277 
278  /*!
279  \brief Open disk file handler.
280  */
281  class OpenDiskFileHandler : private boost::noncopyable
282  {
283  public :
284 
285  FILE* m_filePtr;
286 
288 
290 
291  };
292 
293  typedef boost::shared_ptr< OpenDiskFileHandler > OpenDiskFileHandlerPtrT; //!< Open disk file pointer type.
294 
295  typedef std::list< OpenDiskFileHandlerPtrT > OpenDiskFilesHandlerT; //!< Open dis files handler type.
296 
297 
298  /*!
299  \brief Initialize this instance to an initial state.
300  */
301  void initState();
302 
303  /*!
304  \brief Allocate disk blocks.
305 
306  \param blocksNumber The number of blocks to allocate.
307 
308  \param diskBlocksInfos The info for the allocated blocks.
309 
310  \param diskFilesHandler The file handlers for all the allocated blocks.
311 
312  \return true if OK. false on errors.
313  */
314  bool allocateDiskBlocks( const unsigned int blocksNumber,
315  std::vector< DiskBlockInfo >& diskBlocksInfos,
316  OpenDiskFilesHandlerT& diskFilesHandler ) const;
317 
318  /*!
319  \brief Allocate and activate disk blocks.
320 
321  \param blocksIndxes The blocks indexes inside the internal 3D indexing matrices.
322 
323  \return true if OK. false on errors.
324  */
325  bool allocateAndActivateDiskBlocks( const std::vector< BlockIndex3D >& blocksIndxes );
326 
327  /*!
328  \brief Create a new disk file.
329 
330  \param size The file size.
331 
332  \param fileptr The file pointer.
333 
334  \return true if OK. false on errors.
335  */
336  bool createNewDiskFile( unsigned long int size, FILE** fileptr ) const;
337 
338  /*!
339  \brief Shift coords given a fixed dimention 0 index.
340 
341  \param inputContainer The input coords container.
342 
343  \param dim0index The dimension 0 fixed index.
344 
345  \param dim1Shift The shift to be done over the dimension 1.
346 
347  \param dim2Shift The shift to be done over the dimension 2.
348  */
349  template< typename ContainerType >
351  ContainerType& inputContainer,
352  const unsigned int dim0index, const int dim1Shift, const int dim2Shift ) const
353  {
354  typename ContainerType::iterator it = inputContainer.begin();
355  const typename ContainerType::iterator itE = inputContainer.end();
356  BlockIndex3D newCoords;
357 
358  while( it != itE )
359  {
360  if( it->m_dim0Index == dim0index )
361  {
362  assert( ( dim1Shift < 0 ) ? ( ((int)it->m_dim1Index) > dim1Shift ) : true );
363  it->m_dim1Index = (BlockIndex3D::CoordDataType)
364  ( ((int)it->m_dim1Index) + dim1Shift );
365 
366  assert( ( dim2Shift < 0 ) ? ( ((int)it->m_dim2Index) > dim2Shift ) : true );
367  it->m_dim2Index = (BlockIndex3D::CoordDataType)
368  ( ((int)it->m_dim2Index) + dim2Shift );
369  }
370 
371  ++it;
372  }
373  };
374 
375  /*!
376  \brief Shift 3D coords.
377 
378  \param inputContainer The input coords container.
379 
380  \param dim0Shift The shift to be done over the dimension 0.
381 
382  \param dim1Shift The shift to be done over the dimension 1.
383 
384  \param dim2Shift The shift to be done over the dimension 2.
385  */
386  template< typename ContainerType >
388  ContainerType& inputContainer,
389  const int& dim0Shift, const int& dim1Shift, const int& dim2Shift ) const
390  {
391  typename ContainerType::iterator it = inputContainer.begin();
392  const typename ContainerType::iterator itE = inputContainer.end();
393  BlockIndex3D newCoords;
394 
395  while( it != itE )
396  {
397  assert( ( dim0Shift < 0 ) ? ( ((int)it->m_dim0Index) > dim0Shift ) : true );
398  it->m_dim0Index = (BlockIndex3D::CoordDataType)
399  ( ((int)it->m_dim0Index) + dim0Shift );
400 
401  assert( ( dim1Shift < 0 ) ? ( ((int)it->m_dim1Index) > dim1Shift ) : true );
402  it->m_dim1Index = (BlockIndex3D::CoordDataType)
403  ( ((int)it->m_dim1Index) + dim1Shift );
404 
405  assert( ( dim2Shift < 0 ) ? ( ((int)it->m_dim2Index) > dim2Shift ) : true );
406  it->m_dim2Index = (BlockIndex3D::CoordDataType)
407  ( ((int)it->m_dim2Index) + dim2Shift );
408 
409  ++it;
410  }
411  };
412 
413  protected:
414 
415  typedef unsigned char BlockElementT; //!< Block element type.
416 
417  typedef BlockElementT* BlockelementPtrT; //!< Block element pointer type.
418 
419  typedef boost::shared_array< BlockElementT > RAMBlockHandlerT; //!< RAM Block handler type;
420 
421  typedef std::vector< std::vector< std::vector< BlockelementPtrT > > > RAMBlocksPointersContainerT; //!< RAM blocks pointers container type.
422 
423  typedef std::list< RAMBlockHandlerT > RAMBlocksHandlerT; //!< Blocks handler type;
424 
425  typedef std::vector< std::vector< std::vector< DiskBlockInfo > > > ActiveDiskBlocksInfoT; //!< Active disk blocks info type;
426 
427  typedef std::list< DiskBlockInfo > InactiveDiskBlocksInfoT; //!< Inactive disk blocks info type.
428 
429  typedef std::vector< BlockIndex3D > SwapFifoT; //!< Swap fifo type.
430 
431  bool m_isInitialized; //!< Is this instance initialized ?
432 
433  unsigned int m_maxNumberRAMBlocks; //!< The maximum number of RAM blocks;
434 
435  unsigned long int m_maxDiskFilesSize; //!< The maximum temporary disk file size (bytes).
436 
437  unsigned long int m_maxBlockSizeBytes; //!< The maximum global used block size in bytes.
438 
439  unsigned char* m_currSwapBlockPtr; //!< A pointer to the current block where disk data swap will be done.
440 
441  SwapFifoT::size_type m_nextFIFOPositionOverSwapFifo; //!< The next position where a block swap will occur over m_swapFifo;
442 
443  RAMBlocksHandlerT m_activeRAMBlocksHandler; //!< The active RAM blocks handler.
444 
445  RAMBlocksPointersContainerT m_ramBlocksPointers; //!< 3D Matrix of active RAM blocks pointers indexed in the form [band][blockYIndex][blockXIndex].
446 
447  SwapFifoT m_swapFifo; //!< Disk swapping FIFO.
448 
449  ActiveDiskBlocksInfoT m_activeDiskBlocksInfo; //!< 3D Matrix of active disk block info indexed as [band][blockYIndex][blockXIndex].
450 
451  OpenDiskFilesHandlerT m_diskFilesHandler; //!< The disk files handler;
452 
453  RAMBlockHandlerT m_swapBlockHandler; //!< An extra block for disk swap purposes.
454 
455  private:
456 
457  // Variables used by the method getBlockPointer
459  };
460 
461  } // end namespace mem
462 } // end namespace te
463 
464 #endif // __TERRALIB_MEMORY_INTERNAL_EXPANSIBLEBANDBLOCKSMANAGER_H
SwapFifoT::size_type m_nextFIFOPositionOverSwapFifo
The next position where a block swap will occur over m_swapFifo;.
unsigned int getMaxNumberOfRAMBlocks() const
The maximum number of cache blocks.
std::list< OpenDiskFileHandlerPtrT > OpenDiskFilesHandlerT
Open dis files handler type.
std::list< RAMBlockHandlerT > RAMBlocksHandlerT
Blocks handler type;.
#define TEMEMORYEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:122
Configuration flags for the TerraLib In-memory Data Access driver.
RAM cached and tiled raster band blocks manager.
unsigned int getNumberOfBlocksY(const unsigned int band) const
Returns the number of blocks along the Y directon for the required band.
BlockIndex3D(const CoordDataType &dim0Index, const CoordDataType &dim1Index, const CoordDataType &dim2Index)
unsigned int m_maxNumberRAMBlocks
The maximum number of RAM blocks;.
std::list< DiskBlockInfo > InactiveDiskBlocksInfoT
Inactive disk blocks info type.
unsigned int getNumberOfBands() const
Returns the number of bands.
BlockElementT * BlockelementPtrT
Block element pointer type.
boost::shared_array< BlockElementT > RAMBlockHandlerT
RAM Block handler type;.
unsigned long int getBlockSizeBytes()
Returns the internal size( bytes ) used for all internal blocks.
std::vector< std::vector< std::vector< BlockelementPtrT > > > RAMBlocksPointersContainerT
RAM blocks pointers container type.
bool m_isInitialized
Is this instance initialized ?
boost::shared_ptr< OpenDiskFileHandler > OpenDiskFileHandlerPtrT
Open disk file pointer type.
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.
std::vector< BlockIndex3D > SwapFifoT
Swap fifo type.
unsigned long int m_maxBlockSizeBytes
The maximum global used block size in bytes.
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 int getNumberOfBlocksX(const unsigned int band) const
Returns the number of blocks along the X directon for the required band.
unsigned char BlockElementT
Block element type.
ActiveDiskBlocksInfoT m_activeDiskBlocksInfo
3D Matrix of active disk block info indexed as [band][blockYIndex][blockXIndex].
bool isInitialized() const
Returns true if this instance is initialized.
void shiftDim03DCoords(ContainerType &inputContainer, const unsigned int dim0index, const int dim1Shift, const int dim2Shift) const
Shift coords given a fixed dimention 0 index.
std::vector< std::vector< std::vector< DiskBlockInfo > > > ActiveDiskBlocksInfoT
Active disk blocks info type;.
void shift3DCoords(ContainerType &inputContainer, const int &dim0Shift, const int &dim1Shift, const int &dim2Shift) const
Shift 3D coords.