Hazelcast C++ Client
Hazelcast C++ Client Library
paging_predicate.h
1 /*
2  * Copyright (c) 2008-2022, 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 } // namespace codec
41 } // namespace protocol
42 class imap;
43 namespace query {
45 {};
46 
51 enum struct HAZELCAST_API iteration_type
52 {
56  KEY = 0,
60  VALUE = 1,
64  ENTRY = 2
65 };
66 
67 static const std::string IterationNames[] = { "KEY", "VALUE", "ENTRY" };
68 
70 {
71  std::vector<int32_t> page_list;
72  std::vector<std::pair<serialization::pimpl::data,
73  boost::optional<serialization::pimpl::data>>>
74  data_list;
75 };
76 
115 template<typename K, typename V>
117  : public predicate
118  , public paging_predicate_marker
119 {
120  friend imap;
121  friend protocol::codec::holder::paging_predicate_holder;
123 
124 public:
125  ~paging_predicate() = default;
126 
130  void reset()
131  {
132  iteration_type_ = iteration_type::VALUE;
133  anchor_data_list_.page_list.clear();
134  anchor_data_list_.data_list.clear();
135  page_ = 0;
136  }
137 
141  void next_page() { ++page_; }
142 
147  {
148  if (page_ != 0) {
149  --page_;
150  }
151  }
152 
153  iteration_type get_iteration_type() const { return iteration_type_; }
154 
155  void set_iteration_type(iteration_type type) { iteration_type_ = type; }
156 
157  size_t get_page() const { return page_; }
158 
159  void set_page(size_t page_number) { page_ = page_number; }
160 
161  size_t get_page_size() const { return page_size_; }
162 
163  const query::entry_comparator<K, V>* get_comparator() const
164  {
165  return comparator_.get();
166  }
167 
168  void set_anchor_data_list(anchor_data_list anchor_data_list)
169  {
170  anchor_data_list_ = std::move(anchor_data_list);
171  }
172 
173 private:
174  anchor_data_list anchor_data_list_;
175  std::shared_ptr<query::entry_comparator<K, V>> comparator_;
176  serialization::object_data_output out_stream_;
177  size_t page_size_;
178  size_t page_;
179  iteration_type iteration_type_;
180  boost::optional<serialization::pimpl::data> comparator_data_;
181  boost::optional<serialization::pimpl::data> predicate_data_;
182 
192  paging_predicate(
193  serialization::pimpl::SerializationService& serialization_service,
194  size_t predicate_page_size)
195  : out_stream_(serialization_service.new_output_stream())
196  , page_size_(predicate_page_size)
197  , page_(0)
198  , iteration_type_(iteration_type::VALUE)
199  {
200  out_stream_.write_object<bool>(nullptr);
201  out_stream_.write_object<bool>(nullptr);
202  }
203 
216  template<typename INNER_PREDICATE>
217  paging_predicate(
218  serialization::pimpl::SerializationService& serialization_service,
219  size_t predicate_page_size,
220  const INNER_PREDICATE& predicate)
221  : out_stream_(serialization_service.new_output_stream())
222  , page_size_(predicate_page_size)
223  , page_(0)
224  , iteration_type_(iteration_type::VALUE)
225  {
226  out_stream_.write_object(predicate);
227  out_stream_.write_object<bool>(nullptr);
228  predicate_data_ =
229  serialization_service.to_data<INNER_PREDICATE>(predicate);
230  }
231 
242  template<typename COMPARATOR>
243  paging_predicate(
244  serialization::pimpl::SerializationService& serialization_service,
245  COMPARATOR&& comp,
246  size_t predicate_page_size)
247  : out_stream_(serialization_service.new_output_stream())
248  , page_size_(predicate_page_size)
249  , page_(0)
250  , iteration_type_(iteration_type::VALUE)
251  {
252  out_stream_.write_object<bool>(nullptr);
253  out_stream_.write_object(comp);
254  comparator_data_ = serialization_service.to_data<COMPARATOR>(comp);
255  comparator_ =
256  std::make_shared<COMPARATOR>(std::forward<COMPARATOR>(comp));
257  }
258 
272  template<typename INNER_PREDICATE, typename COMPARATOR>
273  paging_predicate(
274  serialization::pimpl::SerializationService& serialization_service,
275  const INNER_PREDICATE& predicate,
276  COMPARATOR&& comp,
277  size_t predicate_page_size)
278  : out_stream_(serialization_service.new_output_stream())
279  , page_size_(predicate_page_size)
280  , page_(0)
281  , iteration_type_(iteration_type::VALUE)
282  {
283  out_stream_.write_object(predicate);
284  out_stream_.write_object(comp);
285  predicate_data_ =
286  serialization_service.to_data<INNER_PREDICATE>(predicate);
287  comparator_data_ = serialization_service.to_data<COMPARATOR>(comp);
288  comparator_ =
289  std::make_shared<COMPARATOR>(std::forward<COMPARATOR>(comp));
290  }
291 };
292 } // namespace query
293 
294 namespace serialization {
295 template<typename K, typename V>
296 struct hz_serializer<query::paging_predicate<K, V>>
298 {
302  static constexpr int32_t get_factory_id() noexcept
303  {
304  return static_cast<int32_t>(
305  query::predicate_data_serializer_hook::F_ID);
306  }
307 
311  static constexpr int32_t get_class_id() noexcept
312  {
313  return static_cast<int32_t>(
314  query::predicate_data_serializer_hook::PAGING_PREDICATE);
315  }
316 
322  object_data_output& out)
323  {
324  out.write_bytes(obj.outStream.toByteArray());
325  out.write<int32_t>((int32_t)obj.page);
326  out.write<int32_t>((int32_t)obj.pageSize);
327  out.write<std::string>(
328  obj.IterationNames[static_cast<int32_t>(obj.iterationType)]);
329  out.write<int32_t>((int32_t)obj.anchor_data_list_.data_list.size());
330  const auto& data_list = obj.anchor_data_list_.data_list;
331  const auto& page_list = obj.anchor_data_list_.page_list;
332  for (size_t i = 0; i < obj.anchor_data_list_.data_list.size(); ++i) {
333  out.write<int32_t>(page_list[i]);
334  out.write_object<K>(data_list[i].first);
335  out.write_object<V>(data_list[i].second);
336  }
337  }
338 
343  {
344  // Not need to read at the client side
345  BOOST_THROW_EXCEPTION(exception::hazelcast_serialization(
346  "readData", "Client should not need to use readdata method!!!"));
347  }
348 };
349 
350 } // namespace serialization
351 } // namespace client
352 } // namespace hazelcast
353 
354 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
355 #pragma warning(pop)
356 #endif
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:42
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 get_cla...