NCBI C++ ToolKit
Files | Classes | Typedefs | Enumerations | Functions
Generic Cache
+ Collaboration diagram for Generic Cache:

Files

file  ncbi_cache.hpp
 The NCBI C++ generic cache template.
 

Classes

class  limited_size_map< Key, Value, Less >
 
class  limited_size_map< Key, Value, Less >::const_iterator
 
class  limited_size_map< Key, Value, Less >::iterator
 
struct  limited_size_map< Key, Value, Less >::SLess
 
struct  limited_size_map< Key, Value, Less >::SNode
 
struct  SCacheElement< TKey, TSize >
 Generic cache. More...
 
struct  CCacheElement_Less< TCacheElementPtr >
 Compare cache elements by weight/order. More...
 
class  CCacheElement_Handler< TKey, TValue >
 Default (NOP) element handler. More...
 
class  CCache< TKey, TValue, THandler, TLock, TSize >
 Cache template. More...
 
struct  CCache< TKey, TValue, THandler, TLock, TSize >::SValueWithIndex
 
class  CCacheException
 Exception thrown by CCache. More...
 

Typedefs

typedef CMutex TCacheLock_Default
 Default cache lock. More...
 

Enumerations

enum  ECache_InsertFlag { eCache_CheckSize, eCache_CanInsert, eCache_NeedCleanup, eCache_DoNotCache }
 Flag indicating if an element can be inserted into cache. More...
 
enum  CCache< TKey, TValue, THandler, TLock, TSize >::EAddFlags { CCache< TKey, TValue, THandler, TLock, TSize >::fAdd_NoReplace = (1 << 0) }
 Flags to control the details of adding new elements to the cache via Add(). More...
 
enum  CCache< TKey, TValue, THandler, TLock, TSize >::EAddResult { CCache< TKey, TValue, THandler, TLock, TSize >::eAdd_Inserted, CCache< TKey, TValue, THandler, TLock, TSize >::eAdd_Replaced, CCache< TKey, TValue, THandler, TLock, TSize >::eAdd_NotInserted }
 Result of element insertion. More...
 
enum  CCache< TKey, TValue, THandler, TLock, TSize >::EGetFlags { CCache< TKey, TValue, THandler, TLock, TSize >::fGet_NoTouch = (1 << 0), CCache< TKey, TValue, THandler, TLock, TSize >::fGet_NoCreate = (1 << 1), CCache< TKey, TValue, THandler, TLock, TSize >::fGet_NoInsert = (1 << 2) }
 Cache retrieval flags. More...
 
enum  CCache< TKey, TValue, THandler, TLock, TSize >::EGetResult { CCache< TKey, TValue, THandler, TLock, TSize >::eGet_Found, CCache< TKey, TValue, THandler, TLock, TSize >::eGet_CreatedAndAdded, CCache< TKey, TValue, THandler, TLock, TSize >::eGet_CreatedNotAdded }
 Get() result. More...
 
enum  CCacheException::EErrCode { CCacheException::eIndexOverflow, CCacheException::eWeightOverflow, CCacheException::eNotFound, CCacheException::eOtherError }
 

Functions

 CCache< TKey, TValue, THandler, TLock, TSize >::CCache (TSizeType capacity, THandler *handler=NULL)
 Create cache object with the given capacity. More...
 
 CCache< TKey, TValue, THandler, TLock, TSize >::~CCache (void)
 
void CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseElement (TCacheSet_I &set_iter, TCacheMap_I &map_iter)
 
void CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast (void)
 
TCacheElement * CCache< TKey, TValue, THandler, TLock, TSize >::x_InsertElement (const TKeyType &key, TWeight weight)
 
void CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement (TCacheElement *elem)
 
TOrder CCache< TKey, TValue, THandler, TLock, TSize >::x_GetNextCounter (void)
 
void CCache< TKey, TValue, THandler, TLock, TSize >::x_PackElementIndex (void)
 
TOrder CCache< TKey, TValue, THandler, TLock, TSize >::Add (const TKeyType &key, const TValueType &value, TWeight weight=1, TAddFlags add_flags=0, EAddResult *result=NULL)
 Add new element to the cache or replace the existing value. More...
 
