<
classTKey,
classTSize>
80 template<
classTCacheElementPtr>
84 constTCacheElementPtr& y)
const 87 if(x->m_Weight != y->m_Weight) {
88 returnx->m_Weight < y->m_Weight;
90 returnx->m_Order < y->m_Order;
105 template<
classTKey,
classTValue>
147 template<
classTKey,
151 classTSize =
Uint4>
325 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
328: m_Capacity(capacity),
337 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
342 while( !m_CacheSet.empty() ) {
349 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
354 _ASSERT(set_iter != m_CacheSet.end());
355 _ASSERT(map_iter != m_CacheMap.end());
358m_Handler->RemoveElement(map_iter->first, map_iter->second.m_Value);
359m_CacheMap.erase(map_iter);
360m_CacheSet.erase(set_iter);
365 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
370 TCacheMap_Imap_iter = m_CacheMap.find((*set_iter)->m_Key);
371x_EraseElement(set_iter, map_iter);
375 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
385 if(adjusted_weight <
weight) {
386x_PackElementIndex();
388 if(adjusted_weight <
weight) {
390 "Cache element weight overflow");
395m_CacheSet.insert(elem);
400 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
406 _ASSERT(it != m_CacheSet.end());
408m_CacheSet.erase(it);
409elem->
m_Order= x_GetNextCounter();
411x_PackElementIndex();
414m_CacheSet.insert(elem);
418 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
423x_PackElementIndex();
429 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
435 if( !m_CacheSet.empty() ) {
436 TWeightweight_shift = (
TWeight)((*m_CacheSet.begin())->m_Weight - 1);
437 TWeightweight_max = weight_shift;
442 if(e->
m_Order>= (order_shift + order_min)/2) {
453order_shift = (
TOrder)(order_shift - order_min);
454 if(order_shift < 2) {
459orders.insert((*it)->m_Order);
461 if(*orders.rbegin() < order_max) {
462m_Counter = *orders.rbegin();
463order_min = order_max;
471 ITERATE(
typenameTOrderSet, it, orders) {
472 if(*it -
last> rg_to - rg_from) {
478 if(rg_to - rg_from < 2) {
480 "Cache element index overflow");
483order_shift = (
TOrder)(rg_to - rg_from);
486 if(weight_shift <= 1 && weight_max + 1 <= 0) {
489 "Cache element weight overflow");
501m_Counter = (
TOrder)(m_Counter - order_shift);
505 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
515 if(it != m_CacheMap.end() ) {
516 if((add_flags & fAdd_NoReplace) != 0) {
518*
result= eAdd_NotInserted;
522 TCacheSet_Iset_it = m_CacheSet.find(it->second.m_CacheElement);
523x_EraseElement(set_it, it);
533ins_flag = m_Handler->CanInsertElement(
key,
value)) {
535 while(GetSize() >= m_Capacity) {
545*
result= eAdd_NotInserted;
550 if( GetSize() == 0 ) {
553*
result= eAdd_NotInserted;
561m_Handler->InsertElement(
key,
value);
571 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
578 if(it != m_CacheMap.end()) {
579x_UpdateElement(it->second.m_CacheElement);
580 returnit->second.m_Value;
588 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
597 if(it != m_CacheMap.end()) {
598 if((get_flags & fGet_NoTouch) == 0) {
599x_UpdateElement(it->second.m_CacheElement);
604 returnit->second.m_Value;
608 if((get_flags & fGet_NoCreate) != 0) {
610 "Can not find the requested key");
614 if((get_flags & fGet_NoInsert) == 0) {
618*
result= eGet_CreatedAndAdded;
623*
result= eGet_CreatedNotAdded;
630 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
636 if(it == m_CacheMap.end()) {
639 TCacheSet_Iset_it = m_CacheSet.find(it->second.m_CacheElement);
640x_EraseElement(set_it, it);
645 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
648 if(new_capacity <= 0) {
650 "Cache capacity must be positive");
653 while(GetSize() > new_capacity) {
656m_Capacity = new_capacity;
660 template<
classTKey,
classTValue,
classTHandler,
classTLock,
classTSize>
664 while(GetSize() > new_size) {
Default (NOP) element handler.
Exception thrown by CCache.
container_type::iterator iterator
const_iterator begin() const
parent_type::iterator iterator
void(*)(CSeq_entry_Handle seh, IWorkbench *wb, const CSerialObject &obj) handler
Include a standard set of the NCBI C++ Toolkit most basic headers.
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
#define NON_CONST_ITERATE(Type, Var, Cont)
Non constant version of ITERATE macro.
EAddFlags
Flags to control the details of adding new elements to the cache via Add().
TLockType::TWriteLockGuard TGuardType
CCache & operator=(const CCache &)
TSizeType GetSize(void) const
Get current number of elements in the cache.
bool Remove(const TKeyType &key)
Remove element from cache. Do nothing if the key is not cached.
int TAddFlags
bitwise OR of EAddFlags
void SetSize(TSizeType new_size)
Truncate the cache leaving at most new_size elements.
bool operator()(const TCacheElementPtr &x, const TCacheElementPtr &y) const
TValueType Get(const TKeyType &key, TGetFlags get_flags=0, EGetResult *result=NULL)
Get an object from the cache by its key.
NCBI_EXCEPTION_DEFAULT(CCacheException, CException)
int TGetFlags
bitwise OR of EGetFlags
ECache_InsertFlag
Flag indicating if an element can be inserted into cache.
SCacheElement< TKeyType, TSizeType > TCacheElement
TValueType operator[](const TKeyType &key)
Get cache element by the key.
set< TCacheElement *, TCacheLess > TCacheSet
void SetCapacity(TSizeType new_capacity)
Set new capacity of the cache.
void RemoveElement(const TKey &, TValue &)
Special processing of a removed element (e.g. deleting the object)
unique_ptr< THandlerType > m_Handler
EGetFlags
Cache retrieval flags.
CMutex TCacheLock_Default
Default cache lock.
void x_UpdateElement(TCacheElement *elem)
TCacheElement * x_InsertElement(const TKeyType &key, TWeight weight)
map< TKeyType, SValueWithIndex > TCacheMap
void InsertElement(const TKey &, const TValue &)
Special processing of an element to be inserted (e.g.
TSizeType GetCapacity(void) const
Get current capacity of the cache (max allowed number of elements)
TCacheSet::iterator TCacheSet_I
CCacheElement_Less< TCacheElement * > TCacheLess
TWeight x_GetBaseWeight(void) const
TCacheMap::iterator TCacheMap_I
TValue CreateValue(const TKey &)
Element factory â to create elements by key.
TOrder x_GetNextCounter(void)
TCacheElement * m_CacheElement
TOrder 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.
EAddResult
Result of element insertion.
void x_EraseElement(TCacheSet_I &set_iter, TCacheMap_I &map_iter)
void x_PackElementIndex(void)
CCache(TSizeType capacity, THandler *handler=NULL)
Create cache object with the given capacity.
ECache_InsertFlag CanInsertElement(const TKey &, const TValue &)
Check if the element can be inserted into the cache.
SCacheElement(const TKeyType &key, const TSizeType &weight, const TSizeType &order)
@ eIndexOverflow
Element index overflow.
@ eWeightOverflow
Element weight overflow.
@ eNotFound
The requested key was not found in the cache.
@ fAdd_NoReplace
Do not replace existing values if any.
@ eCache_CanInsert
The element can be inserted.
@ eCache_DoNotCache
The element can not be inserted (e.g. too big)
@ eCache_CheckSize
Insert the element after checking max cache size.
@ eCache_NeedCleanup
Need to cleanup cache before inserting the element.
@ fGet_NoTouch
Do not update the object's position.
@ fGet_NoInsert
Do not insert created values.
@ fGet_NoCreate
Do not create value if not found, throw an exception instead.
@ eGet_CreatedNotAdded
A new value was created but not cached.
@ eGet_CreatedAndAdded
A new value was created and cached.
@ eGet_Found
The key was found in the cache.
@ eAdd_NotInserted
The element was not added or replaced.
@ eAdd_Inserted
The element was added to the cache.
@ eAdd_Replaced
The element existed and was replaced.
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
EErrCode
Error types that an application can generate.
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
uint32_t Uint4
4-byte (32-bit) unsigned integer
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
#define NCBI_XNCBI_EXPORT
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Multi-threading â mutexes; rw-locks; semaphore.
Compare cache elements by weight/order.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4