PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/code/vs_solution/PlugInUtilities/Interfaces/CachedPager.h

http://github.com/pratikone/gsoc2011-opticks
C Header | 346 lines | 58 code | 27 blank | 261 comment | 0 complexity | 171f34479222859075046ad16c4bef32 MD5 | raw file
  1. /*
  2. * The information in this file is
  3. * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
  4. * and is subject to the terms and conditions of the
  5. * GNU Lesser General Public License Version 2.1
  6. * The license text is available from
  7. * http://www.gnu.org/licenses/lgpl.html
  8. */
  9. #ifndef CACHEDPAGER_H
  10. #define CACHEDPAGER_H
  11. #include <string>
  12. #include "CachedPage.h"
  13. #include "PageCache.h"
  14. #include "RasterPagerShell.h"
  15. #include "RasterPage.h"
  16. #include <memory>
  17. class RasterDataDescriptor;
  18. class RasterElement;
  19. namespace mta
  20. {
  21. class DMutex;
  22. }
  23. /**
  24. * \ingroup ShellModule
  25. * This class represents provides cached access to pages.
  26. *
  27. * By 'cached', we mean that a block may be indexed into
  28. * by multiple data accessors (ie. multithreading an algorithm
  29. * to function with 2 threads, each reading odd and even rows).
  30. * developers would take this class and extend it to support their
  31. * algorithm specific code.
  32. */
  33. class CachedPager : public RasterPagerShell
  34. {
  35. public:
  36. /**
  37. * The name to use for the raster element argument.
  38. *
  39. * This argument should be populated with the RasterElement
  40. * that this object will page. Arguments
  41. * with this name should be of the type RasterElement.
  42. */
  43. static std::string PagedElementArg()
  44. {
  45. return "Paged Element";
  46. }
  47. /**
  48. * The name to use for the filename argument.
  49. *
  50. * This argument should be populated with the Filename that
  51. * this object will page. Arguments with this name should
  52. * be of the type Filename.
  53. */
  54. static std::string PagedFilenameArg()
  55. {
  56. return "Paged Filename";
  57. }
  58. /**
  59. * Creates a CachedPager PlugIn.
  60. *
  61. * Sets cache size to 10 MB. Sets writable flag to false.
  62. *
  63. * Subclasses need to override private pure virtual methods to
  64. * open the file and get a block from that file.
  65. */
  66. CachedPager();
  67. /**
  68. * Creates a CachedPager PlugIn.
  69. *
  70. * Sets cache size to cacheSize bytes. Sets writable flag to false.
  71. *
  72. * Subclasses need to override private pure virtual methods to
  73. * open the file and get a block from that file.
  74. *
  75. * @param cacheSize
  76. * Number of bytes in the page cache.
  77. */
  78. CachedPager(const size_t cacheSize);
  79. /**
  80. * Destructor
  81. */
  82. ~CachedPager();
  83. /**
  84. * Get Plug-In Input Specification.
  85. *
  86. * The getInputSpecification() method is used by the
  87. * Plug-In Manager to determine the input parameters
  88. * to generically execute the Plug-In.
  89. *
  90. * @param pArgList
  91. * Returns a pointer to a %PlugInArgList specifying the
  92. * the Plug-In input parameters.
  93. *
  94. * @return This method returns true if the input parameter
  95. * argument list was successfully created.
  96. */
  97. bool getInputSpecification(PlugInArgList *&pArgList);
  98. /**
  99. * Get Plug-In Output Specification.
  100. *
  101. * The getOutputSpecification() method is used by the
  102. * Plug-In Manager to determine the output parameters
  103. * of the generically executed the Plug-In.
  104. *
  105. * @param pArgList
  106. * Returns a pointer to a %PlugInArgList specifying the
  107. * the Plug-In output parameters.
  108. *
  109. * @return This method returns true if the output parameter
  110. * argument list was successfully created.
  111. */
  112. bool getOutputSpecification(PlugInArgList *&pArgList);
  113. /**
  114. * Executes the plug-in.
  115. *
  116. * @param pInputArgList
  117. * On input, pInputArgList contains a complete input
  118. * argument list for the plug-in. On return, this
  119. * argument list may be updated to reflect changes made
  120. * by the plug-in.
  121. * @param pOutputArgList
  122. * On input, pOutputArgList contains a complete output
  123. * argument list for the plug-in, although actual
  124. * values and default values will be ignored. On return,
  125. * this argument list will be updated to indicate all
  126. * output parameters made by the plug-in.
  127. *
  128. * @return True if the execution was successful. False is
  129. * returned if the user cancelled the plug-in while in
  130. * interactive mode.
  131. */
  132. bool execute(PlugInArgList *pInputArgList, PlugInArgList *pOutputArgList);
  133. /**
  134. * Parses %PlugInArgList pInputArgList.
  135. *
  136. * Assigns values from the input argument list to member variables for
  137. * use during execute.
  138. *
  139. * @param pInputArgList
  140. * The input argument list to parse. Should not be NULL.
  141. *
  142. * @return TRUE if the operation succeeds; FALSE if pInputArgList is NULL
  143. * or if the operation fails.
  144. */
  145. virtual bool parseInputArgs(PlugInArgList *pInputArgList);
  146. /**
  147. * This method should return a CachedPage (which inherits RasterPage)
  148. * interface that will allow access to an in memory pointer of the
  149. * requested data that has been loaded from the original file on disk.
  150. *
  151. * The in memory pointer should point to a section of memory
  152. * that adheres to the following constraints:
  153. * <ul>
  154. * <li>
  155. * The memory pointer should point to raw cube data that is
  156. * either formatted as specified in the pOriginalRequest parameter.
  157. * </li>
  158. * <li>
  159. * The memory pointer should point to raw cube data where
  160. * each pixel value is RasterDataDescriptor::getBytesPerElement() large.
  161. * The DataDescriptor object should be retrieved from the
  162. * RasterElement that this RasterPager is associated with.
  163. * </li>
  164. * <li>
  165. * The memory pointer should point to raw cube data where
  166. * there are only post-line bytes. If there are post-line
  167. * bytes, they should be equal to DatasetParameters::getPostlineBytes()
  168. * The DatasetParameters object should be retrieved from the
  169. * RasterElement that this RasterPager is associated with.
  170. * </li>
  171. * <li>
  172. * The memory pointer should point to raw cube data that contains
  173. * at minimum concurrentRows, concurrentBands, concurrentColumns worth of data
  174. * that is directly acccessible in memory.
  175. * </li>
  176. * </ul>
  177. * This method may be called simultaneously by multiple threads and is up to
  178. * the implementer of this method to guarantee thread-safety in that case.
  179. *
  180. * @param pOriginalRequest
  181. * The request as originally made. The fields on this object
  182. * should be examined to determine if this pager can handle
  183. * the request, and how to format it. Use the other parameters
  184. * to this method to determine where to start the RasterPage.
  185. * @param startRow
  186. * the start row of data that should be loaded from the original
  187. * data file on disk into memory.
  188. * @param startColumn
  189. * the start column of data that should be loaded from the original
  190. * data file on disk into memory.
  191. * @param startBand
  192. * the start band of data that should be loaded from the original
  193. * data file on disk into memory.
  194. *
  195. * @return a RasterPage object, that when the getRawData() pointer is called
  196. * will return a pointer to the requested cube data. This RasterPage
  197. * object should not be directly deleted, but should be passed to
  198. * the releasePage() method below when the RasterPage is no longer needed.
  199. *
  200. * If the request cannot be fulfilled, return NULL.
  201. */
  202. RasterPage* getPage(DataRequest *pOriginalRequest,
  203. DimensionDescriptor startRow,
  204. DimensionDescriptor startColumn,
  205. DimensionDescriptor startBand);
  206. /**
  207. * This method will release the RasterPage* that was requested earlier
  208. * via the getPage() method.
  209. *
  210. * NOTE: This method will check to ensure that the RasterPage is a CachedPage
  211. * prior to removal and deletion.
  212. *
  213. * This method should only release those
  214. * RasterPage* that were returned by the getPage() method of the same
  215. * instance of the RasterPager.
  216. * This method may be called simultaneously by multiple threads and is up to
  217. * the implementer of this method to guarantee thread-safety in that case.
  218. *
  219. * @param pPage
  220. * the RasterPage that should be released.
  221. */
  222. void releasePage(RasterPage *pPage);
  223. /**
  224. * Get the highest version of DataRequest that this pager supports.
  225. *
  226. * RasterPagers can support a variety of conversions from the native data
  227. * to that request in a DataRequest. getPage() should be implemented to check for
  228. * these conversions and return NULL if unsupported.
  229. *
  230. * As features are added, additional fields may be added to DataRequest.
  231. * The defaults for these fields will always be the same as on the RasterElement
  232. * being accessed. Since these fields may be added without breaking compatibility with
  233. * existing RasterPager plug-ins, there will be existing plug-ins which do not know
  234. * to check these new fields and return NULL if unsupported.
  235. *
  236. * Return a value here to state what version of DataRequest is supported.
  237. * If any higher-version fields are changed from the defaults, the core will
  238. * assume that the RasterPager is unable to handle them, and the request will not be fulfilled.
  239. *
  240. * @return The highest request version supported.
  241. *
  242. * @see DataRequest::getRequestVersion()
  243. */
  244. int getSupportedRequestVersion() const;
  245. protected:
  246. /**
  247. * Accessor function for subclasses to gain access to private member variables.
  248. *
  249. * @return The number of bytes in a single element of data.
  250. * For a 200 row x 100 column x 10 band x INT2UBYTES dataset, this
  251. * function would return 2.
  252. */
  253. const int getBytesPerBand() const;
  254. /**
  255. * Accessor function for subclasses to gain access to private member variables.
  256. *
  257. * @return The number of columns in the dataset.
  258. */
  259. const int getColumnCount() const;
  260. /**
  261. * Accessor function for subclasses to gain access to private member variables.
  262. *
  263. * @return The number of bands in the dataset.
  264. */
  265. const int getBandCount() const;
  266. /**
  267. * Accessor function for subclasses to gain access to private member variables.
  268. *
  269. * @return A pointer to the RasterElement.
  270. */
  271. const RasterElement* getRasterElement() const;
  272. /**
  273. * Returns a reasonable chunk size.
  274. *
  275. * Reasonable chunk sizes are important in keeping performance high, since reading row
  276. * by row could be as small as 16KB at a time (ie 2 bytes x 1024 columns x 8 bands) and
  277. * would not optimize for IO. Instead, the CachedPager uses chunk sizes to
  278. * read in X MB of whole rows (including bands if BIP).
  279. *
  280. * @return A reasonable chunk size, in bytes. Default implementation returns 1048576 bytes (1 MB).
  281. */
  282. virtual double getChunkSize() const;
  283. private:
  284. PageCache mCache;
  285. std::auto_ptr<mta::DMutex> mpMutex;
  286. std::string mFilename;
  287. RasterDataDescriptor* mpDescriptor;
  288. RasterElement* mpRaster;
  289. int mBytesPerBand;
  290. int mColumnCount;
  291. int mBandCount;
  292. int mRowCount;
  293. /**
  294. * This method should be implemented to open the file and store a file handle to be
  295. * closed upon destruction.
  296. *
  297. * Open the file and maintain handles in derived class constructor, close in destructor.
  298. *
  299. * @param filename
  300. * The file name to open.
  301. *
  302. * @return TRUE if the open succeeds, FALSE otherwise.
  303. */
  304. virtual bool openFile(const std::string& filename) = 0;
  305. /**
  306. * Fetches a CacheUnit from disk.
  307. *
  308. * Essentially provides the same functionality as RasterPage::getPage() but for
  309. * use in a cache. CachedPages also have an offset that would allow for
  310. * higher performance if there is a circumstance where a block is read once
  311. * and two separate DataAccessors wish to access different parts of the same
  312. * page.
  313. *
  314. * @param pOriginalRequest
  315. * The request to fulfill.
  316. */
  317. virtual CachedPage::UnitPtr fetchUnit(DataRequest *pOriginalRequest) = 0;
  318. };
  319. #endif