This file is indexed.

/usr/include/ossim/base/ItemCache.h is in libossim-dev 2.2.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#ifndef ossimItemCache_HEADER
#define ossimItemCache_HEADER
#include <ossim/base/RWLock.h>
#include <map>
#include <memory>
#include <mutex>

namespace ossim
{
   /**
   * This is a generic cache.  The only requirement is that it expects 
   * the item to support shared_ptr.  The ItemCache allows one to specify the
   * maximum items to cache and will use a "Least Recently Used" (LRU) to purge old
   * items from the cache back to a minimum cache size.
   *
   * Example Use:
   *
   * @code
    #include <ossim/base/ItemCache.h>
    #include <ossim/support_data/TiffHandlerState.h>
    int main(int argc, char *argv[])
    {
      int returnCode = 0;
       
       ossimArgumentParser ap(&argc, argv);
       ossimInit::instance()->addOptions(ap);
       ossimInit::instance()->initialize(ap);
    
       try
       {
          ossim_uint32 maxStates=10;
          ossim::ItemCache<ossim::ImageHandlerState> stateCache;
          ossim_uint32 idx = 0;
          stateCache.setMaxItemsToCache(maxStates);
          while(idx < maxStates)
          {
            ossimString id = ossimString::toString(idx);
            stateCache.addItem(id, std::make_shared<ossim::TiffHandlerState>()); 
            ++idx;  
         }
         stateCache.getItem(ossimString::toString(0)
         stateCache.getItem(ossimString::toString(maxStates-1);

         // should be shrinking cache size back to a minimum size of default
         // 80% capacity
         stateCache.addItem(ossimString::toString(idx++), std::make_shared<ossim::TiffHandlerState>()); 
         stateCache.addItem(ossimString::toString(idx++), std::make_shared<ossim::TiffHandlerState>()); 
         stateCache.addItem(ossimString::toString(idx++), std::make_shared<ossim::TiffHandlerState>()); 

      }
      catch(const ossimException& e)
      {
         ossimNotify(ossimNotifyLevel_WARN) << e.what() << std::endl;
         returnCode = 1;
      }
      catch( ... )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << "ossim-foo caught unhandled exception!" << std::endl;
         returnCode = 1;
      }
      
      return returnCode;
   }
   * @endCode
   */
   template<class ItemType>
   class ItemCache
   {
   public:
      typename std::shared_ptr<ItemType> SharedItemType;
      /**
      * Holds information about the Item we are chaching.  
      * Holds the current lruId and the cacheId and the item we
      * are caching
      *
      * Lru is adjusted when the item is searched with the getItem
      * 
      */
      struct Node
      {
         ossim_uint64              m_lruId;
         ossimString               m_cacheId;
         std::shared_ptr<ItemType> m_item;
      };
      typedef std::map<ossimString,  std::shared_ptr<Node> > CacheType;
      typedef std::map<ossim_uint64, std::shared_ptr<Node> > LruType;
   
      /**
      * Get item will adjust the LRU if the item is present.
      *
      * @param key Is the key used to identify the item you are
      *            trying to retrieve.
      * @return the shared pointer to the item you are returning or
      *         null otherwise
      */
      std::shared_ptr<ItemType> getItem(const ossimString& key);
      std::shared_ptr<const ItemType> getItem(const ossimString& key)const;

      /**
      * Add an item to the cache given the key or id
      *
      * @param key Is the identifier for the Item
      * @param item Is the item to cache
      */ 
      void addItem(const ossimString& key, 
                   std::shared_ptr<ItemType> item);

      /**
      * Will remove an item from the cache
      *
      * @param key is the identifier for the item to remove
      * @return the removed item from the cache
      */ 
      std::shared_ptr<ItemType> removeItem(const ossimString& key);

      /**
      * will reset the cache back to empty clearing both the LRU 
      * map and the cache map.
      */
      void reset();

      /**
      * Sets the min and max cavche size.  If the cache reaches max size
      * it uses the min setting to flush values based on an LRU back to the
      * minimum size
      */
      void setMinAndMaxItemsToCache(ossim_uint32 minItemsToCache, ossim_uint32 maxItemsToCache);
      
