33 #include "hazelcast/client/internal/nearcache/impl/KeyStateMarkerImpl.h"
34 #include "hazelcast/client/internal/nearcache/NearCacheManager.h"
35 #include "hazelcast/util/HashUtil.h"
36 #include "hazelcast/client/internal/eviction/EvictionChecker.h"
43 NearCacheManager::NearCacheManager(
const std::shared_ptr<spi::impl::ClientExecutionServiceImpl> &es,
44 serialization::pimpl::SerializationService &ss,
46 : execution_service_(es), serialization_service_(ss), logger_(lg) {
49 bool NearCacheManager::clear_near_cache(
const std::string &name) {
50 std::shared_ptr<BaseNearCache> nearCache = near_cache_map_.get(name);
51 if (nearCache.get() != NULL) {
54 return nearCache.get() != NULL;
57 void NearCacheManager::clear_all_near_caches() {
58 std::vector<std::shared_ptr<BaseNearCache> > caches = near_cache_map_.values();
59 for (std::vector<std::shared_ptr<BaseNearCache> >::iterator it = caches.begin();
60 it != caches.end(); ++it) {
65 bool NearCacheManager::destroy_near_cache(
const std::string &name) {
66 std::shared_ptr<BaseNearCache> nearCache = near_cache_map_.remove(name);
67 if (nearCache.get() != NULL) {
70 return nearCache.get() != NULL;
73 void NearCacheManager::destroy_all_near_caches() {
74 std::vector<std::shared_ptr<BaseNearCache> > caches = near_cache_map_.values();
75 for (std::vector<std::shared_ptr<BaseNearCache> >::iterator it = caches.begin();
76 it != caches.end(); ++it) {
81 std::vector<std::shared_ptr<BaseNearCache> > NearCacheManager::list_all_near_caches() {
82 return near_cache_map_.values();
87 NearCacheDataRecord::NearCacheDataRecord(
88 const std::shared_ptr<serialization::pimpl::data> &data_value,
89 int64_t create_time, int64_t expiry_time)
90 : AbstractNearCacheRecord<serialization::pimpl::data>(data_value,
96 KeyStateMarkerImpl::KeyStateMarkerImpl(
int count) : mark_count_(count),
97 marks_(new std::atomic<int32_t>[count]) {
98 for (
int i = 0; i < count; ++i) {
103 KeyStateMarkerImpl::~KeyStateMarkerImpl() {
107 bool KeyStateMarkerImpl::try_mark(
const serialization::pimpl::data &key) {
108 return cas_state(key, UNMARKED, MARKED);
111 bool KeyStateMarkerImpl::try_unmark(
const serialization::pimpl::data &key) {
112 return cas_state(key, MARKED, UNMARKED);
115 bool KeyStateMarkerImpl::try_remove(
const serialization::pimpl::data &key) {
116 return cas_state(key, MARKED, REMOVED);
119 void KeyStateMarkerImpl::force_unmark(
const serialization::pimpl::data &key) {
120 int slot = get_slot(key);
121 marks_[slot] = UNMARKED;
124 void KeyStateMarkerImpl::init() {
125 for (
int i = 0; i < mark_count_; ++i) {
126 marks_[i] = UNMARKED;
131 KeyStateMarkerImpl::cas_state(
const serialization::pimpl::data &key, state expect, state update) {
132 int slot = get_slot(key);
133 int expected = expect;
134 return marks_[slot].compare_exchange_strong(expected, update);
137 int KeyStateMarkerImpl::get_slot(
const serialization::pimpl::data &key) {
138 return util::HashUtil::hash_to_index(key.get_partition_hash(), mark_count_);
145 bool EvictAlways::is_eviction_required()
const {
150 const std::unique_ptr<EvictionChecker> EvictionChecker::EVICT_ALWAYS = std::unique_ptr<EvictionChecker>(
157 namespace nearcache {
158 bool TrueMarkerImpl::try_mark(
const serialization::pimpl::data &key) {
162 bool TrueMarkerImpl::try_unmark(
const serialization::pimpl::data &key) {
166 bool TrueMarkerImpl::try_remove(
const serialization::pimpl::data &key) {
170 void TrueMarkerImpl::force_unmark(
const serialization::pimpl::data &key) {
173 void TrueMarkerImpl::init() {
176 const std::unique_ptr<KeyStateMarker> KeyStateMarker::TRUE_MARKER =
177 std::unique_ptr<KeyStateMarker>(
new TrueMarkerImpl());