JBoss Data Grid HotRod C++ Client  7.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RemoteCache.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_REMOTECACHE_H
2 #define ISPN_HOTROD_REMOTECACHE_H
3 
4 
5 
14 #include "query.pb.h"
15 
16 #include <cmath>
17 #include <set>
18 #include <map>
19 #include <sstream>
20 #include <stdexcept>
21 #include <vector>
22 #include <functional>
23 #include <future>
24 
25 namespace infinispan {
26 namespace hotrod {
27 
58 template <class K, class V> class RemoteCache : private RemoteCacheBase
59 {
60  private:
61 #ifndef SWIG // Let SWIG ignore this method
62  template<typename Function>
63  inline std::future<typename std::result_of<Function()>::type> goAsync(Function&& f,
64  std::function<typename std::result_of<Function()>::type (typename std::result_of<Function()>::type)> success, std::function<typename std::result_of<Function()>::type (std::exception&)> fail)
65  {
66  auto fq= [=]{ try{ return success==0 ? f() : success(f());}
67  catch (std::exception& ex)
68  {if (fail!=0){return fail(ex);}
69  else {throw ex;}}
70  };
71  return std::async(fq);
72  }
73 #endif
74 
75  public:
81  std::string getName() {
82  return std::string(base_getName());
83  }
84 
90  std::string getVersion() {
92  }
93 
99  std::string getProtocolVersion() {
101  }
102 
110  V* get(const K& key) {
111  return (V *) base_get(&key);
112  }
113 
123  std::future<V*> getAsync(const K& key, std::function<V* (V*)> success=nullptr, std::function<V* (std::exception&)> fail=nullptr) {
124  auto pKey=&key;
125  auto f= [=] { return this->get(*pKey); };
126  return goAsync(f,success,fail);
127  }
128 
149  V* put(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
150  return put(key, val, lifespan, SECONDS, maxIdle, SECONDS);
151  }
171  V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
172  return put(key, val, lifespan, lifespanUnit, 0, SECONDS);
173  }
174 
197  V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
198  return (V *) base_put(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
199  }
200 
209  std::future<V*> putAsync(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0, std::function<V* (V*)> success=nullptr, std::function<V* (std::exception&)> fail=nullptr) {
210  auto pKey=&key, pVal=&val;
211  auto f=[=] { return this->put(*pKey,*pVal,lifespan,maxIdle); };
212  return goAsync(f,success,fail);
213  }
214 
223  V* putAsync(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, std::function<V* (V*)> success=nullptr, std::function<V* (std::exception&)> fail=nullptr) {
224  auto pKey=&key, pVal=&val;
225  auto f= [=] { return this->put(*pKey,*pVal,lifespan,lifespanUnit); };
226  return goAsync(f,success,fail);
227  }
228 
237  V* putAsync(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit, std::function<V* (V*)> success=nullptr, std::function<V* (std::exception&)> fail=nullptr) {
238  auto pKey=&key, pVal=&val;
239  auto f= [=] { this->put(*pKey,*pVal,lifespan, lifespanUnit,maxIdle,maxIdleUnit); };
240  return goAsync(f,success,fail);
241  }
242 
254  V* putIfAbsent(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
255  return putIfAbsent(key, val, lifespan, SECONDS, maxIdle, SECONDS);
256  }
267  V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
268  return putIfAbsent(key, val, lifespan, lifespanUnit, 0, SECONDS);
269  }
283  V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
284  return (V *)base_putIfAbsent(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
285  }
296  void putAll(const std::map<K, V>& map, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
297  putAll(map, lifespan, SECONDS, maxIdle, SECONDS);
298  }
299 
310  void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit) {
311  putAll(map, lifespan, lifespanUnit, 0, SECONDS);
312  }
313 
327  void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
328  uint64_t lifespanMillis = toSeconds(lifespan, lifespanUnit);
329  uint64_t maxIdleMillis = toSeconds(maxIdle, maxIdleUnit);
330 
331  for (typename std::map<K, V>::const_iterator it = map.begin(); it != map.end(); ++it) {
332  put(it->first, it->second, lifespanMillis, maxIdleMillis);
333  }
334  }
335 
344  std::future<void> putAllAsync(const std::map<K, V>& map, uint64_t lifespan = 0, uint64_t maxIdle = 0, std::function<V* (V*)> success=nullptr, std::function<void (std::exception&)> fail=nullptr) {
345  auto pMap = &pMap;
346  auto f = [=] { this->putAll(*pMap,lifespan, maxIdle); };
347  return goAsync(f,success,fail);
348  }
349 
358  std::future<void> putAllAsync(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit, std::function<V* (V*)> success=nullptr, std::function<void (std::exception&)> fail=nullptr) {
359  auto pMap = &pMap;
360  auto f = [=] { this->putAll(*pMap,lifespan, lifespanUnit); };
361  return goAsync(f,success,fail);
362  }
363 
372  std::future<void> putAllAsync(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit, std::function<V* (V*)> success=nullptr, std::function<void (std::exception&)> fail=nullptr) {
373  auto pMap = &pMap;
374  auto f = [=] { this->putAll(*pMap,lifespan, lifespanUnit, maxIdle, maxIdleUnit); };
375  return goAsync(f,success,fail);
376  }
377 
389  V* replace(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
390  return replace(key, val, lifespan, SECONDS, maxIdle, SECONDS);
391  }
403  V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
404  return replace(key, val, lifespan, lifespanUnit, 0, SECONDS);
405  }
419  V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
420  return (V *) base_replace(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
421  }
434  V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
435  return replace(key, oldVal, val, lifespan, SECONDS, maxIdle, SECONDS);
436  }
448  V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
449  return replace(key, oldVal, val, lifespan, lifespanUnit, 0, SECONDS);
450  }
464  V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
466  }
467 
474  V* remove(const K& key) {
475  return (V *) base_remove(&key);
476  }
477 
483  bool containsKey(const K& key) {
484  return base_containsKey(&key);
485  }
486 
491  bool containsValue(const V& val) {
493  }
509  const K& key, const V& val,
510  uint64_t version, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
511  return base_replaceWithVersion(&key, &val, version, lifespan, maxIdle);
512  }
513 
522  std::future<void> replaceWithVersionAsync(const K& key, const V& val,
523  uint64_t version, uint64_t lifespan, uint64_t maxIdle, std::function<V* (V*)> success=nullptr, std::function<void (std::exception&)> fail=nullptr) {
524  auto pMap = &pMap;
525  auto f = [=] { this->replaceWithVersion(&key, &val, version, lifespan, maxIdle); };
526  return goAsync(f,success,fail);
527  }
528 
547  bool removeWithVersion(const K& key, uint64_t version) {
548  return base_removeWithVersion(&key, version);
549  }
560  std::pair<std::shared_ptr<V>, VersionedValue> getWithVersion(const K& key) {
561  VersionedValue version;
562  void *value = base_getWithVersion(&key, &version);
563  return std::make_pair(std::shared_ptr<V>((V *) value), version);
564  }
575  std::pair<std::shared_ptr<V>, MetadataValue> getWithMetadata(const K& key) {
576  MetadataValue metadata;
577  void *value = base_getWithMetadata(&key, &metadata);
578  return std::make_pair(std::shared_ptr<V>((V *) value), metadata);
579  }
584  std::map<std::shared_ptr<K>, std::shared_ptr<V> > getBulk() {
585  return getBulk(0);
586  }
591  std::map<std::shared_ptr<K>, std::shared_ptr<V> > getBulk(int nrOfEntries) {
593  base_getBulk(nrOfEntries, mbuf);
594 
595  std::map<std::shared_ptr<K>, std::shared_ptr<V> > result;
596  const portable::pair<void*, void*> *data = mbuf.data();
597  for (size_t i = 0; i < mbuf.size(); i++) {
598  result.insert(std::make_pair(
599  std::shared_ptr<K>((K*)data[i].key), std::shared_ptr<V>((V*)data[i].value)));
600  }
601  return result;
602  }
609  std::vector<unsigned char> execute(const std::string& name, const std::map<std::string,std::string>& args)
610  {
611  return base_execute(name,args);
612  }
613 
620  QueryResponse query(const QueryRequest &qr)
621  {
622  return base_query(qr);
623  }
624 
631  std::vector<unsigned char> query(std::vector<unsigned char> qr, size_t size)
632  {
633  return base_query_char(qr,size);
634  }
635 
640  std::set<std::pair<K, V> > entrySet() {
642  }
650  std::set<std::shared_ptr<K> > keySet() {
652  base_keySet(0, p);
653 
654  std::set<std::shared_ptr<K> > result;
655  for (size_t i = 0; i < p.size(); ++i) {
656  result.insert(std::shared_ptr<K>((K*)p.data()[i]));
657  }
658  return result;
659  }
665  uint64_t size() {
666  return base_size();
667  }
673  bool isEmpty() {
674  return 0 == size();
675  }
680  std::vector<V> values() {
682  }
689  std::map<std::string, std::string> stats() {
691  base_stats(statistics);
692  return statistics.std_map<std::string, portable::string::convert, std::string, portable::string::convert>
693  (portable::string::convert(), portable::string::convert());
694  }
700  void clear() {
701  base_clear();
702  }
708  void ping() {
709  base_ping();
710  }
711 
713  return base_getCacheTopologyInfo();
714  }
722  base_withFlags(flags);
723  return *this;
724  }
725 
726  RemoteCache(const RemoteCache &other) :
727  RemoteCacheBase(other), keyMarshaller(other.keyMarshaller), valueMarshaller(other.valueMarshaller)
728  {
729  setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
730  }
731 
733  RemoteCacheBase::operator=(other);
734  keyMarshaller = other.keyMarshaller;
735  valueMarshaller = other.valueMarshaller;
736  setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
737  return *this;
738  }
739 
740  private:
742  setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
743  }
744 
745  uint64_t toSeconds(uint64_t time, TimeUnit unit) {
746  uint64_t result;
747  switch (unit) {
748  case NANOSECONDS:
749  result = (uint64_t) ceil(time / 1000000000.0);
750  break;
751  case MICROSECONDS:
752  result = (uint64_t) ceil(time / 1000000.0);
753  break;
754  case MILLISECONDS:
755  result = (uint64_t) ceil(time / 1000.0);
756  break;
757  case SECONDS:
758  result = time;
759  break;
760  case MINUTES:
761  result = time * 60;
762  break;
763  case HOURS:
764  result = time * 3600;
765  break;
766  case DAYS:
767  result = time * 86400;
768  break;
769  default:
770  std::stringstream ss;
771  ss << "Unhandled TimeUnit specified: " << unit << ".";
772  throw std::invalid_argument(ss.str());
773  }
774  return result;
775  }
776 
777  // type-hiding and resurrecting support
778  static void keyMarshall(void *thisp, const void* key, std::vector<char> &buf) {
779  ((RemoteCache<K, V> *) thisp)->keyMarshaller->marshall(*(const K *) key, buf);
780  }
781  static void valueMarshall(void* thisp, const void* val, std::vector<char> &buf) {
782  ((RemoteCache<K, V> *)thisp)->valueMarshaller->marshall(*(const V *) val, buf);
783  }
784  static void* keyUnmarshall(void *thisp, const std::vector<char> &buf) {
785  return ((RemoteCache<K, V> *) thisp)->keyMarshaller->unmarshall(buf);
786  }
787  static void* valueUnmarshall(void* thisp, const std::vector<char> &buf) {
788  return ((RemoteCache<K, V> *)thisp)->valueMarshaller->unmarshall(buf);
789  }
790 
791  portable::counting_ptr<Marshaller<K> > keyMarshaller;
792  portable::counting_ptr<Marshaller<V> > valueMarshaller;
793 
794  friend class RemoteCacheManager;
795 };
796 
797 }} // namespace
798 
799 #endif /* ISPN_HOTROD_REMOTECACHE_H */
void putAll(const std::map< K, V > &map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
Definition: RemoteCache.h:327
HR_EXTERN QueryResponse base_query(const QueryRequest &qr)
std::map< std::shared_ptr< K >, std::shared_ptr< V > > getBulk(int nrOfEntries)
Definition: RemoteCache.h:591
V * putIfAbsent(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:267
std::vector< unsigned char > query(std::vector< unsigned char > qr, size_t size)
Definition: RemoteCache.h:631
HR_EXTERN void * base_replace(const void *key, const void *value, int64_t life, int64_t idle)
V * put(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
Definition: RemoteCache.h:197
std::string getName()
Definition: RemoteCache.h:81
std::pair< std::shared_ptr< V >, VersionedValue > getWithVersion(const K &key)
Definition: RemoteCache.h:560
std::map< K, V > std_map() const
Definition: portable.h:355
std::set< std::pair< K, V > > entrySet()
Definition: RemoteCache.h:640
HR_EXTERN bool base_replaceWithVersion(const void *key, const void *value, int64_t version, int64_t life, int64_t idle)
HR_EXTERN bool base_containsKey(const void *key)
HR_EXTERN void base_withFlags(Flag flag)
HR_EXTERN void setMarshallers(void *rc, MarshallHelperFn kf, MarshallHelperFn vf, UnmarshallHelperFn ukf, UnmarshallHelperFn uvf)
static const char * getProtocolVersionCString()
Definition: Version.h:33
V * putAsync(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit, std::function< V *(V *)> success=nullptr, std::function< V *(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:237
HR_EXTERN void base_getBulk(int size, portable::map< void *, void * > &mbuf)
size_t size() const
Definition: portable.h:263
Definition: CacheTopologyInfo.h:10
bool containsKey(const K &key)
Definition: RemoteCache.h:483
HR_EXTERN void * base_remove(const void *key)
std::map< std::string, std::string > stats()
Definition: RemoteCache.h:689
HR_EXTERN void * base_put(const void *key, const void *value, int64_t life, int64_t idle)
V * replace(const K &key, const V &oldVal, const V &val, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:434
std::set< std::shared_ptr< K > > keySet()
Definition: RemoteCache.h:650
void putAll(const std::map< K, V > &map, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:296
RemoteCache< K, V > & withFlags(Flag flags)
Definition: RemoteCache.h:721
std::vector< V > values()
Definition: RemoteCache.h:680
HR_EXTERN bool base_removeWithVersion(const void *key, int64_t version)
V * putIfAbsent(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
Definition: RemoteCache.h:283
Definition: portable.h:275
std::map< std::shared_ptr< K >, std::shared_ptr< V > > getBulk()
Definition: RemoteCache.h:584
RemoteCache< K, V > & operator=(const RemoteCache &other)
Definition: RemoteCache.h:732
HR_EXTERN const char * base_getName()
Definition: RemoteCache.h:58
Definition: TimeUnit.h:14
Definition: TimeUnit.h:18
HR_EXTERN void base_keySet(int scope, portable::vector< void * > &sbuf)
V * putIfAbsent(const K &key, const V &val, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:254
Definition: TimeUnit.h:15
std::future< void > putAllAsync(const std::map< K, V > &map, uint64_t lifespan, TimeUnit lifespanUnit, std::function< V *(V *)> success=nullptr, std::function< void(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:358
std::future< V * > putAsync(const K &key, const V &val, uint64_t lifespan=0, uint64_t maxIdle=0, std::function< V *(V *)> success=nullptr, std::function< V *(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:209
V * replace(const K &key, const V &oldVal, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:448
Definition: portable.h:152
uint64_t size()
Definition: RemoteCache.h:665
std::future< V * > getAsync(const K &key, std::function< V *(V *)> success=nullptr, std::function< V *(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:123
std::vector< unsigned char > execute(const std::string &name, const std::map< std::string, std::string > &args)
Definition: RemoteCache.h:609
std::future< void > putAllAsync(const std::map< K, V > &map, uint64_t lifespan=0, uint64_t maxIdle=0, std::function< V *(V *)> success=nullptr, std::function< void(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:344
CacheTopologyInfo getCacheTopologyInfo()
Definition: RemoteCache.h:712
void clear()
Definition: RemoteCache.h:700
size_t size() const
Definition: portable.h:389
bool isEmpty()
Definition: RemoteCache.h:673
V * put(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:171
RemoteCache(const RemoteCache &other)
Definition: RemoteCache.h:726
HR_EXTERN std::vector< unsigned char > base_execute(const std::string &cmdName, const std::map< std::string, std::string > &args)
HR_EXTERN void * base_putIfAbsent(const void *key, const void *value, int64_t life, int64_t idle)
bool replaceWithVersion(const K &key, const V &val, uint64_t version, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:508
std::string getVersion()
Definition: RemoteCache.h:90
Definition: RemoteCacheManager.h:39
Definition: TimeUnit.h:17
std::string getProtocolVersion()
Definition: RemoteCache.h:99
HR_EXTERN void * base_getWithVersion(const void *key, VersionedValue *version)
V * replace(const K &key, const V &oldVal, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
Definition: RemoteCache.h:464
V * replace(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
Definition: RemoteCache.h:419
void ping()
Definition: RemoteCache.h:708
HR_EXTERN CacheTopologyInfo base_getCacheTopologyInfo()
QueryResponse query(const QueryRequest &qr)
Definition: RemoteCache.h:620
const T * data() const
Definition: portable.h:258
HR_EXTERN void * base_getWithMetadata(const void *key, MetadataValue *metadata)
static const char * getVersionCString()
Definition: Version.h:42
TimeUnit
Definition: TimeUnit.h:13
Definition: RemoteCacheBase.h:32
HR_EXTERN std::vector< unsigned char > base_query_char(std::vector< unsigned char > qr, size_t size)
std::future< void > putAllAsync(const std::map< K, V > &map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit, std::function< V *(V *)> success=nullptr, std::function< void(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:372
Definition: portable.h:269
bool containsValue(const V &val)
Definition: RemoteCache.h:491
V * replace(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:403
Definition: TimeUnit.h:16
Definition: TimeUnit.h:19
V * putAsync(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, std::function< V *(V *)> success=nullptr, std::function< V *(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:223
Definition: TimeUnit.h:20
Definition: MetadataValue.h:9
V * replace(const K &key, const V &val, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:389
std::pair< std::shared_ptr< V >, MetadataValue > getWithMetadata(const K &key)
Definition: RemoteCache.h:575
Flag
Definition: Flag.h:7
std::future< void > replaceWithVersionAsync(const K &key, const V &val, uint64_t version, uint64_t lifespan, uint64_t maxIdle, std::function< V *(V *)> success=nullptr, std::function< void(std::exception &)> fail=nullptr)
Definition: RemoteCache.h:522
HR_EXTERN void base_stats(portable::map< portable::string, portable::string > &sbuf)
Definition: VersionedValue.h:9
V * put(const K &key, const V &val, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:149
HR_EXTERN void * base_get(const void *key)
void putAll(const std::map< K, V > &map, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:310
const my_pair * data() const
Definition: portable.h:384
bool removeWithVersion(const K &key, uint64_t version)
Definition: RemoteCache.h:547