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 base_getName();
00064 }
00065
00071 std::string getVersion() {
00072 return Version::getVersion();
00073 }
00074
00080 std::string getProtocolVersion() {
00081 return Version::getProtocolVersion();
00082 }
00083
00091 V* get(const K& key) {
00092 ScopedBuffer vbuf;
00093 base_get(&key, &vbuf);
00094 return vbuf.getBytes() ? valueMarshaller->unmarshall(vbuf) : NULL;
00095 }
00116 V* put(
00117 const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0)
00118 {
00119 return put(key, val, lifespan, SECONDS, maxIdle, SECONDS);
00120 }
00140 V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit)
00141 {
00142 return put(key, val, lifespan, lifespanUnit, 0, SECONDS);
00143 }
00166 V* put(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
00167 {
00168 ScopedBuffer vbuf;
00169 base_put(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit), &vbuf);
00170 return vbuf.getBytes() ? valueMarshaller->unmarshall(vbuf) : NULL;
00171 }
00183 V* putIfAbsent(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0)
00184 {
00185 return putIfAbsent(key, val, lifespan, SECONDS, maxIdle, SECONDS);
00186 }
00197 V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit)
00198 {
00199 return putIfAbsent(key, val, lifespan, lifespanUnit, 0, SECONDS);
00200 }
00214 V* putIfAbsent(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
00215 {
00216 ScopedBuffer vbuf;
00217 base_putIfAbsent(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit), &vbuf);
00218 return vbuf.getBytes() ? valueMarshaller->unmarshall(vbuf) : NULL;
00219 }
00230 void putAll(const std::map<K, V>& map, uint64_t lifespan = 0, uint64_t maxIdle = 0)
00231 {
00232 putAll(map, lifespan, SECONDS, maxIdle, SECONDS);
00233 }
00244 void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit)
00245 {
00246 putAll(map, lifespan, lifespanUnit, 0, SECONDS);
00247 }
00261 void putAll(const std::map<K, V>& map, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
00262 {
00263 uint64_t lifespanMillis = toSeconds(lifespan, lifespanUnit);
00264 uint64_t maxIdleMillis = toSeconds(maxIdle, maxIdleUnit);
00265
00266 for (typename std::map<K, V>::const_iterator it = map.begin(); it != map.end(); ++it) {
00267 put(it->first, it->second, lifespanMillis, maxIdleMillis);
00268 }
00269 }
00281 V* replace(const K& key, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0)
00282 {
00283 return replace(key, val, lifespan, SECONDS, maxIdle, SECONDS);
00284 }
00296 V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit)
00297 {
00298 return replace(key, val, lifespan, lifespanUnit, 0, SECONDS);
00299 }
00313 V* replace(const K& key, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
00314 {
00315 ScopedBuffer vbuf;
00316 base_replace(&key, &val, toSeconds(lifespan, lifespanUnit), toSeconds(maxIdle, maxIdleUnit), &vbuf);
00317 return vbuf.getBytes() ? valueMarshaller->unmarshall(vbuf) : NULL;
00318 }
00331 V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan = 0, uint64_t maxIdle = 0)
00332 {
00333 return replace(key, oldVal, val, lifespan, SECONDS, maxIdle, SECONDS);
00334 }
00346 V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit)
00347 {
00348 return replace(key, oldVal, val, lifespan, lifespanUnit, 0, SECONDS);
00349 }
00363 V* replace(const K& key, const V& oldVal, const V& val, uint64_t lifespan, TimeUnit lifespanUnit, uint64_t maxIdle, TimeUnit maxIdleUnit)
00364 {
00365 throw UnsupportedOperationException();
00366 }
00367
00374 V* remove(const K& key) {
00375 ScopedBuffer vbuf;
00376 base_remove(&key, &vbuf);
00377 return vbuf.getBytes() ? valueMarshaller->unmarshall(vbuf) : NULL;
00378 }
00384 bool containsKey(const K& key) {
00385 bool res;
00386 base_containsKey(&key, &res);
00387 return res;
00388 }
00393 bool containsValue(const V& val) {
00394 throw UnsupportedOperationException();
00395 }
00410 bool replaceWithVersion(
00411 const K& key, const V& val,
00412 uint64_t version, uint64_t lifespan = 0, uint64_t maxIdle = 0)
00413 {
00414 bool res;
00415 base_replaceWithVersion(&key, &val, version, lifespan, maxIdle, &res);
00416 return res;
00417 }
00436 bool removeWithVersion(const K& key, uint64_t version) {
00437 bool res;
00438 base_removeWithVersion(&key, version, &res);
00439 return res;
00440 }
00451 std::pair<HR_SHARED_PTR<V>, VersionedValue> getWithVersion(const K& key) {
00452 ScopedBuffer vbuf;
00453 VersionedValue version;
00454 base_getWithVersion(&key, &vbuf, &version);
00455 return vbuf.getBytes() ?
00456 std::make_pair(HR_SHARED_PTR<V>(valueMarshaller->unmarshall(vbuf)), version) :
00457 std::make_pair(HR_SHARED_PTR<V>(), version);
00458 }
00469 std::pair<HR_SHARED_PTR<V>, MetadataValue> getWithMetadata(const K& key) {
00470 ScopedBuffer vbuf;
00471 MetadataValue metadata;
00472 base_getWithMetadata(&key, &vbuf, &metadata);
00473 return vbuf.getBytes() ?
00474 std::make_pair(HR_SHARED_PTR<V>(valueMarshaller->unmarshall(vbuf)), metadata) :
00475 std::make_pair(HR_SHARED_PTR<V>(), metadata);
00476 }
00481 std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > getBulk() {
00482 return getBulk(0);
00483 }
00488 std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > getBulk(int nrOfEntries) {
00489 std::map<void*, void*> mbuf;
00490 base_getBulk(nrOfEntries, &mbuf);
00491
00492 std::map<HR_SHARED_PTR<K>, HR_SHARED_PTR<V> > result;
00493 for(std::map<void*, void*>::const_iterator i = mbuf.begin(); i != mbuf.end(); i++) {
00494 result.insert(std::make_pair(
00495 HR_SHARED_PTR<K>((K*)i->first), HR_SHARED_PTR<V>((V*)i->second)));
00496 }
00497 return result;
00498 }
00503 std::set<std::pair<K, V> > entrySet() {
00504 throw UnsupportedOperationException();
00505 }
00513 std::set<HR_SHARED_PTR<K> > keySet() {
00514 std::set<void*> p;
00515 base_keySet(0,&p);
00516
00517 std::set<HR_SHARED_PTR<K> > result;
00518 for(std::set<void*>::const_iterator i = p.begin(); i != p.end(); i++) {
00519 result.insert(HR_SHARED_PTR<K>((K*)*i));
00520 }
00521 return result;
00522 }
00528 uint64_t size() {
00529 std::map<std::string,std::string> statistics;
00530 base_stats(&statistics);
00531 std::istringstream sizeAsStream;
00532 sizeAsStream.str(statistics["currentNumberOfEntries"]);
00533 int result;
00534 sizeAsStream >> result;
00535
00536 return result;
00537 }
00543 bool isEmpty() {
00544 return 0 == size();
00545 }
00550 std::vector<V> values() {
00551 throw UnsupportedOperationException();
00552 }
00559 std::map<std::string, std::string> stats() {
00560 std::map<std::string,std::string> statistics;
00561 base_stats(&statistics);
00562 return statistics;
00563 }
00569 void clear() {
00570 base_clear();
00571 }
00577 void ping() {
00578 base_ping();
00579 }
00586 RemoteCache<K,V>& withFlags(Flag flags) {
00587 base_withFlags(flags);
00588 return *this;
00589 }
00590
00591 RemoteCache(const RemoteCache &other) :
00592 RemoteCacheBase(other), keyMarshaller(other.keyMarshaller), valueMarshaller(other.valueMarshaller)
00593 {
00594 setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
00595 }
00596
00597 RemoteCache<K,V>& operator=(const RemoteCache& other) {
00598 RemoteCacheBase::operator=(other);
00599 keyMarshaller = other.keyMarshaller;
00600 valueMarshaller = other.valueMarshaller;
00601 setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
00602 return *this;
00603 }
00604
00605 private:
00606 RemoteCache() : RemoteCacheBase() {
00607 setMarshallers(this, &keyMarshall, &valueMarshall, &keyUnmarshall, &valueUnmarshall);
00608 }
00609
00610 uint64_t toSeconds(uint64_t time, TimeUnit unit) {
00611 uint64_t result;
00612 switch (unit) {
00613 case NANOSECONDS:
00614 result = (uint64_t) ceil(time / 1000000000.0);
00615 break;
00616 case MICROSECONDS:
00617 result = (uint64_t) ceil(time / 1000000.0);
00618 break;
00619 case MILLISECONDS:
00620 result = (uint64_t) ceil(time / 1000.0);
00621 break;
00622 case SECONDS:
00623 result = time;
00624 break;
00625 case MINUTES:
00626 result = time * 60;
00627 break;
00628 case HOURS:
00629 result = time * 3600;
00630 break;
00631 case DAYS:
00632 result = time * 86400;
00633 break;
00634 default:
00635 std::stringstream ss;
00636 ss << "Unhandled TimeUnit specified: " << unit << ".";
00637 throw std::invalid_argument(ss.str());
00638 }
00639 return result;
00640 }
00641
00642
00643 static void keyMarshall(void *thisp, const void* key, void* buf) {
00644 ((RemoteCache<K, V> *) thisp)->keyMarshaller->marshall(*(const K *) key, *(ScopedBuffer *) buf);
00645 }
00646 static void valueMarshall(void* thisp, const void* val, void* buf) {
00647 ((RemoteCache<K, V> *)thisp)->valueMarshaller->marshall(*(const V *) val, *(ScopedBuffer *) buf);
00648 }
00649 static void* keyUnmarshall(void *thisp, const void* buf) {
00650 return ((RemoteCache<K, V> *) thisp)->keyMarshaller->unmarshall(*(const ScopedBuffer *) buf);
00651 }
00652 static void* valueUnmarshall(void* thisp, const void* buf) {
00653 return ((RemoteCache<K, V> *)thisp)->valueMarshaller->unmarshall(*(const ScopedBuffer *) buf);
00654 }
00655
00656 HR_SHARED_PTR<Marshaller<K> > keyMarshaller;
00657 HR_SHARED_PTR<Marshaller<V> > valueMarshaller;
00658
00659 friend class RemoteCacheManager;
00660 };
00661
00662 }}
00663
00664 #endif