Hazelcast C++ Client
NearCachedClientMapProxy.h
1 /*
2  * Copyright (c) 2008-2017, 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 tryPutInternal(const serialization::pimpl::Data &key, const serialization::pimpl::Data &value,
83  long timeoutInMillis);
84 
85  virtual std::auto_ptr<serialization::pimpl::Data> putInternal(const serialization::pimpl::Data &key,
86  const serialization::pimpl::Data &value,
87  long timeoutInMillis);
88 
89  virtual void tryPutTransientInternal(const serialization::pimpl::Data &key,
90  const serialization::pimpl::Data &value, int ttlInMillis);
91 
92  virtual std::auto_ptr<serialization::pimpl::Data> putIfAbsentInternal(const serialization::pimpl::Data &keyData,
93  const serialization::pimpl::Data &valueData,
94  int ttlInMillis);
95 
96  virtual bool replaceIfSameInternal(const serialization::pimpl::Data &keyData,
97  const serialization::pimpl::Data &valueData,
98  const serialization::pimpl::Data &newValueData);
99 
100  virtual std::auto_ptr<serialization::pimpl::Data> replaceInternal(const serialization::pimpl::Data &keyData,
101  const serialization::pimpl::Data &valueData);
102 
103  virtual void setInternal(const serialization::pimpl::Data &keyData, const serialization::pimpl::Data &valueData,
104  int ttlInMillis);
105 
106  virtual bool evictInternal(const serialization::pimpl::Data &keyData);
107 
108  EntryVector getAllInternal(ClientMapProxy::PID_TO_KEY_MAP &pIdToKeyData);
109 
110  virtual std::auto_ptr<serialization::pimpl::Data>
111  executeOnKeyInternal(const serialization::pimpl::Data &keyData,
112  const serialization::pimpl::Data &processor);
113 
114  virtual void
115  putAllInternal(const std::map<int, EntryVector> &entries);
116  private:
117  typedef std::map<boost::shared_ptr<serialization::pimpl::Data>, bool> MARKER_MAP;
118 
119  map::impl::nearcache::KeyStateMarker *getKeyStateMarker();
120 
121  void addNearCacheInvalidateListener(std::auto_ptr<client::impl::BaseEventHandler> handler);
122 
123  class ClientMapAddNearCacheEventHandler
124  : public protocol::codec::MapAddNearCacheEntryListenerCodec::AbstractEventHandler {
125  public:
126  ClientMapAddNearCacheEventHandler(
128  : nearCache(cache) {
129  }
130 
131  //@Override
132  void beforeListenerRegister() {
133  nearCache->clear();
134  }
135 
136  //@Override
137  void onListenerRegister() {
138  nearCache->clear();
139  }
140 
141  //@Override
142  void handleIMapInvalidation(std::auto_ptr<serialization::pimpl::Data> key) {
143  // null key means Near Cache has to remove all entries in it (see MapAddNearCacheEntryListenerMessageTask)
144  if (key.get() == NULL) {
145  nearCache->clear();
146  } else {
147  nearCache->remove(boost::shared_ptr<serialization::pimpl::Data>(key));
148  }
149  }
150 
151  //@Override
152  void handleIMapBatchInvalidation(const std::vector<serialization::pimpl::Data> &keys) {
153  for (std::vector<serialization::pimpl::Data>::const_iterator it = keys.begin();
154  it != keys.end(); ++it) {
155  nearCache->remove(boost::shared_ptr<serialization::pimpl::Data>(
156  new serialization::pimpl::Data(*it)));
157  }
158  }
159 
160  private:
161  boost::shared_ptr<internal::nearcache::NearCache<serialization::pimpl::Data, TypedData> > nearCache;
162  };
163 
164  std::auto_ptr<protocol::codec::IAddListenerCodec> createNearCacheEntryListenerCodec();
165 
166  void resetToUnmarkedState(boost::shared_ptr<serialization::pimpl::Data> &key);
167 
168  void unmarkRemainingMarkedKeys(std::map<boost::shared_ptr<serialization::pimpl::Data>, bool> &markers);
169 
170  void tryToPutNearCache(boost::shared_ptr<serialization::pimpl::Data> &keyData,
171  boost::shared_ptr<TypedData> &response);
172 
177  void invalidateNearCache(const serialization::pimpl::Data &key);
178 
179  void invalidateNearCache(boost::shared_ptr<serialization::pimpl::Data> key);
180 
181 
182  private:
183  bool cacheLocalEntries;
184  bool invalidateOnChange;
185  map::impl::nearcache::KeyStateMarker *keyStateMarker;
186  const mixedtype::config::MixedNearCacheConfig &nearCacheConfig;
187  boost::shared_ptr<internal::nearcache::NearCache<serialization::pimpl::Data, TypedData> > nearCache;
188  // since we don't have atomic support in the project yet, using shared_ptr
189  boost::shared_ptr<std::string> invalidationListenerId;
190 
198  EntryVector populateFromNearCache(PID_TO_KEY_MAP &pIdToKeyData, MARKER_MAP &markers);
199  };
200  }
201  }
202 }
203 
204 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
205 #pragma warning(pop)
206 #endif
207 
208 #endif /* HAZELCAST_CLIENT_NEARCACHEDMIXEDMAP_H_ */
209 
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:68
Concurrent, distributed, observable and queryable map client.
Definition: ClientMapProxy.h:61
Concurrent, distributed, observable and queryable map client.
Definition: NearCachedClientMapProxy.h:51
Definition: MapEntryView.h:32