Loading...
Searching...
No Matches
request.hpp
1/* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
2 *
3 * Distributed under the Boost Software License, Version 1.0. (See
4 * accompanying file LICENSE.txt)
5 */
6
7#ifndef BOOST_REDIS_REQUEST_HPP
8#define BOOST_REDIS_REQUEST_HPP
9
10#include <boost/redis/resp3/serialization.hpp>
11#include <boost/redis/resp3/type.hpp>
12
13#include <algorithm>
14#include <string>
15#include <tuple>
16
17// NOTE: For some commands like hset it would be a good idea to assert
18// the value type is a pair.
19
20namespace boost::redis {
21
22namespace detail {
23auto has_response(std::string_view cmd) -> bool;
24}
25
46class request {
47public:
49 struct config {
55
61
69
77 };
78
83 explicit request(config cfg = config{true, false, true, true})
84 : cfg_{cfg}
85 { }
86
88 [[nodiscard]] auto get_expected_responses() const noexcept -> std::size_t
89 {
90 return expected_responses_;
91 };
92
94 [[nodiscard]] auto get_commands() const noexcept -> std::size_t { return commands_; };
95
96 [[nodiscard]] auto payload() const noexcept -> std::string_view { return payload_; }
97
98 [[nodiscard]] auto has_hello_priority() const noexcept -> auto const&
99 {
100 return has_hello_priority_;
101 }
102
104 void clear()
105 {
106 payload_.clear();
107 commands_ = 0;
108 expected_responses_ = 0;
109 has_hello_priority_ = false;
110 }
111
113 void reserve(std::size_t new_cap = 0) { payload_.reserve(new_cap); }
114
116 [[nodiscard]] auto get_config() const noexcept -> auto const& { return cfg_; }
117
119 [[nodiscard]] auto get_config() noexcept -> auto& { return cfg_; }
120
146 template <class... Ts>
147 void push(std::string_view cmd, Ts const&... args)
148 {
149 auto constexpr pack_size = sizeof...(Ts);
150 resp3::add_header(payload_, resp3::type::array, 1 + pack_size);
151 resp3::add_bulk(payload_, cmd);
152 resp3::add_bulk(payload_, std::tie(std::forward<Ts const&>(args)...));
153
154 check_cmd(cmd);
155 }
156
188 template <class ForwardIterator>
190 std::string_view const& cmd,
191 std::string_view const& key,
192 ForwardIterator begin,
193 ForwardIterator end,
194 typename std::iterator_traits<ForwardIterator>::value_type* = nullptr)
195 {
196 using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
197
198 if (begin == end)
199 return;
200
201 auto constexpr size = resp3::bulk_counter<value_type>::size;
202 auto const distance = std::distance(begin, end);
203 resp3::add_header(payload_, resp3::type::array, 2 + size * distance);
204 resp3::add_bulk(payload_, cmd);
205 resp3::add_bulk(payload_, key);
206
207 for (; begin != end; ++begin)
208 resp3::add_bulk(payload_, *begin);
209
210 check_cmd(cmd);
211 }
212
240 template <class ForwardIterator>
242 std::string_view const& cmd,
243 ForwardIterator begin,
244 ForwardIterator end,
245 typename std::iterator_traits<ForwardIterator>::value_type* = nullptr)
246 {
247 using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
248
249 if (begin == end)
250 return;
251
252 auto constexpr size = resp3::bulk_counter<value_type>::size;
253 auto const distance = std::distance(begin, end);
254 resp3::add_header(payload_, resp3::type::array, 1 + size * distance);
255 resp3::add_bulk(payload_, cmd);
256
257 for (; begin != end; ++begin)
258 resp3::add_bulk(payload_, *begin);
259
260 check_cmd(cmd);
261 }
262
273 template <class Range>
275 std::string_view const& cmd,
276 std::string_view const& key,
277 Range const& range,
278 decltype(std::begin(range))* = nullptr)
279 {
280 using std::begin;
281 using std::end;
282 push_range(cmd, key, begin(range), end(range));
283 }
284
294 template <class Range>
296 std::string_view cmd,
297 Range const& range,
298 decltype(std::cbegin(range))* = nullptr)
299 {
300 using std::cbegin;
301 using std::cend;
302 push_range(cmd, cbegin(range), cend(range));
303 }
304
305private:
306 void check_cmd(std::string_view cmd)
307 {
308 ++commands_;
309
310 if (!detail::has_response(cmd))
311 ++expected_responses_;
312
313 if (cmd == "HELLO")
314 has_hello_priority_ = cfg_.hello_with_priority;
315 }
316
317 config cfg_;
318 std::string payload_;
319 std::size_t commands_ = 0;
320 std::size_t expected_responses_ = 0;
321 bool has_hello_priority_ = false;
322};
323
324} // namespace boost::redis
325
326#endif // BOOST_REDIS_REQUEST_HPP
Creates Redis requests.
Definition request.hpp:46
bool cancel_if_not_connected
If true connection::async_exec will complete with boost::redis::error::not_connected if the call happ...
Definition request.hpp:60
auto get_config() noexcept -> auto &
Returns a reference to the config object.
Definition request.hpp:119
bool hello_with_priority
If this request has a HELLO command and this flag is true, the boost::redis::connection will move it ...
Definition request.hpp:76
void push_range(std::string_view cmd, Range const &range, decltype(std::cbegin(range)) *=nullptr)
Appends a new command to the end of the request.
Definition request.hpp:295
void reserve(std::size_t new_cap=0)
Calls std::string::reserve on the internal storage.
Definition request.hpp:113
request(config cfg=config{true, false, true, true})
Constructor.
Definition request.hpp:83
bool cancel_if_unresponded
If false connection::async_exec will not automatically cancel this request if the connection is lost....
Definition request.hpp:68
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition request.hpp:147
void clear()
Clears the request preserving allocated memory.
Definition request.hpp:104
void push_range(std::string_view const &cmd, std::string_view const &key, ForwardIterator begin, ForwardIterator end, typename std::iterator_traits< ForwardIterator >::value_type *=nullptr)
Appends a new command to the end of the request.
Definition request.hpp:189
bool cancel_on_connection_lost
If true calls to connection::async_exec will complete with error if the connection is lost while the ...
Definition request.hpp:54
auto get_config() const noexcept -> auto const &
Returns a const reference to the config object.
Definition request.hpp:116
void push_range(std::string_view const &cmd, ForwardIterator begin, ForwardIterator end, typename std::iterator_traits< ForwardIterator >::value_type *=nullptr)
Appends a new command to the end of the request.
Definition request.hpp:241
void push_range(std::string_view const &cmd, std::string_view const &key, Range const &range, decltype(std::begin(range)) *=nullptr)
Appends a new command to the end of the request.
Definition request.hpp:274
Request configuration options.
Definition request.hpp:49