/builddir/build/BUILD/infinispan-hotrod-cpp-6.2.1.Final-Source/include/infinispan/hotrod/RemoteCache.h

Go to the documentation of this file.
00001 #ifndef ISPN_HOTROD_REMOTECACHE_H
00002 #define ISPN_HOTROD_REMOTECACHE_H
00003 
00004 
00005 
00006 #include "infinispan/hotrod/RemoteCacheBase.h"
00007 #include "infinispan/hotrod/Marshaller.h"
00008 #include "infinispan/hotrod/Flag.h"
00009 #include "infinispan/hotrod/MetadataValue.h"
00010 #include "infinispan/hotrod/TimeUnit.h"
00011 #include "infinispan/hotrod/VersionedValue.h"
00012 #include "infinispan/hotrod/Version.h"
00013 
00014 #include <cmath>
00015 #include <set>
00016 #include <map>
00017 #include <sstream>
00018 #include <stdexcept>
00019 #include <vector>
00020 
00021 namespace infinispan {
00022 namespace hotrod {
00023 
00054 template <class K, class V> class RemoteCache : private RemoteCacheBase
00055 {
00056   public:
00062     std::string getName() {
00063         return std::string(base_getName());
00064     }
00065 
00071     std::string getVersion() {
00072         return Version::getVersionCString();
00073     }
00074 
00080     std::string getProtocolVersion() {
00081         return Version::getProtocolVersionCString();
00082     }
00083 
00091     V* get(const K& key) {
00092         return (V *) base_get(&key);
00093     }
00114     V* put(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
00115         return put(key, val, lifespan, SECONDS, maxIdle, SECONDS);
00116     }
00136     V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
00137         return put(key, val, lifespan, lifespanUnit, 0, SECONDS);
00138     }
00161     V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
00162         return (V *) base_put(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
00163     }
00175     V* putIfAbsent(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
00176         return putIfAbsent(key, val, lifespan, SECONDS, maxIdle, SECONDS);
00177     }
00188     V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
00189         return putIfAbsent(key, val, lifespan, lifespanUnit, 0, SECONDS);
00190     }
00204     V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
00205         return (V *)base_putIfAbsent(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
00206     }
00217     void putAll(const std::map<K, V>& map, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
00218         putAll(map, lifespan, SECONDS, maxIdle, SECONDS);
00219     }
00230     void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit) {
00231         putAll(map, lifespan, lifespanUnit, 0, SECONDS);
00232     }
00246     void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
00247         uint64_t lifespanMillis = toSeconds(lifespan, lifespanUnit);
00248         uint64_t maxIdleMillis = toSeconds(maxIdle, maxIdleUnit);
00249 
00250         for (typename std::map<K, V>::const_iterator it = map.begin(); it != map.end(); ++it) {
00251             put(it->first, it->second, lifespanMillis, maxIdleMillis);
00252         }
00253     }
00265     V* replace(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
00266         return replace(key, val, lifespan, SECONDS, maxIdle, SECONDS);
00267     }
00279     V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
00280         return replace(key, val, lifespan, lifespanUnit, 0, SECONDS);
00281     }
00295     V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
00296         return (V *) base_replace(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit));
00297     }
00310     V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
00311         return replace(key, oldVal, val, lifespan, SECONDS, maxIdle, SECONDS);
00312     }
00324     V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit) {
00325         return replace(key, oldVal, val, lifespan, lifespanUnit, 0, SECONDS);
00326     }
00340     V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit) {
00341         throw UnsupportedOperationException();
00342     }
00343 
00350     V* remove(const K& key) {
00351         return (V *) base_remove(&key);
00352     }
00353 
00359     bool containsKey(const K& key) {
00360         return base_containsKey(&key);
00361     }
00362 
00367     bool containsValue(const V& val) {
00368         throw UnsupportedOperationException();
00369     }
00384     bool replaceWithVersion(
00385         const K& key, const V& val,
00386         uint64_t version, uint64_t lifespan = 0, uint64_t maxIdle = 0) {
00387         return base_replaceWithVersion(&key, &val, version, lifespan, maxIdle);
00388     }
00407     bool removeWithVersion(const K& key, uint64_t version) {
00408         return base_removeWithVersion(&key, version);
00409     }
00420     std::pair<HR_SHARED_PTR<V>, VersionedValue> getWithVersion(const K& key) {
00421         VersionedValue version;
00422         void *value = base_getWithVersion(&key, &version);
00423         return std::make_pair(HR_SHARED_PTR<V>((V *) value), version);
00424     }
00435     std::pair<HR_SHARED_PTR<V>, MetadataValue> getWithMetadata(const K& key) {
00436         MetadataValue metadata;
00437         void *value = base_getWithMetadata(&key, &metadata);
00438         return std::make_pair(HR_SHARED_PTR<V>((V *) value), metadata);
00439     }
00444     std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > getBulk() {
00445         return getBulk(0);
00446     }
00451     std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > getBulk(int nrOfEntries) {
00452         portable::map<void*, void*> mbuf;
00453         base_getBulk(nrOfEntries, mbuf);
00454 
00455         std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > result;
00456         const portable::pair<void*, void*> *data = mbuf.data();
00457         for (size_t i = 0; i < mbuf.size(); i++) {
00458             result.insert(std::make_pair(
00459                 HR_SHARED_PTR<K>((K*)data[i].key), HR_SHARED_PTR<V>((V*)data[i].value)));
00460         }
00461         return result;
00462     }
00467     std::set<std::pair<K, V> > entrySet() {
00468         throw UnsupportedOperationException();
00469     }
00477     std::set<HR_SHARED_PTR<K> > keySet() {
00478         portable::vector<void*> p;
00479         base_keySet(0, p);
00480 
00481         std::set<HR_SHARED_PTR<K> > result;
00482         for (size_t i = 0; i < p.size(); ++i) {
00483             result.insert(HR_SHARED_PTR<K>((K*)p.data()[i]));
00484         }
00485         return result;
00486     }
00492     uint64_t size() {
00493         return keySet().size();
00494     }
00500     bool isEmpty() {
00501         return 0 == size();
00502     }
00507     std::vector<V> values() {
00508         throw UnsupportedOperationException();
00509     }
00516     std::map<std::string, std::string> stats() {
00517         portable::map<portable::string,portable::string> statistics;
00518         base_stats(statistics);
00519         return statistics.std_map<std::string, portable::string::convert, std::string, portable::string::convert>
00520             (portable::string::convert(), portable::string::convert());
00521     }
00527     void clear() {
00528         base_clear();
00529     }
00535     void ping() {
00536         base_ping();
00537     }
00544     RemoteCache<K,V>& withFlags(Flag flags) {
00545         base_withFlags(flags);
00546         return *this;
00547     }
00548 
00549     RemoteCache(const RemoteCache &other) :
00550       RemoteCacheBase(other), keyMarshaller(other.keyMarshaller), valueMarshaller(other.valueMarshaller)
00551     {
00552         setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
00553     }
00554 
00555     RemoteCache<K,V>& operator=(const RemoteCache& other) {
00556         RemoteCacheBase::operator=(other);
00557         keyMarshaller = other.keyMarshaller;
00558         valueMarshaller = other.valueMarshaller;
00559         setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
00560         return *this;
00561     }
00562 
00563   private:
00564     RemoteCache() : RemoteCacheBase() {
00565         setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
00566     }
00567 
00568     uint64_t toSeconds(uint64_t time, TimeUnit unit) {
00569         uint64_t result;
00570         switch (unit) {
00571         case NANOSECONDS:
00572             result = (uint64_t) ceil(time / 1000000000.0);
00573             break;
00574         case MICROSECONDS:
00575             result = (uint64_t) ceil(time / 1000000.0);
00576             break;
00577         case MILLISECONDS:
00578             result = (uint64_t) ceil(time / 1000.0);
00579             break;
00580         case SECONDS:
00581             result = time;
00582             break;
00583         case MINUTES:
00584             result = time * 60;
00585             break;
00586         case HOURS:
00587             result = time * 3600;
00588             break;
00589         case DAYS:
00590             result = time * 86400;
00591             break;
00592         default:
00593             std::stringstream ss;
00594             ss << "Unhandled TimeUnit specified: " << unit << ".";
00595             throw std::invalid_argument(ss.str());
00596         }
00597         return result;
00598     }
00599 
00600     // type-hiding and resurrecting support
00601     static void keyMarshall(void *thisp, const void* key, ScopedBuffer &buf) {
00602         ((RemoteCache<K, V> *) thisp)->keyMarshaller->marshall(*(const K *) key, buf);
00603     }
00604     static void valueMarshall(void* thisp, const void* val, ScopedBuffer &buf) {
00605         ((RemoteCache<K, V> *)thisp)->valueMarshaller->marshall(*(const V *) val, buf);
00606     }
00607     static void* keyUnmarshall(void *thisp, const ScopedBuffer &buf) {
00608         return ((RemoteCache<K, V> *) thisp)->keyMarshaller->unmarshall(buf);
00609     }
00610     static void* valueUnmarshall(void* thisp, const ScopedBuffer &buf) {
00611         return ((RemoteCache<K, V> *)thisp)->valueMarshaller->unmarshall(buf);
00612     }
00613 
00614     portable::counting_ptr<Marshaller<K> > keyMarshaller;
00615     portable::counting_ptr<Marshaller<V> > valueMarshaller;
00616 
00617     // DEPRECATED: Library code must not use these fields
00618     portable::counting_ptr<portable::counted_wrapper<HR_SHARED_PTR<Marshaller<K> > > > keyMarshallerPtr;
00619     portable::counting_ptr<portable::counted_wrapper<HR_SHARED_PTR<Marshaller<V> > > > valueMarshallerPtr;
00620 
00621   friend class RemoteCacheManager;
00622 };
00623 
00624 }} // namespace
00625 
00626 #endif  /* ISPN_HOTROD_REMOTECACHE_H */

Generated on 25 Mar 2015 for JBoss Data Grid HotRod C++ Client by  doxygen 1.4.7