Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
PagingPredicate.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_QUERY_PAGINGPREDICATE_H_
17 #define HAZELCAST_CLIENT_QUERY_PAGINGPREDICATE_H_
18 
19 #include <string>
20 #include <memory>
21 
22 #include "hazelcast/client/exception/IllegalStateException.h"
23 #include "hazelcast/client/exception/IllegalArgumentException.h"
24 #include "hazelcast/util/Util.h"
25 #include "hazelcast/util/Comparator.h"
26 #include "hazelcast/client/query/Predicate.h"
27 #include "hazelcast/client/serialization/ObjectDataOutput.h"
28 #include "hazelcast/client/serialization/ObjectDataInput.h"
29 #include "hazelcast/client/exception/IException.h"
30 #include "hazelcast/client/query/impl/predicates/PredicateDataSerializerHook.h"
31 #include "hazelcast/client/query/EntryComparator.h"
32 
33 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
34 #pragma warning(push)
35 #pragma warning(disable: 4251) //for dll export
36 #endif
37 
38 namespace hazelcast {
39  namespace client {
40  namespace query {
45  enum HAZELCAST_API IterationType {
49  KEY = 0,
53  VALUE = 1,
57  ENTRY = 2
58  };
59 
60  template<typename K, typename V>
62  public:
63  int compare(const std::pair<const K *, const V *> &lhs, const std::pair<const K *, const V *> &rhs) const {
64  assert(0);
65  return -1;
66  }
67 
68  int getFactoryId() const {
69  assert(0);
70  return -1;
71  }
72 
73  int getClassId() const {
74  assert(0);
75  return -1;
76  }
77 
79  assert(0);
80  }
81 
83  assert(0);
84  }
85  };
86 
87  static const std::string IterationNames[] = {"KEY", "VALUE", "ENTRY"};
88 
126  template<typename K, typename V>
127  class PagingPredicate : public Predicate {
128  public:
137  PagingPredicate(size_t predicatePageSize) : pageSize(predicatePageSize), page(0),iterationType(VALUE) {
138  }
139 
150  PagingPredicate(std::auto_ptr<Predicate> predicate, size_t predicatePageSize) : innerPredicate(predicate),
151  pageSize(predicatePageSize),
152  page(0),
153  iterationType(VALUE) {
154  }
155 
165  PagingPredicate(std::auto_ptr<query::EntryComparator<K, V> > comparatorObj, size_t predicatePageSize) : comparator(
166  comparatorObj), pageSize(predicatePageSize), page(0), iterationType(VALUE) {
167  }
168 
180  PagingPredicate(std::auto_ptr<Predicate> predicate, std::auto_ptr<query::EntryComparator<K, V> > comparatorObj,
181  size_t predicatePageSize) : innerPredicate(predicate), comparator(comparatorObj),
182  pageSize(predicatePageSize), page(0), iterationType(VALUE) {
183 
184  }
185 
186  ~PagingPredicate() {
187  for (typename std::vector<std::pair<size_t, std::pair<K *, V *> > >::const_iterator it = anchorList.begin();
188  it != anchorList.end(); ++it) {
189  delete it->second.first;
190  delete it->second.second;
191  }
192  }
193 
197  void reset() {
198  iterationType = VALUE;
199  for (typename std::vector<std::pair<size_t, std::pair<K *, V *> > >::const_iterator it = anchorList.begin();
200  it != anchorList.end(); ++it) {
201  delete it->second.first;
202  delete it->second.second;
203  }
204  anchorList.clear();
205  page = 0;
206  }
207 
211  void nextPage() {
212  ++page;
213  }
214 
218  void previousPage() {
219  if (page != 0) {
220  --page;
221  }
222  }
223 
224  IterationType getIterationType() const {
225  return iterationType;
226  }
227 
228  void setIterationType(IterationType type) {
229  iterationType = type;
230  }
231 
232  size_t getPage() const {
233  return page;
234  }
235 
236  void setPage(size_t pageNumber) {
237  page = pageNumber;
238  }
239 
240  size_t getPageSize() const {
241  return pageSize;
242  }
243 
244  const Predicate *getPredicate() const {
245  return innerPredicate.get();
246  }
247 
248  const query::EntryComparator<K, V> *getComparator() const {
249  return comparator.get();
250  }
251 
252 
260  const std::pair<K *, V *> *getAnchor() const {
261  if (0 == anchorList.size()) {
262  return (const std::pair<K *, V *> *)NULL;
263  }
264 
265  return &anchorList[page].second;
266  }
267 
277  const std::pair<size_t, std::pair<K *, V *> > *getNearestAnchorEntry() {
278  size_t anchorCount = anchorList.size();
279 
280  if (page == 0 || anchorCount == 0) {
281  return (const std::pair<size_t, std::pair<K *, V *> > *) NULL;
282  }
283 
284  if (page < anchorCount) {
285  return &anchorList[page - 1];
286  } else {
287  return &anchorList[anchorCount - 1];
288  }
289  }
290 
294  int getFactoryId() const {
295  return impl::predicates::F_ID;
296  }
297 
301  int getClassId() const {
302  return impl::predicates::PAGING_PREDICATE;
303  }
304 
310  out.writeObject<serialization::IdentifiedDataSerializable>(innerPredicate.get());
312  out.writeInt((int)page);
313  out.writeInt((int)pageSize);
314  out.writeUTF(&IterationNames[iterationType]);
315  out.writeInt((int) anchorList.size());
316  for (typename std::vector<std::pair<size_t, std::pair<K *, V *> > >::const_iterator it = anchorList.begin();
317  it != anchorList.end(); ++it) {
318  out.writeInt((int)it->first);
319  out.writeObject<K>(it->second.first);
320  out.writeObject<V>(it->second.second);
321  }
322  }
323 
329  // Not need to read at the client side
330  throw exception::IException("PagingPredicate::readData",
331  "Client should not need to use readData method!!!");
332  }
333 
334  void setAnchor(size_t page, const std::pair<K *, V *> &anchorEntry) {
335  size_t anchorCount = anchorList.size();
336  if (page < anchorCount) {
337  // release the previous anchoe entry
338  delete anchorList[page].second.first;
339  delete anchorList[page].second.second;
340  anchorList[page] = std::pair<size_t, std::pair<K *, V *> >(page, anchorEntry);
341  } else if (page == anchorCount) {
342  anchorList.push_back(std::pair<size_t, std::pair<K *, V *> >(page, anchorEntry));
343  } else {
344  char msg[200];
345  util::snprintf(msg, 200, "Anchor index is not correct, expected: %d but found: %d", page,
346  anchorCount);
347  throw exception::IllegalArgumentException("PagingPredicate::setAnchor", msg);
348  }
349  }
350 
351  private:
352  std::auto_ptr<Predicate> innerPredicate;
353  // key is the page number, the value is the map entry as the anchor
354  std::vector<std::pair<size_t, std::pair<K *, V *> > > anchorList;
355  std::auto_ptr<query::EntryComparator<K, V> > comparator;
356  size_t pageSize;
357  size_t page;
358  IterationType iterationType;
359  };
360  }
361  }
362 }
363 
364 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
365 #pragma warning(pop)
366 #endif
367 
368 #endif /* HAZELCAST_CLIENT_QUERY_PAGINGPREDICATE_H_ */
int getFactoryId() const
Definition: PagingPredicate.h:294
PagingPredicate(std::auto_ptr< query::EntryComparator< K, V > > comparatorObj, size_t predicatePageSize)
Construct with a comparator and pageSize results will not be filtered results will be ordered via com...
Definition: PagingPredicate.h:165
Base class for all exception originated from Hazelcast methods.
Definition: IException.h:49
void writeData(serialization::ObjectDataOutput &out) const
Defines how this class will be written.
Definition: PagingPredicate.h:309
Definition: PagingPredicate.h:61
void writeObject(const Portable *object)
Definition: ObjectDataOutput.h:181
void previousPage()
sets the page value to previous page
Definition: PagingPredicate.h:218
void writeData(serialization::ObjectDataOutput &writer) const
Defines how this class will be written.
Definition: PagingPredicate.h:78
Provides serialization methods for primitive types,a arrays of primitive types, Portable, IdentifiedDataSerializable and custom serializables.
Definition: ObjectDataOutput.h:51
Classes that will be used with hazelcast data structures like IMap, IQueue etc should either inherit ...
Definition: IdentifiedDataSerializable.h:42
NOTE: PagingPredicate can only be used with values(), keySet() and entries() methods!!! ...
Definition: PagingPredicate.h:127
void readData(serialization::ObjectDataInput &reader)
Defines how this class will be read.
Definition: PagingPredicate.h:82
PagingPredicate(std::auto_ptr< Predicate > predicate, std::auto_ptr< query::EntryComparator< K, V > > comparatorObj, size_t predicatePageSize)
Construct with an inner predicate, comparator and pageSize results will be filtered via inner predica...
Definition: PagingPredicate.h:180
int getFactoryId() const
Definition: PagingPredicate.h:68
int getClassId() const
Definition: PagingPredicate.h:73
void readData(serialization::ObjectDataInput &in)
Defines how this class will be read.
Definition: PagingPredicate.h:328
const std::pair< size_t, std::pair< K *, V * > > * getNearestAnchorEntry()
After each query, an anchor entry is set for that page.
Definition: PagingPredicate.h:277
const std::pair< K *, V * > * getAnchor() const
Retrieve the anchor object which is the last value object on the previous page.
Definition: PagingPredicate.h:260
PagingPredicate(std::auto_ptr< Predicate > predicate, size_t predicatePageSize)
Construct with an inner predicate and pageSize results will be filtered via inner predicate results w...
Definition: PagingPredicate.h:150
This is a merker class for Predicate classes.
Definition: Predicate.h:36
void nextPage()
sets the page value to next page
Definition: PagingPredicate.h:211
Provides deserialization methods for primitives types, arrays of primitive types Portable, IdentifiedDataSerializable and custom serializable types.
Definition: ObjectDataInput.h:66
int getClassId() const
Definition: PagingPredicate.h:301
void writeUTF(const std::string *value)
Definition: ObjectDataOutput.cpp:95
void writeInt(int32_t value)
Definition: ObjectDataOutput.cpp:75
PagingPredicate(size_t predicatePageSize)
Construct with a pageSize results will not be filtered results will be natural ordered throws Illegal...
Definition: PagingPredicate.h:137
Definition: EntryComparator.h:31
void reset()
resets for reuse
Definition: PagingPredicate.h:197