Hazelcast C++ Client
Hazelcast C++ Client Library
near_cache.cpp
1 /*
2  * Copyright (c) 2008-2021, 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 /*
18  * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
19  *
20  * Licensed under the Apache License, Version 2.0 (the "License");
21  * you may not use this file except in compliance with the License.
22  * You may obtain a copy of the License at
23  *
24  * http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */
32 
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"
37 
38 namespace hazelcast {
39  namespace client {
40  namespace internal {
41  namespace nearcache {
42 
43  NearCacheManager::NearCacheManager(const std::shared_ptr<spi::impl::ClientExecutionServiceImpl> &es,
44  serialization::pimpl::SerializationService &ss,
45  logger &lg)
46  : execution_service_(es), serialization_service_(ss), logger_(lg) {
47  }
48 
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) {
52  nearCache->clear();
53  }
54  return nearCache.get() != NULL;
55  }
56 
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) {
61  (*it)->clear();
62  }
63  }
64 
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) {
68  nearCache->destroy();
69  }
70  return nearCache.get() != NULL;
71  }
72 
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) {
77  (*it)->destroy();
78  }
79  }
80 
81  std::vector<std::shared_ptr<BaseNearCache> > NearCacheManager::list_all_near_caches() {
82  return near_cache_map_.values();
83  }
84 
85  namespace impl {
86  namespace record {
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,
91  create_time,
92  expiry_time) {
93  }
94  }
95 
96  KeyStateMarkerImpl::KeyStateMarkerImpl(int count) : mark_count_(count),
97  marks_(new std::atomic<int32_t>[count]) {
98  for (int i = 0; i < count; ++i) {
99  marks_[i] = 0;
100  }
101  }
102 
103  KeyStateMarkerImpl::~KeyStateMarkerImpl() {
104  delete[] marks_;
105  }
106 
107  bool KeyStateMarkerImpl::try_mark(const serialization::pimpl::data &key) {
108  return cas_state(key, UNMARKED, MARKED);
109  }
110 
111  bool KeyStateMarkerImpl::try_unmark(const serialization::pimpl::data &key) {
112  return cas_state(key, MARKED, UNMARKED);
113  }
114 
115  bool KeyStateMarkerImpl::try_remove(const serialization::pimpl::data &key) {
116  return cas_state(key, MARKED, REMOVED);
117  }
118 
119  void KeyStateMarkerImpl::force_unmark(const serialization::pimpl::data &key) {
120  int slot = get_slot(key);
121  marks_[slot] = UNMARKED;
122  }
123 
124  void KeyStateMarkerImpl::init() {
125  for (int i = 0; i < mark_count_; ++i) {
126  marks_[i] = UNMARKED;
127  }
128  }
129 
130  bool
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);
135  }
136 
137  int KeyStateMarkerImpl::get_slot(const serialization::pimpl::data &key) {
138  return util::HashUtil::hash_to_index(key.get_partition_hash(), mark_count_);
139  }
140 
141  }
142  }
143 
144  namespace eviction {
145  bool EvictAlways::is_eviction_required() const {
146  // Evict always at any case
147  return true;
148  }
149 
150  const std::unique_ptr<EvictionChecker> EvictionChecker::EVICT_ALWAYS = std::unique_ptr<EvictionChecker>(
151  new EvictAlways());
152  }
153  }
154 
155  namespace map {
156  namespace impl {
157  namespace nearcache {
158  bool TrueMarkerImpl::try_mark(const serialization::pimpl::data &key) {
159  return true;
160  }
161 
162  bool TrueMarkerImpl::try_unmark(const serialization::pimpl::data &key) {
163  return true;
164  }
165 
166  bool TrueMarkerImpl::try_remove(const serialization::pimpl::data &key) {
167  return true;
168  }
169 
170  void TrueMarkerImpl::force_unmark(const serialization::pimpl::data &key) {
171  }
172 
173  void TrueMarkerImpl::init() {
174  }
175 
176  const std::unique_ptr<KeyStateMarker> KeyStateMarker::TRUE_MARKER =
177  std::unique_ptr<KeyStateMarker>(new TrueMarkerImpl());
178  }
179  }
180  }
181  }
182 }
183