Boost.Redis 1.4.2
A redis client library
Loading...
Searching...
No Matches
request.hpp
1/* Copyright (c) 2018-2023 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/type.hpp>
11#include <boost/redis/resp3/serialization.hpp>
12
13#include <string>
14#include <tuple>
15#include <algorithm>
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 {
56
62
70
77 };
78
83 explicit
84 request(config cfg = config{true, false, true, true})
85 : cfg_{cfg} {}
86
88 [[nodiscard]] auto get_expected_responses() const noexcept -> std::size_t
89 { return expected_responses_;};
90
92 [[nodiscard]] auto get_commands() const noexcept -> std::size_t
93 { return commands_;};
94
95 [[nodiscard]] auto payload() const noexcept -> std::string_view
96 { return payload_;}
97
98 [[nodiscard]] auto has_hello_priority() const noexcept -> auto const&
99 { return has_hello_priority_;}
100
102 void clear()
103 {
104 payload_.clear();
105 commands_ = 0;
106 expected_responses_ = 0;
107 has_hello_priority_ = false;
108 }
109
111 void reserve(std::size_t new_cap = 0)
112 { payload_.reserve(new_cap); }
113
115 [[nodiscard]] auto get_config() const noexcept -> auto const& {return cfg_; }
116
118 [[nodiscard]] auto get_config() noexcept -> auto& {return cfg_; }
119
145 template <class... Ts>
146 void push(std::string_view cmd, Ts const&... args)
147 {
148 auto constexpr pack_size = sizeof...(Ts);
149 resp3::add_header(payload_, resp3::type::array, 1 + pack_size);
150 resp3::add_bulk(payload_, cmd);
151 resp3::add_bulk(payload_, std::tie(std::forward<Ts const&>(args)...));
152
153 check_cmd(cmd);
154 }
155
187 template <class ForwardIterator>
188 void
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>
241 void
243 std::string_view const& cmd,
244 ForwardIterator begin,
245 ForwardIterator end,
246 typename std::iterator_traits<ForwardIterator>::value_type * = nullptr)
247 {
248 using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
249
250 if (begin == end)
251 return;
252
253 auto constexpr size = resp3::bulk_counter<value_type>::size;
254 auto const distance = std::distance(begin, end);
255 resp3::add_header(payload_, resp3::type::array, 1 + size * distance);
256 resp3::add_bulk(payload_, cmd);
257
258 for (; begin != end; ++begin)
259 resp3::add_bulk(payload_, *begin);
260
261 check_cmd(cmd);
262 }
263
274 template <class Range>
275 void
277 std::string_view const& cmd,
278 std::string_view const& key,
279 Range const& range,
280 decltype(std::begin(range)) * = nullptr)
281 {
282 using std::begin;
283 using std::end;
284 push_range(cmd, key, begin(range), end(range));
285 }
286
296 template <class Range>
297 void
299 std::string_view cmd,
300 Range const& range,
301 decltype(std::cbegin(range)) * = nullptr)
302 {
303 using std::cbegin;
304 using std::cend;
305 push_range(cmd, cbegin(range), cend(range));
306 }
307
308private:
309 void check_cmd(std::string_view cmd)
310 {
311 ++commands_;
312
313 if (!detail::has_response(cmd))
314 ++expected_responses_;
315
316 if (cmd == "HELLO")
317 has_hello_priority_ = cfg_.hello_with_priority;
318 }
319
320 config cfg_;
321 std::string payload_;
322 std::size_t commands_ = 0;
323 std::size_t expected_responses_ = 0;
324 bool has_hello_priority_ = false;
325};
326
327} // boost::redis::resp3
328
329#endif // BOOST_REDIS_REQUEST_HPP
Creates Redis requests.
Definition: request.hpp:46
bool cancel_if_not_connected
If true the request will complete with boost::redis::error::not_connected if async_exec is called bef...
Definition: request.hpp:61
auto get_config() noexcept -> auto &
Returns a reference to the config object.
Definition: request.hpp:118
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:298
void reserve(std::size_t new_cap=0)
Calls std::string::reserve on the internal storage.
Definition: request.hpp:111
request(config cfg=config{true, false, true, true})
Constructor.
Definition: request.hpp:84
bool cancel_if_unresponded
If false boost::redis::connection::async_exec will not automatically cancel this request if the conne...
Definition: request.hpp:69
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition: request.hpp:146
void clear()
Clears the request preserving allocated memory.
Definition: request.hpp:102
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 boost::redis::connection::async_exec will complete with error if the connection is lost....
Definition: request.hpp:55
auto get_config() const noexcept -> auto const &
Returns a const reference to the config object.
Definition: request.hpp:115
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:242
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:276
Request configuration options.
Definition: request.hpp:49