Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
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 
148  void deleteEntry(const K &key) {
149  map.deleteEntry(key);
150  }
151 
156  void flush() {
157  map.flush();
158  }
159 
170  bool tryRemove(const K &key, long timeoutInMillis) {
171  return map.tryRemove(key, timeoutInMillis);
172  }
173 
186  bool tryPut(const K &key, const V &value, long timeoutInMillis) {
187  return map.tryPut(key, value, timeoutInMillis);
188  }
189 
199  void putTransient(const K &key, const V &value, long ttlInMillis) {
200  map.putTransient(key, value, ttlInMillis);
201  }
202 
211  std::auto_ptr<V> putIfAbsent(const K &key, const V &value) {
212  return serializationService->toObject<V>(mapProxy.putIfAbsentData(serializationService->toData<K>(&key),
213  serializationService->toData<V>(&value),
214  -1).get());
215  }
216 
228  std::auto_ptr<V> putIfAbsent(const K &key, const V &value, long ttlInMillis) {
229  return serializationService->toObject<V>(
230  mapProxy.putIfAbsentData(serializationService->toData<K>(&key),
231  serializationService->toData<V>(&value),
232  ttlInMillis).get());
233  }
234 
242  bool replace(const K &key, const V &oldValue, const V &newValue) {
243  return map.replace(key, oldValue, newValue);
244  }
245 
253  std::auto_ptr<V> replace(const K &key, const V &value) {
254  return serializationService->toObject<V>(mapProxy.replaceData(
255  serializationService->toData<K>(&key), serializationService->toData<V>(&value)).get());
256  }
257 
265  void set(const K &key, const V &value) {
266  set(key, value, -1);
267  }
268 
278  void set(const K &key, const V &value, long ttl) {
279  map.set(key, value, ttl);
280  }
281 
296  void lock(const K &key) {
297  map.lock(key);
298  }
299 
318  void lock(const K &key, long leaseTime) {
319  map.lock(key, leaseTime);
320  }
321 
330  bool isLocked(const K &key) {
331  return map.isLocked(key);
332  }
333 
343  bool tryLock(const K &key) {
344  return map.tryLock(key, 0);
345  }
346 
363  bool tryLock(const K &key, long timeInMillis) {
364  return map.tryLock(key, timeInMillis);
365  }
366 
380  void unlock(const K &key) {
381  map.unlock(key);
382  }
383 
392  void forceUnlock(const K &key) {
393  map.forceUnlock(key);
394  }
395 
407  template<typename MapInterceptor>
408  std::string addInterceptor(MapInterceptor &interceptor) {
409  return map.template addInterceptor<MapInterceptor>(interceptor);
410  }
411 
418  void removeInterceptor(const std::string &id) {
419  map.removeInterceptor(id);
420  }
421 
436  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
437  return map.addEntryListener(listener, includeValue);
438  }
439 
455  std::string addEntryListener(EntryListener<K, V> &listener, const query::Predicate &predicate, bool includeValue) {
456  return map.addEntryListener(listener, predicate, includeValue);
457  }
458 
468  bool removeEntryListener(const std::string &registrationId) {
469  return map.removeEntryListener(registrationId);
470  }
471 
472 
486  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
487  return map.addEntryListener(listener, key, includeValue);
488  }
489 
498  std::auto_ptr<MapEntryView<K, V> > getEntryView(const K &key) {
499  std::auto_ptr<map::DataEntryView> dataView = mapProxy.getEntryViewData(serializationService->toData<K>(&key));
500  if ((map::DataEntryView *)NULL == dataView.get()) {
501  return std::auto_ptr<MapEntryView<K, V> >();
502  }
503  return std::auto_ptr<MapEntryView<K, V> >(new MapEntryView<K, V>(dataView, *serializationService));
504  }
505 
516  bool evict(const K &key) {
517  return map.evict(key);
518  }
519 
531  void evictAll() {
532  map.evictAll();
533  }
534 
541  std::auto_ptr<EntryArray<K, V> > getAll(const std::set<K> &keys) {
542  if (keys.empty()) {
543  return std::auto_ptr<EntryArray<K, V> >();
544  }
545 
546  std::map<int, std::vector<serialization::pimpl::Data> > partitionToKeyData;
547  // group the request per parition id
548  for (typename std::set<K>::const_iterator it = keys.begin();it != keys.end(); ++it) {
549  serialization::pimpl::Data keyData = mapProxy.template toData<K>(*it);
550 
551  int partitionId = mapProxy.getPartitionId(keyData);
552 
553  partitionToKeyData[partitionId].push_back(keyData);
554  }
555 
556  EntryVector entrySet = mapProxy.getAllData(partitionToKeyData);
557 
558  return std::auto_ptr<EntryArray<K, V> >(
559  new client::impl::EntryArrayImpl<K, V>(entrySet, *serializationService));
560  }
561 
569  std::auto_ptr<DataArray<K> > keySet() {
570  std::vector<serialization::pimpl::Data> dataResult = mapProxy.keySetData();
571  return std::auto_ptr<DataArray<K> >(new hazelcast::client::impl::DataArrayImpl<K>(
572  dataResult, *serializationService));
573  }
574 
585  std::auto_ptr<DataArray<K> > keySet(const query::Predicate &predicate) {
586  std::vector<serialization::pimpl::Data> dataResult = mapProxy.keySetData(predicate);
587  return std::auto_ptr<DataArray<K> >(new hazelcast::client::impl::DataArrayImpl<K>(
588  dataResult, *serializationService));
589  }
590 
602  std::auto_ptr<DataArray<K> > keySet(query::PagingPredicate<K, V> &predicate) {
603  predicate.setIterationType(query::KEY);
604 
605  std::vector<serialization::pimpl::Data> dataResult = mapProxy.keySetForPagingPredicateData(predicate);
606 
607  EntryVector entryResult;
608  for (std::vector<serialization::pimpl::Data>::iterator it = dataResult.begin();it != dataResult.end(); ++it) {
609  entryResult.push_back(std::pair<serialization::pimpl::Data, serialization::pimpl::Data>(
610  *it, serialization::pimpl::Data()));
611  }
612 
613  client::impl::EntryArrayImpl<K, V> entries(entryResult, *serializationService);
614 
615  entries.sort(query::KEY, predicate.getComparator());
616 
617  std::pair<size_t, size_t> range = mapProxy.template updateAnchor<K, V>(entries, predicate, query::KEY);
618 
619  std::auto_ptr<EntryArray<K, V> > subList(new client::impl::EntryArrayImpl<K, V>(
620  entries, range.first, range.second));
621 
622  std::auto_ptr<DataArray<K> > result = std::auto_ptr<DataArray<K> >(
623  new impl::EntryArrayKeyAdaptor<K, V>(subList));
624 
625  return result;
626  }
627 
635  std::auto_ptr<DataArray<V> > values() {
636  std::vector<serialization::pimpl::Data> dataResult = mapProxy.valuesData();
637  return std::auto_ptr<DataArray<V> >(
638  new hazelcast::client::impl::DataArrayImpl<V>(dataResult, *serializationService));
639  }
640 
649  std::auto_ptr<DataArray<V> > values(const query::Predicate &predicate) {
650  std::vector<serialization::pimpl::Data> dataResult = mapProxy.valuesData(predicate);
651  return std::auto_ptr<DataArray<V> >(
652  new hazelcast::client::impl::DataArrayImpl<V>(dataResult, *serializationService));
653  }
654 
663  std::auto_ptr<DataArray<V> > values(query::PagingPredicate<K, V> &predicate) {
664  predicate.setIterationType(query::VALUE);
665 
666  EntryVector entryResult = mapProxy.valuesForPagingPredicateData(predicate);
667 
668  client::impl::EntryArrayImpl<K, V> entries(entryResult, *serializationService);
669 
670  entries.sort(query::VALUE, predicate.getComparator());
671 
672  std::pair<size_t, size_t> range = mapProxy.template updateAnchor<K, V>(entries, predicate, query::VALUE);
673 
674  std::auto_ptr<EntryArray<K, V> > subList(new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
675  std::auto_ptr<DataArray<V> > result = std::auto_ptr<DataArray<V> >(
676  new impl::EntryArrayValueAdaptor<K, V>(subList));
677 
678  return result;
679  }
680 
688  std::auto_ptr<EntryArray<K, V> > entrySet() {
689  EntryVector entries = mapProxy.entrySetData();
690  return std::auto_ptr<EntryArray<K, V> >(
691  new client::impl::EntryArrayImpl<K, V>(entries, *serializationService));
692  }
693 
704  std::auto_ptr<EntryArray<K, V> > entrySet(const query::Predicate &predicate) {
705  EntryVector entries = mapProxy.entrySetData(predicate);
706  return std::auto_ptr<EntryArray<K, V> >(
707  new client::impl::EntryArrayImpl<K, V>(entries, *serializationService));
708  }
709 
720  std::auto_ptr<EntryArray<K, V> > entrySet(query::PagingPredicate<K, V> &predicate) {
721  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult =
722  mapProxy.entrySetForPagingPredicateData(predicate);
723 
724  client::impl::EntryArrayImpl<K, V> entries(dataResult, *serializationService);
725 
726  entries.sort(query::ENTRY, predicate.getComparator());
727 
728  std::pair<size_t, size_t> range = mapProxy.template updateAnchor<K, V>(entries, predicate, query::ENTRY);
729 
730  return std::auto_ptr<EntryArray<K, V> >(
731  new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
732  }
733 
766  void addIndex(const std::string &attribute, bool ordered) {
767  map.addIndex(attribute, ordered);
768  }
769 
784  template<typename ResultType, typename EntryProcessor>
785  std::auto_ptr<ResultType> executeOnKey(const K &key, EntryProcessor &entryProcessor) {
786  serialization::pimpl::Data keyData = serializationService->toData<K>(&key);
787  serialization::pimpl::Data processorData = serializationService->toData<EntryProcessor>(&entryProcessor);
788 
789  std::auto_ptr<serialization::pimpl::Data> resultData = mapProxy.executeOnKeyInternal(keyData, processorData);
790 
791  return serializationService->toObject<ResultType>(resultData.get());
792  }
793 
808  template<typename ResultType, typename EntryProcessor>
809  std::auto_ptr<EntryArray<K, ResultType> > executeOnEntries(EntryProcessor &entryProcessor) {
810  EntryVector results = mapProxy.template executeOnEntriesData<EntryProcessor>(entryProcessor);
811 
812  return std::auto_ptr<EntryArray<K, ResultType> >(
813  new client::impl::EntryArrayImpl<K, ResultType>(results, *serializationService));
814  }
815 
830  template<typename ResultType, typename EntryProcessor>
831  std::auto_ptr<EntryArray<K, ResultType> > executeOnEntries(EntryProcessor &entryProcessor, const query::Predicate &predicate) {
832  EntryVector results = mapProxy.template executeOnEntriesData<EntryProcessor>(entryProcessor, predicate);
833 
834  return std::auto_ptr<EntryArray<K, ResultType> >(
835  new client::impl::EntryArrayImpl<K, ResultType>(results, *serializationService));
836  }
837 
845  int size() {
846  return map.size();
847  }
848 
854  bool isEmpty() {
855  return map.isEmpty();
856  }
857 
858 
869  void putAll(const std::map<K, V> &entries) {
870  map.putAll(entries);
871  }
872 
877  void clear() {
878  map.clear();
879  }
880 
881  private:
882  IMap<K, V> &map;
883  map::ClientMapProxy<K, V> &mapProxy;
884  serialization::pimpl::SerializationService *serializationService;
885  };
886  }
887  }
888 }
889 
890 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
891 #pragma warning(pop)
892 #endif
893 
894 #endif /* HAZELCAST_CLIENT_ADAPTOR_RAWPOINTERMAP_H_ */
895 
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: RawPointerMap.h:156
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:688
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: RawPointerMap.h:392
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: RawPointerMap.h:318
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:869
std::auto_ptr< ResultType > executeOnKey(const K &key, EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the entry mapped by the key.
Definition: RawPointerMap.h:785
std::auto_ptr< EntryArray< K, ResultType > > executeOnEntries(EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: RawPointerMap.h:809
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: RawPointerMap.h:468
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:663
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:766
void lock(const K &key)
Acquires the lock for the specified key.
Definition: RawPointerMap.h:296
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:253
bool containsValue(const V &value)
check if this map contains value.
Definition: RawPointerMap.h:74
std::auto_ptr< DataArray< V > > values()
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:635
bool evict(const K &key)
Evicts the specified key from this map.
Definition: RawPointerMap.h:516
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: RawPointerMap.h:418
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:704
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: RawPointerMap.h:486
void deleteEntry(const K &key)
removes entry from map.
Definition: RawPointerMap.h:148
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:170
NOTE: PagingPredicate can only be used with values(), keySet() and entries() methods!!! ...
Definition: PagingPredicate.h:127
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:228
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:199
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: RawPointerMap.h:408
bool isEmpty()
Returns true if this map contains no key-value mappings.
Definition: RawPointerMap.h:854
bool tryLock(const K &key, long timeInMillis)
Tries to acquire the lock for the specified key.
Definition: RawPointerMap.h:363
std::auto_ptr< MapEntryView< K, V > > getEntryView(const K &key)
Returns the MapEntryView for the specified key.
Definition: RawPointerMap.h:498
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: RawPointerMap.h:330
int size()
Returns the number of key-value mappings in this map.
Definition: RawPointerMap.h:845
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:877
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:242
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: RawPointerMap.h:455
std::auto_ptr< DataArray< K > > keySet()
Returns a snaphot of the deys data in the map.
Definition: RawPointerMap.h:569
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:720
std::auto_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: RawPointerMap.h:98
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:602
std::auto_ptr< EntryArray< K, ResultType > > executeOnEntries(EntryProcessor &entryProcessor, const query::Predicate &predicate)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: RawPointerMap.h:831
This is a merker class for Predicate classes.
Definition: Predicate.h:36
Concurrent, distributed, observable and queryable map client.
Definition: IMap.h:65
std::auto_ptr< EntryArray< K, V > > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: RawPointerMap.h:541
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: RawPointerMap.h:278
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: RawPointerMap.h:265
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:436
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:186
void evictAll()
Evicts all keys from this map except locked ones.
Definition: RawPointerMap.h:531
std::auto_ptr< DataArray< V > > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:649
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: RawPointerMap.h:343
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:211
void unlock(const K &key)
Releases the lock for the specified key.
Definition: RawPointerMap.h:380
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:585