TValueType CCache< TKey, TValue, THandler, TLock, TSize >::operator[] (const TKeyType &key)
 Get cache element by the key. More...
 
TValueType CCache< TKey, TValue, THandler, TLock, TSize >::Get (const TKeyType &key, TGetFlags get_flags=0, EGetResult *result=NULL)
 Get an object from the cache by its key. More...
 
bool CCache< TKey, TValue, THandler, TLock, TSize >::Remove (const TKeyType &key)
 Remove element from cache. Do nothing if the key is not cached. More...
 
void CCache< TKey, TValue, THandler, TLock, TSize >::SetCapacity (TSizeType new_capacity)
 Set new capacity of the cache. More...
 
void CCache< TKey, TValue, THandler, TLock, TSize >::SetSize (TSizeType new_size)
 Truncate the cache leaving at most new_size elements. More...
 

Detailed Description

Typedef Documentation

Default cache lock.

Definition at line 134 of file ncbi_cache.hpp.

Enumeration Type Documentation

template<class TKey, class TValue, class THandler = CCacheElement_Handler<TKey, TValue>, class TLock = TCacheLock_Default, class TSize = Uint4>
enum CCache::EAddFlags

Flags to control the details of adding new elements to the cache via Add().

See also
Add()
Enumerator
fAdd_NoReplace 

Do not replace existing values if any.

Definition at line 193 of file ncbi_cache.hpp.

template<class TKey, class TValue, class THandler = CCacheElement_Handler<TKey, TValue>, class TLock = TCacheLock_Default, class TSize = Uint4>
enum CCache::EAddResult

Result of element insertion.

Enumerator
eAdd_Inserted 

The element was added to the cache.

eAdd_Replaced 

The element existed and was replaced.

eAdd_NotInserted 

The element was not added or replaced.

Definition at line 199 of file ncbi_cache.hpp.

Flag indicating if an element can be inserted into cache.

Enumerator
eCache_CheckSize 

Insert the element after checking max cache size.

eCache_CanInsert 

The element can be inserted.

eCache_NeedCleanup 

Need to cleanup cache before inserting the element.

eCache_DoNotCache 

The element can not be inserted (e.g. too big)

Definition at line 96 of file ncbi_cache.hpp.

Enumerator
eIndexOverflow 

Element index overflow.

eWeightOverflow 

Element weight overflow.

eNotFound 

The requested key was not found in the cache.

eOtherError 

Definition at line 307 of file ncbi_cache.hpp.

template<class TKey, class TValue, class THandler = CCacheElement_Handler<TKey, TValue>, class TLock = TCacheLock_Default, class TSize = Uint4>
enum CCache::EGetFlags

Cache retrieval flags.

Enumerator
fGet_NoTouch 

Do not update the object's position.

fGet_NoCreate 

Do not create value if not found, throw an exception instead.

fGet_NoInsert 

Do not insert created values.

Definition at line 226 of file ncbi_cache.hpp.

template<class TKey, class TValue, class THandler = CCacheElement_Handler<TKey, TValue>, class TLock = TCacheLock_Default, class TSize = Uint4>
enum CCache::EGetResult

Get() result.

Enumerator
eGet_Found 

The key was found in the cache.

eGet_CreatedAndAdded 

A new value was created and cached.

eGet_CreatedNotAdded 

A new value was created but not cached.

Definition at line 235 of file ncbi_cache.hpp.

Function Documentation

template<class TKey , class TValue , class THandler , class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::TOrder CCache< TKey, TValue, THandler, TLock, TSize >::Add ( const TKeyType key,
const TValueType value,
TWeight  weight = 1,
TAddFlags  add_flags = 0,
EAddResult result = NULL 
)

Add new element to the cache or replace the existing value.

Parameters
keyElement key
valueElement value
weightWeight adjustment. The lifetime of each object in the cache is proportional to its weight.
add_flagsFlags to control Add() behavior.
resultPointer to a variable to store operation result code to.
Returns
Index of the new element in the cache.

Definition at line 507 of file ncbi_cache.hpp.

