16 #ifndef HAZELCAST_IMAP
17 #define HAZELCAST_IMAP
25 #include "hazelcast/client/protocol/codec/MapAddEntryListenerWithPredicateCodec.h"
26 #include "hazelcast/client/impl/EntryArrayImpl.h"
27 #include "hazelcast/client/proxy/IMapImpl.h"
28 #include "hazelcast/client/impl/EntryEventHandler.h"
29 #include "hazelcast/client/EntryListener.h"
30 #include "hazelcast/client/EntryView.h"
32 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
34 #pragma warning(disable: 4251) //for dll export
40 template <
typename K,
typename V>
57 template<
typename K,
typename V>
58 class IMap :
public proxy::IMapImpl {
71 return proxy::IMapImpl::containsKey(toData(key));
81 return proxy::IMapImpl::containsValue(toData(value));
91 boost::shared_ptr<V>
get(
const K &key) {
92 return boost::shared_ptr<V>(toObject<V>(proxy::IMapImpl::getData(toData(key))));
103 boost::shared_ptr<V>
put(
const K &key,
const V &value) {
104 return boost::shared_ptr<V>(toObject<V>(proxy::IMapImpl::putData(toData(key), toData(value))));
114 boost::shared_ptr<V>
remove(
const K &key) {
115 return boost::shared_ptr<V>(toObject<V>(proxy::IMapImpl::removeData(toData(key))));
125 bool remove(
const K &key,
const V &value) {
126 return proxy::IMapImpl::remove(toData(key), toData(value));
136 proxy::IMapImpl::deleteEntry(toData(key));
144 proxy::IMapImpl::flush();
158 return proxy::IMapImpl::tryRemove(toData(key), timeoutInMillis);
173 bool tryPut(
const K &key,
const V &value,
long timeoutInMillis) {
174 return proxy::IMapImpl::tryPut(toData(key), toData(value), timeoutInMillis);
188 boost::shared_ptr<V>
put(
const K &key,
const V &value,
long ttlInMillis) {
189 return boost::shared_ptr<V>(toObject<V>(proxy::IMapImpl::putData(toData(key), toData(value), ttlInMillis)));
202 proxy::IMapImpl::putTransient(toData(key), toData(value), ttlInMillis);
228 boost::shared_ptr<V>
putIfAbsent(
const K &key,
const V &value,
long ttlInMillis) {
229 return boost::shared_ptr<V>(toObject<V>(proxy::IMapImpl::putIfAbsentData(toData(key), toData(value), ttlInMillis)));
239 bool replace(
const K &key,
const V &oldValue,
const V &newValue) {
240 return proxy::IMapImpl::replace(toData(key), toData(oldValue), toData(newValue));
250 boost::shared_ptr<V>
replace(
const K &key,
const V &value) {
251 return boost::shared_ptr<V>(toObject<V>(proxy::IMapImpl::replaceData(toData(key), toData(value))));
263 void set(
const K &key,
const V &value,
long ttl) {
264 proxy::IMapImpl::set(toData(key), toData(value), ttl);
282 proxy::IMapImpl::lock(toData(key));
303 void lock(
const K &key,
long leaseTime) {
304 proxy::IMapImpl::lock(toData(key), leaseTime);
316 return proxy::IMapImpl::isLocked(toData(key));
348 bool tryLock(
const K &key,
long timeInMillis) {
349 return proxy::IMapImpl::tryLock(toData(key), timeInMillis);
366 proxy::IMapImpl::unlock(toData(key));
378 proxy::IMapImpl::forceUnlock(toData(key));
392 template<
typename MapInterceptor>
394 return proxy::IMapImpl::addInterceptor(interceptor);
404 proxy::IMapImpl::removeInterceptor(
id);
422 impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler> *entryEventHandler =
423 new impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler>(
424 getName(), context->getClusterService(), context->getSerializationService(), listener,
426 return proxy::IMapImpl::addEntryListener(entryEventHandler, includeValue);
445 impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerWithPredicateCodec::AbstractEventHandler> *entryEventHandler =
446 new impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerWithPredicateCodec::AbstractEventHandler>(
447 getName(), context->getClusterService(), context->getSerializationService(), listener,
449 return proxy::IMapImpl::addEntryListener(entryEventHandler, predicate, includeValue);
462 return proxy::IMapImpl::removeEntryListener(registrationId);
480 serialization::pimpl::Data keyData = toData(key);
481 impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler> *entryEventHandler =
482 new impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler>(
483 getName(), context->getClusterService(), context->getSerializationService(), listener,
485 return proxy::IMapImpl::addEntryListener(entryEventHandler, keyData, includeValue);
497 serialization::pimpl::Data keyData = toData(key);
498 std::auto_ptr<map::DataEntryView> dataEntryView = proxy::IMapImpl::getEntryViewData(keyData);
499 std::auto_ptr<V> v = toObject<V>(dataEntryView->getValue());
515 return proxy::IMapImpl::evict(toData(key));
530 proxy::IMapImpl::evictAll();
539 std::map<K, V>
getAll(
const std::set<K> &keys) {
540 std::vector<serialization::pimpl::Data>
keySet(keys.size());
542 for (
typename std::set<K>::iterator it = keys.begin(); it != keys.end(); ++it) {
543 keySet[i++] = toData(*it);
545 std::map<K, V> result;
546 std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> >
entrySet = proxy::IMapImpl::getAllData(
548 for (std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> >::const_iterator it = entrySet.begin();
549 it != entrySet.end(); ++it) {
550 std::auto_ptr<K> key = toObject<K>(it->first);
551 std::auto_ptr<V> value = toObject<V>(it->second);
552 result[*key] = *value;
565 std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::keySetData();
566 size_t size = dataResult.size();
567 std::vector<K> keys(size);
568 for (
size_t i = 0; i <
size; ++i) {
569 std::auto_ptr<K> key = toObject<K>(dataResult[i]);
604 std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::keySetData(predicate);
605 size_t size = dataResult.size();
606 std::vector<K> keys(size);
607 for (
size_t i = 0; i <
size; ++i) {
608 std::auto_ptr<K> key = toObject<K>(dataResult[i]);
626 predicate.setIterationType(query::KEY);
628 std::vector<serialization::pimpl::Data> dataResult = keySetForPagingPredicateData(predicate);
630 EntryVector entryResult;
631 for (std::vector<serialization::pimpl::Data>::iterator it = dataResult.begin();it != dataResult.end(); ++it) {
632 entryResult.push_back(std::pair<serialization::pimpl::Data, serialization::pimpl::Data>(*it, serialization::pimpl::Data()));
635 impl::EntryArrayImpl<K, V> entries(entryResult, context->getSerializationService());
636 entries.sort(query::KEY, predicate.getComparator());
638 std::pair<size_t, size_t> range = updateAnchor<K, V>(entries, predicate, query::KEY);
640 std::vector<K> result;
641 for (
size_t i = range.first; i < range.second; ++i) {
642 result.push_back(*entries.getKey(i));
656 std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::valuesData();
657 size_t size = dataResult.size();
658 std::vector<K>
values(size);
659 for (
size_t i = 0; i <
size; ++i) {
660 std::auto_ptr<K> value = toObject<K>(dataResult[i]);
690 std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::valuesData(predicate);
691 size_t size = dataResult.size();
692 std::vector<V>
values(size);
693 for (
size_t i = 0; i <
size; ++i) {
694 std::auto_ptr<V> value = toObject<V>(dataResult[i]);
710 predicate.setIterationType(query::VALUE);
712 EntryVector dataResult = proxy::IMapImpl::valuesForPagingPredicateData(predicate);
714 impl::EntryArrayImpl<K, V> entries(dataResult, context->getSerializationService());
716 entries.sort(query::VALUE, predicate.getComparator());
718 std::pair<size_t, size_t> range = updateAnchor<K, V>(entries, predicate, query::VALUE);
720 std::vector<V> result;
721 for (
size_t i = range.first; i < range.second; ++i) {
722 result.push_back(*entries.getValue(i));
735 std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult = proxy::IMapImpl::entrySetData();
736 size_t size = dataResult.size();
737 std::vector<std::pair<K, V> > entries(size);
738 for (
size_t i = 0; i <
size; ++i) {
739 std::auto_ptr<K> key = toObject<K>(dataResult[i].first);
740 std::auto_ptr<V> value = toObject<V>(dataResult[i].second);
741 entries[i] = std::make_pair(*key, *value);
759 std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult = proxy::IMapImpl::entrySetData(
761 size_t size = dataResult.size();
762 std::vector<std::pair<K, V> > entries(size);
763 for (
size_t i = 0; i <
size; ++i) {
764 std::auto_ptr<K> key = toObject<K>(dataResult[i].first);
765 std::auto_ptr<V> value = toObject<V>(dataResult[i].second);
766 entries[i] = std::make_pair(*key, *value);
782 std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult = proxy::IMapImpl::entrySetData(
784 size_t size = dataResult.size();
785 std::vector<std::pair<K, V> > entries(size);
786 for (
size_t i = 0; i <
size; ++i) {
787 std::auto_ptr<K> key = toObject<K>(dataResult[i].first);
788 std::auto_ptr<V> value = toObject<V>(dataResult[i].second);
789 entries[i] = std::make_pair(*key, *value);
805 std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult = proxy::IMapImpl::entrySetForPagingPredicateData(
808 impl::EntryArrayImpl<K, V> entries(dataResult, context->getSerializationService());
809 entries.sort(query::ENTRY, predicate.getComparator());
811 std::pair<size_t, size_t> range = updateAnchor<K, V>(entries, predicate, query::ENTRY);
813 std::vector<std::pair<K, V> > result;
814 for (
size_t i = range.first; i < range.second; ++i) {
815 std::pair<const K *, const V *> entry = entries[i];
816 result.push_back(std::pair<K, V>(*entry.first, *entry.second));
853 void addIndex(
const std::string &attribute,
bool ordered) {
854 proxy::IMapImpl::addIndex(attribute, ordered);
871 template<
typename ResultType,
typename EntryProcessor>
872 boost::shared_ptr<ResultType>
executeOnKey(
const K &key, EntryProcessor &entryProcessor) {
873 std::auto_ptr<serialization::pimpl::Data> resultData = proxy::IMapImpl::executeOnKeyData<K, EntryProcessor>(key, entryProcessor);
875 return boost::shared_ptr<ResultType>(toObject<ResultType>(resultData));
891 template<
typename ResultType,
typename EntryProcessor>
892 std::map<K, boost::shared_ptr<ResultType> >
executeOnEntries(EntryProcessor &entryProcessor) {
893 EntryVector entries = proxy::IMapImpl::executeOnEntriesData<EntryProcessor>(entryProcessor);
894 std::map<K, boost::shared_ptr<ResultType> > result;
895 for (
size_t i = 0; i < entries.size(); ++i) {
896 std::auto_ptr<K> keyObj = toObject<K>(entries[i].first);
897 std::auto_ptr<ResultType> resObj = toObject<ResultType>(entries[i].second);
898 result[*keyObj] = resObj;
920 template<
typename ResultType,
typename EntryProcessor>
923 return executeOnEntries<ResultType, EntryProcessor>(entryProcessor, *p);
940 template<
typename ResultType,
typename EntryProcessor>
942 EntryVector entries = proxy::IMapImpl::executeOnEntriesData<EntryProcessor>(entryProcessor, predicate);
943 std::map<K, boost::shared_ptr<ResultType> > result;
944 for (
size_t i = 0; i < entries.size(); ++i) {
945 std::auto_ptr<K> keyObj = toObject<K>(entries[i].first);
946 std::auto_ptr<ResultType> resObj = toObject<ResultType>(entries[i].second);
947 result[*keyObj] = resObj;
959 void set(
const K &key,
const V &value) {
971 return proxy::IMapImpl::size();
980 return proxy::IMapImpl::isEmpty();
994 void putAll(
const std::map<K, V> &entries) {
995 return proxy::IMapImpl::putAll(toDataEntries(entries));
1003 proxy::IMapImpl::clear();
1007 IMap(
const std::string &instanceName, spi::ClientContext *context)
1008 : proxy::IMapImpl(instanceName, context) {
1014 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
1015 #pragma warning(pop)
void putTransient(const K &key, const V &value, long ttlInMillis)
Same as put(K, V, long, TimeUnit) but MapStore, if defined, will not be called to store/persist the e...
Definition: IMap.h:201
std::vector< std::pair< K, V > > entrySet()
Returns a std::vector< std::pair<K, V> > clone of the mappings contained in this map.
Definition: IMap.h:734
boost::shared_ptr< V > putIfAbsent(const K &key, const V &value, long ttlInMillis)
Puts an entry into this map with a given ttl (time to live) value if the specified key is not already...
Definition: IMap.h:228
std::map< K, V > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: IMap.h:539
void putAll(const std::map< K, V > &entries)
Copies all of the mappings from the specified map to this map (optional operation).
Definition: IMap.h:994
boost::shared_ptr< V > replace(const K &key, const V &value)
Replaces the entry for a key only if currently mapped to some value.
Definition: IMap.h:250
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: IMap.h:328
Map Entry listener to get notified when a map entry is added, removed, updated or evicted...
Definition: EntryListener.h:45
bool tryLock(const K &key, long timeInMillis)
Tries to acquire the lock for the specified key.
Definition: IMap.h:348
bool tryPut(const K &key, const V &value, long timeoutInMillis)
Tries to put the given key, value into this map within specified timeout value.
Definition: IMap.h:173
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: IMap.h:959
std::vector< V > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:689
std::vector< V > values()
Returns a vector clone of the values contained in this map.
Definition: IMap.h:655
void deleteEntry(const K &key)
removes entry from map.
Definition: IMap.h:135
void evictAll()
Evicts all keys from this map except locked ones.
Definition: IMap.h:529
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: IMap.h:892
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:421
std::vector< K > keySet()
Returns a vector clone of the keys contained in this map.
Definition: IMap.h:564
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: IMap.h:263
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: IMap.h:479
std::vector< std::pair< K, V > > entrySet(query::PagingPredicate< K, V > &predicate)
Queries the map based on the specified predicate and returns the matching entries.
Definition: IMap.h:804
bool replace(const K &key, const V &oldValue, const V &newValue)
Replaces the entry for a key only if currently mapped to a given value.
Definition: IMap.h:239
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(EntryProcessor &entryProcessor, const query::Predicate &predicate)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: IMap.h:941
boost::shared_ptr< V > putIfAbsent(const K &key, const V &value)
Puts an entry into this map, if the specified key is not already associated with a value...
Definition: IMap.h:213
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: IdentifiedDataSerializable.h:42
NOTE: PagingPredicate can only be used with values(), keySet() and entries() methods!!! ...
Definition: PagingPredicate.h:127
void addIndex(const std::string &attribute, bool ordered)
Adds an index to this map for the specified entries so that queries can run faster.
Definition: IMap.h:853
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: IMap.h:315
std::vector< K > keySet(const query::Predicate &predicate)
Queries the map based on the specified predicate and returns the keys of matching entries...
Definition: IMap.h:603
boost::shared_ptr< V > put(const K &key, const V &value, long ttlInMillis)
Puts an entry into this map with a given ttl (time to live) value.
Definition: IMap.h:188
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: IMap.h:143
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: IMap.h:393
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(EntryProcessor &entryProcessor, const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:921
Adaptor class to IMap which provides releasable raw pointers for returned objects.
Definition: RawPointerMap.h:50
void lock(const K &key)
Acquires the lock for the specified key.
Definition: IMap.h:281
bool tryRemove(const K &key, long timeoutInMillis)
Tries to remove the entry with the given key from this map within specified timeout value...
Definition: IMap.h:157
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: IMap.h:303
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: IMap.h:403
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:444
This is a merker class for Predicate classes.
Definition: Predicate.h:36
std::vector< std::pair< K, V > > entrySet(const query::Predicate &predicate)
Queries the map based on the specified predicate and returns the matching entries.
Definition: IMap.h:781
EntryView represents a readonly view of a map entry.
Definition: EntryView.h:39
std::vector< K > keySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:587
Concurrent, distributed, observable and queryable map client.
Definition: IMap.h:58
bool isEmpty()
Returns true if this map contains no key-value mappings.
Definition: IMap.h:979
void unlock(const K &key)
Releases the lock for the specified key.
Definition: IMap.h:365
bool evict(const K &key)
Evicts the specified key from this map.
Definition: IMap.h:514
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: IMap.h:377
std::vector< V > values(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:676
void clear()
Removes all of the mappings from this map (optional operation).
Definition: IMap.h:1002
std::vector< V > values(query::PagingPredicate< K, V > &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:709
boost::shared_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: IMap.h:103
EntryView< K, V > getEntryView(const K &key)
Returns the EntryView for the specified key.
Definition: IMap.h:496
bool containsValue(const V &value)
check if this map contains value.
Definition: IMap.h:80
std::vector< K > keySet(query::PagingPredicate< K, V > &predicate)
Queries the map based on the specified predicate and returns the keys of matching entries...
Definition: IMap.h:625
Hazelcast Client enables you to do all Hazelcast operations without being a member of the cluster...
Definition: HazelcastClient.h:453
bool containsKey(const K &key)
check if this map contains key.
Definition: IMap.h:70
std::vector< std::pair< K, V > > entrySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:758
bool removeEntryListener(const std::string ®istrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: IMap.h:461
int size()
Returns the number of key-value mappings in this map.
Definition: IMap.h:970
boost::shared_ptr< ResultType > executeOnKey(const K &key, EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: IMap.h:872