Hazelcast C++ Client
Hazelcast C++ Client Library
Loading...
Searching...
No Matches
near_cache.cpp
1/*
2 * Copyright (c) 2008-2025, Hazelcast, Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "hazelcast/client/internal/nearcache/impl/KeyStateMarkerImpl.h"
18#include "hazelcast/client/internal/nearcache/NearCacheManager.h"
19#include "hazelcast/util/HashUtil.h"
20#include "hazelcast/client/internal/eviction/EvictionChecker.h"
21
22namespace hazelcast {
23namespace client {
24namespace internal {
25namespace nearcache {
26
27NearCacheManager::NearCacheManager(
28 const std::shared_ptr<spi::impl::ClientExecutionServiceImpl>& es,
29 serialization::pimpl::SerializationService& ss,
30 logger& lg)
31 : execution_service_(es)
32 , serialization_service_(ss)
33 , logger_(lg)
34{}
35
36bool
37NearCacheManager::clear_near_cache(const std::string& name)
38{
39 std::shared_ptr<BaseNearCache> nearCache = near_cache_map_.get(name);
40 if (nearCache.get() != NULL) {
41 nearCache->clear();
42 }
43 return nearCache.get() != NULL;
44}
45
46void
47NearCacheManager::clear_all_near_caches()
48{
49 std::vector<std::shared_ptr<BaseNearCache>> caches =
50 near_cache_map_.values();
51 for (std::vector<std::shared_ptr<BaseNearCache>>::iterator it =
52 caches.begin();
53 it != caches.end();
54 ++it) {
55 (*it)->clear();
56 }
57}
58
59bool
60NearCacheManager::destroy_near_cache(const std::string& name)
61{
62 std::shared_ptr<BaseNearCache> nearCache = near_cache_map_.remove(name);
63 if (nearCache.get() != NULL) {
64 nearCache->destroy();
65 }
66 return nearCache.get() != NULL;
67}
68
69void
70NearCacheManager::destroy_all_near_caches()
71{
72 std::vector<std::shared_ptr<BaseNearCache>> caches =
73 near_cache_map_.values();
74 for (std::vector<std::shared_ptr<BaseNearCache>>::iterator it =
75 caches.begin();
76 it != caches.end();
77 ++it) {
78 (*it)->destroy();
79 }
80}
81
82std::vector<std::shared_ptr<BaseNearCache>>
83NearCacheManager::list_all_near_caches()
84{
85 return near_cache_map_.values();
86}
87
88namespace impl {
89namespace record {
90NearCacheDataRecord::NearCacheDataRecord(
91 const std::shared_ptr<serialization::pimpl::data>& data_value,
92 int64_t create_time,
93 int64_t expiry_time)
94 : AbstractNearCacheRecord<serialization::pimpl::data>(data_value,
95 create_time,
96 expiry_time)
97{}
98} // namespace record
99
100KeyStateMarkerImpl::KeyStateMarkerImpl(int count)
101 : mark_count_(count)
102 , marks_(new std::atomic<int32_t>[count])
103{
104 for (int i = 0; i < count; ++i) {
105 marks_[i] = 0;
106 }
107}
108
109KeyStateMarkerImpl::~KeyStateMarkerImpl()
110{
111 delete[] marks_;
112}
113
114bool
115KeyStateMarkerImpl::try_mark(const serialization::pimpl::data& key)
116{
117 return cas_state(key, UNMARKED, MARKED);
118}
119
120bool
121KeyStateMarkerImpl::try_unmark(const serialization::pimpl::data& key)
122{
123 return cas_state(key, MARKED, UNMARKED);
124}
125
126bool
127KeyStateMarkerImpl::try_remove(const serialization::pimpl::data& key)
128{
129 return cas_state(key, MARKED, REMOVED);
130}
131
132void
133KeyStateMarkerImpl::force_unmark(const serialization::pimpl::data& key)
134{
135 int slot = get_slot(key);
136 marks_[slot] = UNMARKED;
137}
138
139void
140KeyStateMarkerImpl::init()
141{
142 for (int i = 0; i < mark_count_; ++i) {
143 marks_[i] = UNMARKED;
144 }
145}
146
147bool
148KeyStateMarkerImpl::cas_state(const serialization::pimpl::data& key,
149 state expect,
150 state update)
151{
152 int slot = get_slot(key);
153 int expected = expect;
154 return marks_[slot].compare_exchange_strong(expected, update);
155}
156
157int
158KeyStateMarkerImpl::get_slot(const serialization::pimpl::data& key)
159{
160 return util::HashUtil::hash_to_index(key.get_partition_hash(), mark_count_);
161}
162
163} // namespace impl
164} // namespace nearcache
165
166namespace eviction {
167bool
168EvictAlways::is_eviction_required() const
169{
170 // Evict always at any case
171 return true;
172}
173
174const std::unique_ptr<EvictionChecker> EvictionChecker::EVICT_ALWAYS =
175 std::unique_ptr<EvictionChecker>(new EvictAlways());
176} // namespace eviction
177} // namespace internal
178
179namespace map {
180namespace impl {
181namespace nearcache {
182bool
183TrueMarkerImpl::try_mark(const serialization::pimpl::data& /* key */)
184{
185 return true;
186}
187
188bool
189TrueMarkerImpl::try_unmark(const serialization::pimpl::data& /* key */)
190{
191 return true;
192}
193
194bool
195TrueMarkerImpl::try_remove(const serialization::pimpl::data& /* key */)
196{
197 return true;
198}
199
200void
201TrueMarkerImpl::force_unmark(const serialization::pimpl::data& /* key */)
202{}
203
204void
205TrueMarkerImpl::init()
206{}
207
208const std::unique_ptr<KeyStateMarker> KeyStateMarker::TRUE_MARKER =
209 std::unique_ptr<KeyStateMarker>(new TrueMarkerImpl());
210} // namespace nearcache
211} // namespace impl
212} // namespace map
213} // namespace client
214} // namespace hazelcast
STL namespace.