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