16 #ifndef HAZELCAST_CLIENT_MIXEDTYPE_MAP_H_
17 #define HAZELCAST_CLIENT_MIXEDTYPE_MAP_H_
27 #include "hazelcast/client/TypedData.h"
28 #include "hazelcast/client/monitor/LocalMapStats.h"
29 #include "hazelcast/client/monitor/impl/NearCacheStatsImpl.h"
30 #include "hazelcast/client/monitor/impl/LocalMapStatsImpl.h"
31 #include "hazelcast/client/protocol/codec/MapAddEntryListenerWithPredicateCodec.h"
32 #include "hazelcast/client/impl/EntryArrayImpl.h"
33 #include "hazelcast/client/proxy/IMapImpl.h"
34 #include "hazelcast/client/impl/EntryEventHandler.h"
35 #include "hazelcast/client/EntryListener.h"
36 #include "hazelcast/client/EntryView.h"
37 #include "hazelcast/client/serialization/pimpl/SerializationService.h"
38 #include "hazelcast/client/Future.h"
39 #include "hazelcast/client/protocol/codec/MapSubmitToKeyCodec.h"
41 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
43 #pragma warning(disable: 4251) //for dll export
63 typedef std::map<int, std::vector<boost::shared_ptr<serialization::pimpl::Data> > > PID_TO_KEY_MAP;
65 ClientMapProxy(
const std::string &instanceName, spi::ClientContext *context);
75 serialization::pimpl::Data keyData = toData(key);
76 return containsKeyInternal(keyData);
87 return proxy::IMapImpl::containsValue(toData(value));
99 serialization::pimpl::Data keyData = toData(key);
100 boost::shared_ptr<TypedData> value = getInternal(keyData);
113 template <
typename K,
typename V>
115 return put(key, value, -1);
129 template <
typename K,
typename V>
131 serialization::pimpl::Data keyData = toData(key);
132 serialization::pimpl::Data valueData = toData(value);
134 return TypedData(putInternal(keyData, valueData, ttlInMillis), context->getSerializationService());
144 template <
typename K>
146 serialization::pimpl::Data keyData = toData(key);
148 std::auto_ptr<serialization::pimpl::Data> response = removeInternal(keyData);
149 return TypedData(response, context->getSerializationService());
159 template <
typename K,
typename V>
160 bool remove(
const K &key,
const V &value) {
161 serialization::pimpl::Data keyData = toData(key);
162 serialization::pimpl::Data valueData = toData(value);
164 return removeInternal(keyData, valueData);
181 template <
typename K>
183 serialization::pimpl::Data keyData = toData(key);
185 deleteInternal(keyData);
204 template <
typename K>
206 serialization::pimpl::Data keyData = toData(key);
208 return tryRemoveInternal(keyData, timeoutInMillis);
223 template <
typename K,
typename V>
224 bool tryPut(
const K &key,
const V &value,
long timeoutInMillis) {
225 serialization::pimpl::Data keyData = toData(key);
226 serialization::pimpl::Data valueData = toData(value);
228 return tryPutInternal(keyData, valueData, timeoutInMillis);
240 template <
typename K,
typename V>
242 serialization::pimpl::Data keyData = toData(key);
243 serialization::pimpl::Data valueData = toData(value);
245 tryPutTransientInternal(keyData, valueData, ttlInMillis);
256 template <
typename K,
typename V>
258 return putIfAbsent(key, value, -1);
272 template <
typename K,
typename V>
274 serialization::pimpl::Data keyData = toData(key);
275 serialization::pimpl::Data valueData = toData(value);
277 std::auto_ptr<serialization::pimpl::Data> response = putIfAbsentInternal(keyData, valueData, ttlInMillis);
278 return TypedData(response, context->getSerializationService());
288 template <
typename K,
typename V,
typename NEWTYPE>
289 bool replace(
const K &key,
const V &oldValue,
const NEWTYPE &newValue) {
290 serialization::pimpl::Data keyData = toData(key);
291 serialization::pimpl::Data oldValueData = toData(oldValue);
292 serialization::pimpl::Data newValueData = toData(newValue);
294 return replaceIfSameInternal(keyData, oldValueData, newValueData);
304 template <
typename K,
typename V>
306 serialization::pimpl::Data keyData = toData(key);
307 serialization::pimpl::Data valueData = toData(value);
309 return TypedData(replaceInternal(keyData, valueData), context->getSerializationService());
319 template <
typename K,
typename V>
320 void set(
const K &key,
const V &value) {
333 template <
typename K,
typename V>
334 void set(
const K &key,
const V &value,
long ttl) {
335 serialization::pimpl::Data keyData = toData(key);
336 serialization::pimpl::Data valueData = toData(value);
338 setInternal(keyData, valueData, ttl);
355 template <
typename K>
378 template <
typename K>
379 void lock(
const K &key,
long leaseTime) {
380 serialization::pimpl::Data keyData = toData(key);
382 proxy::IMapImpl::lock(toData(key), leaseTime);
393 template <
typename K>
395 return proxy::IMapImpl::isLocked(toData(key));
407 template <
typename K>
409 return tryLock(key, 0);
428 template <
typename K>
429 bool tryLock(
const K &key,
long timeInMillis) {
430 return proxy::IMapImpl::tryLock(toData(key), timeInMillis);
446 template <
typename K>
448 proxy::IMapImpl::unlock(toData(key));
459 template <
typename K>
461 proxy::IMapImpl::forceUnlock(toData(key));
475 template<
typename MapInterceptor>
477 return proxy::IMapImpl::addInterceptor(interceptor);
486 void removeInterceptor(
const std::string &
id);
531 bool removeEntryListener(
const std::string ®istrationId);
546 template <
typename K>
548 serialization::pimpl::Data keyData = toData(key);
549 impl::MixedEntryEventHandler<protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler> *entryEventHandler =
550 new impl::MixedEntryEventHandler<protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler>(
551 getName(), context->getClusterService(), context->getSerializationService(),
554 return proxy::IMapImpl::addEntryListener(entryEventHandler, keyData, includeValue);
565 template <
typename K>
566 std::auto_ptr<EntryView<TypedData, TypedData> >
getEntryView(
const K &key) {
567 std::auto_ptr<serialization::pimpl::Data>keyData(
new serialization::pimpl::Data(toData(key)));
568 std::auto_ptr<map::DataEntryView> dataEntryView = proxy::IMapImpl::getEntryViewData(*keyData);
569 if ((map::DataEntryView *)NULL == dataEntryView.get()) {
570 return std::auto_ptr<EntryView<TypedData, TypedData> >();
572 TypedData value(std::auto_ptr<serialization::pimpl::Data>(
573 new serialization::pimpl::Data(std::auto_ptr<std::vector<byte> >(
574 new std::vector<byte>(dataEntryView->getValue().toByteArray())))),
575 context->getSerializationService());
576 const TypedData &keyTypedData =
TypedData(keyData, context->getSerializationService());
578 keyTypedData, value, *dataEntryView));
592 template <
typename K>
594 serialization::pimpl::Data keyData = toData(key);
596 return evictInternal(keyData);
618 template <
typename K>
619 std::vector<std::pair<TypedData, TypedData> >
getAll(
const std::set<K> &keys) {
621 return std::vector<std::pair<TypedData, TypedData> >();
624 PID_TO_KEY_MAP partitionToKeyData;
626 for (
typename std::set<K>::const_iterator it = keys.begin();it != keys.end();++it) {
628 serialization::pimpl::Data keyData = toData<K>(key);
630 int partitionId = getPartitionId(keyData);
632 partitionToKeyData[partitionId].push_back(toShared(keyData));
635 return toTypedDataEntrySet(getAllInternal(partitionToKeyData));
645 std::vector<TypedData> keySet();
685 template <
typename K,
typename V>
687 predicate.setIterationType(query::KEY);
689 std::vector<serialization::pimpl::Data> dataResult = keySetForPagingPredicateData(predicate);
691 EntryVector entryResult;
692 for (std::vector<serialization::pimpl::Data>::iterator it = dataResult.begin();
693 it != dataResult.end(); ++it) {
694 entryResult.push_back(std::pair<serialization::pimpl::Data, serialization::pimpl::Data>(*it,
695 serialization::pimpl::Data()));
698 client::impl::EntryArrayImpl<K, V> entries(entryResult, context->getSerializationService());
699 entries.sort(query::KEY, predicate.getComparator());
701 std::pair<size_t, size_t> range = updateAnchor<K, V>(entries, predicate, query::KEY);
703 std::vector<K> result;
704 for (
size_t i = range.first; i < range.second; ++i) {
705 result.push_back(*entries.getKey(i));
718 std::vector<TypedData> values();
751 template <
typename K,
typename V>
753 predicate.setIterationType(query::VALUE);
755 EntryVector dataResult = proxy::IMapImpl::valuesForPagingPredicateData(predicate);
757 client::impl::EntryArrayImpl<K, V> entries(dataResult, context->getSerializationService());
759 entries.sort(query::VALUE, predicate.getComparator());
761 std::pair<size_t, size_t> range = updateAnchor<K, V>(entries, predicate, query::VALUE);
763 std::vector<V> result;
764 for (
size_t i = range.first; i < range.second; ++i) {
765 result.push_back(*entries.getValue(i));
777 std::vector<std::pair<TypedData, TypedData> > entrySet();
803 std::vector<std::pair<TypedData, TypedData> > entrySet(
const query::Predicate &predicate);
815 template <
typename K,
typename V>
817 std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult =
818 proxy::IMapImpl::entrySetForPagingPredicateData(predicate);
820 client::impl::EntryArrayImpl<K, V> entries(dataResult, context->getSerializationService());
821 entries.sort(query::ENTRY, predicate.getComparator());
823 std::pair<size_t, size_t> range = updateAnchor<K, V>(entries, predicate, query::ENTRY);
825 std::vector<std::pair<K, V> > result;
826 for (
size_t i = range.first; i < range.second; ++i) {
827 std::pair<const K *, const V *> entry = entries[i];
828 result.push_back(std::pair<K, V>(*entry.first, *entry.second));
865 void addIndex(
const std::string &attribute,
bool ordered);
881 template<
typename K,
typename EntryProcessor>
883 serialization::pimpl::Data keyData = toData(key);
884 serialization::pimpl::Data processorData = toData(entryProcessor);
886 std::auto_ptr<serialization::pimpl::Data> response = executeOnKeyInternal(keyData, processorData);
888 return TypedData(response, getSerializationService());
891 template<
typename K,
typename EntryProcessor>
892 Future<TypedData> submitToKey(
const K &key,
const EntryProcessor &entryProcessor) {
893 serialization::pimpl::Data keyData = toData(key);
894 serialization::pimpl::Data processorData = toData(entryProcessor);
896 return submitToKeyInternal<TypedData>(keyData, processorData);
899 template<
typename K,
typename EntryProcessor>
900 std::map<K, TypedData> executeOnKeys(
const std::set<K> &keys,
const EntryProcessor &entryProcessor) {
901 EntryVector entries = executeOnKeysInternal<K, EntryProcessor>(keys, entryProcessor);
903 std::map<K, TypedData> result;
904 serialization::pimpl::SerializationService &serializationService = getSerializationService();
905 for (
size_t i = 0; i < entries.size(); ++i) {
906 std::auto_ptr<K> keyObj = toObject<K>(entries[i].first);
907 result[*keyObj] =
TypedData(std::auto_ptr<serialization::pimpl::Data>(
908 new serialization::pimpl::Data(entries[i].second)), serializationService);
925 template<
typename EntryProcessor>
927 EntryVector entries = proxy::IMapImpl::executeOnEntriesData<EntryProcessor>(entryProcessor);
928 std::map<TypedData, TypedData> result;
929 for (
size_t i = 0; i < entries.size(); ++i) {
930 std::auto_ptr<serialization::pimpl::Data> keyData(
new serialization::pimpl::Data(entries[i].first));
931 std::auto_ptr<serialization::pimpl::Data> valueData(
new serialization::pimpl::Data(entries[i].second));
932 serialization::pimpl::SerializationService &serializationService = context->getSerializationService();
933 result[
TypedData(keyData, serializationService)] =
TypedData(valueData, serializationService);
952 template<
typename EntryProcessor>
953 std::map<TypedData, TypedData>
955 EntryVector entries = proxy::IMapImpl::executeOnEntriesData<EntryProcessor>(entryProcessor,
957 std::map<TypedData, TypedData> result;
958 for (
size_t i = 0; i < entries.size(); ++i) {
959 std::auto_ptr<serialization::pimpl::Data> keyData(
new serialization::pimpl::Data(entries[i].first));
960 std::auto_ptr<serialization::pimpl::Data> valueData(
new serialization::pimpl::Data(entries[i].second));
961 serialization::pimpl::SerializationService &serializationService = context->getSerializationService();
962 result[
TypedData(keyData, serializationService)] =
TypedData(valueData, serializationService);
994 template<
typename K,
typename V>
995 void putAll(
const std::map<K, V> &entries) {
996 std::map<int, EntryVector> entryMap;
998 for (
typename std::map<K, V>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
999 serialization::pimpl::Data keyData = toData(it->first);
1000 serialization::pimpl::Data valueData = toData(it->second);
1002 int partitionId = getPartitionId(keyData);
1004 entryMap[partitionId].push_back(std::make_pair(keyData, valueData));
1007 putAllInternal(entryMap);
1016 serialization::pimpl::SerializationService &getSerializationService()
const;
1020 virtual boost::shared_ptr<TypedData> getInternal(serialization::pimpl::Data &keyData);
1022 virtual bool containsKeyInternal(
const serialization::pimpl::Data &keyData);
1024 virtual std::auto_ptr<serialization::pimpl::Data> removeInternal(
1025 const serialization::pimpl::Data &keyData);
1027 virtual bool removeInternal(
1028 const serialization::pimpl::Data &keyData,
const serialization::pimpl::Data &valueData);
1030 virtual void removeAllInternal(
const serialization::pimpl::Data &predicateData);
1032 virtual void deleteInternal(
const serialization::pimpl::Data &keyData);
1034 virtual bool tryRemoveInternal(
const serialization::pimpl::Data &keyData,
long timeoutInMillis);
1036 virtual bool tryPutInternal(
const serialization::pimpl::Data &keyData,
1037 const serialization::pimpl::Data &valueData,
long timeoutInMillis);
1039 virtual std::auto_ptr<serialization::pimpl::Data> putInternal(
const serialization::pimpl::Data &keyData,
1040 const serialization::pimpl::Data &valueData,
long timeoutInMillis);
1042 virtual void tryPutTransientInternal(
const serialization::pimpl::Data &keyData,
1043 const serialization::pimpl::Data &valueData,
int ttlInMillis);
1045 virtual std::auto_ptr<serialization::pimpl::Data> putIfAbsentInternal(
const serialization::pimpl::Data &keyData,
1046 const serialization::pimpl::Data &valueData,
1049 virtual bool replaceIfSameInternal(
const serialization::pimpl::Data &keyData,
1050 const serialization::pimpl::Data &valueData,
1051 const serialization::pimpl::Data &newValueData);
1053 virtual std::auto_ptr<serialization::pimpl::Data> replaceInternal(
const serialization::pimpl::Data &keyData,
1054 const serialization::pimpl::Data &valueData);
1056 virtual void setInternal(
const serialization::pimpl::Data &keyData,
const serialization::pimpl::Data &valueData,
1059 virtual bool evictInternal(
const serialization::pimpl::Data &keyData);
1061 EntryVector getAllInternal(
const PID_TO_KEY_MAP &partitionToKeyData) {
1062 std::map<int, std::vector<serialization::pimpl::Data> > datas;
1063 for (PID_TO_KEY_MAP::const_iterator it = partitionToKeyData.begin();
1064 it != partitionToKeyData.end(); ++it) {
1065 const std::vector<boost::shared_ptr<serialization::pimpl::Data> > &valueDatas = it->second;
1066 for (std::vector<boost::shared_ptr<serialization::pimpl::Data> >::const_iterator valueIt = valueDatas.begin();
1067 valueIt != valueDatas.end(); ++valueIt) {
1068 datas[it->first].push_back(*(*valueIt));
1071 return proxy::IMapImpl::getAllData(datas);
1074 virtual std::auto_ptr<serialization::pimpl::Data>
1075 executeOnKeyInternal(
const serialization::pimpl::Data &keyData,
1076 const serialization::pimpl::Data &processor);
1078 template <
typename ResultType>
1080 submitToKeyInternal(
const serialization::pimpl::Data &keyData,
1081 const serialization::pimpl::Data &processor) {
1082 int partitionId = getPartitionId(keyData);
1084 std::auto_ptr<protocol::ClientMessage> request =
1085 protocol::codec::MapSubmitToKeyCodec::RequestParameters::encode(getName(),
1088 util::getThreadId());
1090 connection::CallFuture callFuture = invokeAndGetFuture(request, partitionId);
1092 return client::Future<ResultType>(callFuture, getSerializationService(), submitToKeyDecoder);
1095 static std::auto_ptr<serialization::pimpl::Data> submitToKeyDecoder(protocol::ClientMessage &response);
1097 template<
typename K,
typename EntryProcessor>
1098 EntryVector executeOnKeysInternal(
const std::set<K> &keys,
const EntryProcessor &entryProcessor) {
1100 return EntryVector();
1103 std::vector<serialization::pimpl::Data> keysData;
1104 for (
typename std::set<K>::const_iterator it = keys.begin(); it != keys.end(); ++it) {
1105 keysData.push_back(toData<K>(*it));
1108 serialization::pimpl::Data entryProcessorData = toData<EntryProcessor>(entryProcessor);
1110 return proxy::IMapImpl::executeOnKeysData(keysData, entryProcessorData);
1114 putAllInternal(
const std::map<int, EntryVector> &entries);
1117 monitor::impl::LocalMapStatsImpl stats;
1123 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
1124 #pragma warning(pop)
std::vector< V > values(query::PagingPredicate< K, V > &predicate)
Returns a vector clone of the values contained in this map.
Definition: ClientMapProxy.h:752
bool containsValue(const V &value)
check if this map contains value.
Definition: ClientMapProxy.h:86
bool replace(const K &key, const V &oldValue, const NEWTYPE &newValue)
Replaces the entry for a key only if currently mapped to a given value.
Definition: ClientMapProxy.h:289
void lock(const K &key)
Acquires the lock for the specified key.
Definition: ClientMapProxy.h:356
bool evict(const K &key)
Evicts the specified key from this map.
Definition: ClientMapProxy.h:593
Definition: LocalMapStats.h:31
TypedData put(const K &key, const V &value, long ttlInMillis)
Puts an entry into this map with a given ttl (time to live) value.
Definition: ClientMapProxy.h:130
Definition: EntryListener.h:108
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: IdentifiedDataSerializable.h:47
void unlock(const K &key)
Releases the lock for the specified key.
Definition: ClientMapProxy.h:447
TypedData executeOnKey(const K &key, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: ClientMapProxy.h:882
NOTE: PagingPredicate can only be used with values(), keySet() and entries() methods!!! ...
Definition: PagingPredicate.h:127
TypedData put(const K &key, const V &value)
put new entry into map.
Definition: ClientMapProxy.h:114
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: ClientMapProxy.h:816
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: ClientMapProxy.h:394
std::auto_ptr< EntryView< TypedData, TypedData > > getEntryView(const K &key)
Returns the EntryView for the specified key.
Definition: ClientMapProxy.h:566
std::string addEntryListener(const K &key, MixedEntryListener &listener, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: ClientMapProxy.h:547
bool containsKey(const K &key)
check if this map contains key.
Definition: ClientMapProxy.h:74
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: ClientMapProxy.h:224
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: ClientMapProxy.h:334
void putAll(const std::map< K, V > &entries)
Copies all of the mappings from the specified map to this map (optional operation).
Definition: ClientMapProxy.h:995
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: ClientMapProxy.h:320
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: ClientMapProxy.h:686
std::map< TypedData, TypedData > executeOnEntries(const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: ClientMapProxy.h:926
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: ClientMapProxy.h:241
This is a merker class for Predicate classes.
Definition: Predicate.h:36
EntryView represents a readonly view of a map entry.
Definition: EntryView.h:39
TypedData 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: ClientMapProxy.h:257
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: ClientMapProxy.h:460
Concurrent, distributed, observable and queryable map client.
Definition: ClientMapProxy.h:61
TypedData 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: ClientMapProxy.h:273
Definition: MapEntryView.h:32
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: ClientMapProxy.h:408
bool tryRemove(const K &key, long timeoutInMillis)
Tries to remove the entry with the given key from this map within specified timeout value...
Definition: ClientMapProxy.h:205
This specialization overwrites the get method to return TypedData.
Definition: Future.h:235
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: ClientMapProxy.h:379
bool tryLock(const K &key, long timeInMillis)
Tries to acquire the lock for the specified key.
Definition: ClientMapProxy.h:429
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: ClientMapProxy.h:476
TypedData class is a wrapper class for the serialized binary data.
Definition: TypedData.h:40
std::vector< std::pair< TypedData, TypedData > > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: ClientMapProxy.h:619
TypedData replace(const K &key, const V &value)
Replaces the entry for a key only if currently mapped to some value.
Definition: ClientMapProxy.h:305
void deleteEntry(const K &key)
removes entry from map.
Definition: ClientMapProxy.h:182
std::map< TypedData, TypedData > executeOnEntries(const EntryProcessor &entryProcessor, const query::Predicate &predicate)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: ClientMapProxy.h:954