123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- #ifndef BOOST_REDIS_REQUEST_HPP
- #define BOOST_REDIS_REQUEST_HPP
- #include <boost/redis/resp3/type.hpp>
- #include <boost/redis/resp3/serialization.hpp>
- #include <string>
- #include <tuple>
- #include <algorithm>
- namespace boost::redis {
- namespace detail{
- auto has_response(std::string_view cmd) -> bool;
- }
- class request {
- public:
-
- struct config {
-
- bool cancel_on_connection_lost = true;
-
- bool cancel_if_not_connected = false;
-
- bool cancel_if_unresponded = true;
-
- bool hello_with_priority = true;
- };
-
- explicit
- request(config cfg = config{true, false, true, true})
- : cfg_{cfg} {}
-
- [[nodiscard]] auto get_expected_responses() const noexcept -> std::size_t
- { return expected_responses_;};
-
- [[nodiscard]] auto get_commands() const noexcept -> std::size_t
- { return commands_;};
- [[nodiscard]] auto payload() const noexcept -> std::string_view
- { return payload_;}
- [[nodiscard]] auto has_hello_priority() const noexcept -> auto const&
- { return has_hello_priority_;}
-
- void clear()
- {
- payload_.clear();
- commands_ = 0;
- expected_responses_ = 0;
- has_hello_priority_ = false;
- }
-
- void reserve(std::size_t new_cap = 0)
- { payload_.reserve(new_cap); }
-
- [[nodiscard]] auto get_config() const noexcept -> auto const& {return cfg_; }
-
- [[nodiscard]] auto get_config() noexcept -> auto& {return cfg_; }
-
- template <class... Ts>
- void push(std::string_view cmd, Ts const&... args)
- {
- auto constexpr pack_size = sizeof...(Ts);
- resp3::add_header(payload_, resp3::type::array, 1 + pack_size);
- resp3::add_bulk(payload_, cmd);
- resp3::add_bulk(payload_, std::tie(std::forward<Ts const&>(args)...));
- check_cmd(cmd);
- }
-
- template <class ForwardIterator>
- 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)
- {
- using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
- if (begin == end)
- return;
- auto constexpr size = resp3::bulk_counter<value_type>::size;
- auto const distance = std::distance(begin, end);
- resp3::add_header(payload_, resp3::type::array, 2 + size * distance);
- resp3::add_bulk(payload_, cmd);
- resp3::add_bulk(payload_, key);
- for (; begin != end; ++begin)
- resp3::add_bulk(payload_, *begin);
- check_cmd(cmd);
- }
-
- template <class ForwardIterator>
- void
- push_range(
- std::string_view const& cmd,
- ForwardIterator begin,
- ForwardIterator end,
- typename std::iterator_traits<ForwardIterator>::value_type * = nullptr)
- {
- using value_type = typename std::iterator_traits<ForwardIterator>::value_type;
- if (begin == end)
- return;
- auto constexpr size = resp3::bulk_counter<value_type>::size;
- auto const distance = std::distance(begin, end);
- resp3::add_header(payload_, resp3::type::array, 1 + size * distance);
- resp3::add_bulk(payload_, cmd);
- for (; begin != end; ++begin)
- resp3::add_bulk(payload_, *begin);
- check_cmd(cmd);
- }
-
- template <class Range>
- void
- push_range(
- std::string_view const& cmd,
- std::string_view const& key,
- Range const& range,
- decltype(std::begin(range)) * = nullptr)
- {
- using std::begin;
- using std::end;
- push_range(cmd, key, begin(range), end(range));
- }
-
- template <class Range>
- void
- push_range(
- std::string_view cmd,
- Range const& range,
- decltype(std::cbegin(range)) * = nullptr)
- {
- using std::cbegin;
- using std::cend;
- push_range(cmd, cbegin(range), cend(range));
- }
- private:
- void check_cmd(std::string_view cmd)
- {
- ++commands_;
- if (!detail::has_response(cmd))
- ++expected_responses_;
- if (cmd == "HELLO")
- has_hello_priority_ = cfg_.hello_with_priority;
- }
- config cfg_;
- std::string payload_;
- std::size_t commands_ = 0;
- std::size_t expected_responses_ = 0;
- bool has_hello_priority_ = false;
- };
- }
- #endif
|