OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimAppTileCache.cpp
Go to the documentation of this file.
1 //******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 // Description: This file contains the Application cache algorithm
11 //
12 //***********************************
13 // $Id: ossimAppTileCache.cpp 15766 2009-10-20 12:37:09Z gpotts $
14 
18 #include <ossim/base/ossimNotify.h>
20 
22 
23 // we will need to grab this from the preferences
27 
29 {
30  if(!theInstance)
31  {
32  if(maxSize < 1)
33  {
34  ossimString cacheSize = ossimPreferences::instance()->findPreference("cache_size");
35  if(cacheSize!="")
36  {
37  maxSize = cacheSize.toUInt32()*1024*1024;
38  }
39  else
40  {
41  maxSize = DEFAULT_SIZE;
42  }
44  << "Setting SIZE----------------------- " << maxSize << std::endl;
45  }
46  theInstance = new ossimAppTileCache(maxSize);
47  }
48 
49  return theInstance;
50 }
51 
53 {
54  deleteAll();
55 }
56 
58 {
59  ossimTileCache *aCache = 0;
60  ossimAppCacheId result = 0;
61 
62  aCache = new ossimTileCache(bucketSize);
63 
64  if(aCache)
65  {
66  theAppCache.insert(make_pair(theUniqueAppIdCounter, aCache));
67  result = theUniqueAppIdCounter;
69  }
70 
71  return result;
72 }
73 
78  const ossimDpt3d &origin,
79  ossim_uint32 resLevel)
80 {
81  ossimDataObject* result = 0;
82  if(id>0)
83  {
84  ossimTileCache *aCache = this->get(id);
85  if(aCache)
86  {
87  result = aCache->get(origin,
88  resLevel);
89  if(result)
90  {
91  adjustQueue(id, origin, resLevel);
92  }
93  }
94  }
95 
96  return result;
97 }
98 
100  const ossimDpt3d &origin,
101  unsigned long resLevel)
102 {
103  ossimRefPtr<ossimDataObject> result = 0;
104  if(id>0)
105  {
106  ossimTileCache *aCache = this->get(id);
107  if(aCache)
108  {
109  // remove from cache
110  result = aCache->remove(origin,
111  resLevel);
112  // adjust the byte count
113  if(result.valid())
114  {
116  }
117  removeTileFromQueue(id, origin, resLevel);
118  }
119  }
120 
121  return result;
122 }
123 
125  const ossimDpt3d &origin,
126  const ossimDataObject* data,
127  ossim_uint32 resLevel)
128 {
129  static const char MODULE[] = "ossimAppTileCache::insert";
130  ossimDataObject *result = 0;
131 
132  // find the cache and if it's not there then return NULL
133  ossimTileCache *aCache = this->get(appId);
134  if(!aCache)
135  {
136  return result;
137  }
138 
139  ossimRefPtr<ossimDataObject> tileToInsert = 0;
140  long dataSize = data->getDataSizeInBytes();
141 
142  if( (theCurrentCacheSize+dataSize) > theMaxCacheSize)
143  {
144  do
145  {
147  }while((theCurrentCacheSize+dataSize) > theMaxCacheSize);
148  }
149 
150  if(data)
151  {
152  tileToInsert = (ossimDataObject*)data->dup();
153 
154  result = aCache->insert(origin,
155  tileToInsert.get(),
156  resLevel);
157  if(!result)
158  {
160  << MODULE << " ERROR: can't insert and should not happen"
161  << endl;
162 
163  tileToInsert = 0;
164  }
165  else
166  {
167  theCurrentCacheSize += dataSize;
168  theUsedQueue.push_back(ossimAppCacheTileInfo(appId,
169  origin,
170  resLevel));
171  }
172  }
173 
174  return result;
175 }
176 
177 
179 {
180  ossimTileCache *result=0;
181 
182  AppIdIterator anIterator = theAppCache.find(id);
183 
184  if(anIterator != theAppCache.end())
185  {
186  result = (*anIterator).second;
187  }
188 
189  return result;
190 }
191 
193 {
194  // first delete the cache
195  AppIdIterator anIterator = theAppCache.find(appId);
196  if(anIterator != theAppCache.end())
197  {
198  ossimTileCache *aCache = (*anIterator).second;
199  theCurrentCacheSize -= aCache->sizeInBytes();
200 
201  delete aCache;
202 
203  theAppCache.erase(anIterator);
204  }
205  // now delete all occurences of the appCacheId in the queue
206  // used for LRU algorithm.
208 }
209 
211 {
212  AppIdIterator iter = theAppCache.begin();
213 
214  while(iter != theAppCache.end())
215  {
216  delete (*iter).second;
217  ++iter;
218  }
219 
220  theAppCache.clear();
221 }
222 
224  const ossimDpt3d &origin,
225  ossim_uint32 resLevel)
226 {
227  list<ossimAppCacheTileInfo>::iterator anIterator;
228 
229  anIterator = theUsedQueue.begin();
230  while(anIterator != theUsedQueue.end())
231  {
232  if( ((*anIterator).theAppCacheId == appId) &&
233  ((*anIterator).theOrigin == origin)&&
234  ((*anIterator).theResLevel == resLevel))
235  {
236  theUsedQueue.erase(anIterator);
237  return;
238  }
239  else
240  {
241  ++anIterator;
242  }
243  }
244 }
245 
247 {
248  list<ossimAppCacheTileInfo>::iterator anIterator;
249 
250  anIterator = theUsedQueue.begin();
251  while(anIterator != theUsedQueue.end())
252  {
253  if( (*anIterator).theAppCacheId == appId)
254  {
255  anIterator = theUsedQueue.erase(anIterator);
256  }
257  else
258  {
259  ++anIterator;
260  }
261  }
262 }
263 
265 {
267  if(!theUsedQueue.empty())
268  {
269  ossimAppCacheTileInfo &info = *(theUsedQueue.begin());
270 
271  ossimTileCache *aCache = get(info.theAppCacheId);
272  if(aCache)
273  {
274  result = aCache->remove(info.theOrigin,
275  info.theResLevel);
277  }
278  theUsedQueue.erase(theUsedQueue.begin());
279  }
280 
281  return result;
282 }
283 
285  const ossimDpt3d &origin,
286  ossim_uint32 resLevel)
287 {
288  list<ossimAppCacheTileInfo>::iterator anIterator = theUsedQueue.begin();
289  ossimAppCacheTileInfo info(id, origin, resLevel);
290 
291  while(anIterator != theUsedQueue.end())
292  {
293 
294  if((*anIterator) == info)
295  {
296  theUsedQueue.erase(anIterator);
297  theUsedQueue.push_back(info);
298  return;
299  }
300  ++anIterator;
301  }
302 }
virtual ossimDataObject * remove(const ossimDpt3d &origin, unsigned long resLevel=0)
static ossimAppTileCache * theInstance
ossimRefPtr< ossimDataObject > insert(ossimAppCacheId id, const ossimDpt3d &origin, const ossimDataObject *data, ossim_uint32 resLevel=0)
static const ossim_uint32 DEFAULT_SIZE
virtual ossim_uint32 sizeInBytes()
bool valid() const
Definition: ossimRefPtr.h:75
ossimDataObject * get(ossimAppCacheId id, const ossimDpt3d &origin, ossim_uint32 resLevel=0)
static ossimAppTileCache * instance(ossim_uint32 maxSize=0)
ossimAppCacheId newTileCache(ossim_uint32 bucketSize=DEFAULT_BUCKET_SIZE)
virtual ossimDataObject * get(const ossimDpt3d &origin, unsigned long resLevel=0)
ossim_uint32 theCurrentCacheSize
ossim_uint32 toUInt32() const
ossimAppTileCache(ossim_uint32 maxSize=DEFAULT_SIZE)
void deleteCache(ossimAppCacheId appId)
map< ossimAppCacheId, ossimTileCache * > theAppCache
static ossimAppCacheId theUniqueAppIdCounter
list< ossimAppCacheTileInfo > theUsedQueue
void removeTileFromQueue(ossimAppCacheId appId, const ossimDpt3d &origin, ossim_uint32 resLevel)
const char * findPreference(const char *key) const
ossim_int32 ossimAppCacheId
unsigned int ossim_uint32
ossimRefPtr< ossimDataObject > removeTile()
static ossimPreferences * instance()
static const ossim_uint32 DEFAULT_BUCKET_SIZE
virtual ossimDataObject * insert(const ossimDpt3d &origin, ossimDataObject *data, unsigned long resLevel=0)
void deleteAppCacheFromQueue(ossimAppCacheId appId)
map< ossimAppCacheId, ossimTileCache * >::iterator AppIdIterator
virtual ossimObject * dup() const
Definition: ossimObject.cpp:29
ossim_uint32 theMaxCacheSize
void adjustQueue(ossimAppCacheId id, const ossimDpt3d &origin, ossim_uint32 resLevel)
virtual ossim_uint32 getDataSizeInBytes() const =0
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)