00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef PAGECACHE_H 00011 #define PAGECACHE_H 00012 00013 #include <list> 00014 00015 #include <boost/shared_array.hpp> 00016 #include <boost/shared_ptr.hpp> 00017 #include "CachedPage.h" 00018 #include "DimensionDescriptor.h" 00019 #include "LocationType.h" 00020 00021 #include "TypesFile.h" 00022 00023 class DataRequest; 00024 00025 /** 00026 * Provides an LRU cache designed to provide faster access to pages if such 00027 * a page has already been read. 00028 * 00029 * For example, a multi-threaded algorithm could get a DataAccessor to odd 00030 * and even rows. These two threads would be able to share the same page. 00031 * 00032 * When clearing units from the cache, it simply removed the oldest units 00033 * from the cache. It is possible that a CachedPage still holds a reference 00034 * to the released unit. Since the units are consistently referred to with 00035 * shared_ptrs, the actual memory will not be released until the last page 00036 * is destroyed. This does, however, allow duplicate units -- one that the cache 00037 * knows about, and one that a lingering CachedPage references. 00038 */ 00039 class PageCache 00040 { 00041 public: 00042 /** 00043 * Creates a thread-safe LRU PageCache. 00044 * 00045 * @param maxCacheSize 00046 * The maximum size of the cache in bytes. 00047 */ 00048 PageCache(const size_t maxCacheSize = 20000000); 00049 00050 /** 00051 * Destroys the thread-safe LRU PageCache. 00052 */ 00053 ~PageCache(); 00054 00055 /** 00056 * Fetches a unit from the cache. 00057 * 00058 * See RasterPager::getPage() for details on the parameters. 00059 * 00060 * @return A CacheUnit object containing the startRow, startColumn, and startBand, 00061 * and containing and least concurrentRows number of rows, concurrentColumns number 00062 * of columns, and concurrentBands number of bands. 00063 */ 00064 CachedPage::UnitPtr getUnit(DataRequest *pOriginalRequest, 00065 DimensionDescriptor startRow, 00066 DimensionDescriptor startBand); 00067 00068 /** 00069 * An STL list of PagePtr. 00070 * 00071 * This list is provided in such a fashion so that calling std::list<UnitPtr>::erase() 00072 * on an iterator will also delete the CacheUnit properly if it is the last 00073 * reference. 00074 */ 00075 typedef std::list<CachedPage::UnitPtr> UnitList; 00076 00077 /** 00078 * Initializes member variables of the cache & resets hit/miss counts. 00079 * 00080 * This must be done after construction of the cache. 00081 * 00082 * @param bytesPerBand 00083 * The number of bytes each element takes up. Requires the value 00084 * contained by RasterDataDescriptor::getBytesPerElement(). 00085 * @param columnCount 00086 * The number of columns in file on disk. 00087 * @param bandCount 00088 * The number of bands in the file on disk. 00089 */ 00090 void initialize(int bytesPerBand, int columnCount, int bandCount); 00091 00092 /** 00093 * Create a CachedPage for the given cache unit. 00094 * 00095 * @param pUnit 00096 * The unit to create the page for. 00097 * @param requestedFormat 00098 * The format of the page provided. 00099 * @param startRow 00100 * The desired row. 00101 * @param startColumn 00102 * The desired column. 00103 * @param startBand 00104 * The desired band. 00105 * 00106 * @return The created page, or NULL if pUnit is NULL. The caller 00107 * takes ownership over the created page. 00108 */ 00109 CachedPage *createPage(CachedPage::UnitPtr pUnit, InterleaveFormatType requestedFormat, 00110 DimensionDescriptor startRow, DimensionDescriptor startColumn, DimensionDescriptor startBand); 00111 00112 protected: 00113 const size_t MAX_CACHE_SIZE; 00114 UnitList mUnits; 00115 std::string mFilename; 00116 size_t mCacheSize; 00117 int mBytesPerBand; 00118 int mColumnCount; 00119 int mBandCount; 00120 00121 void enforceCacheSize(); 00122 00123 private: 00124 PageCache& operator=(const PageCache& rhs); 00125 }; 00126 00127 #endif