Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
IMap.h
1 /*
2  * Copyright (c) 2008-2017, 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 <string>
20 #include <map>
21 #include <set>
22 #include <vector>
23 #include <stdexcept>
24 #include <climits>
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"
31 #include "hazelcast/client/map/ClientMapProxy.h"
32 #include "hazelcast/client/Future.h"
33 
34 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
35 #pragma warning(push)
36 #pragma warning(disable: 4251) //for dll export
37 #endif
38 
39 namespace hazelcast {
40  namespace client {
41  class HazelcastClient;
42 
43  namespace adaptor {
44  template<typename K, typename V>
45  class RawPointerMap;
46  }
47 
48  namespace spi {
49  class ProxyManager;
50  }
51 
65  template<typename K, typename V>
66  class IMap {
67  friend class spi::ProxyManager;
68  friend class HazelcastClient;
69  friend class adaptor::RawPointerMap<K, V>;
70 
71  public:
72  static const std::string SERVICE_NAME;
73 
80  bool containsKey(const K &key) {
81  return mapImpl->containsKey(key);
82  }
83 
90  bool containsValue(const V &value) {
91  return mapImpl->containsValue(value);
92  }
93 
101  boost::shared_ptr<V> get(const K &key) {
102  return mapImpl->get(key);
103  }
104 
113  boost::shared_ptr<V> put(const K &key, const V &value) {
114  return mapImpl->put(key, value);
115  }
116 
128  boost::shared_ptr<V> put(const K &key, const V &value, long ttlInMillis) {
129  return mapImpl->put(key, value, ttlInMillis);
130  }
131 
139  boost::shared_ptr<V> remove(const K &key) {
140  return mapImpl->remove(key);
141  }
142 
150  bool remove(const K &key, const V &value) {
151  return mapImpl->remove(key, value);
152  }
153 
160  void deleteEntry(const K &key) {
161  mapImpl->deleteEntry(key);
162  }
163 
168  void flush() {
169  mapImpl->flush();
170  }
171 
182  bool tryRemove(const K &key, long timeoutInMillis) {
183  return mapImpl->tryRemove(key, timeoutInMillis);
184  }
185 
198  bool tryPut(const K &key, const V &value, long timeoutInMillis) {
199  return mapImpl->tryPut(key, value, timeoutInMillis);
200  }
201 
211  void putTransient(const K &key, const V &value, long ttlInMillis) {
212  mapImpl->putTransient(key, value, ttlInMillis);
213  }
214 
223  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value) {
224  return mapImpl->putIfAbsent(key, value);
225  }
226 
238  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value, long ttlInMillis) {
239  return mapImpl->putIfAbsent(key, value, ttlInMillis);
240  }
241 
249  bool replace(const K &key, const V &oldValue, const V &newValue) {
250  return mapImpl->replace(key, oldValue, newValue);
251  }
252 
260  boost::shared_ptr<V> replace(const K &key, const V &value) {
261  return mapImpl->replace(key, value);
262  }
263 
278  void lock(const K &key) {
279  mapImpl->lock(key);
280  }
281 
300  void lock(const K &key, long leaseTime) {
301  mapImpl->lock(key, leaseTime);
302  }
303 
312  bool isLocked(const K &key) {
313  return mapImpl->isLocked(key);
314  }
315 
325  bool tryLock(const K &key) {
326  return mapImpl->tryLock(key);
327  }
328 
345  bool tryLock(const K &key, long timeInMillis) {
346  return mapImpl->tryLock(key, timeInMillis);
347  }
348 
362  void unlock(const K &key) {
363  mapImpl->unlock(key);
364  }
365 
374  void forceUnlock(const K &key) {
375  mapImpl->forceUnlock(key);
376  }
377 
389  template<typename MapInterceptor>
390  std::string addInterceptor(MapInterceptor &interceptor) {
391  return mapImpl->template addInterceptor<MapInterceptor>(interceptor);
392  }
393 
400  void removeInterceptor(const std::string &id) {
401  mapImpl->removeInterceptor(id);
402  }
403 
418  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
419  return mapImpl->addEntryListener(listener, includeValue);
420  }
421 
437  std::string
438  addEntryListener(EntryListener<K, V> &listener, const query::Predicate &predicate, bool includeValue) {
439  return mapImpl->addEntryListener(listener, predicate, includeValue);
440  }
441 
451  bool removeEntryListener(const std::string &registrationId) {
452  return mapImpl->removeEntryListener(registrationId);
453  }
454 
455 
469  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
470  return mapImpl->addEntryListener(listener, key, includeValue);
471  }
472 
482  return mapImpl->getEntryView(key);
483  }
484 
495  bool evict(const K &key) {
496  return mapImpl->evict(key);
497  }
498 
510  void evictAll() {
511  mapImpl->evictAll();
512  }
513 
520  std::map<K, V> getAll(const std::set<K> &keys) {
521  return mapImpl->getAll(keys);
522  }
523 
531  std::vector<K> keySet() {
532  return mapImpl->keySet();
533  }
534 
547  std::vector<K> keySet(const serialization::IdentifiedDataSerializable &predicate) {
548  return mapImpl->keySet(predicate);
549  }
550 
562  std::vector<K> keySet(const query::Predicate &predicate) {
563  return mapImpl->keySet(predicate);
564  }
565 
577  std::vector<K> keySet(query::PagingPredicate<K, V> &predicate) {
578  return mapImpl->keySet(predicate);
579  }
580 
588  std::vector<V> values() {
589  return mapImpl->values();
590  }
591 
602  std::vector<V> values(const serialization::IdentifiedDataSerializable &predicate) {
603  return mapImpl->values(predicate);
604  }
605 
614  std::vector<V> values(const query::Predicate &predicate) {
615  return mapImpl->values(predicate);
616  }
617 
627  std::vector<V> values(query::PagingPredicate<K, V> &predicate) {
628  return mapImpl->values(predicate);
629  }
630 
638  std::vector<std::pair<K, V> > entrySet() {
639  return mapImpl->entrySet();
640  }
641 
654  std::vector<std::pair<K, V> > entrySet(const serialization::IdentifiedDataSerializable &predicate) {
655  return mapImpl->entrySet(predicate);
656  }
657 
668  std::vector<std::pair<K, V> > entrySet(const query::Predicate &predicate) {
669  return mapImpl->entrySet(predicate);
670  }
671 
682  std::vector<std::pair<K, V> > entrySet(query::PagingPredicate<K, V> &predicate) {
683  return mapImpl->entrySet(predicate);
684  }
685 
718  void addIndex(const std::string &attribute, bool ordered) {
719  mapImpl->addIndex(attribute, ordered);
720  }
721 
736  template<typename ResultType, typename EntryProcessor>
737  boost::shared_ptr<ResultType> executeOnKey(const K &key, EntryProcessor &entryProcessor) {
738  return mapImpl->template executeOnKey<ResultType, EntryProcessor>(key, entryProcessor);
739  }
740 
753  template<typename ResultType, typename EntryProcessor>
754  std::map<K, boost::shared_ptr<ResultType> >
755  executeOnKeys(const std::set<K> &keys, EntryProcessor &entryProcessor) {
756  return mapImpl->template executeOnKeys<ResultType, EntryProcessor>(keys, entryProcessor);
757  }
758 
768  template<typename ResultType, typename EntryProcessor>
769  Future<ResultType> submitToKey(const K &key, EntryProcessor &entryProcessor) {
770  return mapImpl->template submitToKey<ResultType, EntryProcessor>(key, entryProcessor);
771  }
772 
786  template<typename ResultType, typename EntryProcessor>
787  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(EntryProcessor &entryProcessor) {
788  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor);
789  }
790 
808  template<typename ResultType, typename EntryProcessor>
809  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(EntryProcessor &entryProcessor,
811  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor, predicate);
812  }
813 
828  template<typename ResultType, typename EntryProcessor>
829  std::map<K, boost::shared_ptr<ResultType> >
830  executeOnEntries(EntryProcessor &entryProcessor, const query::Predicate &predicate) {
831  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor, predicate);
832  }
833 
841  void set(const K &key, const V &value) {
842  mapImpl->set(key, value);
843  }
844 
854  void set(const K &key, const V &value, long ttl) {
855  mapImpl->set(key, value, ttl);
856  }
857 
865  int size() {
866  return mapImpl->size();
867  }
868 
874  bool isEmpty() {
875  return mapImpl->isEmpty();
876  }
877 
878 
889  void putAll(const std::map<K, V> &entries) {
890  return mapImpl->putAll(entries);
891  }
892 
897  void clear() {
898  mapImpl->clear();
899  }
900 
905  void destroy() {
906  mapImpl->destroy();
907  }
908 
922  return mapImpl->getLocalMapStats();
923  }
924  private:
925  IMap(boost::shared_ptr<spi::ClientProxy> clientProxy) {
926  mapImpl = boost::static_pointer_cast<map::ClientMapProxy<K, V> >(clientProxy);
927  }
928 
929  boost::shared_ptr<map::ClientMapProxy<K, V> > mapImpl;
930  };
931 
932  template <typename K, typename V>
933  const std::string IMap<K, V>::SERVICE_NAME = "hz:impl:mapService";
934  }
935 }
936 
937 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
938 #pragma warning(pop)
939 #endif
940 
941 #endif /* HAZELCAST_IMAP */
942 
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:211
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:638
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:238
std::map< K, V > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: IMap.h:520
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:889
void destroy()
Destroys this object cluster-wide.
Definition: IMap.h:905
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:260
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: IMap.h:325
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:345
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:198
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: IMap.h:841
std::vector< V > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:614
std::vector< V > values()
Returns a vector clone of the values contained in this map.
Definition: IMap.h:588
void deleteEntry(const K &key)
removes entry from map.
Definition: IMap.h:160
void evictAll()
Evicts all keys from this map except locked ones.
Definition: IMap.h:510
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:787
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:418
std::vector< K > keySet()
Returns a vector clone of the keys contained in this map.
Definition: IMap.h:531
Definition: LocalMapStats.h:31
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: IMap.h:854
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: IMap.h:469
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:682
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:249
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:830
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:223
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
Future< ResultType > submitToKey(const K &key, EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: IMap.h:769
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:718
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: IMap.h:312
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:562
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:128
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: IMap.h:168
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: IMap.h:390
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(EntryProcessor &entryProcessor, const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:809
Adaptor class to IMap which provides releasable raw pointers for returned objects.
Definition: RawPointerMap.h:51
void lock(const K &key)
Acquires the lock for the specified key.
Definition: IMap.h:278
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:182
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: IMap.h:300
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: IMap.h:400
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:438
This is a unique Future.
Definition: Future.h:111
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:668
EntryView represents a readonly view of a map entry.
Definition: EntryView.h:39
std::vector< K > keySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:547
Concurrent, distributed, observable and queryable map client.
Definition: IMap.h:66
bool isEmpty()
Returns true if this map contains no key-value mappings.
Definition: IMap.h:874
void unlock(const K &key)
Releases the lock for the specified key.
Definition: IMap.h:362
std::map< K, boost::shared_ptr< ResultType > > executeOnKeys(const std::set< K > &keys, EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entries mapped by the collection of keys...
Definition: IMap.h:755
bool evict(const K &key)
Evicts the specified key from this map.
Definition: IMap.h:495
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: IMap.h:374
monitor::LocalMapStats & getLocalMapStats()
Returns LocalMapStats for this map.
Definition: IMap.h:921
std::vector< V > values(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:602
void clear()
Removes all of the mappings from this map (optional operation).
Definition: IMap.h:897
std::vector< V > values(query::PagingPredicate< K, V > &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:627
boost::shared_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: IMap.h:113
EntryView< K, V > getEntryView(const K &key)
Returns the EntryView for the specified key.
Definition: IMap.h:481
bool containsValue(const V &value)
check if this map contains value.
Definition: IMap.h:90
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:577
Hazelcast Client enables you to do all Hazelcast operations without being a member of the cluster...
Definition: HazelcastClient.h:459
bool containsKey(const K &key)
check if this map contains key.
Definition: IMap.h:80
std::vector< std::pair< K, V > > entrySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:654
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: IMap.h:451
int size()
Returns the number of key-value mappings in this map.
Definition: IMap.h:865
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:737