JBoss Data Grid HotRod C++ Client  6.6.1
 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 
13 
14 #include <cmath>
15 #include <set>
16 #include <map>
17 #include <sstream>
18 #include <stdexcept>
19 #include <vector>
20 
21 namespace infinispan {
22 namespace hotrod {
23 
54 template <class K, class V> class RemoteCache : private RemoteCacheBase
55 {
56  public:
62  std::string getName() {
63  return std::string(base_getName());
64  }
65 
71  std::string getVersion() {
73  }
74 
80  std::string getProtocolVersion() {
82  }
83 
91  V* get(const K& key) {
92  return (V *) base_get(&key);
93  }
114  V* put(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
115  return put(key, val, lifespan, SECONDS, maxIdle, SECONDS);
116  }
136  V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
137  return put(key, val, lifespan, lifespanUnit, 0, SECONDS);
138  }
161  V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
162  return (V *) base_put(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
163  }
175  V* putIfAbsent(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
176  return putIfAbsent(key, val, lifespan, SECONDS, maxIdle, SECONDS);
177  }
188  V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
189  return putIfAbsent(key, val, lifespan, lifespanUnit, 0, SECONDS);
190  }
204  V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
205  return (V *)base_putIfAbsent(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
206  }
217  void putAll(const std::map<K, V>& map, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
218  putAll(map, lifespan, SECONDS, maxIdle, SECONDS);
219  }
230  void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit) {
231  putAll(map, lifespan, lifespanUnit, 0, SECONDS);
232  }
246  void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
247  uint64_t lifespanMillis = toSeconds(lifespan, lifespanUnit);
248  uint64_t maxIdleMillis = toSeconds(maxIdle, maxIdleUnit);
249 
250  for (typename std::map<K, V>::const_iterator it = map.begin(); it != map.end(); ++it) {
251  put(it->first, it->second, lifespanMillis, maxIdleMillis);
252  }
253  }
265  V* replace(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
266  return replace(key, val, lifespan, SECONDS, maxIdle, SECONDS);
267  }
279  V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
280  return replace(key, val, lifespan, lifespanUnit, 0, SECONDS);
281  }
295  V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
296  return (V *) base_replace(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
297  }
310  V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
311  return replace(key, oldVal, val, lifespan, SECONDS, maxIdle, SECONDS);
312  }
324  V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
325  return replace(key, oldVal, val, lifespan, lifespanUnit, 0, SECONDS);
326  }
340  V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
342  }
343 
350  V* remove(const K& key) {
351  return (V *) base_remove(&key);
352  }
353 
359  bool containsKey(const K& key) {
360  return base_containsKey(&key);
361  }
362 
367  bool containsValue(const V& val) {
369  }
385  const K& key, const V& val,
386  uint64_t version, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
387  return base_replaceWithVersion(&key, &val, version, lifespan, maxIdle);
388  }
407  bool removeWithVersion(const K& key, uint64_t version) {
408  return base_removeWithVersion(&key, version);
409  }
420  std::pair<HR_SHARED_PTR<V>, VersionedValue> getWithVersion(const K& key) {
421  VersionedValue version;
422  void *value = base_getWithVersion(&key, &version);
423  return std::make_pair(HR_SHARED_PTR<V>((V *) value), version);
424  }
435  std::pair<HR_SHARED_PTR<V>, MetadataValue> getWithMetadata(const K& key) {
436  MetadataValue metadata;
437  void *value = base_getWithMetadata(&key, &metadata);
438  return std::make_pair(HR_SHARED_PTR<V>((V *) value), metadata);
439  }
444  std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > getBulk() {
445  return getBulk(0);
446  }
451  std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > getBulk(int nrOfEntries) {
453  base_getBulk(nrOfEntries, mbuf);
454 
455  std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > result;
456  const portable::pair<void*, void*> *data = mbuf.data();
457  for (size_t i = 0; i < mbuf.size(); i++) {
458  result.insert(std::make_pair(
459  HR_SHARED_PTR<K>((K*)data[i].key), HR_SHARED_PTR<V>((V*)data[i].value)));
460  }
461  return result;
462  }
467  std::set<std::pair<K, V> > entrySet() {
469  }
477  std::set<HR_SHARED_PTR<K> > keySet() {
479  base_keySet(0, p);
480 
481  std::set<HR_SHARED_PTR<K> > result;
482  for (size_t i = 0; i < p.size(); ++i) {
483  result.insert(HR_SHARED_PTR<K>((K*)p.data()[i]));
484  }
485  return result;
486  }
492  uint64_t size() {
493  return keySet().size();
494  }
500  bool isEmpty() {
501  return 0 == size();
502  }
507  std::vector<V> values() {
509  }
516  std::map<std::string, std::string> stats() {
518  base_stats(statistics);
519  return statistics.std_map<std::string, portable::string::convert, std::string, portable::string::convert>
520  (portable::string::convert(), portable::string::convert());
521  }
527  void clear() {
528  base_clear();
529  }
535  void ping() {
536  base_ping();
537  }
545  base_withFlags(flags);
546  return *this;
547  }
548 
549  RemoteCache(const RemoteCache &other) :
550  RemoteCacheBase(other), keyMarshaller(other.keyMarshaller), valueMarshaller(other.valueMarshaller)
551  {
552  setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
553  }
554 
556  RemoteCacheBase::operator=(other);
557  keyMarshaller = other.keyMarshaller;
558  valueMarshaller = other.valueMarshaller;
559  setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
560  return *this;
561  }
562 
563  private:
565  setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
566  }
567 
568  uint64_t toSeconds(uint64_t time, TimeUnit unit) {
569  uint64_t result;
570  switch (unit) {
571  case NANOSECONDS:
572  result = (uint64_t) ceil(time / 1000000000.0);
573  break;
574  case MICROSECONDS:
575  result = (uint64_t) ceil(time / 1000000.0);
576  break;
577  case MILLISECONDS:
578  result = (uint64_t) ceil(time / 1000.0);
579  break;
580  case SECONDS:
581  result = time;
582  break;
583  case MINUTES:
584  result = time * 60;
585  break;
586  case HOURS:
587  result = time * 3600;
588  break;
589  case DAYS:
590  result = time * 86400;
591  break;
592  default:
593  std::stringstream ss;
594  ss << "Unhandled TimeUnit specified: " << unit << ".";
595  throw std::invalid_argument(ss.str());
596  }
597  return result;
598  }
599 
600  // type-hiding and resurrecting support
601  static void keyMarshall(void *thisp, const void* key, ScopedBuffer &buf) {
602  ((RemoteCache<K, V> *) thisp)->keyMarshaller->marshall(*(const K *) key, buf);
603  }
604  static void valueMarshall(void* thisp, const void* val, ScopedBuffer &buf) {
605  ((RemoteCache<K, V> *)thisp)->valueMarshaller->marshall(*(const V *) val, buf);
606  }
607  static void* keyUnmarshall(void *thisp, const ScopedBuffer &buf) {
608  return ((RemoteCache<K, V> *) thisp)->keyMarshaller->unmarshall(buf);
609  }
610  static void* valueUnmarshall(void* thisp, const ScopedBuffer &buf) {
611  return ((RemoteCache<K, V> *)thisp)->valueMarshaller->unmarshall(buf);
612  }
613 
614  portable::counting_ptr<Marshaller<K> > keyMarshaller;
615  portable::counting_ptr<Marshaller<V> > valueMarshaller;
616 
617  // DEPRECATED: Library code must not use these fields
618  portable::counting_ptr<portable::counted_wrapper<HR_SHARED_PTR<Marshaller<K> > > > keyMarshallerPtr;
619  portable::counting_ptr<portable::counted_wrapper<HR_SHARED_PTR<Marshaller<V> > > > valueMarshallerPtr;
620 
621  friend class RemoteCacheManager;
622 };
623 
624 }} // namespace
625 
626 #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:246
std::map< HR_SHARED_PTR< K >, HR_SHARED_PTR< V > > getBulk()
Definition: RemoteCache.h:444
V * putIfAbsent(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:188
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:161
std::string getName()
Definition: RemoteCache.h:62
std::map< K, V > std_map() const
Definition: portable.h:354
std::set< std::pair< K, V > > entrySet()
Definition: RemoteCache.h:467
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
HR_EXTERN void base_getBulk(int size, portable::map< void *, void * > &mbuf)
size_t size() const
Definition: portable.h:262
bool containsKey(const K &key)
Definition: RemoteCache.h:359
HR_EXTERN void * base_remove(const void *key)
std::map< std::string, std::string > stats()
Definition: RemoteCache.h:516
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:310
void putAll(const std::map< K, V > &map, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:217
RemoteCache< K, V > & withFlags(Flag flags)
Definition: RemoteCache.h:544
std::vector< V > values()
Definition: RemoteCache.h:507
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:204
Definition: portable.h:274
RemoteCache< K, V > & operator=(const RemoteCache &other)
Definition: RemoteCache.h:555
HR_EXTERN const char * base_getName()
Definition: RemoteCache.h:54
Definition: TimeUnit.h:17
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:175
Definition: TimeUnit.h:16
V * replace(const K &key, const V &oldVal, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:324
Definition: portable.h:151
uint64_t size()
Definition: RemoteCache.h:492
void clear()
Definition: RemoteCache.h:527
size_t size() const
Definition: portable.h:388
bool isEmpty()
Definition: RemoteCache.h:500
V * put(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:136
RemoteCache(const RemoteCache &other)
Definition: RemoteCache.h:549
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:384
std::string getVersion()
Definition: RemoteCache.h:71
Definition: RemoteCacheManager.h:36
Definition: TimeUnit.h:15
std::string getProtocolVersion()
Definition: RemoteCache.h:80
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:340
std::map< HR_SHARED_PTR< K >, HR_SHARED_PTR< V > > getBulk(int nrOfEntries)
Definition: RemoteCache.h:451
V * replace(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
Definition: RemoteCache.h:295
void ping()
Definition: RemoteCache.h:535
const T * data() const
Definition: portable.h:257
HR_EXTERN void * base_getWithMetadata(const void *key, MetadataValue *metadata)
static const char * getVersionCString()
Definition: Version.h:42
TimeUnit
Definition: TimeUnit.h:12
Definition: RemoteCacheBase.h:27
std::pair< HR_SHARED_PTR< V >, MetadataValue > getWithMetadata(const K &key)
Definition: RemoteCache.h:435
Definition: portable.h:268
std::set< HR_SHARED_PTR< K > > keySet()
Definition: RemoteCache.h:477
bool containsValue(const V &val)
Definition: RemoteCache.h:367
V * replace(const K &key, const V &val, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:279
Definition: TimeUnit.h:14
Definition: TimeUnit.h:20
Definition: TimeUnit.h:19
Definition: MetadataValue.h:9
V * replace(const K &key, const V &val, uint64_t lifespan=0, uint64_t maxIdle=0)
Definition: RemoteCache.h:265
Flag
Definition: Flag.h:7
std::pair< HR_SHARED_PTR< V >, VersionedValue > getWithVersion(const K &key)
Definition: RemoteCache.h:420
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:114
HR_EXTERN void * base_get(const void *key)
void putAll(const std::map< K, V > &map, uint64_t lifespan, TimeUnit lifespanUnit)
Definition: RemoteCache.h:230
const my_pair * data() const
Definition: portable.h:383
bool removeWithVersion(const K &key, uint64_t version)
Definition: RemoteCache.h:407