Hazelcast C++ Client
Hazelcast C++ Client Library
ringbuffer.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 "hazelcast/client/proxy/RingbufferImpl.h"
19 
20 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
21 #pragma warning(push)
22 #pragma warning(disable : 4251) // for dll export
23 #endif
24 
25 namespace hazelcast {
26 namespace client {
65 class HAZELCAST_API ringbuffer : public proxy::RingbufferImpl
66 {
67  friend class spi::ProxyManager;
68 
69 public:
92  template<typename E>
93  boost::future<int64_t> add(const E& item)
94  {
95  return add_data(to_data(item));
96  }
97 
136  template<typename E>
137  boost::future<boost::optional<E>> read_one(int64_t sequence)
138  {
139  return to_object<E>(read_one_data(sequence));
140  }
141 
172  template<typename E>
173  boost::future<int64_t> add(const E& item,
174  rb::overflow_policy overflow_policy)
175  {
176  return addData(to_data(item), overflow_policy);
177  }
178 
197  template<typename E>
198  boost::future<int64_t> add_all(const std::vector<E>& items,
199  rb::overflow_policy overflow_policy)
200  {
201  return add_all_data(to_data_collection(items), overflow_policy);
202  }
203 
243  template<typename IFUNCTION>
244  boost::future<rb::read_result_set> read_many(
245  int64_t start_sequence,
246  int32_t min_count,
247  int32_t max_count,
248  const IFUNCTION* filter = nullptr)
249  {
250  auto filter_data = to_data<IFUNCTION>(filter);
251  return read_many_data(
252  start_sequence, min_count, max_count, &filter_data)
253  .then(boost::launch::sync,
254  [=](boost::future<protocol::ClientMessage> f) {
255  return get_result_set(std::move(f));
256  });
257  }
258 
259  rb::read_result_set get_result_set(boost::future<protocol::ClientMessage> f)
260  {
261  auto msg = f.get();
262  auto* initial_frame =
263  reinterpret_cast<protocol::ClientMessage::frame_header_type*>(
264  msg.rd_ptr(protocol::ClientMessage::RESPONSE_HEADER_LEN));
265  auto read_count = msg.get<int32_t>();
266  auto next_seq = msg.get<int64_t>();
267  msg.rd_ptr(static_cast<int32_t>(initial_frame->frame_len) -
268  protocol::ClientMessage::RESPONSE_HEADER_LEN -
269  protocol::ClientMessage::INT32_SIZE -
270  protocol::ClientMessage::INT64_SIZE);
271 
272  auto datas = msg.get<std::vector<serialization::pimpl::data>>();
273  auto item_seqs = msg.get_nullable<std::vector<int64_t>>();
274  return rb::read_result_set(read_count,
275  std::move(datas),
276  get_serialization_service(),
277  item_seqs,
278  next_seq);
279  }
280 
281  boost::future<rb::read_result_set> read_many(int64_t start_sequence,
282  int32_t min_count,
283  int32_t max_count)
284  {
285  return read_many_data(start_sequence, min_count, max_count, nullptr)
286  .then(boost::launch::sync,
287  [=](boost::future<protocol::ClientMessage> f) {
288  return get_result_set(std::move(f));
289  });
290  }
291 
292 private:
293  ringbuffer(const std::string& object_name, spi::ClientContext* context)
294  : RingbufferImpl(object_name, context)
295  {}
296 };
297 } // namespace client
298 } // namespace hazelcast
299 
300 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
301 #pragma warning(pop)
302 #endif
A Ringbuffer is a data-structure where the content is stored in a ring like structure.
Definition: ringbuffer.h:66
boost::future< int64_t > add_all(const std::vector< E > &items, rb::overflow_policy overflow_policy)
Adds all the items of a collection to the tail of the Ringbuffer.
Definition: ringbuffer.h:198
boost::future< rb::read_result_set > read_many(int64_t start_sequence, int32_t min_count, int32_t max_count, const IFUNCTION *filter=nullptr)
Reads a batch of items from the Ringbuffer.
Definition: ringbuffer.h:244
boost::future< int64_t > add(const E &item, rb::overflow_policy overflow_policy)
Asynchronously writes an item with a configurable {}.
Definition: ringbuffer.h:173
boost::future< int64_t > add(const E &item)
Adds an item to the tail of the Ringbuffer.
Definition: ringbuffer.h:93
boost::future< boost::optional< E > > read_one(int64_t sequence)
Reads one item from the Ringbuffer.
Definition: ringbuffer.h:137