Hazelcast C++ Client
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 
151  void removeAll(const query::Predicate &predicate) {
152  mapImpl->removeAll(predicate);
153  }
154 
162  bool remove(const K &key, const V &value) {
163  return mapImpl->remove(key, value);
164  }
165 
172  void deleteEntry(const K &key) {
173  mapImpl->deleteEntry(key);
174  }
175 
180  void flush() {
181  mapImpl->flush();
182  }
183 
194  bool tryRemove(const K &key, long timeoutInMillis) {
195  return mapImpl->tryRemove(key, timeoutInMillis);
196  }
197 
210  bool tryPut(const K &key, const V &value, long timeoutInMillis) {
211  return mapImpl->tryPut(key, value, timeoutInMillis);
212  }
213 
223  void putTransient(const K &key, const V &value, long ttlInMillis) {
224  mapImpl->putTransient(key, value, ttlInMillis);
225  }
226 
235  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value) {
236  return mapImpl->putIfAbsent(key, value);
237  }
238 
250  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value, long ttlInMillis) {
251  return mapImpl->putIfAbsent(key, value, ttlInMillis);
252  }
253 
261  bool replace(const K &key, const V &oldValue, const V &newValue) {
262  return mapImpl->replace(key, oldValue, newValue);
263  }
264 
272  boost::shared_ptr<V> replace(const K &key, const V &value) {
273  return mapImpl->replace(key, value);
274  }
275 
290  void lock(const K &key) {
291  mapImpl->lock(key);
292  }
293 
312  void lock(const K &key, long leaseTime) {
313  mapImpl->lock(key, leaseTime);
314  }
315 
324  bool isLocked(const K &key) {
325  return mapImpl->isLocked(key);
326  }
327 
337  bool tryLock(const K &key) {
338  return mapImpl->tryLock(key);
339  }
340 
357  bool tryLock(const K &key, long timeInMillis) {
358  return mapImpl->tryLock(key, timeInMillis);
359  }
360 
374  void unlock(const K &key) {
375  mapImpl->unlock(key);
376  }
377 
386  void forceUnlock(const K &key) {
387  mapImpl->forceUnlock(key);
388  }
389 
401  template<typename MapInterceptor>
402  std::string addInterceptor(MapInterceptor &interceptor) {
403  return mapImpl->template addInterceptor<MapInterceptor>(interceptor);
404  }
405 
412  void removeInterceptor(const std::string &id) {
413  mapImpl->removeInterceptor(id);
414  }
415 
430  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
431  return mapImpl->addEntryListener(listener, includeValue);
432  }
433 
449  std::string
450  addEntryListener(EntryListener<K, V> &listener, const query::Predicate &predicate, bool includeValue) {
451  return mapImpl->addEntryListener(listener, predicate, includeValue);
452  }
453 
463  bool removeEntryListener(const std::string &registrationId) {
464  return mapImpl->removeEntryListener(registrationId);
465  }
466 
467 
481  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
482  return mapImpl->addEntryListener(listener, key, includeValue);
483  }
484 
494  return mapImpl->getEntryView(key);
495  }
496 
507  bool evict(const K &key) {
508  return mapImpl->evict(key);
509  }
510 
522  void evictAll() {
523  mapImpl->evictAll();
524  }
525 
532  std::map<K, V> getAll(const std::set<K> &keys) {
533  return mapImpl->getAll(keys);
534  }
535 
543  std::vector<K> keySet() {
544  return mapImpl->keySet();
545  }
546 
559  std::vector<K> keySet(const serialization::IdentifiedDataSerializable &predicate) {
560  return mapImpl->keySet(predicate);
561  }
562 
574  std::vector<K> keySet(const query::Predicate &predicate) {
575  return mapImpl->keySet(predicate);
576  }
577 
589  std::vector<K> keySet(query::PagingPredicate<K, V> &predicate) {
590  return mapImpl->keySet(predicate);
591  }
592 
600  std::vector<V> values() {
601  return mapImpl->values();
602  }
603 
614  std::vector<V> values(const serialization::IdentifiedDataSerializable &predicate) {
615  return mapImpl->values(predicate);
616  }
617 
626  std::vector<V> values(const query::Predicate &predicate) {
627  return mapImpl->values(predicate);
628  }
629 
639  std::vector<V> values(query::PagingPredicate<K, V> &predicate) {
640  return mapImpl->values(predicate);
641  }
642 
650  std::vector<std::pair<K, V> > entrySet() {
651  return mapImpl->entrySet();
652  }
653 
666  std::vector<std::pair<K, V> > entrySet(const serialization::IdentifiedDataSerializable &predicate) {
667  return mapImpl->entrySet(predicate);
668  }
669 
680  std::vector<std::pair<K, V> > entrySet(const query::Predicate &predicate) {
681  return mapImpl->entrySet(predicate);
682  }
683 
694  std::vector<std::pair<K, V> > entrySet(query::PagingPredicate<K, V> &predicate) {
695  return mapImpl->entrySet(predicate);
696  }
697 
730  void addIndex(const std::string &attribute, bool ordered) {
731  mapImpl->addIndex(attribute, ordered);
732  }
733 
748  template<typename ResultType, typename EntryProcessor>
749  boost::shared_ptr<ResultType> executeOnKey(const K &key, const EntryProcessor &entryProcessor) {
750  return mapImpl->template executeOnKey<ResultType, EntryProcessor>(key, entryProcessor);
751  }
752 
765  template<typename ResultType, typename EntryProcessor>
766  std::map<K, boost::shared_ptr<ResultType> >
767  executeOnKeys(const std::set<K> &keys, const EntryProcessor &entryProcessor) {
768  return mapImpl->template executeOnKeys<ResultType, EntryProcessor>(keys, entryProcessor);
769  }
770 
780  template<typename ResultType, typename EntryProcessor>
781  Future<ResultType> submitToKey(const K &key, const EntryProcessor &entryProcessor) {
782  return mapImpl->template submitToKey<ResultType, EntryProcessor>(key, entryProcessor);
783  }
784 
798  template<typename ResultType, typename EntryProcessor>
799  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(const EntryProcessor &entryProcessor) {
800  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor);
801  }
802 
820  template<typename ResultType, typename EntryProcessor>
821  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(const EntryProcessor &entryProcessor,
823  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor, predicate);
824  }
825 
840  template<typename ResultType, typename EntryProcessor>
841  std::map<K, boost::shared_ptr<ResultType> >
842  executeOnEntries(const EntryProcessor &entryProcessor, const query::Predicate &predicate) {
843  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor, predicate);
844  }
845 
853  void set(const K &key, const V &value) {
854  mapImpl->set(key, value);
855  }
856 
866  void set(const K &key, const V &value, long ttl) {
867  mapImpl->set(key, value, ttl);
868  }
869 
877  int size() {
878  return mapImpl->size();
879  }
880 
886  bool isEmpty() {
887  return mapImpl->isEmpty();
888  }
889 
890 
901  void putAll(const std::map<K, V> &entries) {
902  return mapImpl->putAll(entries);
903  }
904 
909  void clear() {
910  mapImpl->clear();
911  }
912 
917  void destroy() {
918  mapImpl->destroy();
919  }
920 
934  return mapImpl->getLocalMapStats();
935  }
936  private:
937  IMap(boost::shared_ptr<spi::ClientProxy> clientProxy) {
938  mapImpl = boost::static_pointer_cast<map::ClientMapProxy<K, V> >(clientProxy);
939  }
940 
941  boost::shared_ptr<map::ClientMapProxy<K, V> > mapImpl;
942  };
943 
944  template <typename K, typename V>
945  const std::string IMap<K, V>::SERVICE_NAME = "hz:impl:mapService";
946  }
947 }
948 
949 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
950 #pragma warning(pop)
951 #endif
952 
953 #endif /* HAZELCAST_IMAP */
954 
void removeAll(const query::Predicate &predicate)
Removes all entries which match with the supplied predicate.
Definition: IMap.h:151
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:223
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:650
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:250
std::map< K, V > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: IMap.h:532
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:901
void destroy()
Destroys this object cluster-wide.
Definition: IMap.h:917
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(const EntryProcessor &entryProcessor, const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:821
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:272
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: IMap.h:337
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:357
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:210
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: IMap.h:853
std::map< K, boost::shared_ptr< ResultType > > executeOnKeys(const std::set< K > &keys, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entries mapped by the collection of keys...
Definition: IMap.h:767
std::vector< V > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:626
std::vector< V > values()
Returns a vector clone of the values contained in this map.
Definition: IMap.h:600
void deleteEntry(const K &key)
removes entry from map.
Definition: IMap.h:172
void evictAll()
Evicts all keys from this map except locked ones.
Definition: IMap.h:522
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:430
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(const EntryProcessor &entryProcessor, const query::Predicate &predicate)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: IMap.h:842
std::vector< K > keySet()
Returns a vector clone of the keys contained in this map.
Definition: IMap.h:543
Definition: LocalMapStats.h:31
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: IMap.h:866
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: IMap.h:481
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:694
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:261
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:235
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: IdentifiedDataSerializable.h:47
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:730
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: IMap.h:324
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:574
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
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: IMap.h:799
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: IMap.h:180
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: IMap.h:402
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:290
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:194
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: IMap.h:312
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: IMap.h:412
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:450
This is a unique Future.
Definition: Future.h:112
boost::shared_ptr< ResultType > executeOnKey(const K &key, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: IMap.h:749
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:680
EntryView represents a readonly view of a map entry.
Definition: EntryView.h:39
std::vector< K > keySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:559
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:886
void unlock(const K &key)
Releases the lock for the specified key.
Definition: IMap.h:374
Future< ResultType > submitToKey(const K &key, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: IMap.h:781
bool evict(const K &key)
Evicts the specified key from this map.
Definition: IMap.h:507
Definition: MapEntryView.h:32
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: IMap.h:386
monitor::LocalMapStats & getLocalMapStats()
Returns LocalMapStats for this map.
Definition: IMap.h:933
std::vector< V > values(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:614
void clear()
Removes all of the mappings from this map (optional operation).
Definition: IMap.h:909
std::vector< V > values(query::PagingPredicate< K, V > &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:639
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:493
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:589
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:666
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: IMap.h:463
int size()
Returns the number of key-value mappings in this map.
Definition: IMap.h:877