References _ASSERT, eCache_CanInsert, eCache_CheckSize, eCache_DoNotCache, eCache_NeedCleanup, CCache< TKey, TValue, THandler, TLock, TSize >::SValueWithIndex::m_CacheElement, SCacheElement< TKey, TSize >::m_Order, CCache< TKey, TValue, THandler, TLock, TSize >::SValueWithIndex::m_Value, and value.

Referenced by CSeqVectorCache::PrefetchSequence().

template<class TKey , class TValue , class THandler, class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::CCache ( TSizeType  capacity,
THandler *  handler = NULL 
)

Create cache object with the given capacity.

Definition at line 326 of file ncbi_cache.hpp.

References _ASSERT, CCache< TKey, TValue, THandler, TLock, TSize >::m_Handler, NULL, and auto_ptr< X >::reset().

template<class TKey , class TValue , class THandler , class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::TValueType CCache< TKey, TValue, THandler, TLock, TSize >::Get ( const TKeyType key,
TGetFlags  get_flags = 0,
EGetResult result = NULL 
)

Get an object from the cache by its key.

Depending on flags create and cache a new value if the key is not found. If the flags do not allow creating new elements, throws an exception.

Parameters
keyElement key
get_flagsFlags to control element retrieval
resultpointer to a variable to store the result code to.
Returns
The value referenced by the key or a new value created by the handler's CreateValue().

Definition at line 590 of file ncbi_cache.hpp.

References eNotFound, NCBI_THROW, and value.

Referenced by CSeqVectorCache::PrefetchSequence().

template<class TKey , class TValue , class THandler , class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::TValueType CCache< TKey, TValue, THandler, TLock, TSize >::operator[] ( const TKeyType key)

Get cache element by the key.

If the key is not cached yet, the handler's CreateValue() will be called to create one and the new element will be stored in the cache.

See also
Get()

Definition at line 573 of file ncbi_cache.hpp.

References value.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
bool CCache< TKey, TValue, THandler, TLock, TSize >::Remove ( const TKeyType key)

Remove element from cache. Do nothing if the key is not cached.

Definition at line 631 of file ncbi_cache.hpp.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
void CCache< TKey, TValue, THandler, TLock, TSize >::SetCapacity ( TSizeType  new_capacity)

Set new capacity of the cache.

The number of elements in the cache may be reduced to match the new capacity.

Parameters
new_capacitynew cache capacity, must be > 0.

Definition at line 646 of file ncbi_cache.hpp.

References NCBI_THROW.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
void CCache< TKey, TValue, THandler, TLock, TSize >::SetSize ( TSizeType  new_size)

Truncate the cache leaving at most new_size elements.

Does not affect cache capacity. If new_size is zero all elements will be removed.

Definition at line 661 of file ncbi_cache.hpp.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
void CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseElement ( TCacheSet_I set_iter,
TCacheMap_I map_iter 
)
private

Definition at line 350 of file ncbi_cache.hpp.

References _ASSERT.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
void CCache< TKey, TValue, THandler, TLock, TSize >::x_EraseLast ( void  )
private

Definition at line 366 of file ncbi_cache.hpp.

References _ASSERT.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::TOrder CCache< TKey, TValue, THandler, TLock, TSize >::x_GetNextCounter ( void  )
private

Definition at line 420 of file ncbi_cache.hpp.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::TCacheElement * CCache< TKey, TValue, THandler, TLock, TSize >::x_InsertElement ( const TKeyType key,
TWeight  weight 
)
private

Definition at line 377 of file ncbi_cache.hpp.

References NCBI_THROW.

template<class TKey , class TValue , class THandler , class TLock , class TSize >
void CCache< TKey, TValue, THandler, TLock, TSize >::x_PackElementIndex ( void  )
private
template<class TKey , class TValue , class THandler , class TLock , class TSize >
void CCache< TKey, TValue, THandler, TLock, TSize >::x_UpdateElement ( TCacheElement elem)
private
template<class TKey , class TValue , class THandler , class TLock , class TSize >
CCache< TKey, TValue, THandler, TLock, TSize >::~CCache ( void  )

Definition at line 338 of file ncbi_cache.hpp.

References _ASSERT.

Modified on Fri Sep 30 18:09:55 2016 by modify_doxy.py rev. 506947