Hazelcast C++ Client
 All Classes Functions Variables Enumerations Pages
IMap.h
1 /*
2  * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef HAZELCAST_IMAP
17 #define HAZELCAST_IMAP
18 
19 #include "hazelcast/client/proxy/IMapImpl.h"
20 #include "hazelcast/client/impl/EntryEventHandler.h"
21 #include "hazelcast/client/EntryListener.h"
22 #include "hazelcast/client/EntryView.h"
23 #include <string>
24 #include <map>
25 #include <set>
26 #include <vector>
27 #include <stdexcept>
28 #include <climits>
29 
30 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
31 #pragma warning(push)
32 #pragma warning(disable: 4251) //for dll export
33 #endif
34 
35 namespace hazelcast {
36  namespace client {
37 
51  template<typename K, typename V>
52  class IMap : public proxy::IMapImpl {
53  friend class HazelcastClient;
54 
55  public:
56 
63  bool containsKey(const K &key) {
64  return proxy::IMapImpl::containsKey(toData(key));
65  }
66 
73  bool containsValue(const V &value) {
74  return proxy::IMapImpl::containsValue(toData(value));
75  }
76 
84  boost::shared_ptr<V> get(const K &key) {
85  return toObject<V>(proxy::IMapImpl::get(toData(key)));
86  }
87 
96  boost::shared_ptr<V> put(const K &key, const V &value) {
97  return toObject<V>(proxy::IMapImpl::put(toData(key), toData(value)));
98  }
99 
107  boost::shared_ptr<V> remove(const K &key) {
108  return toObject<V>(proxy::IMapImpl::remove(toData(key)));
109  }
110 
118  bool remove(const K &key, const V &value) {
119  return proxy::IMapImpl::remove(toData(key), toData(value));
120  }
121 
128  void deleteEntry(const K &key) {
129  proxy::IMapImpl::deleteEntry(toData(key));
130  }
131 
136  void flush() {
137  proxy::IMapImpl::flush();
138  }
139 
150  bool tryRemove(const K &key, long timeoutInMillis) {
151  return proxy::IMapImpl::tryRemove(toData(key), timeoutInMillis);
152  }
153 
166  bool tryPut(const K &key, const V &value, long timeoutInMillis) {
167  return proxy::IMapImpl::tryPut(toData(key), toData(value), timeoutInMillis);
168  }
169 
181  boost::shared_ptr<V> put(const K &key, const V &value, long ttlInMillis) {
182  return toObject<V>(proxy::IMapImpl::put(toData(key), toData(value), ttlInMillis));
183  }
184 
194  void putTransient(const K &key, const V &value, long ttlInMillis) {
195  proxy::IMapImpl::putTransient(toData(key), toData(value), ttlInMillis);
196  }
197 
206  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value) {
207  return putIfAbsent(key, value, -1);
208  }
209 
221  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value, long ttlInMillis) {
222  return toObject<V>(proxy::IMapImpl::putIfAbsent(toData(key), toData(value), ttlInMillis));
223  }
224 
232  bool replace(const K &key, const V &oldValue, const V &newValue) {
233  return proxy::IMapImpl::replace(toData(key), toData(oldValue), toData(newValue));
234  }
235 
243  boost::shared_ptr<V> replace(const K &key, const V &value) {
244  return toObject<V>(proxy::IMapImpl::replace(toData(key), toData(value)));
245  }
246 
256  void set(const K &key, const V &value, long ttl) {
257  proxy::IMapImpl::set(toData(key), toData(value), ttl);
258  }
259 
274  void lock(const K &key) {
275  proxy::IMapImpl::lock(toData(key));
276  }
277 
296  void lock(const K &key, long leaseTime) {
297  proxy::IMapImpl::lock(toData(key), leaseTime);
298  }
299 
308  bool isLocked(const K &key) {
309  return proxy::IMapImpl::isLocked(toData(key));
310  }
311 
321  bool tryLock(const K &key) {
322  return tryLock(key, 0);
323  }
324 
341  bool tryLock(const K &key, long timeInMillis) {
342  return proxy::IMapImpl::tryLock(toData(key), timeInMillis);
343  }
344 
358  void unlock(const K &key) {
359  proxy::IMapImpl::unlock(toData(key));
360  }
361 
370  void forceUnlock(const K &key) {
371  proxy::IMapImpl::forceUnlock(toData(key));
372  }
373 
385  template<typename MapInterceptor>
386  std::string addInterceptor(MapInterceptor &interceptor) {
387  return proxy::IMapImpl::addInterceptor(interceptor);
388  }
389 
396  void removeInterceptor(const std::string &id) {
397  proxy::IMapImpl::removeInterceptor(id);
398  }
399 
414  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
415  impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler> *entryEventHandler =
416  new impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler>(
417  getName(), context->getClusterService(), context->getSerializationService(), listener,
418  includeValue);
419  return proxy::IMapImpl::addEntryListener(entryEventHandler, includeValue);
420  }
421 
431  bool removeEntryListener(const std::string &registrationId) {
432  return proxy::IMapImpl::removeEntryListener(registrationId);
433  }
434 
435 
449  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
450  serialization::pimpl::Data keyData = toData(key);
451  impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler> *entryEventHandler =
452  new impl::EntryEventHandler<K, V, protocol::codec::MapAddEntryListenerCodec::AbstractEventHandler>(
453  getName(), context->getClusterService(), context->getSerializationService(), listener,
454  includeValue);
455  return proxy::IMapImpl::addEntryListener(entryEventHandler, keyData, includeValue);
456  }
457 
467  serialization::pimpl::Data keyData = toData(key);
468  std::auto_ptr<map::DataEntryView> dataEntryView = proxy::IMapImpl::getEntryView(keyData);
469  boost::shared_ptr<V> v = toObject<V>(dataEntryView->getValue());
470  EntryView<K, V> view(key, *v, *dataEntryView);
471  return view;
472  }
473 
484  bool evict(const K &key) {
485  return proxy::IMapImpl::evict(toData(key));
486  }
487 
499  void evictAll() {
500  proxy::IMapImpl::evictAll();
501  }
502 
509  std::map<K, V> getAll(const std::set<K> &keys) {
510  std::vector<serialization::pimpl::Data> keySet(keys.size());
511  int i = 0;
512  for (typename std::set<K>::iterator it = keys.begin(); it != keys.end(); ++it) {
513  keySet[i++] = toData(*it);
514  }
515  std::map<K, V> result;
516  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > entrySet = proxy::IMapImpl::getAll(
517  keySet);
518  for (std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> >::const_iterator it = entrySet.begin();
519  it != entrySet.end(); ++it) {
520  boost::shared_ptr<K> key = toObject<K>(it->first);
521  boost::shared_ptr<V> value = toObject<V>(it->second);
522  result[*key] = *value;
523  }
524  return result;
525  }
526 
534  std::vector<K> keySet() {
535  std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::keySet();
536  int size = dataResult.size();
537  std::vector<K> keys(size);
538  for (int i = 0; i < size; ++i) {
539  boost::shared_ptr<K> key = toObject<K>(dataResult[i]);
540  keys[i] = *key;
541  }
542  return keys;
543  }
544 
555  std::vector<K> keySet(const serialization::IdentifiedDataSerializable &predicate) {
556  std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::keySet(predicate);
557  int size = dataResult.size();
558  std::vector<K> keys(size);
559  for (int i = 0; i < size; ++i) {
560  boost::shared_ptr<K> key = toObject<K>(dataResult[i]);
561  keys[i] = *key;
562  }
563  return keys;
564  }
565 
573  std::vector<V> values() {
574  std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::values();
575  int size = dataResult.size();
576  std::vector<K> values(size);
577  for (int i = 0; i < size; ++i) {
578  boost::shared_ptr<K> value = toObject<K>(dataResult[i]);
579  values[i] = *value;
580  }
581  return values;
582  }
583 
592  std::vector<V> values(const serialization::IdentifiedDataSerializable &predicate) {
593  std::vector<serialization::pimpl::Data> dataResult = proxy::IMapImpl::values(predicate);
594  int size = dataResult.size();
595  std::vector<V> values(size);
596  for (int i = 0; i < size; ++i) {
597  boost::shared_ptr<V> value = toObject<V>(dataResult[i]);
598  values[i] = *value;
599  }
600  return values;
601  }
602 
610  std::vector<std::pair<K, V> > entrySet() {
611  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult = proxy::IMapImpl::entrySet();
612  size_t size = dataResult.size();
613  std::vector<std::pair<K, V> > entries(size);
614  for (size_t i = 0; i < size; ++i) {
615  boost::shared_ptr<K> key = toObject<K>(dataResult[i].first);
616  boost::shared_ptr<V> value = toObject<V>(dataResult[i].second);
617  entries[i] = std::make_pair(*key, *value);
618  }
619  return entries;
620  }
621 
632  std::vector<std::pair<K, V> > entrySet(const serialization::IdentifiedDataSerializable &predicate) {
633  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult = proxy::IMapImpl::entrySet(
634  predicate);
635  size_t size = dataResult.size();
636  std::vector<std::pair<K, V> > entries(size);
637  for (size_t i = 0; i < size; ++i) {
638  boost::shared_ptr<K> key = toObject<K>(dataResult[i].first);
639  boost::shared_ptr<V> value = toObject<V>(dataResult[i].second);
640  entries[i] = std::make_pair(*key, *value);
641  }
642  return entries;
643  }
644 
677  void addIndex(const std::string &attribute, bool ordered) {
678  proxy::IMapImpl::addIndex(attribute, ordered);
679  }
680 
695  template<typename ResultType, typename EntryProcessor>
696  boost::shared_ptr<ResultType> executeOnKey(const K &key, EntryProcessor &entryProcessor) {
697  return proxy::IMapImpl::executeOnKey<K, ResultType, EntryProcessor>(key, entryProcessor);
698  }
699 
713  template<typename ResultType, typename EntryProcessor>
714  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(EntryProcessor &entryProcessor) {
715  return proxy::IMapImpl::executeOnEntries<K, ResultType, EntryProcessor>(entryProcessor);
716  }
717 
725  void set(const K &key, const V &value) {
726  set(key, value, -1);
727  }
728 
736  int size() {
737  return proxy::IMapImpl::size();
738  }
739 
745  bool isEmpty() {
746  return proxy::IMapImpl::isEmpty();
747  }
748 
749 
760  void putAll(const std::map<K, V> &entries) {
761  return proxy::IMapImpl::putAll(toDataEntriesSet(entries));
762  }
763 
768  void clear() {
769  proxy::IMapImpl::clear();
770  }
771 
772  private:
773  IMap(const std::string &instanceName, spi::ClientContext *context)
774  : proxy::IMapImpl(instanceName, context) {
775  }
776 
777 
778  };
779  }
780 }
781 
782 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
783 #pragma warning(pop)
784 #endif
785 
786 #endif /* HAZELCAST_IMAP */
787 
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:194
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:610
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:221
std::map< K, V > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: IMap.h:509
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:760
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:243
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: IMap.h:321
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:341
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:166
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: IMap.h:725
std::vector< V > values()
Returns a vector clone of the values contained in this map.
Definition: IMap.h:573
void deleteEntry(const K &key)
removes entry from map.
Definition: IMap.h:128
void evictAll()
Evicts all keys from this map except locked ones.
Definition: IMap.h:499
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:714
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:414
std::vector< K > keySet()
Returns a vector clone of the keys contained in this map.
Definition: IMap.h:534
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: IMap.h:256
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: IMap.h:449
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:232
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:206
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: IdentifiedDataSerializable.h:42
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:677
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: IMap.h:308
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:181
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: IMap.h:136
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: IMap.h:386
void lock(const K &key)
Acquires the lock for the specified key.
Definition: IMap.h:274
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:150
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: IMap.h:296
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: IMap.h:396
EntryView represents a readonly view of a map entry.
Definition: EntryView.h:39
std::vector< K > keySet(const serialization::IdentifiedDataSerializable &predicate)
Queries the map based on the specified predicate and returns the keys of matching entries...
Definition: IMap.h:555
Concurrent, distributed, observable and queryable map client.
Definition: IMap.h:52
bool isEmpty()
Returns true if this map contains no key-value mappings.
Definition: IMap.h:745
void unlock(const K &key)
Releases the lock for the specified key.
Definition: IMap.h:358
bool evict(const K &key)
Evicts the specified key from this map.
Definition: IMap.h:484
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: IMap.h:370
std::vector< V > values(const serialization::IdentifiedDataSerializable &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:592
void clear()
Removes all of the mappings from this map (optional operation).
Definition: IMap.h:768
boost::shared_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: IMap.h:96
EntryView< K, V > getEntryView(const K &key)
Returns the EntryView for the specified key.
Definition: IMap.h:466
bool containsValue(const V &value)
check if this map contains value.
Definition: IMap.h:73
Hazelcast Client enables you to do all Hazelcast operations without being a member of the cluster...
Definition: HazelcastClient.h:410
bool containsKey(const K &key)
check if this map contains key.
Definition: IMap.h:63
std::vector< std::pair< K, V > > entrySet(const serialization::IdentifiedDataSerializable &predicate)
Queries the map based on the specified predicate and returns the matching entries.
Definition: IMap.h:632
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: IMap.h:431
int size()
Returns the number of key-value mappings in this map.
Definition: IMap.h:736
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:696