Hazelcast C++ Client
Hazelcast C++ Client Library
paging_predicate.h
1 /*
2  * Copyright (c) 2008-2021, 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 #pragma once
17 
18 #include <string>
19 #include <memory>
20 #include <cassert>
21 
22 #include "hazelcast/client/exception/protocol_exceptions.h"
23 #include "hazelcast/util/Util.h"
24 #include "hazelcast/util/Comparator.h"
25 #include "hazelcast/client/query/predicates.h"
26 #include "hazelcast/client/query/entry_comparator.h"
27 
28 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
29 #pragma warning(push)
30 #pragma warning(disable: 4251) //for dll export
31 #endif
32 
33 namespace hazelcast {
34  namespace client {
35  namespace protocol {
36  namespace codec {
37  namespace holder {
38  struct paging_predicate_holder;
39  }
40  }
41  }
42  class imap;
43  namespace query {
45 
50  enum struct HAZELCAST_API iteration_type {
54  KEY = 0,
58  VALUE = 1,
62  ENTRY = 2
63  };
64 
65  static const std::string IterationNames[] = {"KEY", "VALUE", "ENTRY"};
66 
68  std::vector<int32_t> page_list;
69  std::vector<std::pair<serialization::pimpl::data, boost::optional<serialization::pimpl::data>>> data_list;
70  };
71 
108  template<typename K, typename V>
110  friend imap;
111  friend protocol::codec::holder::paging_predicate_holder;
113  public:
114  ~paging_predicate() = default;
115 
119  void reset() {
120  iteration_type_ = iteration_type::VALUE;
121  anchor_data_list_.page_list.clear();
122  anchor_data_list_.data_list.clear();
123  page_ = 0;
124  }
125 
129  void next_page() {
130  ++page_;
131  }
132 
136  void previous_page() {
137  if (page_ != 0) {
138  --page_;
139  }
140  }
141 
142  iteration_type get_iteration_type() const {
143  return iteration_type_;
144  }
145 
146  void set_iteration_type(iteration_type type) {
147  iteration_type_ = type;
148  }
149 
150  size_t get_page() const {
151  return page_;
152  }
153 
154  void set_page(size_t page_number) {
155  page_ = page_number;
156  }
157 
158  size_t get_page_size() const {
159  return page_size_;
160  }
161 
162  const query::entry_comparator<K, V> *get_comparator() const {
163  return comparator_.get();
164  }
165 
166  void set_anchor_data_list(anchor_data_list anchor_data_list) {
167  anchor_data_list_ = std::move(anchor_data_list);
168  }
169 
170  private:
171  anchor_data_list anchor_data_list_;
172  std::shared_ptr<query::entry_comparator<K, V>> comparator_;
173  serialization::object_data_output out_stream_;
174  size_t page_size_;
175  size_t page_;
176  iteration_type iteration_type_;
177  boost::optional<serialization::pimpl::data> comparator_data_;
178  boost::optional<serialization::pimpl::data> predicate_data_;
179 
188  paging_predicate(serialization::pimpl::SerializationService &serialization_service,
189  size_t predicate_page_size) : out_stream_(serialization_service.new_output_stream()),
190  page_size_(predicate_page_size), page_(0), iteration_type_(iteration_type::VALUE) {
191  out_stream_.write_object<bool>(nullptr);
192  out_stream_.write_object<bool>(nullptr);
193  }
194 
205  template<typename INNER_PREDICATE>
206  paging_predicate(serialization::pimpl::SerializationService &serialization_service,
207  size_t predicate_page_size, const INNER_PREDICATE &predicate)
208  : out_stream_(serialization_service.new_output_stream()), page_size_(predicate_page_size), page_(0),
209  iteration_type_(iteration_type::VALUE) {
210  out_stream_.write_object(predicate);
211  out_stream_.write_object<bool>(nullptr);
212  predicate_data_ = serialization_service.to_data<INNER_PREDICATE>(predicate);
213  }
214 
224  template<typename COMPARATOR>
225  paging_predicate(serialization::pimpl::SerializationService &serialization_service,
226  COMPARATOR &&comp, size_t predicate_page_size)
227  : out_stream_(serialization_service.new_output_stream()), page_size_(predicate_page_size), page_(0),
228  iteration_type_(iteration_type::VALUE) {
229  out_stream_.write_object<bool>(nullptr);
230  out_stream_.write_object(comp);
231  comparator_data_ = serialization_service.to_data<COMPARATOR>(comp);
232  comparator_ = std::make_shared<COMPARATOR>(std::forward<COMPARATOR>(comp));
233  }
234 
246  template<typename INNER_PREDICATE, typename COMPARATOR>
247  paging_predicate(serialization::pimpl::SerializationService &serialization_service,
248  const INNER_PREDICATE &predicate, COMPARATOR &&comp, size_t predicate_page_size)
249  : out_stream_(serialization_service.new_output_stream()), page_size_(predicate_page_size), page_(0),
250  iteration_type_(iteration_type::VALUE) {
251  out_stream_.write_object(predicate);
252  out_stream_.write_object(comp);
253  predicate_data_ = serialization_service.to_data<INNER_PREDICATE>(predicate);
254  comparator_data_ = serialization_service.to_data<COMPARATOR>(comp);
255  comparator_ = std::make_shared<COMPARATOR>(std::forward<COMPARATOR>(comp));
256  }
257  };
258  }
259 
260  namespace serialization {
261  template<typename K, typename V>
262  struct hz_serializer<query::paging_predicate<K, V>> : public identified_data_serializer {
266  static constexpr int32_t get_factory_id() noexcept {
267  return static_cast<int32_t>(query::predicate_data_serializer_hook::F_ID);
268  }
269 
273  static constexpr int32_t get_class_id() noexcept {
274  return static_cast<int32_t>(query::predicate_data_serializer_hook::PAGING_PREDICATE);
275  }
276 
282  out.write_bytes(obj.outStream.toByteArray());
283  out.write<int32_t>((int32_t) obj.page);
284  out.write<int32_t>((int32_t) obj.pageSize);
285  out.write<std::string>(obj.IterationNames[static_cast<int32_t>(obj.iterationType)]);
286  out.write<int32_t>((int32_t) obj.anchor_data_list_.data_list.size());
287  const auto &data_list = obj.anchor_data_list_.data_list;
288  const auto &page_list = obj.anchor_data_list_.page_list;
289  for (size_t i = 0; i < obj.anchor_data_list_.data_list.size(); ++i) {
290  out.write<int32_t>(page_list[i]);
291  out.write_object<K>(data_list[i].first);
292  out.write_object<V>(data_list[i].second);
293  }
294  }
295 
300  // Not need to read at the client side
301  BOOST_THROW_EXCEPTION(exception::hazelcast_serialization("readData",
302  "Client should not need to use readdata method!!!"));
303  }
304  };
305 
306  }
307  }
308 }
309 
310 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
311 #pragma warning(pop)
312 #endif
313 
314 
Concurrent, distributed, observable and queryable map client.
Definition: imap.h:63
NOTE: paging_predicate can only be used with values(), keySet() and entries() methods!...
void next_page()
sets the page value to next page
void previous_page()
sets the page value to previous page
This is a marker class for Predicate classes.
Definition: predicates.h:39
static query::paging_predicate< K, V > read_data(object_data_input &in)
Should not be called at the client side!
static void write_data(const query::paging_predicate< K, V > &obj, object_data_output &out)
Defines how this class will be written.
Classes derived from this class should implement the following static methods: static int32_t getClas...