Hazelcast C++ Client
Hazelcast C++ Client Library
near_cache.cpp
1 /*
2  * Copyright (c) 2008-2022, 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 
22 namespace hazelcast {
23 namespace client {
24 namespace internal {
25 namespace nearcache {
26 
27 NearCacheManager::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 
36 bool
37 NearCacheManager::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 
46 void
47 NearCacheManager::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 
59 bool
60 NearCacheManager::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 
69 void
70 NearCacheManager::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 
82 std::vector<std::shared_ptr<BaseNearCache>>
83 NearCacheManager::list_all_near_caches()
84 {
85  return near_cache_map_.values();
86 }
87 
88 namespace impl {
89 namespace record {
90 NearCacheDataRecord::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 
100 KeyStateMarkerImpl::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 
109 KeyStateMarkerImpl::~KeyStateMarkerImpl()
110 {
111  delete[] marks_;
112 }
113 
114 bool
115 KeyStateMarkerImpl::try_mark(const serialization::pimpl::data& key)
116 {
117  return cas_state(key, UNMARKED, MARKED);
118 }
119 
120 bool
121 KeyStateMarkerImpl::try_unmark(const serialization::pimpl::data& key)
122 {
123  return cas_state(key, MARKED, UNMARKED);
124 }
125 
126 bool
127 KeyStateMarkerImpl::try_remove(const serialization::pimpl::data& key)
128 {
129  return cas_state(key, MARKED, REMOVED);
130 }
131 
132 void
133 KeyStateMarkerImpl::force_unmark(const serialization::pimpl::data& key)
134 {
135  int slot = get_slot(key);
136  marks_[slot] = UNMARKED;
137 }
138 
139 void
140 KeyStateMarkerImpl::init()
141 {
142  for (int i = 0; i < mark_count_; ++i) {
143  marks_[i] = UNMARKED;
144  }
145 }
146 
147 bool
148 KeyStateMarkerImpl::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 
157 int
158 KeyStateMarkerImpl::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 
166 namespace eviction {
167 bool
168 EvictAlways::is_eviction_required() const
169 {
170  // Evict always at any case
171  return true;
172 }
173 
174 const std::unique_ptr<EvictionChecker> EvictionChecker::EVICT_ALWAYS =
175  std::unique_ptr<EvictionChecker>(new EvictAlways());
176 } // namespace eviction
177 } // namespace internal
178 
179 namespace map {
180 namespace impl {
181 namespace nearcache {
182 bool
183 TrueMarkerImpl::try_mark(const serialization::pimpl::data& key)
184 {
185  return true;
186 }
187 
188 bool
189 TrueMarkerImpl::try_unmark(const serialization::pimpl::data& key)
190 {
191  return true;
192 }
193 
194 bool
195 TrueMarkerImpl::try_remove(const serialization::pimpl::data& key)
196 {
197  return true;
198 }
199 
200 void
201 TrueMarkerImpl::force_unmark(const serialization::pimpl::data& key)
202 {}
203 
204 void
205 TrueMarkerImpl::init()
206 {}
207 
208 const 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