Hazelcast C++ Client
 All Classes Functions Variables Enumerations Pages
RawPointerMap.h
1 /*
2  * Copyright (c) 2008-2015, 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 
26 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
27 #pragma warning(push)
28 #pragma warning(disable: 4251) //for dll export
29 #endif
30 
31 namespace hazelcast {
32  namespace client {
33  namespace adaptor {
49  template<typename K, typename V>
50  class RawPointerMap {
51  public:
52  RawPointerMap(IMap<K, V> &mapToBeAdopted) : map(mapToBeAdopted), serializationService(
53  map.context->getSerializationService()) {
54  }
55 
62  bool containsKey(const K &key) {
63  return map.containsKey(key);
64  }
65 
72  bool containsValue(const V &value) {
73  return map.containsValue(value);
74  }
75 
83  std::auto_ptr<V> get(const K &key) {
84  return serializationService.toObject<V>(map.getData(serializationService.toData<K>(&key)).get());
85  }
86 
95  std::auto_ptr<V> put(const K &key, const V &value) {
96  return serializationService.toObject<V>(
97  map.putData(serializationService.toData<K>(&key),
98  serializationService.toData<V>(&value)).get());
99  }
100 
108  std::auto_ptr<V> remove(const K &key) {
109  return serializationService.toObject<V>(map.removeData(serializationService.toData<K>(&key)).get());
110  }
111 
119  bool remove(const K &key, const V &value) {
120  return map.remove(key, value);
121  }
122 
129  void deleteEntry(const K &key) {
130  map.deleteEntry(key);
131  }
132 
137  void flush() {
138  map.flush();
139  }
140 
151  bool tryRemove(const K &key, long timeoutInMillis) {
152  return map.tryRemove(key, timeoutInMillis);
153  }
154 
167  bool tryPut(const K &key, const V &value, long timeoutInMillis) {
168  return map.tryPut(key, value, timeoutInMillis);
169  }
170 
182  std::auto_ptr<V> put(const K &key, const V &value, long ttlInMillis) {
183  return serializationService.toObject<V>(
184  map.putData(serializationService.toData<K>(&key), serializationService.toData<V>(&value),
185  ttlInMillis).get());
186  }
187 
197  void putTransient(const K &key, const V &value, long ttlInMillis) {
198  map.putTransient(key, value, ttlInMillis);
199  }
200 
209  std::auto_ptr<V> putIfAbsent(const K &key, const V &value) {
210  return serializationService.toObject<V>(map.putIfAbsentData(serializationService.toData<K>(&key),
211  serializationService.toData<V>(&value),
212  -1).get());
213  }
214 
226  std::auto_ptr<V> putIfAbsent(const K &key, const V &value, long ttlInMillis) {
227  return serializationService.toObject<V>(
228  map.putIfAbsentData(serializationService.toData<K>(&key),
229  serializationService.toData<V>(&value),
230  ttlInMillis).get());
231  }
232 
240  bool replace(const K &key, const V &oldValue, const V &newValue) {
241  return map.replace(key, oldValue, newValue);
242  }
243 
251  std::auto_ptr<V> replace(const K &key, const V &value) {
252  return serializationService.toObject<V>(
253  map.replaceData(serializationService.toData<K>(&key), serializationService.toData<V>(&value)).get());
254  }
255 
265  void set(const K &key, const V &value, long ttl) {
266  map.set(key, value, ttl);
267  }
268 
283  void lock(const K &key) {
284  map.lock(key);
285  }
286 
305  void lock(const K &key, long leaseTime) {
306  map.lock(key, leaseTime);
307  }
308 
317  bool isLocked(const K &key) {
318  return map.isLocked(key);
319  }
320 
330  bool tryLock(const K &key) {
331  return map.tryLock(key, 0);
332  }
333 
350  bool tryLock(const K &key, long timeInMillis) {
351  return map.tryLock(key, timeInMillis);
352  }
353 
367  void unlock(const K &key) {
368  map.unlock(key);
369  }
370 
379  void forceUnlock(const K &key) {
380  map.forceUnlock(key);
381  }
382 
394  template<typename MapInterceptor>
395  std::string addInterceptor(MapInterceptor &interceptor) {
396  return map.template addInterceptor<MapInterceptor>(interceptor);
397  }
398 
405  void removeInterceptor(const std::string &id) {
406  map.removeInterceptor(id);
407  }
408 
423  std::string addEntryListener(EntryListener<K, V> &listener, bool includeValue) {
424  return map.addEntryListener(listener, includeValue);
425  }
426 
442  std::string addEntryListener(EntryListener<K, V> &listener, const query::Predicate &predicate, bool includeValue) {
443  return map.addEntryListener(listener, predicate, includeValue);
444  }
445 
455  bool removeEntryListener(const std::string &registrationId) {
456  return map.removeEntryListener(registrationId);
457  }
458 
459 
473  std::string addEntryListener(EntryListener<K, V> &listener, const K &key, bool includeValue) {
474  return map.addEntryListener(listener, key, includeValue);
475  }
476 
485  std::auto_ptr<MapEntryView<K, V> > getEntryView(const K &key) {
486  std::auto_ptr<map::DataEntryView> dataView = map.getEntryViewData(serializationService.toData<K>(&key));
487  return std::auto_ptr<MapEntryView<K, V> >(new MapEntryView<K, V>(dataView, serializationService));
488  }
489 
500  bool evict(const K &key) {
501  return map.evict(key);
502  }
503 
515  void evictAll() {
516  map.evictAll();
517  }
518 
525  std::auto_ptr<EntryArray<K, V> > getAll(const std::set<K> &keys) {
526  std::vector<serialization::pimpl::Data> allKeys(keys.size());
527  int i = 0;
528  for (typename std::set<K>::iterator it = keys.begin(); it != keys.end(); ++it) {
529  allKeys[i++] = serializationService.toData<K>(&(*it));
530  }
531  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > entrySet = map.getAllData(
532  allKeys);
533 
534  return std::auto_ptr<EntryArray<K, V> >(new client::impl::EntryArrayImpl<K, V>(entrySet, serializationService));
535  }
536 
544  std::auto_ptr<DataArray<K> > keySet() {
545  std::vector<serialization::pimpl::Data> dataResult = map.keySetData();
546  return std::auto_ptr<DataArray<K> >(new client::impl::DataArrayImpl<K>(dataResult, serializationService));
547  }
548 
559  std::auto_ptr<DataArray<K> > keySet(const query::Predicate &predicate) {
560  std::vector<serialization::pimpl::Data> dataResult = map.keySetData(predicate);
561  return std::auto_ptr<DataArray<K> >(new client::impl::DataArrayImpl<K>(dataResult, serializationService));
562  }
563 
575  std::auto_ptr<DataArray<K> > keySet(query::PagingPredicate<K, V> &predicate) {
576  predicate.setIterationType(query::KEY);
577 
578  std::vector<serialization::pimpl::Data> dataResult = map.keySetForPagingPredicateData(predicate);
579 
580  EntryVector entryResult;
581  for (std::vector<serialization::pimpl::Data>::iterator it = dataResult.begin();it != dataResult.end(); ++it) {
582  entryResult.push_back(std::pair<serialization::pimpl::Data, serialization::pimpl::Data>(*it, serialization::pimpl::Data()));
583  }
584 
585  client::impl::EntryArrayImpl<K, V> entries(entryResult, serializationService);
586 
587  entries.sort(query::KEY, predicate.getComparator());
588 
589  std::pair<size_t, size_t> range = map.template updateAnchor<K, V>(entries, predicate, query::KEY);
590 
591  std::auto_ptr<EntryArray<K, V> > subList(new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
592 
593  std::auto_ptr<DataArray<K> > result = std::auto_ptr<DataArray<K> >(new impl::EntryArrayKeyAdaptor<K, V>(subList));
594 
595  return result;
596  }
597 
605  std::auto_ptr<DataArray<V> > values() {
606  std::vector<serialization::pimpl::Data> dataResult = map.valuesData();
607  return std::auto_ptr<DataArray<V> >(new client::impl::DataArrayImpl<V>(dataResult, serializationService));
608  }
609 
618  std::auto_ptr<DataArray<V> > values(const query::Predicate &predicate) {
619  std::vector<serialization::pimpl::Data> dataResult = map.valuesData(predicate);
620  return std::auto_ptr<DataArray<V> >(new client::impl::DataArrayImpl<V>(dataResult, serializationService));
621  }
622 
631  std::auto_ptr<DataArray<V> > values(query::PagingPredicate<K, V> &predicate) {
632  predicate.setIterationType(query::VALUE);
633 
634  EntryVector entryResult = map.valuesForPagingPredicateData(predicate);
635 
636  client::impl::EntryArrayImpl<K, V> entries(entryResult, serializationService);
637 
638  entries.sort(query::VALUE, predicate.getComparator());
639 
640  std::pair<size_t, size_t> range = map.template updateAnchor<K, V>(entries, predicate, query::VALUE);
641 
642  std::auto_ptr<EntryArray<K, V> > subList(new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
643  std::auto_ptr<DataArray<V> > result = std::auto_ptr<DataArray<V> >(new impl::EntryArrayValueAdaptor<K, V>(subList));
644 
645  return result;
646  }
647 
655  std::auto_ptr<EntryArray<K, V> > entrySet() {
656  EntryVector entries = map.entrySetData();
657  return std::auto_ptr<EntryArray<K, V> >(new client::impl::EntryArrayImpl<K, V>(entries, serializationService));
658  }
659 
670  std::auto_ptr<EntryArray<K, V> > entrySet(const query::Predicate &predicate) {
671  EntryVector entries = map.entrySetData(predicate);
672  return std::auto_ptr<EntryArray<K, V> >(new client::impl::EntryArrayImpl<K, V>(entries, serializationService));
673  }
674 
685  std::auto_ptr<EntryArray<K, V> > entrySet(query::PagingPredicate<K, V> &predicate) {
686  std::vector<std::pair<serialization::pimpl::Data, serialization::pimpl::Data> > dataResult =
687  map.entrySetForPagingPredicateData(predicate);
688 
689  client::impl::EntryArrayImpl<K, V> entries(dataResult, map.context->getSerializationService());
690 
691  entries.sort(query::ENTRY, predicate.getComparator());
692 
693  std::pair<size_t, size_t> range = map.template updateAnchor<K, V>(entries, predicate, query::ENTRY);
694 
695  return std::auto_ptr<EntryArray<K, V> >(new client::impl::EntryArrayImpl<K, V>(entries, range.first, range.second));
696  }
697 
730  void addIndex(const std::string &attribute, bool ordered) {
731  map.addIndex(attribute, ordered);
732  }
733 
748  template<typename ResultType, typename EntryProcessor>
749  std::auto_ptr<ResultType> executeOnKey(const K &key, EntryProcessor &entryProcessor) {
750  std::auto_ptr<serialization::pimpl::Data> resultData =
751  map.template executeOnKeyData<K, EntryProcessor>(key, entryProcessor);
752  return serializationService.toObject<ResultType>(resultData.get());
753  }
754 
769  template<typename ResultType, typename EntryProcessor>
770  std::auto_ptr<EntryArray<K, ResultType> > executeOnEntries(EntryProcessor &entryProcessor) {
771  EntryVector results = map.template executeOnEntriesData<EntryProcessor>(entryProcessor);
772 
773  return std::auto_ptr<EntryArray<K, ResultType> >(new client::impl::EntryArrayImpl<K, ResultType>(results, serializationService));
774  }
775 
790  template<typename ResultType, typename EntryProcessor>
791  std::auto_ptr<EntryArray<K, ResultType> > executeOnEntries(EntryProcessor &entryProcessor, const query::Predicate &predicate) {
792  EntryVector results = map.template executeOnEntriesData<EntryProcessor>(entryProcessor, predicate);
793 
794  return std::auto_ptr<EntryArray<K, ResultType> >(new client::impl::EntryArrayImpl<K, ResultType>(results, serializationService));
795  }
796 
804  void set(const K &key, const V &value) {
805  set(key, value, -1);
806  }
807 
815  int size() {
816  return map.size();
817  }
818 
824  bool isEmpty() {
825  return map.isEmpty();
826  }
827 
828 
839  void putAll(const std::map<K, V> &entries) {
840  map.putAll(entries);
841  }
842 
847  void clear() {
848  map.clear();
849  }
850 
851 
852  private:
853  IMap<K, V> &map;
854  serialization::pimpl::SerializationService &serializationService;
855  };
856  }
857  }
858 }
859 
860 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
861 #pragma warning(pop)
862 #endif
863 
864 #endif /* HAZELCAST_CLIENT_ADAPTOR_RAWPOINTERMAP_H_ */
865 
void flush()
If this map has a MapStore this method flushes all the local dirty entries by calling MapStore...
Definition: RawPointerMap.h:137
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:655
void forceUnlock(const K &key)
Releases the lock for the specified key regardless of the lock owner.
Definition: RawPointerMap.h:379
void lock(const K &key, long leaseTime)
Acquires the lock for the specified key for the specified lease time.
Definition: RawPointerMap.h:305
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:839
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:749
std::auto_ptr< EntryArray< K, ResultType > > executeOnEntries(EntryProcessor &entryProcessor)
Applies the user defined EntryProcessor to the all entries in the map.
Definition: RawPointerMap.h:770
bool removeEntryListener(const std::string &registrationId)
Removes the specified entry listener Returns silently if there is no such listener added before...
Definition: RawPointerMap.h:455
bool containsKey(const K &key)
check if this map contains key.
Definition: RawPointerMap.h:62
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:631
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:730
void lock(const K &key)
Acquires the lock for the specified key.
Definition: RawPointerMap.h:283
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:251
bool containsValue(const V &value)
check if this map contains value.
Definition: RawPointerMap.h:72
std::auto_ptr< DataArray< V > > values()
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:605
bool evict(const K &key)
Evicts the specified key from this map.
Definition: RawPointerMap.h:500
void removeInterceptor(const std::string &id)
Removes the given interceptor for this map.
Definition: RawPointerMap.h:405
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:670
std::string addEntryListener(EntryListener< K, V > &listener, const K &key, bool includeValue)
Adds the specified entry listener for the specified key.
Definition: RawPointerMap.h:473
void deleteEntry(const K &key)
removes entry from map.
Definition: RawPointerMap.h:129
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:151
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:226
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:197
std::string addInterceptor(MapInterceptor &interceptor)
Adds an interceptor for this map.
Definition: RawPointerMap.h:395
bool isEmpty()
Returns true if this map contains no key-value mappings.
Definition: RawPointerMap.h:824
bool tryLock(const K &key, long timeInMillis)
Tries to acquire the lock for the specified key.
Definition: RawPointerMap.h:350
std::auto_ptr< MapEntryView< K, V > > getEntryView(const K &key)
Returns the MapEntryView for the specified key.
Definition: RawPointerMap.h:485
bool isLocked(const K &key)
Checks the lock for the specified key.
Definition: RawPointerMap.h:317
int size()
Returns the number of key-value mappings in this map.
Definition: RawPointerMap.h:815
Adaptor class to IMap which provides releasable raw pointers for returned objects.
Definition: RawPointerMap.h:50
void clear()
Removes all of the mappings from this map (optional operation).
Definition: RawPointerMap.h:847
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:240
std::string addEntryListener(EntryListener< K, V > &listener, const query::Predicate &predicate, bool includeValue)
Adds an entry listener for this map.
Definition: RawPointerMap.h:442
std::auto_ptr< DataArray< K > > keySet()
Returns a snaphot of the deys data in the map.
Definition: RawPointerMap.h:544
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:685
std::auto_ptr< V > put(const K &key, const V &value)
put new entry into map.
Definition: RawPointerMap.h:95
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:575
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:791
This is a merker class for Predicate classes.
Definition: Predicate.h:36
Concurrent, distributed, observable and queryable map client.
Definition: IMap.h:58
std::auto_ptr< EntryArray< K, V > > getAll(const std::set< K > &keys)
Returns the entries for the given keys.
Definition: RawPointerMap.h:525
void set(const K &key, const V &value, long ttl)
Puts an entry into this map.
Definition: RawPointerMap.h:265
void set(const K &key, const V &value)
Puts an entry into this map.
Definition: RawPointerMap.h:804
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:182
std::string addEntryListener(EntryListener< K, V > &listener, bool includeValue)
Adds an entry listener for this map.
Definition: RawPointerMap.h:423
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:167
void evictAll()
Evicts all keys from this map except locked ones.
Definition: RawPointerMap.h:515
std::auto_ptr< DataArray< V > > values(const query::Predicate &predicate)
Returns a vector clone of the values contained in this map.
Definition: RawPointerMap.h:618
bool tryLock(const K &key)
Tries to acquire the lock for the specified key.
Definition: RawPointerMap.h:330
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:209
void unlock(const K &key)
Releases the lock for the specified key.
Definition: RawPointerMap.h:367
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:559