Hazelcast C++ Client
IMap.h
1 /*
2  * Copyright (c) 2008-2019, 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 
69  friend class impl::HazelcastClientInstanceImpl;
70 
71  friend class adaptor::RawPointerMap<K, V>;
72 
73  public:
74  static const std::string SERVICE_NAME;
75 
82  bool containsKey(const K &key) {
83  return mapImpl->containsKey(key);
84  }
85 
92  bool containsValue(const V &value) {
93  return mapImpl->containsValue(value);
94  }
95 
103  boost::shared_ptr<V> get(const K &key) {
104  return mapImpl->get(key);
105  }
106 
115  boost::shared_ptr<V> put(const K &key, const V &value) {
116  return mapImpl->put(key, value);
117  }
118 
130  boost::shared_ptr<V> put(const K &key, const V &value, int64_t ttlInMillis) {
131  return mapImpl->put(key, value, ttlInMillis);
132  }
133 
141  boost::shared_ptr<V> remove(const K &key) {
142  return mapImpl->remove(key);
143  }
144 
153  void removeAll(const query::Predicate &predicate) {
154  mapImpl->removeAll(predicate);
155  }
156 
164  bool remove(const K &key, const V &value) {
165  return mapImpl->remove(key, value);
166  }
167 
174  void deleteEntry(const K &key) {
175  mapImpl->deleteEntry(key);
176  }
177 
182  void flush() {
183  mapImpl->flush();
184  }
185 
196  bool tryRemove(const K &key, int64_t timeoutInMillis) {
197  return mapImpl->tryRemove(key, timeoutInMillis);
198  }
199 
212  bool tryPut(const K &key, const V &value, int64_t timeoutInMillis) {
213  return mapImpl->tryPut(key, value, timeoutInMillis);
214  }
215 
225  void putTransient(const K &key, const V &value, int64_t ttlInMillis) {
226  mapImpl->putTransient(key, value, ttlInMillis);
227  }
228 
237  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value) {
238  return mapImpl->putIfAbsent(key, value);
239  }
240 
252  boost::shared_ptr<V> putIfAbsent(const K &key, const V &value, int64_t ttlInMillis) {
253  return mapImpl->putIfAbsent(key, value, ttlInMillis);
254  }
255 
263  bool replace(const K &key, const V &oldValue, const V &newValue) {
264  return mapImpl->replace(key, oldValue, newValue);
265  }
266 
274  boost::shared_ptr<V> replace(const K &key, const V &value) {
275  return mapImpl->replace(key, value);
276  }
277 
292  void lock(const K &key) {
293  mapImpl->lock(key);
294  }
295 
314  void lock(const K &key, int64_t leaseTime) {
315  mapImpl->lock(key, leaseTime);
316  }
317 
326  bool isLocked(const K &key) {
327  return mapImpl->isLocked(key);
328  }
329 
339  bool tryLock(const K &key) {
340  return mapImpl->tryLock(key);
341  }
342 
359  bool tryLock(const K &key, int64_t timeInMillis) {
360  return mapImpl->tryLock(key, timeInMillis);
361  }
362 
376  void unlock(const K &key) {
377  mapImpl->unlock(key);
378  }
379 
388  void forceUnlock(const K &key) {
389  mapImpl->forceUnlock(key);
390  }
391 
403  template<typename MapInterceptor>
404  std::string addInterceptor(MapInterceptor &interceptor) {
405  return mapImpl->template addInterceptor<MapInterceptor>(interceptor);
406  }
407 
414  void removeInterceptor(const std::string &id) {
415  mapImpl->removeInterceptor(id);
416  }
417 
432  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
433  return mapImpl->addEntryListener(listener, includeValue);
434  }
435 
451  std::string
452  addEntryListener(EntryListener<K, V> &listener, const query::Predicate &predicate, bool includeValue) {
453  return mapImpl->addEntryListener(listener, predicate, includeValue);
454  }
455 
465  bool removeEntryListener(const std::string &registrationId) {
466  return mapImpl->removeEntryListener(registrationId);
467  }
468 
469 
483  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
484  return mapImpl->addEntryListener(listener, key, includeValue);
485  }
486 
496  return mapImpl->getEntryView(key);
497  }
498 
509  bool evict(const K &key) {
510  return mapImpl->evict(key);
511  }
512 
524  void evictAll() {
525  mapImpl->evictAll();
526  }
527 
534  std::map<K, V> getAll(const std::set<K> &keys) {
535  return mapImpl->getAll(keys);
536  }
537 
545  std::vector<K> keySet() {
546  return mapImpl->keySet();
547  }
548 
561  std::vector<K> keySet(const serialization::IdentifiedDataSerializable &predicate) {
562  return mapImpl->keySet(predicate);
563  }
564 
576  std::vector<K> keySet(const query::Predicate &predicate) {
577  return mapImpl->keySet(predicate);
578  }
579 
591  std::vector<K> keySet(query::PagingPredicate<K, V> &predicate) {
592  return mapImpl->keySet(predicate);
593  }
594 
602  std::vector<V> values() {
603  return mapImpl->values();
604  }
605 
616  std::vector<V> values(const serialization::IdentifiedDataSerializable &predicate) {
617  return mapImpl->values(predicate);
618  }
619 
628  std::vector<V> values(const query::Predicate &predicate) {
629  return mapImpl->values(predicate);
630  }
631 
641  std::vector<V> values(query::PagingPredicate<K, V> &predicate) {
642  return mapImpl->values(predicate);
643  }
644 
652  std::vector<std::pair<K, V> > entrySet() {
653  return mapImpl->entrySet();
654  }
655 
668  std::vector<std::pair<K, V> > entrySet(const serialization::IdentifiedDataSerializable &predicate) {
669  return mapImpl->entrySet(predicate);
670  }
671 
682  std::vector<std::pair<K, V> > entrySet(const query::Predicate &predicate) {
683  return mapImpl->entrySet(predicate);
684  }
685 
696  std::vector<std::pair<K, V> > entrySet(query::PagingPredicate<K, V> &predicate) {
697  return mapImpl->entrySet(predicate);
698  }
699 
732  void addIndex(const std::string &attribute, bool ordered) {
733  mapImpl->addIndex(attribute, ordered);
734  }
735 
750  template<typename ResultType, typename EntryProcessor>
751  boost::shared_ptr<ResultType> executeOnKey(const K &key, const EntryProcessor &entryProcessor) {
752  return mapImpl->template executeOnKey<ResultType, EntryProcessor>(key, entryProcessor);
753  }
754 
767  template<typename ResultType, typename EntryProcessor>
768  std::map<K, boost::shared_ptr<ResultType> >
769  executeOnKeys(const std::set<K> &keys, const EntryProcessor &entryProcessor) {
770  return mapImpl->template executeOnKeys<ResultType, EntryProcessor>(keys, entryProcessor);
771  }
772 
782  template<typename ResultType, typename EntryProcessor>
783  Future<ResultType> submitToKey(const K &key, const EntryProcessor &entryProcessor) {
784  return mapImpl->template submitToKey<ResultType, EntryProcessor>(key, entryProcessor);
785  }
786 
800  template<typename ResultType, typename EntryProcessor>
801  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(const EntryProcessor &entryProcessor) {
802  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor);
803  }
804 
822  template<typename ResultType, typename EntryProcessor>
823  std::map<K, boost::shared_ptr<ResultType> > executeOnEntries(const EntryProcessor &entryProcessor,
825  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor, predicate);
826  }
827 
842  template<typename ResultType, typename EntryProcessor>
843  std::map<K, boost::shared_ptr<ResultType> >
844  executeOnEntries(const EntryProcessor &entryProcessor, const query::Predicate &predicate) {
845  return mapImpl->template executeOnEntries<ResultType, EntryProcessor>(entryProcessor, predicate);
846  }
847 
855  void set(const K &key, const V &value) {
856  mapImpl->set(key, value);
857  }
858 
868  void set(const K &key, const V &value, int64_t ttl) {
869  mapImpl->set(key, value, ttl);
870  }
871 
879  int size() {
880  return mapImpl->size();
881  }
882 
888  bool isEmpty() {
889  return mapImpl->isEmpty();
890  }
891 
892 
903  void putAll(const std::map<K, V> &entries) {
904  return mapImpl->putAll(entries);
905  }
906 
911  void clear() {
912  mapImpl->clear();
913  }
914 
919  void destroy() {
920  mapImpl->destroy();
921  }
922 
936  return mapImpl->getLocalMapStats();
937  }
938 
978  boost::shared_ptr<ICompletableFuture<V> > getAsync(const K &key) {
979  return mapImpl->getAsync(key);
980  }
981 
1037  boost::shared_ptr<ICompletableFuture<V> > putAsync(const K &key, const V &value) {
1038  return mapImpl->putAsync(key, value);
1039  }
1040 
1107  boost::shared_ptr<ICompletableFuture<V> >
1108  putAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit) {
1109  return mapImpl->putAsync(key, value, ttl, ttlUnit);
1110  }
1111 
1185  boost::shared_ptr<ICompletableFuture<V> >
1186  putAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit,
1187  int64_t maxIdle, const util::concurrent::TimeUnit &maxIdleUnit) {
1188  return mapImpl->putAsync(key, value, ttl, ttlUnit, maxIdle, maxIdleUnit);
1189  }
1190 
1238  boost::shared_ptr<ICompletableFuture<void> > setAsync(const K &key, const V &value) {
1239  return mapImpl->setAsync(key, value);
1240  }
1241 
1303  boost::shared_ptr<ICompletableFuture<void> >
1304  setAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit) {
1305  return mapImpl->setAsync(key, value, ttl, ttlUnit);
1306  }
1307 
1372  boost::shared_ptr<ICompletableFuture<void> >
1373  setAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit,
1374  int64_t maxIdle, const util::concurrent::TimeUnit &maxIdleUnit) {
1375  return mapImpl->setAsync(key, value, ttl, ttlUnit, maxIdle, maxIdleUnit);
1376  }
1377 
1402  boost::shared_ptr<ICompletableFuture<V> > removeAsync(const K &key) {
1403  return mapImpl->removeAsync(key);
1404  }
1405 
1406  private:
1407  IMap(boost::shared_ptr<spi::ClientProxy> clientProxy) {
1408  mapImpl = boost::static_pointer_cast<map::ClientMapProxy<K, V> >(clientProxy);
1409  }
1410 
1411  boost::shared_ptr<map::ClientMapProxy<K, V> > mapImpl;
1412  };
1413 
1414  template<typename K, typename V>
1415  const std::string IMap<K, V>::SERVICE_NAME = "hz:impl:mapService";
1416  }
1417 }
1418 
1419 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
1420 #pragma warning(pop)
1421 #endif
1422 
1423 #endif /* HAZELCAST_IMAP */
1424 
void removeAll(const query::Predicate &predicate)
Removes all entries which match with the supplied predicate.
Definition: IMap.h:153
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:652
std::map< K, V > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: IMap.h:534
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:903
void destroy()
Destroys this object cluster-wide.
Definition: IMap.h:919
bool tryLock(const K &key, int64_t timeInMillis)
Tries to acquire the lock for the specified key.
Definition: IMap.h:359
std::map< K, boost::shared_ptr< ResultType > > executeOnEntries(const EntryProcessor &entryProcessor, const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:823
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:274
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: IMap.h:339
Map Entry listener to get notified when a map entry is added, removed, updated or evicted...
Definition: EntryListener.h:45
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:769
boost::shared_ptr< ICompletableFuture< V > > removeAsync(const K &key)
Asynchronously removes the given key, returning an ICompletableFuture on which the caller can provide...
Definition: IMap.h:1402
std::vector< V > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:628
std::vector< V > values()
Returns a vector clone of the values contained in this map.
Definition: IMap.h:602
boost::shared_ptr< ICompletableFuture< void > > setAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit)
Asynchronously puts an entry into this map with a given TTL (time to live) value, without returning t...
Definition: IMap.h:1304
void deleteEntry(const K &key)
removes entry from map.
Definition: IMap.h:174
void evictAll()
Evicts all keys from this map except locked ones.
Definition: IMap.h:524
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:432
bool tryRemove(const K &key, int64_t timeoutInMillis)
Tries to remove the entry with the given key from this map within specified timeout value...
Definition: IMap.h:196
boost::shared_ptr< ICompletableFuture< V > > putAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit, int64_t maxIdle, const util::concurrent::TimeUnit &maxIdleUnit)
Asynchronously puts the given key and value into this map with a given TTL (time to live) value and m...
Definition: IMap.h:1186
boost::shared_ptr< ICompletableFuture< void > > setAsync(const K &key, const V &value)
Asynchronously puts the given key and value.
Definition: IMap.h:1238
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:844
std::vector< K > keySet()
Returns a vector clone of the keys contained in this map.
Definition: IMap.h:545
Definition: LocalMapStats.h:31
boost::shared_ptr< ICompletableFuture< void > > setAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit, int64_t maxIdle, const util::concurrent::TimeUnit &maxIdleUnit)
Asynchronously puts an entry into this map with a given TTL (time to live) value and max idle time va...
Definition: IMap.h:1373
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: IMap.h:483
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:696
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:263
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:237
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:732
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: IMap.h:326
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:576
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:801
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: IMap.h:182
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: IMap.h:404
void putTransient(const K &key, const V &value, int64_t ttlInMillis)
Same as put(K, V, int64_t, TimeUnit) but MapStore, if defined, will not be called to store/persist th...
Definition: IMap.h:225
void lock(const K &key, int64_t leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: IMap.h:314
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:292
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: IMap.h:414
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: IMap.h:452
This is a unique Future.
Definition: Future.h:105
bool tryPut(const K &key, const V &value, int64_t timeoutInMillis)
Tries to put the given key, value into this map within specified timeout value.
Definition: IMap.h:212
boost::shared_ptr< ICompletableFuture< V > > getAsync(const K &key)
Asynchronously gets the given key.
Definition: IMap.h:978
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:751
This is a marker 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:682
EntryView represents a readonly view of a map entry.
Definition: EntryView.h:41
std::vector< K > keySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:561
boost::shared_ptr< V > put(const K &key, const V &value, int64_t ttlInMillis)
Puts an entry into this map with a given ttl (time to live) value.
Definition: IMap.h:130
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:888
void unlock(const K &key)
Releases the lock for the specified key.
Definition: IMap.h:376
boost::shared_ptr< V > putIfAbsent(const K &key, const V &value, int64_t 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:252
Future< ResultType > submitToKey(const K &key, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: IMap.h:783
bool evict(const K &key)
Evicts the specified key from this map.
Definition: IMap.h:509
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: IMap.h:388
monitor::LocalMapStats & getLocalMapStats()
Returns LocalMapStats for this map.
Definition: IMap.h:935
std::vector< V > values(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:616
void clear()
Removes all of the mappings from this map (optional operation).
Definition: IMap.h:911
std::vector< V > values(query::PagingPredicate< K, V > &predicate)
Returns a vector clone of the values contained in this map.
Definition: IMap.h:641
boost::shared_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: IMap.h:115
EntryView< K, V > getEntryView(const K &key)
Returns the EntryView for the specified key.
Definition: IMap.h:495
boost::shared_ptr< ICompletableFuture< V > > putAsync(const K &key, const V &value, int64_t ttl, const util::concurrent::TimeUnit &ttlUnit)
Asynchronously puts the given key and value into this map with a given TTL (time to live) value...
Definition: IMap.h:1108
bool containsValue(const V &value)
check if this map contains value.
Definition: IMap.h:92
boost::shared_ptr< ICompletableFuture< V > > putAsync(const K &key, const V &value)
Asynchronously puts the given key and value.
Definition: IMap.h:1037
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:591
bool containsKey(const K &key)
check if this map contains key.
Definition: IMap.h:82
std::vector< std::pair< K, V > > entrySet(const serialization::IdentifiedDataSerializable &predicate)
Definition: IMap.h:668
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: IMap.h:465
int size()
Returns the number of key-value mappings in this map.
Definition: IMap.h:879