OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimAppFixedTileCache.cpp
Go to the documentation of this file.
1 //******************************************************************
2 //
3 // License: LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: Garrett Potts
8 //
9 // Description: This file contains the Application cache algorithm
10 //
11 //***********************************
12 // $Id: ossimAppFixedTileCache.cpp 20127 2011-10-12 11:27:10Z gpotts $
13 #include <algorithm>
14 #include <sstream>
18 #include <ossim/base/ossimCommon.h>
19 #include <ossim/base/ossimTrace.h>
20 
24 
25 static const ossimTrace traceDebug("ossimAppFixedTileCache:debug");
27 {
28  std::map<ossimAppFixedTileCache::ossimAppFixedCacheId, ossimFixedTileCache*>::const_iterator iter = rhs.theAppCacheMap.begin();
29 
30  if(iter == rhs.theAppCacheMap.end())
31  {
33  << "***** APP CACHE EMPTY *****" << endl;
34  }
35  else
36  {
37  while(iter != rhs.theAppCacheMap.end())
38  {
39  out << "Cache id = "<< (*iter).first << " size = " << (*iter).second->getCacheSize() << endl;
40  ++iter;
41  }
42  }
43 
44  return out;
45 }
46 
47 
49 {
50  if(traceDebug())
51  {
52  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimAppFixedTileCache::ossimAppFixedTileCache() DEBUG: entered ..." << std::endl;
53  }
54  theInstance = this;
55  theTileSize = ossimIpt(64, 64);
57 
58  // ossim::defaultTileSize(theTileSize);
59 
60  ossim_uint32 cacheSize = ossimString(ossimPreferences::instance()->findPreference("cache_size")).toUInt32()*(1024*1024);
61  const char* tileSize = ossimPreferences::instance()->findPreference("tile_size");
62  if(tileSize)
63  {
64  ossimString tempString(tileSize);
65  std::vector<ossimString> splitString;
66  tempString = tempString.trim();
67  tempString.split(splitString, " ");
68  if(splitString.size() > 1)
69  {
70  theTileSize.x = splitString[0].toInt32();
71  theTileSize.y = splitString[1].toInt32();
72  }
73  else
74  {
75  theTileSize = ossimIpt(64,64);
76  }
77 
78  }
79  if(cacheSize)
80  {
81  setMaxCacheSize(cacheSize);
82  }
83  else
84  {
86  }
87  if(traceDebug())
88  {
90  << "DEBUG: cache tile size = " << theTileSize << std::endl
91  << "Cache size = " << cacheSize << " bytes" << std::endl;
92  }
93 
94  if(traceDebug())
95  {
97  << "ossimAppFixedTileCache::ossimAppFixedTileCache() DEBUG: leaving ..." << std::endl;
98  }
99 }
100 
102 {
103  deleteAll();
104 }
105 
107 {
108  if(!theInstance)
109  {
111  if(maxSize)
112  {
113  theInstance->setMaxCacheSize(maxSize);
114  }
115  }
116  return theInstance;
117 }
118 
120 {
121  std::lock_guard<std::mutex> lock(theMutex);
122  theMaxGlobalCacheSize = cacheSize;
123  theMaxCacheSize = cacheSize;
124  // theMaxCacheSize = (ossim_uint32)(theMaxGlobalCacheSize*.2);
125 }
126 
128 {
129  std::lock_guard<std::mutex> lock(theMutex);
130  std::map<ossimAppFixedCacheId, ossimFixedTileCache*>::iterator currentIter = theAppCacheMap.begin();
131 
132  while(currentIter != theAppCacheMap.end())
133  {
134  (*currentIter).second->flush();
135  ++currentIter;
136  }
138 }
139 
141 {
142  std::lock_guard<std::mutex> lock(theMutex);
143  ossimFixedTileCache* cache = getCache(cacheId);
144  {
145  if(cache)
146  {
147  theCurrentCacheSize -= cache->getCacheSize();
148  cache->flush();
149  }
150  }
151 }
152 
154 {
155  std::lock_guard<std::mutex> lock(theMutex);
157  {
158  std::map<ossimAppFixedCacheId, ossimFixedTileCache*>::iterator iter = theAppCacheMap.find(cacheId);
159 
160  if(cache.valid())
161  {
162  theAppCacheMap.erase(iter);
163  theCurrentCacheSize -= cache->getCacheSize();
164  }
165  cache = 0;
166  }
167 }
168 
170  const ossimIpt& tileSize)
171 {
172  std::lock_guard<std::mutex> lock(theMutex);
173  ossimAppFixedCacheId result = -1;
175  if(tileSize.x == 0 ||
176  tileSize.y == 0)
177  {
178  // newCache->setRect(tileBoundaryRect, theTileSize);
179  newCache->setRect(tileBoundaryRect,
180  newCache->getTileSize());
181  }
182  else
183  {
184  newCache->setRect(tileBoundaryRect, tileSize);
185  }
186  result = theUniqueAppIdCounter;
187  theAppCacheMap.insert(std::make_pair(result, newCache));
189 
190  return result;
191 }
192 
194 {
195  std::lock_guard<std::mutex> lock(theMutex);
196  ossimAppFixedCacheId result = -1;
198 
199  {
200  result = theUniqueAppIdCounter;
201  theAppCacheMap.insert(std::make_pair(result, newCache));
203  }
204 
205  return result;
206 
207 }
208 
210  const ossimIrect& boundaryTileRect)
211 {
212  std::lock_guard<std::mutex> lock(theMutex);
213  ossimFixedTileCache* cache = getCache(cacheId);
214  if(cache)
215  {
216 
217  ossim_uint32 cacheSize = cache->getCacheSize();
218  // cache->setRect(boundaryTileRect, theTileSize);
219  cache->setRect(boundaryTileRect,
220  cache->getTileSize());
221  theCurrentCacheSize += (cache->getCacheSize() - cacheSize);
222  }
223 }
224 
226  const ossimIpt& tileSize)
227 {
228  std::lock_guard<std::mutex> lock(theMutex);
229  ossimFixedTileCache* cache = getCache(cacheId);
230  if(cache)
231  {
232  ossim_uint32 cacheSize = cache->getCacheSize();
233  cache->setRect(cache->getTileBoundaryRect(), tileSize);
234  theCurrentCacheSize += (cache->getCacheSize() - cacheSize);
235  theTileSize = cache->getTileSize();
236  }
237 }
238 
240  ossimAppFixedCacheId cacheId,
241  const ossimIpt& origin)
242 {
243  std::lock_guard<std::mutex> lock(theMutex);
244  ossimRefPtr<ossimImageData> result = 0;
245  ossimFixedTileCache* cache = getCache(cacheId);
246  if(cache)
247  {
248  result = cache->getTile(origin);
249  }
250 
251  return result;
252 }
253 
254 
256  ossimAppFixedCacheId cacheId,
258  bool duplicateData)
259 {
260  std::lock_guard<std::mutex> lock(theMutex);
261  ossimRefPtr<ossimImageData> result = 0;
262  ossimFixedTileCache *aCache = this->getCache(cacheId);
263  if(!aCache)
264  {
265  return result;
266  }
267  ossim_uint32 dataSize = data->getDataSizeInBytes();
268 
270  {
272  }
273 
274  ossim_uint32 cacheSize = 0;
275  {
276  cacheSize = aCache->getCacheSize();
277  }
278  if(cacheSize > theMaxCacheSize)
279  {
280 // shrinkCacheSize(aCache,
281 // (ossim_int32)(aCache->getCacheSize()*.1));
282  shrinkCacheSize(aCache,
283  (ossim_int32)(1024*1024));
284  }
285  {
286  cacheSize = aCache->getCacheSize();
287  result = aCache->addTile(data, duplicateData);
288 
289  theCurrentCacheSize += (aCache->getCacheSize() - cacheSize);
290  }
291 
292  return result;
293 }
294 
296 {
297  std::lock_guard<std::mutex> lock(theMutex);
299  theAppCacheMap.clear();
300 }
301 
303  ossimAppFixedCacheId cacheId,
304  const ossimIpt& origin)
305 {
306  std::lock_guard<std::mutex> lock(theMutex);
307  ossimRefPtr<ossimImageData> result = 0;
308 
309  ossimFixedTileCache* cache = getCache(cacheId);
310  if(cache)
311  {
312  ossim_uint32 cacheSize = cache->getCacheSize();
313  result = cache->removeTile(origin);
314  theCurrentCacheSize += (cache->getCacheSize() - cacheSize);
315  }
316 
317  return result;
318 }
319 
321  const ossimIpt& origin)
322 {
323  std::lock_guard<std::mutex> lock(theMutex);
324  ossimFixedTileCache* cache = getCache(cacheId);
325  if(cache)
326  {
327  ossim_uint32 cacheSize = cache->getCacheSize();
328  cache->deleteTile(origin);
329  theCurrentCacheSize += (cache->getCacheSize() - cacheSize);
330  }
331 }
332 
334  ossimAppFixedCacheId cacheId)
335 {
336  std::map<ossimAppFixedCacheId, ossimFixedTileCache*>::const_iterator
337  currentIter = theAppCacheMap.find(cacheId);
338  ossimFixedTileCache* result = 0;
339 
340  if(currentIter != theAppCacheMap.end())
341  {
342  result = (*currentIter).second;
343  }
344 
345  return result;
346 }
347 
349 {
350  if(static_cast<ossim_uint32>(byteCount) >= theCurrentCacheSize)
351  {
352  flush();
353  }
354  else
355  {
356  while(byteCount > 0)
357  {
358  std::map<ossimAppFixedCacheId, ossimFixedTileCache*>::iterator iter = theAppCacheMap.begin();
359  while( (iter != theAppCacheMap.end())&&(byteCount>0))
360  {
361  ossimFixedTileCache* cache = getCache((*iter).first);
362  if(cache)
363  {
364  ossim_uint32 before = cache->getCacheSize();
365  cache->deleteTile();
366  ossim_uint32 after = cache->getCacheSize();
367  ossim_uint32 delta = (before - after);
368  byteCount -= delta;
369  theCurrentCacheSize -= (delta);
370  }
371  ++iter;
372  }
373  }
374  }
375 }
376 
378  ossim_int32 byteCount)
379 {
380  ossimFixedTileCache* cache = getCache(id);
381 
382  if(cache)
383  {
384  shrinkCacheSize(cache, byteCount);
385  }
386 }
387 
389  ossim_int32 byteCount)
390 {
391  if(cache)
392  {
393  ossim_int32 cacheSize = cache->getCacheSize();
394  if(cacheSize <= byteCount)
395  {
396  cache->flush();
397  }
398  else
399  {
400  while(byteCount > 0)
401  {
402  ossim_uint32 before = cache->getCacheSize();
403  cache->deleteTile();
404  ossim_uint32 after = cache->getCacheSize();
405  ossim_uint32 delta = std::abs((int)(before - after));
406  if(delta)
407  {
408  byteCount -= delta;
409  theCurrentCacheSize -= (delta);
410  }
411  else
412  {
413  byteCount = 0;
414  }
415  }
416  }
417  }
418 }
419 
421 {
422  std::lock_guard<std::mutex> lock(theMutex);
423  ossimFixedTileCache* cache = getCache(cacheId);
424  if(cache)
425  {
426  return cache->getTileSize();
427  }
428  return theTileSize;
429 }
virtual void deleteCache(ossimAppFixedCacheId cacheId)
ossimRefPtr< ossimImageData > removeTile(ossimAppFixedCacheId cacheId, const ossimIpt &origin)
virtual ossimRefPtr< ossimImageData > getTile(ossim_int32 id)
virtual const ossimIpt & getTileSize() const
virtual const ossimIrect & getTileBoundaryRect() const
virtual void deleteTile(const ossimIpt &origin)
virtual void setTileSize(ossimAppFixedCacheId cacheId, const ossimIpt &tileSize)
bool valid() const
Definition: ossimRefPtr.h:75
const ossimIpt & getTileSize(ossimAppFixedCacheId cacheId)
void deleteTile(ossimAppFixedCacheId cacheId, const ossimIpt &origin)
void split(std::vector< ossimString > &result, const ossimString &separatorList, bool skipBlankFields=false) const
Splits this string into a vector of strings (fields) using the delimiter list specified.
ossim_uint32 toUInt32() const
#define abs(a)
Definition: auxiliary.h:74
ossimAppFixedCacheId newTileCache()
static ossimAppFixedTileCache * instance(ossim_uint32 maxSize=0)
ossimFixedTileCache * getCache(ossimAppFixedCacheId cacheId)
ossimRefPtr< ossimImageData > getTile(ossimAppFixedCacheId cacheId, const ossimIpt &origin)
virtual ossim_uint32 getCacheSize() const
const char * findPreference(const char *key) const
virtual void setRect(const ossimIrect &rect)
unsigned int ossim_uint32
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
static ossimAppFixedCacheId theUniqueAppIdCounter
void shrinkGlobalCacheSize(ossim_int32 byteCount)
static ossimPreferences * instance()
void shrinkCacheSize(ossimAppFixedCacheId id, ossim_int32 byteCount)
std::ostream & operator<<(std::ostream &out, const ossimAppFixedTileCache &rhs)
std::map< ossimAppFixedCacheId, ossimFixedTileCache * > theAppCacheMap
ossim_int32 y
Definition: ossimIpt.h:142
virtual void setRect(ossimAppFixedCacheId cacheId, const ossimIrect &boundaryTileRect)
static ossimAppFixedTileCache * theInstance
virtual void setMaxCacheSize(ossim_uint32 cacheSize)
virtual ossim_uint32 getDataSizeInBytes() const
virtual ossimRefPtr< ossimImageData > removeTile(const ossimIpt &origin)
ossim_int32 x
Definition: ossimIpt.h:141
static const ossim_uint32 DEFAULT_SIZE
virtual ossimRefPtr< ossimImageData > addTile(ossimRefPtr< ossimImageData > imageData, bool duplicateData=true)
ossimRefPtr< ossimImageData > addTile(ossimAppFixedCacheId cacheId, ossimRefPtr< ossimImageData > data, bool duplicateData=true)
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32