      /**
      * @return the setting for the max items to cache
      */
      ossim_uint32 getMaxItemsToCache()const;

      /**
      * @return the setting for the min items to cache
      */
      ossim_uint32 getMinItemsToCache()const;
      
   protected:
      mutable RWLock       m_itemCacheMutex;
      mutable RWLock       m_lruCacheMutex;
      mutable ossim_uint64 m_currentId{0};
      CacheType            m_cache;
      mutable LruType      m_lruCache;
      ossim_uint32         m_maxItemsToCache{100};
      ossim_uint32         m_minItemsToCache{80};

      /**
      * The public methods locks the cache down to a read 
      * or write access.  This will not lock and will assume
      * that a lock has already been done for the Cache
      *
      * @param key identifies the item to cache
      * @param item the item to cache.
      */ 
      void protectedAddItem(const ossimString& key, 
                             std::shared_ptr<ItemType> item);

      /**
      * Will shrink the cache based on an LRU back to 
      * the min cache size.
      */
      void shrinkCache();

      /**
      * Will bump the LRU id so that the node is marked
      * newest
      *
      *  @param node the node to mark as new for LRU
      */
      void touchNode(std::shared_ptr<Node> node)const;
      
      /**
      * Removes a node from the item cache
      *
      * @param key the identifier for the node to remove
      * @return shared ptr to the node removed
      */
      std::shared_ptr<Node> removeItemFromCache(const ossimString& key);

      /**
      * Removes a node from the LRU cache
      *
      * @param key the identifier for the node to remove
      * @return shared ptr to the node removed
      */
      std::shared_ptr<Node> removeItemFromLruCache(ossim_uint64 key)const;
      
      /**
      * This is an LRU id generator.  It will bump the id and return the
      * value
      *
      * @return the next LRU id for a node to use.
      */
      ossim_uint64 nextId()const;
   };

   template<class ItemType>
   typename std::shared_ptr<ItemType> ItemCache<ItemType>::getItem(const ossimString& key)
   {
      ossim::ScopeReadLock lock(m_itemCacheMutex);
      std::shared_ptr<ItemType> result;
      typename CacheType::iterator iter = m_cache.find(key);
      if(iter != m_cache.end())
      {
         result = iter->second->m_item;
         touchNode(iter->second);
      }
      return result;
   }

   template<class ItemType>
   typename std::shared_ptr<const ItemType> ItemCache<ItemType>::getItem(const ossimString& key)const
   {
      ossim::ScopeReadLock lock(m_itemCacheMutex);
      std::shared_ptr<const ItemType> result;
      typename CacheType::const_iterator iter = m_cache.find(key);
      if(iter != m_cache.end())
      {
         result = iter->second->m_item;
         touchNode(iter->second);
      }
      return result;
   }

   template<class ItemType>
   void ItemCache<ItemType>::addItem(const ossimString& key, 
                                                  std::shared_ptr<ItemType> item)
   {
      ossim::ScopeWriteLock lock(m_itemCacheMutex);
      protectedAddItem(key, item);
   }
   template<class ItemType>
   typename std::shared_ptr<ItemType> ItemCache<ItemType>::removeItem(const ossimString& key)
   {
      ossim::ScopeWriteLock lock(m_itemCacheMutex);
      std::shared_ptr<ItemType> result;
      std::shared_ptr<Node> node = removeItemFromCache(key);
      if(node)
      {
         result = node->m_item;
         removeItemFromLruCache(node->m_lruId);
      }

      return result;
   }

