Hazelcast C++ Client
NearCachedClientMapProxy.h
1 /*
2  * Copyright (c) 2008-2018, 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 #ifndef HAZELCAST_CLIENT_NEARCACHEDMIXEDMAP_H_
17 #define HAZELCAST_CLIENT_NEARCACHEDMIXEDMAP_H_
18 
19 #include "hazelcast/client/internal/nearcache/NearCache.h"
20 #include "hazelcast/client/protocol/codec/MapAddNearCacheEntryListenerCodec.h"
21 #include "hazelcast/client/mixedtype/ClientMapProxy.h"
22 #include "hazelcast/client/config/NearCacheConfig.h"
23 
24 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
25 #pragma warning(push)
26 #pragma warning(disable: 4251) //for dll export
27 #endif
28 
29 namespace hazelcast {
30  namespace client {
31  namespace map {
32  namespace impl {
33  namespace nearcache {
34  class KeyStateMarker;
35  }
36  }
37  }
38 
39  namespace mixedtype {
51  class HAZELCAST_API NearCachedClientMapProxy : public ClientMapProxy {
52  public:
53  NearCachedClientMapProxy(const std::string &instanceName, spi::ClientContext *context,
55 
56  virtual monitor::LocalMapStats &getLocalMapStats();
57 
58  protected:
59  //@override
60  void onInitialize();
61 
62  //@Override
63  bool containsKeyInternal(const serialization::pimpl::Data &keyData);
64 
65  //@override
66  boost::shared_ptr<TypedData> getInternal(serialization::pimpl::Data &keyData);
67 
68  //@Override
69  virtual std::auto_ptr<serialization::pimpl::Data> removeInternal(
70  const serialization::pimpl::Data &key);
71 
72  //@Override
73  virtual bool removeInternal(
74  const serialization::pimpl::Data &key, const serialization::pimpl::Data &value);
75 
76  virtual void removeAllInternal(const serialization::pimpl::Data &predicateData);
77 
78  virtual void deleteInternal(const serialization::pimpl::Data &key);
79 
80  virtual bool tryRemoveInternal(const serialization::pimpl::Data &key, long timeoutInMillis);
81 
82  virtual bool
83  tryPutInternal(const serialization::pimpl::Data &key, const serialization::pimpl::Data &value,
84  long timeoutInMillis);
85 
86  virtual std::auto_ptr<serialization::pimpl::Data> putInternal(const serialization::pimpl::Data &key,
87  const serialization::pimpl::Data &value,
88  long timeoutInMillis);
89 
90  virtual void tryPutTransientInternal(const serialization::pimpl::Data &key,
91  const serialization::pimpl::Data &value, int ttlInMillis);
92 
93  virtual std::auto_ptr<serialization::pimpl::Data>
94  putIfAbsentInternal(const serialization::pimpl::Data &keyData,
95  const serialization::pimpl::Data &valueData,
96  int ttlInMillis);
97 
98  virtual bool replaceIfSameInternal(const serialization::pimpl::Data &keyData,
99  const serialization::pimpl::Data &valueData,
100  const serialization::pimpl::Data &newValueData);
101 
102  virtual std::auto_ptr<serialization::pimpl::Data>
103  replaceInternal(const serialization::pimpl::Data &keyData,
104  const serialization::pimpl::Data &valueData);
105 
106  virtual void
107  setInternal(const serialization::pimpl::Data &keyData, const serialization::pimpl::Data &valueData,
108  int ttlInMillis);
109 
110  virtual bool evictInternal(const serialization::pimpl::Data &keyData);
111 
112  virtual EntryVector getAllInternal(const ClientMapProxy::PID_TO_KEY_MAP &pIdToKeyData);
113 
114  virtual std::auto_ptr<serialization::pimpl::Data>
115  executeOnKeyInternal(const serialization::pimpl::Data &keyData,
116  const serialization::pimpl::Data &processor);
117 
118  virtual void
119  putAllInternal(const std::map<int, EntryVector> &entries);
120 
121  private:
122  typedef std::map<boost::shared_ptr<serialization::pimpl::Data>, bool> MARKER_MAP;
123 
124  map::impl::nearcache::KeyStateMarker *getKeyStateMarker();
125 
126  void addNearCacheInvalidateListener(std::auto_ptr<client::impl::BaseEventHandler> handler);
127 
128  class ClientMapAddNearCacheEventHandler
129  : public protocol::codec::MapAddNearCacheEntryListenerCodec::AbstractEventHandler {
130  public:
131  ClientMapAddNearCacheEventHandler(
133  : nearCache(cache) {
134  }
135 
136  //@Override
137  void beforeListenerRegister() {
138  nearCache->clear();
139  }
140 
141  //@Override
142  void onListenerRegister() {
143  nearCache->clear();
144  }
145 
146  //@Override
147  virtual void handleIMapInvalidationEventV10(std::auto_ptr<Data> key) {
148  // null key means Near Cache has to remove all entries in it (see MapAddNearCacheEntryListenerMessageTask)
149  if (key.get() == NULL) {
150  nearCache->clear();
151  } else {
152  nearCache->invalidate(boost::shared_ptr<serialization::pimpl::Data>(key));
153  }
154  }
155 
156  //@Override
157  virtual void handleIMapBatchInvalidationEventV10(const std::vector<Data> &keys) {
158  for (std::vector<serialization::pimpl::Data>::const_iterator it = keys.begin();
159  it != keys.end(); ++it) {
160  nearCache->invalidate(boost::shared_ptr<serialization::pimpl::Data>(
161  new serialization::pimpl::Data(*it)));
162  }
163  }
164 
165  virtual void handleIMapInvalidationEventV14(std::auto_ptr<serialization::pimpl::Data> key,
166  const std::string &sourceUuid,
167  const util::UUID &partitionUuid,
168  const int64_t &sequence) {
169  // TODO: Change as Java
170  handleIMapInvalidationEventV10(key);
171  }
172 
173  virtual void
174  handleIMapBatchInvalidationEventV14(const std::vector<serialization::pimpl::Data> &keys,
175  const std::vector<std::string> &sourceUuids,
176  const std::vector<util::UUID> &partitionUuids,
177  const std::vector<int64_t> &sequences) {
178  // TODO: Change as Java
179  handleIMapBatchInvalidationEventV10(keys);
180  }
181 
182  private:
183  boost::shared_ptr<internal::nearcache::NearCache<serialization::pimpl::Data, TypedData> > nearCache;
184  };
185 
186  boost::shared_ptr<spi::impl::ListenerMessageCodec> createNearCacheEntryListenerCodec();
187 
188  void resetToUnmarkedState(boost::shared_ptr<serialization::pimpl::Data> &key);
189 
190  void unmarkRemainingMarkedKeys(std::map<boost::shared_ptr<serialization::pimpl::Data>, bool> &markers);
191 
192  void tryToPutNearCache(boost::shared_ptr<serialization::pimpl::Data> &keyData,
193  boost::shared_ptr<TypedData> &response);
194 
199  void invalidateNearCache(const serialization::pimpl::Data &key);
200 
201  void invalidateNearCache(boost::shared_ptr<serialization::pimpl::Data> key);
202 
203  private:
204  class NearCacheEntryListenerMessageCodec : public spi::impl::ListenerMessageCodec {
205  public:
206  NearCacheEntryListenerMessageCodec(const std::string &name, int32_t listenerFlags);
207 
208  virtual std::auto_ptr<protocol::ClientMessage> encodeAddRequest(bool localOnly) const;
209 
210  virtual std::string decodeAddResponse(protocol::ClientMessage &responseMessage) const;
211 
212  virtual std::auto_ptr<protocol::ClientMessage>
213  encodeRemoveRequest(const std::string &realRegistrationId) const;
214 
215  virtual bool decodeRemoveResponse(protocol::ClientMessage &clientMessage) const;
216 
217  private:
218  std::string name;
219  int32_t listenerFlags;
220  };
221 
222  bool cacheLocalEntries;
223  bool invalidateOnChange;
224  map::impl::nearcache::KeyStateMarker *keyStateMarker;
225  const mixedtype::config::MixedNearCacheConfig &nearCacheConfig;
226  boost::shared_ptr<internal::nearcache::NearCache<serialization::pimpl::Data, TypedData> > nearCache;
227  // since we don't have atomic support in the project yet, using shared_ptr
228  boost::shared_ptr<std::string> invalidationListenerId;
229 
238  EntryVector populateFromNearCache(const PID_TO_KEY_MAP &pIdToKeyData, PID_TO_KEY_MAP &nonCachedPidToKeyMap,
239  MARKER_MAP &markers);
240  };
241  }
242  }
243 }
244 
245 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
246 #pragma warning(pop)
247 #endif
248 
249 #endif /* HAZELCAST_CLIENT_NEARCACHEDMIXEDMAP_H_ */
250 
Definition: LocalMapStats.h:31
NearCache is the contract point to store keys and values in underlying com.hazelcast.cache.impl.nearcache.NearCacheRecordStore.
Definition: NearCache.h:82
Concurrent, distributed, observable and queryable map client.
Definition: ClientMapProxy.h:61
Concurrent, distributed, observable and queryable map client.
Definition: NearCachedClientMapProxy.h:51
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32