Infinispan HotRod C++ Client  8.3.2.Final
RemoteCacheManager.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_REMOTECACHEMANAGER_H
2 #define ISPN_HOTROD_REMOTECACHEMANAGER_H
3 
10 
11 
12 
13 #include <string>
14 #include <map>
15 #include <memory>
16 
17 namespace infinispan {
18 namespace hotrod {
19 
20 class RemoteCacheManagerImpl;
22 
44 {
45 public:
46 
54  explicit RemoteCacheManager(bool start_ = true);
55 
57  const std::map<std::string, std::string>& configuration,
58  bool start_ = true) : transactionManager(TransactionManagerLookup::lookup()) {
59  init(configuration, start_);
60  }
61 
71  explicit RemoteCacheManager(
72  const Configuration& configuration,
73  bool start = true);
74 
76 
81  void start();
82 
87  void stop();
88 
96  bool isStarted();
97 
105  const Configuration& getConfiguration();
106 
114  template <class K, class V> RemoteCache<K, V> &getCache(
115  bool forceReturnValue) {
116  const std::string key = forceReturnValue ? "/true" : "/false";
117  if (remoteCacheMap.find(key)==remoteCacheMap.end())
118  {
119  auto rcache = new RemoteCache<K, V>(transactionManager,
120  transactionTable, forceReturnValue,
121  getConfiguration().isTransactional());
122  try {
123  initCache(*rcache, forceReturnValue, getConfiguration().getNearCacheConfiguration());
124  } catch (...) {
125  delete rcache;
126  throw;
127  }
128  remoteCacheMap[key] = std::unique_ptr<RemoteCacheBase>(rcache);
129  }
130  auto cache = (RemoteCache<K, V> *)remoteCacheMap[key].get();
131  cache->keyMarshaller.reset(new BasicMarshaller<K>());
132  cache->valueMarshaller.reset(new BasicMarshaller<V>());
133  return *cache;
134  }
135 
144  template <class K, class V> RemoteCache<K, V> &getCache() {
145  return getCache<K,V>(getConfiguration().isForceReturnValue());
146  }
147 
155  template<class K, class V> RemoteCache<K, V>& getCache(
156  const std::string &name, bool forceReturnValue) {
157  const std::string key =
158  forceReturnValue ? name + "/true" : name + "/false";
159  if (remoteCacheMap.find(key) == remoteCacheMap.end()) {
160  auto rcache = new RemoteCache<K, V>(transactionManager,
161  transactionTable, forceReturnValue,
162  getConfiguration().isTransactional());
163  try {
164  initCache(*rcache, name.c_str(), forceReturnValue);
165  } catch (...) {
166  delete rcache;
167  throw;
168  }
169  remoteCacheMap[key] = std::unique_ptr<RemoteCacheBase >(rcache);
170  }
171  auto cache = (RemoteCache<K, V> *)remoteCacheMap[key].get();
172  cache->keyMarshaller.reset(new BasicMarshaller<K>());
173  cache->valueMarshaller.reset(new BasicMarshaller<V>());
174  return *cache;
175  }
176 
185  template <class K, class V> RemoteCache<K, V> &getCache(
186  const std::string& name) {
187  return getCache<K,V>(name, getConfiguration().isForceReturnValue());
188  }
189 
200  template<class K, class V> RemoteCache<K, V>& getCache(Marshaller<K> *km,
201  void (*kd)(Marshaller<K>*), Marshaller<V> *vm,
202  void (*vd)(Marshaller<V>*), bool forceReturnValue) {
203  const std::string key = forceReturnValue ? "/true" : "/false";
204  if (remoteCacheMap.find(key) == remoteCacheMap.end()) {
205  auto rcache = new RemoteCache<K, V>(transactionManager,
206  transactionTable, forceReturnValue,
207  getConfiguration().isTransactional());
208  try {
209  initCache(*rcache, forceReturnValue,
210  getConfiguration().getNearCacheConfiguration());
211  } catch (...) {
212  delete rcache;
213  throw;
214  }
215  remoteCacheMap[key] = std::unique_ptr<RemoteCacheBase>(rcache);
216  }
217  auto cache = (RemoteCache<K, V>*) remoteCacheMap[key].get();
218  cache->keyMarshaller.reset(km);
219  cache->valueMarshaller.reset(vm);
220  return *cache;
221  }
222 
236  template<class K, class V> RemoteCache<K, V> &getCache(
237  Marshaller<K> *km, void (*kd)(Marshaller<K> *),
238  Marshaller<V> *vm, void (*vd)(Marshaller<V> *)) {
239  return getCache<K,V>(km, kd, vm, vd, getConfiguration().isForceReturnValue());
240  }
252  template<class K, class V> RemoteCache<K, V>& getCache(Marshaller<K> *km,
253  void (*kd)(Marshaller<K>*), Marshaller<V> *vm,
254  void (*vd)(Marshaller<V>*), const std::string &name,
255  bool forceReturnValue) {
256  const std::string key =
257  forceReturnValue ? name + "/true" : name + "/false";
258  if (remoteCacheMap.find(key) == remoteCacheMap.end()) {
259  auto rcache = new RemoteCache<K, V>(transactionManager,
260  transactionTable, forceReturnValue,
261  getConfiguration().isTransactional());
262  try {
263  initCache(*rcache, name.c_str(), forceReturnValue,
264  getConfiguration().getNearCacheConfiguration());
265  } catch (...) {
266  delete rcache;
267  throw;
268  }
269  remoteCacheMap[key] = std::unique_ptr<RemoteCacheBase >(rcache);
270  }
271  auto cache = (RemoteCache<K, V>*) remoteCacheMap[key].get();
272  cache->keyMarshaller.reset(km);
273  cache->valueMarshaller.reset(vm);
274  return *cache;
275  }
276 
287  template <class K, class V> RemoteCache<K, V> &getCache(
288  Marshaller<K> *km, void (*kd)(Marshaller<K> *),
289  Marshaller<V> *vm, void (*vd)(Marshaller<V> *),
290  const std::string& name) {
291  return getCache(km, kd, vm, vd, name, getConfiguration().isForceReturnValue());
292  }
299  bool switchToDefaultCluster();
300 
307  bool switchToCluster(std::string clusterName);
308 
309  RemoteCounterManager& getCounterManager();
316  std::shared_ptr<RemoteCacheManagerAdmin> administration() {
317  return newRemoteCacheManagerAdmin();
318  }
319 
320  std::set<std::string> getCacheNames();
322  {
323  return transactionManager;
324  }
325 
326 private:
327  void *impl;
328  std::map<std::string, std::unique_ptr<RemoteCacheBase> > remoteCacheMap;
329  std::function<void(std::string&)> cacheRemover;
330  TransactionManager& transactionManager;
331  TransactionTable transactionTable;
332 
333  void init(const std::map<std::string, std::string>& configuration, bool start);
334 
335  void initCache(RemoteCacheBase& cache, bool forceReturnValue, NearCacheConfiguration nc = NearCacheConfiguration());
336  void initCache(RemoteCacheBase& cache, const char *name, bool forceReturnValue, NearCacheConfiguration nc = NearCacheConfiguration());
337 
338  // not implemented
340  RemoteCacheManager operator=(const RemoteCacheManager&);
341 
342  std::shared_ptr<RemoteCacheManagerAdmin> newRemoteCacheManagerAdmin();
343 
344  template<typename T> static void genericDelete(T *t) { delete t; }
345  template<typename T> static void genericNoDelete(T *) { }
346 };
347 
348 }} // namespace infinispan::hotrod
349 
350 #endif /* ISPN_HOTROD_REMOTECACHEMANAGER_H */
RemoteCache< K, V > & getCache(Marshaller< K > *km, void(*kd)(Marshaller< K > *), Marshaller< V > *vm, void(*vd)(Marshaller< V > *))
Definition: RemoteCacheManager.h:236
RemoteCache< K, V > & getCache(Marshaller< K > *km, void(*kd)(Marshaller< K > *), Marshaller< V > *vm, void(*vd)(Marshaller< V > *), bool forceReturnValue)
Definition: RemoteCacheManager.h:200
Definition: Transactions.h:174
Definition: RemoteCounterManager.h:20
#define HR_EXTERN
Definition: ImportExport.h:35
RemoteCacheManager(const std::map< std::string, std::string > &configuration, bool start_=true)
Definition: RemoteCacheManager.h:56
RemoteCache< K, V > & getCache(Marshaller< K > *km, void(*kd)(Marshaller< K > *), Marshaller< V > *vm, void(*vd)(Marshaller< V > *), const std::string &name)
Definition: RemoteCacheManager.h:287
Definition: NearCacheConfiguration.h:32
RemoteCache< K, V > & getCache()
Definition: RemoteCacheManager.h:144
RemoteCache< K, V > & getCache(const std::string &name)
Definition: RemoteCacheManager.h:185
Definition: TransactionManager.h:24
Definition: CacheClientListener.h:28
RemoteCache< K, V > & getCache(bool forceReturnValue)
Definition: RemoteCacheManager.h:114
std::shared_ptr< RemoteCacheManagerAdmin > administration()
Definition: RemoteCacheManager.h:316
Definition: AuthenticationConfiguration.h:10
RemoteCache< K, V > & getCache(Marshaller< K > *km, void(*kd)(Marshaller< K > *), Marshaller< V > *vm, void(*vd)(Marshaller< V > *), const std::string &name, bool forceReturnValue)
Definition: RemoteCacheManager.h:252
Definition: Configuration.h:28
Definition: RemoteCacheManager.h:43
Definition: BasicMarshaller.h:18
RemoteCache< K, V > & getCache(const std::string &name, bool forceReturnValue)
Definition: RemoteCacheManager.h:155
Definition: RemoteCacheManagerAdmin.h:39
Definition: Marshaller.h:12
Definition: TransactionManager.h:69
TransactionManager & getTransactionManager() const
Definition: RemoteCacheManager.h:321