Hazelcast C++ Client
Hazelcast C++ Client Library
ringbuffer.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 "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 {
59  class HAZELCAST_API ringbuffer : public proxy::RingbufferImpl {
60  friend class spi::ProxyManager;
61  public:
79  template<typename E>
80  boost::future<int64_t> add(const E &item) {
81  return add_data(to_data(item));
82  }
83 
117  template<typename E>
118  boost::future<boost::optional<E>> read_one(int64_t sequence) {
119  return to_object<E>(read_one_data(sequence));
120  }
121 
154  template<typename E>
155  boost::future<int64_t> add(const E &item, rb::overflow_policy overflow_policy) {
156  return addData(to_data(item), overflow_policy);
157  }
158 
184  template<typename E>
185  boost::future<int64_t>
186  add_all(const std::vector<E> &items, rb::overflow_policy overflow_policy) {
187  return add_all_data(to_data_collection(items), overflow_policy);
188  }
189 
229  template<typename IFUNCTION>
230  boost::future<rb::read_result_set>
231  read_many(int64_t start_sequence, int32_t min_count, int32_t max_count, const IFUNCTION *filter = nullptr) {
232  auto filter_data = to_data<IFUNCTION>(filter);
233  return read_many_data(start_sequence, min_count, max_count, &filter_data).then(boost::launch::sync, [=] (boost::future<protocol::ClientMessage> f) {
234  return get_result_set(std::move(f));
235  });
236  }
237 
238  rb::read_result_set get_result_set(boost::future<protocol::ClientMessage> f) {
239  auto msg = f.get();
240  auto *initial_frame = reinterpret_cast<protocol::ClientMessage::frame_header_t *>(msg.rd_ptr(
241  protocol::ClientMessage::RESPONSE_HEADER_LEN));
242  auto read_count = msg.get<int32_t>();
243  auto next_seq = msg.get<int64_t>();
244  msg.rd_ptr(
245  static_cast<int32_t>(initial_frame->frame_len) - protocol::ClientMessage::RESPONSE_HEADER_LEN -
246  protocol::ClientMessage::INT32_SIZE -
247  protocol::ClientMessage::INT64_SIZE);
248 
249  auto datas = msg.get<std::vector<serialization::pimpl::data>>();
250  auto item_seqs = msg.get_nullable<std::vector<int64_t>>();
251  return rb::read_result_set(read_count, std::move(datas), get_serialization_service(), item_seqs,
252  next_seq);
253  }
254 
255  boost::future<rb::read_result_set>
256  read_many(int64_t start_sequence, int32_t min_count, int32_t max_count) {
257  return read_many_data(start_sequence, min_count, max_count, nullptr).then(boost::launch::sync, [=] (boost::future<protocol::ClientMessage> f) {
258  return get_result_set(std::move(f));
259  });
260  }
261 
262  private:
263  ringbuffer(const std::string &object_name, spi::ClientContext *context) : RingbufferImpl(object_name,
264  context) {}
265  };
266  }
267 }
268 
269 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
270 #pragma warning(pop)
271 #endif
272 
A Ringbuffer is a data-structure where the content is stored in a ring like structure.
Definition: ringbuffer.h:59
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:186
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:231
boost::future< int64_t > add(const E &item, rb::overflow_policy overflow_policy)
Asynchronously writes an item with a configurable rb::OverflowPolicy.
Definition: ringbuffer.h:155
boost::future< int64_t > add(const E &item)
Adds an item to the tail of the Ringbuffer.
Definition: ringbuffer.h:80
boost::future< boost::optional< E > > read_one(int64_t sequence)
Reads one item from the Ringbuffer.
Definition: ringbuffer.h:118