Hazelcast C++ Client
MultiMap.h
1 /*
2  * Copyright (c) 2008-2018, 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_MULTI_MAP
17 #define HAZELCAST_MULTI_MAP
18 
19 #include <string>
20 #include <vector>
21 
22 #include "hazelcast/client/proxy/MultiMapImpl.h"
23 #include "hazelcast/client/impl/EntryEventHandler.h"
24 #include "hazelcast/client/protocol/codec/MultiMapAddEntryListenerCodec.h"
25 #include "hazelcast/client/protocol/codec/MultiMapAddEntryListenerToKeyCodec.h"
26 
27 namespace hazelcast {
28  namespace client {
29  namespace adaptor {
30  template <typename K, typename V>
31  class RawPointerMultiMap;
32  }
33 
39  template<typename K, typename V>
40  class MultiMap : public proxy::MultiMapImpl {
41  friend class impl::HazelcastClientInstanceImpl;
42  friend class adaptor::RawPointerMultiMap<K, V>;
43 
44  public:
54  bool put(const K &key, const V &value) {
55  return proxy::MultiMapImpl::put(toData(key), toData(value));
56  }
57 
64  std::vector<V> get(const K &key) {
65  return toObjectCollection<V>(proxy::MultiMapImpl::getData(toData(key)));
66  }
67 
75  bool remove(const K &key, const V &value) {
76  return proxy::MultiMapImpl::remove(toData(key), toData(value));
77  }
78 
86  std::vector<V> remove(const K &key) {
87  return toObjectCollection<V>(proxy::MultiMapImpl::removeData(toData(key)));
88  }
89 
96  std::vector<K> keySet() {
97  return toObjectCollection<K>(proxy::MultiMapImpl::keySetData());
98  }
99 
106  std::vector<V> values() {
107  return toObjectCollection<V>(proxy::MultiMapImpl::valuesData());
108  }
109 
116  std::vector<std::pair<K, V> > entrySet() {
117  return toObjectEntrySet<K, V>(proxy::MultiMapImpl::entrySetData());
118  }
119 
126  bool containsKey(const K &key) {
127  return proxy::MultiMapImpl::containsKey(toData(key));
128  }
129 
136  bool containsValue(const V &value) {
137  return proxy::MultiMapImpl::containsValue(toData(value));
138  }
139 
147  bool containsEntry(const K &key, const V &value) {
148  return proxy::MultiMapImpl::containsEntry(toData(key), toData(value));
149  }
150 
156  int size() {
157  return proxy::MultiMapImpl::size();
158  }
159 
163  void clear() {
164  proxy::MultiMapImpl::clear();
165  }
166 
174  int valueCount(const K &key) {
175  return proxy::MultiMapImpl::valueCount(toData(key));
176  }
177 
192  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
193  spi::ClientClusterService &clusterService = getContext().getClientClusterService();
194  serialization::pimpl::SerializationService &ss = getContext().getSerializationService();
195  impl::EntryEventHandler<K, V, protocol::codec::MultiMapAddEntryListenerCodec::AbstractEventHandler> *entryEventHandler =
196  new impl::EntryEventHandler<K, V, protocol::codec::MultiMapAddEntryListenerCodec::AbstractEventHandler>(
197  getName(), clusterService, ss, listener, includeValue);
198  return proxy::MultiMapImpl::addEntryListener(entryEventHandler, includeValue);
199  }
200 
217  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
218  impl::EntryEventHandler<K, V, protocol::codec::MultiMapAddEntryListenerToKeyCodec::AbstractEventHandler> *entryEventHandler =
219  new impl::EntryEventHandler<K, V, protocol::codec::MultiMapAddEntryListenerToKeyCodec::AbstractEventHandler>(
220  getName(), getContext().getClientClusterService(), getContext().getSerializationService(), listener,
221  includeValue);
222  serialization::pimpl::Data keyData = toData(key);
223  return proxy::MultiMapImpl::addEntryListener(entryEventHandler, keyData, includeValue);
224  }
225 
234  bool removeEntryListener(const std::string &registrationId) {
235  return proxy::MultiMapImpl::removeEntryListener(registrationId);
236  }
237 
253  void lock(const K &key) {
254  proxy::MultiMapImpl::lock(toData(key));
255  }
256 
273  void lock(const K &key, long leaseTimeInMillis) {
274  proxy::MultiMapImpl::lock(toData(key), leaseTimeInMillis);
275  }
276 
284  bool isLocked(const K &key) {
285  return proxy::MultiMapImpl::isLocked(toData(key));
286  }
287 
297  bool tryLock(const K &key) {
298  return proxy::MultiMapImpl::tryLock(toData(key));
299  }
300 
317  bool tryLock(const K &key, long timeoutInMillis) {
318  return proxy::MultiMapImpl::tryLock(toData(key), timeoutInMillis);
319  }
320 
328  void unlock(const K &key) {
329  proxy::MultiMapImpl::unlock(toData(key));
330  }
331 
338  void forceUnlock(const K &key) {
339  proxy::MultiMapImpl::forceUnlock(toData(key));
340  }
341 
342  private:
343  MultiMap(const std::string &instanceName, spi::ClientContext *context)
344  : proxy::MultiMapImpl(instanceName, context) {
345 
346  }
347  };
348  }
349 }
350 
351 #endif /* HAZELCAST_MULTI_MAP */
352 
std::vector< std::pair< K, V > > entrySet()
Returns the set of key-value pairs in the multimap.
Definition: MultiMap.h:116
Map Entry listener to get notified when a map entry is added, removed, updated or evicted...
Definition: EntryListener.h:45
void lock(const K &key, long leaseTimeInMillis)
Acquires the lock for the specified key for the specified lease time.
Definition: MultiMap.h:273
void lock(const K &key)
Acquires the lock for the specified key.
Definition: MultiMap.h:253
bool tryLock(const K &key, long timeoutInMillis)
Tries to acquire the lock for the specified key.
Definition: MultiMap.h:317
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: MultiMap.h:284
int size()
Returns the number of key-value pairs in the multimap.
Definition: MultiMap.h:156
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this multimap.
Definition: MultiMap.h:192
std::vector< K > keySet()
Returns the set of keys in the multimap.
Definition: MultiMap.h:96
int valueCount(const K &key)
Returns number of values matching to given key in the multimap.
Definition: MultiMap.h:174
A specialized distributed map client whose keys can be associated with multiple values.
Definition: MultiMap.h:40
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: MultiMap.h:297
bool containsValue(const V &value)
Returns whether the multimap contains an entry with the value.
Definition: MultiMap.h:136
void clear()
Clears the multimap.
Definition: MultiMap.h:163
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: MultiMap.h:217
bool put(const K &key, const V &value)
Stores a key-value pair in the multimap.
Definition: MultiMap.h:54
bool containsKey(const K &key)
Returns whether the multimap contains an entry with the key.
Definition: MultiMap.h:126
std::vector< V > values()
Returns the multimap of values in the multimap.
Definition: MultiMap.h:106
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: MultiMap.h:338
void unlock(const K &key)
Releases the lock for the specified key.
Definition: MultiMap.h:328
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: MultiMap.h:234
A specialized distributed map client whose keys can be associated with multiple values.
Definition: RawPointerMultiMap.h:32
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32
bool containsEntry(const K &key, const V &value)
Returns whether the multimap contains the given key-value pair.
Definition: MultiMap.h:147