Hazelcast C++ Client
RawPointerMap.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_CLIENT_ADAPTOR_RAWPOINTERMAP_H_
17 #define HAZELCAST_CLIENT_ADAPTOR_RAWPOINTERMAP_H_
18 
19 #include "hazelcast/client/IMap.h"
20 #include "hazelcast/client/adaptor/MapEntryView.h"
21 #include "hazelcast/client/impl/DataArrayImpl.h"
22 #include "hazelcast/client/impl/EntryArrayImpl.h"
23 #include "hazelcast/client/impl/EntryArrayKeyAdaptor.h"
24 #include "hazelcast/client/impl/EntryArrayValueAdaptor.h"
25 #include "hazelcast/client/map/ClientMapProxy.h"
26 
27 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
28 #pragma warning(push)
29 #pragma warning(disable: 4251) //for dll export
30 #endif
31 
32 namespace hazelcast {
33  namespace client {
34  namespace adaptor {
50  template<typename K, typename V>
51  class RawPointerMap {
52  public:
53  RawPointerMap(IMap<K, V> &mapToBeAdopted) : map(mapToBeAdopted),
54  mapProxy(*map.mapImpl) {
55  serializationService = &mapProxy.getSerializationService();
56  }
57 
64  bool containsKey(const K &key) {
65  return map.containsKey(key);
66  }
67 
74  bool containsValue(const V &value) {
75  return map.containsValue(value);
76  }
77 
85  std::auto_ptr<V> get(const K &key) {
86  return serializationService->toObject<V>(
87  mapProxy.getData(serializationService->toData<K>(&key)).get());
88  }
89 
98  std::auto_ptr<V> put(const K &key, const V &value) {
99  return put(key, value, -1);
100  }
101 
113  std::auto_ptr<V> put(const K &key, const V &value, long ttlInMillis) {
114  return serializationService->toObject<V>(
115  mapProxy.putData(serializationService->toData<K>(&key), serializationService->toData<V>(&value),
116  ttlInMillis).get());
117  }
118 
126  std::auto_ptr<V> remove(const K &key) {
127  return serializationService->toObject<V>(
128  mapProxy.removeData(serializationService->toData<K>(&key)).get());
129  }
130 
138  bool remove(const K &key, const V &value) {
139  return map.remove(key, value);
140  }
141 
150  void removeAll(const query::Predicate &predicate) {
151  map.removeAll(predicate);
152  }
153 
160  void deleteEntry(const K &key) {
161  map.deleteEntry(key);
162  }
163 
168  void flush() {
169  map.flush();
170  }
171 
182  bool tryRemove(const K &key, long timeoutInMillis) {
183  return map.tryRemove(key, timeoutInMillis);
184  }
185 
198  bool tryPut(const K &key, const V &value, long timeoutInMillis) {
199  return map.tryPut(key, value, timeoutInMillis);
200  }
201 
211  void putTransient(const K &key, const V &value, long ttlInMillis) {
212  map.putTransient(key, value, ttlInMillis);
213  }
214 
223  std::auto_ptr<V> putIfAbsent(const K &key, const V &value) {
224  return serializationService->toObject<V>(mapProxy.putIfAbsentData(serializationService->toData<K>(&key),
225  serializationService->toData<V>(&value),
226  -1).get());
227  }
228 
240  std::auto_ptr<V> putIfAbsent(const K &key, const V &value, long ttlInMillis) {
241  return serializationService->toObject<V>(
242  mapProxy.putIfAbsentData(serializationService->toData<K>(&key),
243  serializationService->toData<V>(&value),
244  ttlInMillis).get());
245  }
246 
254  bool replace(const K &key, const V &oldValue, const V &newValue) {
255  return map.replace(key, oldValue, newValue);
256  }
257 
265  std::auto_ptr<V> replace(const K &key, const V &value) {
266  return serializationService->toObject<V>(mapProxy.replaceData(
267  serializationService->toData<K>(&key), serializationService->toData<V>(&value)).get());
268  }
269 
277  void set(const K &key, const V &value) {
278  set(key, value, -1);
279  }
280 
290  void set(const K &key, const V &value, long ttl) {
291  map.set(key, value, ttl);
292  }
293 
308  void lock(const K &key) {
309  map.lock(key);
310  }
311 
330  void lock(const K &key, long leaseTime) {
331  map.lock(key, leaseTime);
332  }
333 
342  bool isLocked(const K &key) {
343  return map.isLocked(key);
344  }
345 
355  bool tryLock(const K &key) {
356  return map.tryLock(key, 0);
357  }
358 
375  bool tryLock(const K &key, long timeInMillis) {
376  return map.tryLock(key, timeInMillis);
377  }
378 
392  void unlock(const K &key) {
393  map.unlock(key);
394  }
395 
404  void forceUnlock(const K &key) {
405  map.forceUnlock(key);
406  }
407 
419  template<typename MapInterceptor>
420  std::string addInterceptor(MapInterceptor &interceptor) {
421  return map.template addInterceptor<MapInterceptor>(interceptor);
422  }
423 
430  void removeInterceptor(const std::string &id) {
431  map.removeInterceptor(id);
432  }
433 
448  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
449  return map.addEntryListener(listener, includeValue);
450  }
451 
467  std::string addEntryListener(EntryListener<K, V> &listener, const query::Predicate &predicate, bool includeValue) {
468  return map.addEntryListener(listener, predicate, includeValue);
469  }
470 
480  bool removeEntryListener(const std::string &registrationId) {
481  return map.removeEntryListener(registrationId);
482  }
483 
484 
498  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
499  return map.addEntryListener(listener, key, includeValue);
500  }
501 
510  std::auto_ptr<MapEntryView<K, V> > getEntryView(const K &key) {
511  std::auto_ptr<map::DataEntryView> dataView = mapProxy.getEntryViewData(serializationService->toData<K>(&key));
512  if ((map::DataEntryView *)NULL == dataView.get()) {
513  return std::auto_ptr<MapEntryView<K, V> >();
514  }
515  return std::auto_ptr<MapEntryView<K, V> >(new MapEntryView<K, V>(dataView, *serializationService));
516  }
517 
528  bool evict(const K &key) {
529  return map.evict(key);
530  }
531 
543  void evictAll() {
544  map.evictAll();
545  }
546 
553  std::auto_ptr<EntryArray<K, V> > getAll(const std::set<K> &keys) {
554  if (keys.empty()) {
555  return std::auto_ptr<EntryArray<K, V> >();
556  }
557 
558  std::map<int, std::vector<serialization::pimpl::Data> > partitionToKeyData;
559  // group the request per parition id
560  for (typename std::set<K>::const_iterator it = keys.begin();it != keys.end(); ++it) {
561  serialization::pimpl::Data keyData = mapProxy.template toData<K>(*it);
562 
563  int partitionId = mapProxy.getPartitionId(keyData);
564 
565  partitionToKeyData[partitionId].push_back(keyData);
566  }
567 
568  EntryVector entrySet = mapProxy.getAllData(partitionToKeyData);
569 
570  return std::auto_ptr<EntryArray<K, V> >(
571  new client::impl::EntryArrayImpl<K, V>(entrySet, *serializationService));
572  }
573 
581  std::auto_ptr<DataArray<K> > keySet() {
582  std::vector<serialization::pimpl::Data> dataResult = mapProxy.keySetData();
583  return std::auto_ptr<DataArray<K> >(new hazelcast::client::impl::DataArrayImpl<K>(
584  dataResult, *serializationService));
585  }
586 
597  std::auto_ptr<DataArray<K> > keySet(const query::Predicate &predicate) {
598  std::vector<serialization::pimpl::Data> dataResult = mapProxy.keySetData(predicate);
599  return std::auto_ptr<DataArray<K> >(new hazelcast::client::impl::DataArrayImpl<K>(
600  dataResult, *serializationService));
601  }
602 
614  std::auto_ptr<DataArray<K> > keySet(query::PagingPredicate<K, V> &predicate) {
615  predicate.setIterationType(query::KEY);
616 
617  std::vector<serialization::pimpl::Data> dataResult = mapProxy.keySetForPagingPredicateData(predicate);
618 
619  EntryVector entryResult;
620  for (std::vector<serialization::pimpl::Data>::iterator it = dataResult.begin();it != dataResult.end(); ++it) {
621  entryResult.push_back(std::pair<serialization::pimpl::Data, serialization::pimpl::Data>(
622  *it, serialization::pimpl::Data()));
623  }
624 
625  client::impl::EntryArrayImpl<K, V> entries(entryResult, *serializationService);
626 
627  entries.sort(query::KEY, predicate.getComparator());
628 
629  std::pair<size_t, size_t> range = mapProxy.template updateAnchor<K, V>(entries, predicate, query::KEY);
630 
631  std::auto_ptr<EntryArray<K, V> > subList(new client::impl::EntryArrayImpl<K, V>(
632  entries, range.first, range.second));
633 
634  std::auto_ptr<DataArray<K> > result = std::auto_ptr<DataArray<K> >(
635  new impl::EntryArrayKeyAdaptor<K, V>(subList));
636 
637  return result;
638  }
639 
647  std::auto_ptr<DataArray<V> > values() {
648  std::vector<serialization::pimpl::Data> dataResult = mapProxy.valuesData();
649  return std::auto_ptr<DataArray<V> >(
650  new hazelcast::client::impl::DataArrayImpl<V>(dataResult, *serializationService));
651  }
652 
661  std::auto_ptr<DataArray<V> > values(const query::Predicate &predicate) {
662  std::vector<serialization::pimpl::Data> dataResult = mapProxy.valuesData(predicate);
663  return std::auto_ptr<DataArray<V> >(
664  new hazelcast::client::impl::DataArrayImpl<V>(dataResult, *serializationService));
665  }
666 
675  std::auto_ptr<DataArray<V> > values(query::PagingPredicate<K, V> &predicate) {
676  predicate.setIterationType(query::VALUE);
677 
678  EntryVector entryResult = mapProxy.valuesForPagingPredicateData(predicate);
679 
680  client::impl::EntryArrayImpl<K, V> entries(entryResult, *serializationService);
681 
682  entries.sort(query::VALUE, predicate.getComparator());
683 
684  std::pair<size_t, size_t> range = mapProxy.template updateAnchor<K, V>(entries, predicate, query::VALUE);
685 
686  std::auto_ptr<EntryArray<K, V> > subList(new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
687  std::auto_ptr<DataArray<V> > result = std::auto_ptr<DataArray<V> >(
688  new impl::EntryArrayValueAdaptor<K, V>(subList));
689 
690  return result;
691  }
692 
700  std::auto_ptr<EntryArray<K, V> > entrySet() {
701  EntryVector entries = mapProxy.entrySetData();
702  return std::auto_ptr<EntryArray<K, V> >(
703  new client::impl::EntryArrayImpl<K, V>(entries, *serializationService));
704  }
705 
716  std::auto_ptr<EntryArray<K, V> > entrySet(const query::Predicate &predicate) {
717  EntryVector entries = mapProxy.entrySetData(predicate);
718  return std::auto_ptr<EntryArray<K, V> >(
719  new client::impl::EntryArrayImpl<K, V>(entries, *serializationService));
720  }
721 
732  std::auto_ptr<EntryArray<K, V> > entrySet(query::PagingPredicate<K, V> &predicate) {
733  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult =
734  mapProxy.entrySetForPagingPredicateData(predicate);
735 
736  client::impl::EntryArrayImpl<K, V> entries(dataResult, *serializationService);
737 
738  entries.sort(query::ENTRY, predicate.getComparator());
739 
740  std::pair<size_t, size_t> range = mapProxy.template updateAnchor<K, V>(entries, predicate, query::ENTRY);
741 
742  return std::auto_ptr<EntryArray<K, V> >(
743  new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
744  }
745 
778  void addIndex(const std::string &attribute, bool ordered) {
779  map.addIndex(attribute, ordered);
780  }
781 
796  template<typename ResultType, typename EntryProcessor>
797  std::auto_ptr<ResultType> executeOnKey(const K &key, const EntryProcessor &entryProcessor) {
798  serialization::pimpl::Data keyData = serializationService->toData<K>(&key);
799  serialization::pimpl::Data processorData = serializationService->toData<EntryProcessor>(&entryProcessor);
800 
801  std::auto_ptr<serialization::pimpl::Data> resultData = mapProxy.executeOnKeyInternal(keyData, processorData);
802 
803  return serializationService->toObject<ResultType>(resultData.get());
804  }
805 
815  template<typename ResultType, typename EntryProcessor>
816  Future<ResultType> submitToKey(const K &key, const EntryProcessor &entryProcessor) {
817  return map.template submitToKey<ResultType, EntryProcessor>(key, entryProcessor);
818  }
819 
820  template<typename ResultType, typename EntryProcessor>
821  std::auto_ptr<EntryArray<K, ResultType> > executeOnKeys(const std::set<K> &keys, const EntryProcessor &entryProcessor) {
822  EntryVector results = mapProxy.template executeOnKeysInternal<EntryProcessor>(keys, entryProcessor);
823 
824  return std::auto_ptr<EntryArray<K, ResultType> >(
825  new client::impl::EntryArrayImpl<K, ResultType>(results, *serializationService));
826  }
827 
842  template<typename ResultType, typename EntryProcessor>
843  std::auto_ptr<EntryArray<K, ResultType> > executeOnEntries(const EntryProcessor &entryProcessor) {
844  EntryVector results = mapProxy.template executeOnEntriesData<EntryProcessor>(entryProcessor);
845 
846  return std::auto_ptr<EntryArray<K, ResultType> >(
847  new client::impl::EntryArrayImpl<K, ResultType>(results, *serializationService));
848  }
849 
864  template<typename ResultType, typename EntryProcessor>
865  std::auto_ptr<EntryArray<K, ResultType> > executeOnEntries(const EntryProcessor &entryProcessor, const query::Predicate &predicate) {
866  EntryVector results = mapProxy.template executeOnEntriesData<EntryProcessor>(entryProcessor, predicate);
867 
868  return std::auto_ptr<EntryArray<K, ResultType> >(
869  new client::impl::EntryArrayImpl<K, ResultType>(results, *serializationService));
870  }
871 
879  int size() {
880  return map.size();
881  }
882 
888  bool isEmpty() {
889  return map.isEmpty();
890  }
891 
892 
903  void putAll(const std::map<K, V> &entries) {
904  map.putAll(entries);
905  }
906 
911  void clear() {
912  map.clear();
913  }
914 
915  private:
916  IMap<K, V> &map;
917  map::ClientMapProxy<K, V> &mapProxy;
918  serialization::pimpl::SerializationService *serializationService;
919  };
920  }
921  }
922 }
923 
924 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
925 #pragma warning(pop)
926 #endif
927 
928 #endif /* HAZELCAST_CLIENT_ADAPTOR_RAWPOINTERMAP_H_ */
929 
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: RawPointerMap.h:168
std::auto_ptr< EntryArray< K, V > > entrySet()
Returns a std::vector< std::pair<K, V> > clone of the mappings contained in this map.
Definition: RawPointerMap.h:700
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: RawPointerMap.h:404
void removeAll(const query::Predicate &predicate)
Removes all entries which match with the supplied predicate.
Definition: RawPointerMap.h:150
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: RawPointerMap.h:330
Map Entry listener to get notified when a map entry is added, removed, updated or evicted...
Definition: EntryListener.h:45
void putAll(const std::map< K, V > &entries)
Copies all of the mappings from the specified map to this map (optional operation).
Definition: RawPointerMap.h:903
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: RawPointerMap.h:480
bool containsKey(const K &key)
check if this map contains key.
Definition: RawPointerMap.h:64
std::auto_ptr< DataArray< V > > values(query::PagingPredicate< K, V > &predicate)
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:675
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: RawPointerMap.h:778
void lock(const K &key)
Acquires the lock for the specified key.
Definition: RawPointerMap.h:308
Future< ResultType > submitToKey(const K &key, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: RawPointerMap.h:816
std::auto_ptr< V > replace(const K &key, const V &value)
Replaces the entry for a key only if currently mapped to some value.
Definition: RawPointerMap.h:265
bool containsValue(const V &value)
check if this map contains value.
Definition: RawPointerMap.h:74
std::auto_ptr< ResultType > executeOnKey(const K &key, const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: RawPointerMap.h:797
std::auto_ptr< DataArray< V > > values()
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:647
bool evict(const K &key)
Evicts the specified key from this map.
Definition: RawPointerMap.h:528
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: RawPointerMap.h:430
std::auto_ptr< EntryArray< K, V > > entrySet(const query::Predicate &predicate)
Queries the map based on the specified predicate and returns the matching entries.
Definition: RawPointerMap.h:716
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: RawPointerMap.h:498
void deleteEntry(const K &key)
removes entry from map.
Definition: RawPointerMap.h:160
bool tryRemove(const K &key, long timeoutInMillis)
Tries to remove the entry with the given key from this map within specified timeout value...
Definition: RawPointerMap.h:182
NOTE: PagingPredicate can only be used with values(), keySet() and entries() methods!!! ...
Definition: PagingPredicate.h:127
std::auto_ptr< EntryArray< K, ResultType > > executeOnEntries(const EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: RawPointerMap.h:843
std::auto_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: RawPointerMap.h:240
std::auto_ptr< EntryArray< K, ResultType > > executeOnEntries(const EntryProcessor &entryProcessor, const query::Predicate &predicate)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: RawPointerMap.h:865
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: RawPointerMap.h:211
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: RawPointerMap.h:420
bool isEmpty()
Returns true if this map contains no key-value mappings.
Definition: RawPointerMap.h:888
bool tryLock(const K &key, long timeInMillis)
Tries to acquire the lock for the specified key.
Definition: RawPointerMap.h:375
std::auto_ptr< MapEntryView< K, V > > getEntryView(const K &key)
Returns the MapEntryView for the specified key.
Definition: RawPointerMap.h:510
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: RawPointerMap.h:342
int size()
Returns the number of key-value mappings in this map.
Definition: RawPointerMap.h:879
Adaptor class to IMap which provides releasable raw pointers for returned objects.
Definition: RawPointerMap.h:51
void clear()
Removes all of the mappings from this map (optional operation).
Definition: RawPointerMap.h:911
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: RawPointerMap.h:254
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: RawPointerMap.h:467
std::auto_ptr< DataArray< K > > keySet()
Returns a snaphot of the deys data in the map.
Definition: RawPointerMap.h:581
std::auto_ptr< EntryArray< K, V > > entrySet(query::PagingPredicate< K, V > &predicate)
Queries the map based on the specified predicate and returns the matching entries.
Definition: RawPointerMap.h:732
std::auto_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: RawPointerMap.h:98
This is a unique Future.
Definition: Future.h:112
std::auto_ptr< DataArray< K > > keySet(query::PagingPredicate< K, V > &predicate)
Queries the map based on the specified predicate and returns the keys of matching entries...
Definition: RawPointerMap.h:614
This is a merker class for Predicate classes.
Definition: Predicate.h:36
Concurrent, distributed, observable and queryable map client.
Definition: IMap.h:66
std::auto_ptr< EntryArray< K, V > > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: RawPointerMap.h:553
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: RawPointerMap.h:290
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: RawPointerMap.h:277
Definition: MapEntryView.h:32
std::auto_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: RawPointerMap.h:113
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: RawPointerMap.h:448
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: RawPointerMap.h:198
void evictAll()
Evicts all keys from this map except locked ones.
Definition: RawPointerMap.h:543
std::auto_ptr< DataArray< V > > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:661
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: RawPointerMap.h:355
std::auto_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: RawPointerMap.h:223
void unlock(const K &key)
Releases the lock for the specified key.
Definition: RawPointerMap.h:392
MapEntryView represents a readonly view of a map entry.
Definition: MapEntryView.h:38
std::auto_ptr< DataArray< K > > keySet(const query::Predicate &predicate)
Queries the map based on the specified predicate and returns the keys of matching entries...
Definition: RawPointerMap.h:597