8#ifndef INCLUDED_BDLCC_CACHE
9#define INCLUDED_BDLCC_CACHE
350#include <bslim_printer.h>
368#include <bsl_memory.h>
370#include <bsl_unordered_map.h>
372#include <bsl_vector.h>
373#include <bsl_functional.h>
374#include <bsl_iostream.h>
375#include <bsl_limits.h>
376#include <bsl_cstddef.h>
490 bsl::size_t d_lowWatermark;
494 bsl::size_t d_highWatermark;
512 void enforceHighWatermark();
526 bool insertValuePtrMoveImp(KEY *key_p,
591 template <
class INPUT_ITERATOR>
592 int eraseBulk(INPUT_ITERATOR begin, INPUT_ITERATOR end);
599 void insert(
const KEY& key,
const VALUE& value);
624 template <
class INPUT_ITERATOR>
659 bool modifyEvictionQueue =
true);
690 template <
class VISITOR>
744 return !d_queue_p || d_queue_p->empty() ? 0
745 : &*d_queue_p->rbegin();
761 while (last() != d_last_p) {
762 d_queue_p->pop_back();
780template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
782: d_allocator_p(
bslma::Default::allocator(basicAllocator))
783, d_map(d_allocator_p)
784, d_queue(d_allocator_p)
786, d_lowWatermark(
bsl::numeric_limits<
bsl::size_t>::max())
787, d_highWatermark(
bsl::numeric_limits<
bsl::size_t>::max())
788, d_postEvictionCallback(
bsl::allocator_arg, d_allocator_p)
792template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
795 bsl::size_t lowWatermark,
796 bsl::size_t highWatermark,
798: d_allocator_p(
bslma::Default::allocator(basicAllocator))
799, d_map(d_allocator_p)
800, d_queue(d_allocator_p)
801, d_evictionPolicy(evictionPolicy)
802, d_lowWatermark(lowWatermark)
803, d_highWatermark(highWatermark)
804, d_postEvictionCallback(
bsl::allocator_arg, d_allocator_p)
811template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
814 bsl::size_t lowWatermark,
815 bsl::size_t highWatermark,
816 const HASH& hashFunction,
817 const EQUAL& equalFunction,
819: d_allocator_p(
bslma::Default::allocator(basicAllocator))
820, d_map(0, hashFunction, equalFunction, d_allocator_p)
821, d_queue(d_allocator_p)
822, d_evictionPolicy(evictionPolicy)
823, d_lowWatermark(lowWatermark)
824, d_highWatermark(highWatermark)
825, d_postEvictionCallback(
bsl::allocator_arg, d_allocator_p)
833template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
836 if (d_map.size() < d_highWatermark) {
840 while (d_map.size() >= d_lowWatermark && d_map.size() > 0) {
841 const typename MapType::iterator mapIt = d_map.find(d_queue.front());
847template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
848void Cache<KEY, VALUE, HASH, EQUAL>::evictItem(
849 const typename MapType::iterator& mapIt)
851 ValuePtrType value = mapIt->second.first;
853 d_queue.erase(mapIt->second.second);
856 if (d_postEvictionCallback) {
857 d_postEvictionCallback(value);
860template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
862bool Cache<KEY, VALUE, HASH, EQUAL>::insertValuePtrMoveImp(
865 ValuePtrType *valuePtr_p,
868#if defined(BSLMF_MOVABLEREF_USES_RVALUE_REFERENCES)
869 enum { k_RVALUE_ASSIGN =
true };
871 enum { k_RVALUE_ASSIGN =
false };
874 enforceHighWatermark();
877 ValuePtrType& valuePtr = *valuePtr_p;
879 typename MapType::iterator mapIt = d_map.find(key);
880 if (mapIt != d_map.end()) {
881 if (k_RVALUE_ASSIGN && moveValuePtr) {
885 mapIt->second.first = valuePtr;
888 typename QueueType::iterator queueIt = mapIt->second.second;
892 d_queue.splice(d_queue.end(), d_queue, queueIt);
897 Cache_QueueProctor<KEY> proctor(&d_queue);
898 d_queue.push_back(key);
899 typename QueueType::iterator queueIt = d_queue.end();
903 MapValue *mapValue_p = mapValueFootprint.
address();
911 new (mapValue_p) MapValue(valuePtr,
933template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
941template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
946 const typename MapType::iterator mapIt = d_map.find(key);
947 if (mapIt == d_map.end()) {
955template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
956template <
class INPUT_ITERATOR>
963 for (; begin != end; ++begin) {
964 const typename MapType::iterator mapIt = d_map.find(*begin);
965 if (mapIt == d_map.end()) {
975template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
979 return eraseBulk(keys.
begin(), keys.
end());
982template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
988 KEY *key_p =
const_cast<KEY *
>(&key);
989 ValuePtrType valuePtr = bsl::allocate_shared<VALUE>(d_allocator_p, value);
991 insertValuePtrMoveImp(key_p,
false, &valuePtr,
true);
994template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1000 KEY *key_p =
const_cast<KEY *
>(&key);
1006 insertValuePtrMoveImp(key_p,
false, &valuePtr,
true);
1009template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1015 KEY& localKey = key;
1016 ValuePtrType valuePtr = bsl::allocate_shared<VALUE>(d_allocator_p, value);
1019 insertValuePtrMoveImp(&localKey,
true, &valuePtr,
true);
1022template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1028 KEY& localKey = key;
1034 insertValuePtrMoveImp(&localKey,
true, &valuePtr,
true);
1037template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1044 KEY *key_p =
const_cast<KEY *
>(&key);
1047 insertValuePtrMoveImp(key_p,
false, valuePtr_p,
false);
1050template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1056 KEY& localKey = key;
1059 insertValuePtrMoveImp(&localKey,
true, valuePtr_p,
false);
1062template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1063template <
class INPUT_ITERATOR>
1070 for (; begin != end; ++begin) {
1071 KEY *key_p =
const_cast<KEY *
>( &begin->first);
1074 count += insertValuePtrMoveImp(key_p,
false, valuePtr_p,
false);
1080template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1084 return insertBulk(data.begin(), data.end());
1087template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1098 for (
typename Vec::iterator it = local.begin(); it < local.end(); ++it) {
1099 KEY *key_p = &it->first;
1102 count += insertValuePtrMoveImp(key_p,
true, valuePtr_p,
true);
1107template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1113 if (d_map.size() > 0) {
1114 const typename MapType::iterator mapIt = d_map.find(d_queue.front());
1123template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1128 d_postEvictionCallback = postEvictionCallback;
1131template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1135 bool modifyEvictionQueue)
1138 modifyEvictionQueue ? 1 : 0;
1140 d_rwlock.lockWrite();
1143 d_rwlock.lockRead();
1152 typename MapType::iterator mapIt = d_map.find(key);
1153 if (mapIt == d_map.end()) {
1157 *value = mapIt->second.first;
1163 if (last != queueIt) {
1164 d_queue.splice(d_queue.end(), d_queue, queueIt);
1172template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1176 return d_map.key_eq();
1179template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1184 return d_evictionPolicy;
1187template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1191 return d_map.hash_function();
1194template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1198 return d_highWatermark;
1201template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1205 return d_lowWatermark;
1208template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1213 return d_map.size();
1216template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1217template <
class VISITOR>
1223 queueIt != d_queue.end(); ++queueIt) {
1225 const KEY& key = *queueIt;
1230 if (!visitor(key, *valuePtr)) {
1241template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1250template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1254 d_cache.d_rwlock.lockRead();
1257template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1261 d_cache.d_rwlock.lockWrite();
1264template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
1268 d_cache.d_rwlock.unlock();
1276template <
class KEY,
class VALUE,
class HASH,
class EQUAL>
Definition bdlcc_cache.h:400
Cache_QueueProctor(bsl::list< KEY > *queue)
Create a Cache_QueueProctor object to monitor the specified queue.
Definition bdlcc_cache.h:751
void release()
Definition bdlcc_cache.h:770
~Cache_QueueProctor()
Definition bdlcc_cache.h:758
Definition bdlcc_cache.h:703
void lockRead()
Call the lockRead method of bdlcc::Cache d_rwlock lock.
Definition bdlcc_cache.h:1252
~Cache_TestUtil()=default
Destroy this object.
void unlock()
Call the unlock method of bdlcc::Cache d_rwlock lock.
Definition bdlcc_cache.h:1266
Cache_TestUtil(Cache< KEY, VALUE, HASH, EQUAL > &cache)
Definition bdlcc_cache.h:1243
void lockWrite()
Call the lockWrite method of bdlcc::Cache d_rwlock lock.
Definition bdlcc_cache.h:1259
Definition bdlcc_cache.h:444
bsl::size_t lowWatermark() const
Definition bdlcc_cache.h:1203
void clear()
Definition bdlcc_cache.h:934
void insert(bslmf::MovableRef< KEY > key, const ValuePtrType &valuePtr)
Definition bdlcc_cache.h:1051
HASH hashFunction() const
Definition bdlcc_cache.h:1189
int insertBulk(bslmf::MovableRef< bsl::vector< KVType > > data)
Definition bdlcc_cache.h:1088
void insert(const KEY &key, bslmf::MovableRef< VALUE > value)
Definition bdlcc_cache.h:995
bsl::size_t size() const
Return the current size of this cache.
Definition bdlcc_cache.h:1210
int tryGetValue(bsl::shared_ptr< VALUE > *value, const KEY &key, bool modifyEvictionQueue=true)
Definition bdlcc_cache.h:1132
int insertBulk(const bsl::vector< KVType > &data)
Definition bdlcc_cache.h:1082
void insert(const KEY &key, const VALUE &value)
Definition bdlcc_cache.h:984
void insert(const KEY &key, const ValuePtrType &valuePtr)
Definition bdlcc_cache.h:1039
Cache(bslma::Allocator *basicAllocator=0)
Definition bdlcc_cache.h:781
void visit(VISITOR &visitor) const
Definition bdlcc_cache.h:1218
int eraseBulk(INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition bdlcc_cache.h:957
bsl::pair< KEY, ValuePtrType > KVType
Value type of a bulk insert entry.
Definition bdlcc_cache.h:456
void insert(bslmf::MovableRef< KEY > key, bslmf::MovableRef< VALUE > value)
Definition bdlcc_cache.h:1023
Cache(CacheEvictionPolicy::Enum evictionPolicy, bsl::size_t lowWatermark, bsl::size_t highWatermark, bslma::Allocator *basicAllocator=0)
Definition bdlcc_cache.h:793
bsl::function< void(const ValuePtrType &)> PostEvictionCallback
Type of function to call after an item has been evicted from the cache.
Definition bdlcc_cache.h:453
Cache(CacheEvictionPolicy::Enum evictionPolicy, bsl::size_t lowWatermark, bsl::size_t highWatermark, const HASH &hashFunction, const EQUAL &equalFunction, bslma::Allocator *basicAllocator=0)
Definition bdlcc_cache.h:812
void insert(bslmf::MovableRef< KEY > key, const VALUE &value)
Definition bdlcc_cache.h:1010
~Cache()=default
Destroy this object.
CacheEvictionPolicy::Enum evictionPolicy() const
Return the eviction policy used by this cache.
Definition bdlcc_cache.h:1182
EQUAL equalFunction() const
Definition bdlcc_cache.h:1174
int eraseBulk(const bsl::vector< KEY > &keys)
Definition bdlcc_cache.h:977
int insertBulk(INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition bdlcc_cache.h:1064
void setPostEvictionCallback(const PostEvictionCallback &postEvictionCallback)
Definition bdlcc_cache.h:1124
int popFront()
Definition bdlcc_cache.h:1109
bsl::shared_ptr< VALUE > ValuePtrType
Shared pointer type pointing to value type.
Definition bdlcc_cache.h:450
int erase(const KEY &key)
Definition bdlcc_cache.h:942
bsl::size_t highWatermark() const
Definition bdlcc_cache.h:1196
Definition bslstl_list.h:739
Forward declaration.
Definition bslstl_function.h:934
Forward declaration required by List_NodeProctor.
Definition bslstl_list.h:1033
Definition bslstl_pair.h:1210
Definition bslstl_sharedptr.h:1830
Definition bslstl_unorderedmap.h:1089
BloombergLP::bslstl::HashTableIterator< value_type, difference_type > iterator
Definition bslstl_unorderedmap.h:1158
BloombergLP::bslstl::HashTableIterator< const value_type, difference_type > const_iterator
Definition bslstl_unorderedmap.h:1160
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2511
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_vector.h:2519
Definition bslstl_vector.h:1025
Definition bslma_allocator.h:457
Definition bslma_destructorguard.h:132
Definition bslmf_movableref.h:751
Definition bslmt_readlockguard.h:287
Definition bslmt_readerwritermutex.h:244
Definition bslmt_writelockguard.h:221
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_REVIEW(X)
Definition bsls_review.h:949
Definition bdlcc_boundedqueue.h:270
Definition bdlb_printmethods.h:283
Definition balxml_encoderoptions.h:68
Definition bdlcc_cache.h:381
Enum
Enumeration of supported cache eviction policies.
Definition bdlcc_cache.h:386
@ e_LRU
Definition bdlcc_cache.h:388
@ e_FIFO
Definition bdlcc_cache.h:389
Definition bslstl_equalto.h:311
Definition bslstl_hash.h:498
Definition bslma_usesbslmaallocator.h:343
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1060
Definition bsls_objectbuffer.h:276
TYPE * address()
Definition bsls_objectbuffer.h:334