   template<class ItemType>
   void ItemCache<ItemType>::protectedAddItem(const ossimString& key, 
                                                     std::shared_ptr<ItemType> item)
   {
      if(m_cache.size() > m_maxItemsToCache)
      {
         shrinkCache();
      }
      typename CacheType::iterator iter = m_cache.find(key);
      if(iter!= m_cache.end())
      {
         // update the item and LRU
         //
         std::shared_ptr<Node> node = iter->second;
         node->m_item = item;

         touchNode(node);
      }
      else
      {
         std::shared_ptr<Node> nodePtr = std::make_shared<Node>();
         nodePtr->m_cacheId = key;
         nodePtr->m_item = item;
         nodePtr->m_lruId = nextId();
         m_cache.insert(std::make_pair(key, nodePtr));
         {
            ossim::ScopeWriteLock lock(m_lruCacheMutex);
            if(m_lruCache.size() > 0)
            {
               m_lruCache.insert(--m_lruCache.end(), 
                                 std::make_pair(nodePtr->m_lruId, 
                                                nodePtr));
            }
            else
            {
               m_lruCache.insert(std::make_pair(nodePtr->m_lruId, 
                                                nodePtr));            
            }
         }
      }
   }

   template<class ItemType>
   void ItemCache<ItemType>::touchNode(std::shared_ptr<Node> node)const
   {
      m_lruCache.erase(node->m_lruId);
      node->m_lruId = nextId();
      {
         ossim::ScopeWriteLock lock(m_lruCacheMutex);
         if(m_lruCache.size() > 0)
         {
            m_lruCache.insert(m_lruCache.end(), std::make_pair(node->m_lruId, node));
         }
         else
         {
            m_lruCache.insert(std::make_pair(node->m_lruId, node));
         }
      }
   }

   template<class ItemType>
   ossim_uint64 ItemCache<ItemType>::nextId()const
   {
      return m_currentId++;
   }

   template<class ItemType>
   void ItemCache<ItemType>::setMinAndMaxItemsToCache(ossim_uint32 maxItemsToCache, 
                                                ossim_uint32 minItemsToCache)
   {
     ossim::ScopeWriteLock lock(m_itemCacheMutex);
     m_maxItemsToCache = maxItemsToCache;
     m_minItemsToCache = minItemsToCache;
   }
   template<class ItemType>
   void ItemCache<ItemType>::reset()
   {
     ossim::ScopeWriteLock lock(m_itemCacheMutex);
     m_cache.clear();
     m_lruCache.clear();
     m_currentId = 0;      
   }

   template<class ItemType>
   void ItemCache<ItemType>::shrinkCache()
   {
      if(m_minItemsToCache < 1)
      {
         m_lruCache.clear();
         m_cache.clear();
      }
      else
      {
         typename LruType::iterator iter = m_lruCache.begin();
         ossim_uint32 previousSize = m_cache.size();
         while((m_cache.size() > m_minItemsToCache)&&
               (iter != m_lruCache.end()))
         {
            removeItemFromCache(iter->second->m_cacheId);
            iter = m_lruCache.erase(iter);

            // sanity check to make sure we continue to shrink at
            // each iteration
            // avoids infinite loop
            if(m_cache.size() >= previousSize)
            {
               break;
            }
         }
      }
   }

   template<class ItemType>
   ossim_uint32 ItemCache<ItemType>::getMaxItemsToCache()const
   {
      ossim::ScopeReadLock lock(m_itemCacheMutex);
      return m_maxItemsToCache;
   }

   template<class ItemType>
   ossim_uint32 ItemCache<ItemType>::getMinItemsToCache()const
   {
      ossim::ScopeReadLock lock(m_itemCacheMutex);
      return m_minItemsToCache;
   }

   template<class ItemType>
   std::shared_ptr< typename ItemCache<ItemType>::Node> ItemCache<ItemType>::removeItemFromCache(const ossimString& key)
   {
      std::shared_ptr<Node> result;

      typename CacheType::iterator iter = m_cache.find(key);
      if(iter != m_cache.end())
      {
         result = iter->second;
         m_cache.erase(iter);
      }   

      return result;
   }

   template<class ItemType>
   std::shared_ptr<typename ItemCache<ItemType>::Node> ItemCache<ItemType>::removeItemFromLruCache(ossim_uint64 key)const
   {
      std::shared_ptr<Node> result;

      typename LruType::iterator iter = m_lruCache.find(key);
      if(iter != m_lruCache.end())
      {
         result = iter->second;
         m_lruCache.erase(iter);
      }   

      return result;
   } 

}



#endif