From 9e43541a5eef783651aed3fd2f9a03a073d8ef09 Mon Sep 17 00:00:00 2001 From: Marcelo Zimbres Date: Mon, 6 Jun 2022 15:11:58 +0200 Subject: [PATCH] Remove Command template parameter from request. --- aedis/connection.hpp | 33 +++++++++++++++------------------ aedis/detail/connection_ops.hpp | 19 ++++++++++--------- aedis/resp3/exec.hpp | 16 ++++++---------- aedis/resp3/request.hpp | 14 +++++++------- examples/adapter.cpp | 4 ++-- examples/chat_room.cpp | 6 +++--- examples/containers.cpp | 6 +++--- examples/echo_server.cpp | 4 ++-- examples/intro.cpp | 4 ++-- examples/subscriber.cpp | 4 ++-- tests/high_level.cpp | 12 ++++++------ tests/intro_sync.cpp | 2 +- tools/commands.cpp | 2 +- 13 files changed, 60 insertions(+), 66 deletions(-) diff --git a/aedis/connection.hpp b/aedis/connection.hpp index 5fd701c0..39d3b785 100644 --- a/aedis/connection.hpp +++ b/aedis/connection.hpp @@ -32,7 +32,7 @@ namespace aedis { * * https://redis.io/docs/reference/sentinel-clients */ -template +template class connection { public: /// Executor type. @@ -41,9 +41,6 @@ public: /// Type of the last layer using next_layer_type = AsyncReadWriteStream; - /// Type of requests used by the connection. - using request_type = resp3::request; - using default_completion_token_type = boost::asio::default_completion_token_t; /** @brief Configuration parameters. @@ -121,7 +118,7 @@ public: * error::idle_timeout. * * @li Starts the healthy check operation that sends - * redis::command::ping to Redis with a frequency equal to + * command::ping to Redis with a frequency equal to * connection::config::ping_interval. * * In addition to the callbacks mentioned above, the read @@ -170,7 +167,7 @@ public: return boost::asio::async_compose < CompletionToken , void(boost::system::error_code) - >(detail::run_op{this, host, port}, token, resv_); + >(detail::run_op{this, host, port}, token, resv_); } /** @brief Asynchrnously schedules a command for execution. @@ -179,7 +176,7 @@ public: class Adapter = detail::response_traits::adapter_type, class CompletionToken = default_completion_token_type> auto async_exec( - request_type const& req, + resp3::request const& req, Adapter adapter = aedis::adapt(), CompletionToken token = CompletionToken{}) { @@ -202,7 +199,7 @@ public: return boost::asio::async_compose < CompletionToken , void(boost::system::error_code, std::size_t) - >(detail::read_push_op{this, adapter}, token, resv_); + >(detail::read_push_op{this, adapter}, token, resv_); } /** @brief Closes the connection with the database. @@ -228,10 +225,10 @@ public: private: using time_point_type = std::chrono::time_point; - template friend struct detail::read_push_op; - template friend struct detail::reader_op; - template friend struct detail::ping_op; - template friend struct detail::run_op; + template friend struct detail::read_push_op; + template friend struct detail::reader_op; + template friend struct detail::ping_op; + template friend struct detail::run_op; template friend struct detail::exec_op; template friend struct detail::exec_internal_op; template friend struct detail::writer_op; @@ -246,7 +243,7 @@ private: bool stop = false; }; - void add_request(request_type const& req) + void add_request(resp3::request const& req) { BOOST_ASSERT(!req.payload().empty()); auto const can_write = reqs_.empty(); @@ -298,7 +295,7 @@ private: return boost::asio::async_compose < CompletionToken , void(boost::system::error_code) - >(detail::reader_op{this}, token, resv_.get_executor()); + >(detail::reader_op{this}, token, resv_.get_executor()); } template @@ -328,7 +325,7 @@ private: return boost::asio::async_compose < CompletionToken , void(boost::system::error_code) - >(detail::ping_op{this}, token, resv_); + >(detail::ping_op{this}, token, resv_); } template @@ -343,7 +340,7 @@ private: template auto async_exec_internal( - request_type const& req, + resp3::request const& req, CompletionToken token = CompletionToken{}) { return boost::asio::async_compose @@ -394,7 +391,7 @@ private: std::string payload_; std::string payload_next_; std::deque> reqs_; - std::queue cmds_; + std::queue cmds_; std::vector> pool_; // Last time we received data. @@ -403,7 +400,7 @@ private: // The result of async_resolve. boost::asio::ip::tcp::resolver::results_type endpoints_; - request_type req_; + resp3::request req_; }; } // aedis diff --git a/aedis/detail/connection_ops.hpp b/aedis/detail/connection_ops.hpp index 99deb125..cf63c975 100644 --- a/aedis/detail/connection_ops.hpp +++ b/aedis/detail/connection_ops.hpp @@ -23,6 +23,7 @@ #include #include #include +#include namespace aedis { namespace detail { @@ -50,7 +51,7 @@ struct connect_with_timeout_op { template struct exec_internal_op { Conn* cli; - typename Conn::request_type const* req; + resp3::request const* req; boost::asio::coroutine coro; template @@ -88,7 +89,7 @@ struct resolve_with_timeout_op { } }; -template +template struct read_push_op { Conn* cli; Adapter adapter; @@ -116,7 +117,7 @@ struct read_push_op { resp3::async_read( *cli->socket_, cli->make_dynamic_buffer(), - [adpt = adapter](resp3::node const& n, boost::system::error_code& ec) mutable { adpt(std::size_t(-1), Command::invalid, n, ec);}, + [adpt = adapter](resp3::node const& n, boost::system::error_code& ec) mutable { adpt(std::size_t(-1), command::invalid, n, ec);}, std::move(self)); cli->wait_read_timer_.cancel_one(); @@ -130,7 +131,7 @@ struct read_push_op { template struct exec_op { Conn* cli; - typename Conn::request_type const* req; + resp3::request const* req; Adapter adapter; std::shared_ptr info; std::size_t read_size = 0; @@ -229,7 +230,7 @@ struct exec_op { } }; -template +template struct ping_op { Conn* cli; boost::asio::coroutine coro; @@ -250,7 +251,7 @@ struct ping_op { } cli->req_.clear(); - cli->req_.push(Command::ping); + cli->req_.push(command::ping); yield cli->async_exec(cli->req_, aedis::adapt(), std::move(self)); if (ec) { self.complete(ec); @@ -343,7 +344,7 @@ struct read_write_check_ping_op { } }; -template +template struct run_op { Conn* cli; boost::string_view host; @@ -375,7 +376,7 @@ struct run_op { } cli->req_.clear(); - cli->req_.push(Command::hello, 3); + cli->req_.push(command::hello, 3); cli->check_idle_timer_.expires_after(2 * cli->cfg_.ping_interval); yield cli->async_exec_internal(cli->req_, std::move(self)); if (ec) { @@ -450,7 +451,7 @@ struct writer_op { } }; -template +template struct reader_op { Conn* cli; boost::asio::coroutine coro; diff --git a/aedis/resp3/exec.hpp b/aedis/resp3/exec.hpp index d244fe31..0f459984 100644 --- a/aedis/resp3/exec.hpp +++ b/aedis/resp3/exec.hpp @@ -28,13 +28,12 @@ namespace detail { template < class AsyncStream, - class Command, class Adapter, class DynamicBuffer > struct exec_op { AsyncStream* socket; - request const* req; + request const* req; Adapter adapter; DynamicBuffer dbuf; boost::asio::coroutine coro; @@ -69,14 +68,13 @@ struct exec_op { template < class AsyncStream, - class Command, class Adapter, class DynamicBuffer, class CompletionToken = boost::asio::default_completion_token_t > auto async_exec( AsyncStream& socket, - request const& req, + request const& req, Adapter adapter, DynamicBuffer dbuf, CompletionToken token = CompletionToken{}) @@ -84,7 +82,7 @@ auto async_exec( return boost::asio::async_compose < CompletionToken , void(boost::system::error_code, std::size_t) - >(detail::exec_op + >(detail::exec_op {&socket, &req, adapter, dbuf}, token, socket); } @@ -94,14 +92,13 @@ namespace detail { template < class AsyncStream, - class Command, class Adapter, class DynamicBuffer > struct exec_with_timeout_op { AsyncStream* socket; boost::asio::steady_timer* timer; - request const* req; + request const* req; Adapter adapter; DynamicBuffer dbuf; boost::asio::coroutine coro; @@ -154,7 +151,6 @@ struct exec_with_timeout_op { template < class AsyncStream, - class Command, class Adapter, class DynamicBuffer, class CompletionToken = boost::asio::default_completion_token_t @@ -162,7 +158,7 @@ template < auto async_exec( AsyncStream& socket, boost::asio::steady_timer& timer, - request const& req, + request const& req, Adapter adapter, DynamicBuffer dbuf, CompletionToken token = CompletionToken{}) @@ -170,7 +166,7 @@ auto async_exec( return boost::asio::async_compose < CompletionToken , void(boost::system::error_code, std::size_t) - >(detail::exec_with_timeout_op + >(detail::exec_with_timeout_op {&socket, &timer, &req, adapter, dbuf}, token, socket, timer); } diff --git a/aedis/resp3/request.hpp b/aedis/resp3/request.hpp index fbca4594..10b44f6c 100644 --- a/aedis/resp3/request.hpp +++ b/aedis/resp3/request.hpp @@ -8,6 +8,7 @@ #define AEDIS_RESP3_REQUEST_HPP #include +#include #include // NOTE: Consider detecting tuples in the type in the parameter pack @@ -43,7 +44,6 @@ namespace resp3 { * \remarks Non-string types will be converted to string by using \c * to_bulk, which must be made available over ADL. */ -template class request { public: /// Returns a list of commands contained in this request. @@ -76,7 +76,7 @@ public: * \param args Command arguments. */ template - void push(Command cmd, Ts const&... args) + void push(command cmd, Ts const&... args) { using boost::hana::for_each; using boost::hana::make_tuple; @@ -115,7 +115,7 @@ public: * \param end Iterator to the end of the range. */ template - void push_range2(Command cmd, Key const& key, ForwardIterator begin, ForwardIterator end) + void push_range2(command cmd, Key const& key, ForwardIterator begin, ForwardIterator end) { using value_type = typename std::iterator_traits::value_type; using resp3::type; @@ -157,7 +157,7 @@ public: * \param end Iterator to the end of the range. */ template - void push_range2(Command cmd, ForwardIterator begin, ForwardIterator end) + void push_range2(command cmd, ForwardIterator begin, ForwardIterator end) { using value_type = typename std::iterator_traits::value_type; using resp3::type; @@ -184,7 +184,7 @@ public: * Equivalent to the overload taking a range (i.e. send_range2). */ template - void push_range(Command cmd, Key const& key, Range const& range) + void push_range(command cmd, Key const& key, Range const& range) { using std::begin; using std::end; @@ -196,7 +196,7 @@ public: * Equivalent to the overload taking a range (i.e. send_range2). */ template - void push_range(Command cmd, Range const& range) + void push_range(command cmd, Range const& range) { using std::begin; using std::end; @@ -204,7 +204,7 @@ public: } private: - using command_info_type = std::pair; + using command_info_type = std::pair; std::string payload_; std::vector commands_; }; diff --git a/examples/adapter.cpp b/examples/adapter.cpp index a18f6015..3f046f41 100644 --- a/examples/adapter.cpp +++ b/examples/adapter.cpp @@ -17,7 +17,7 @@ namespace net = boost::asio; namespace adapter = aedis::adapter; using aedis::command; using aedis::resp3::request; -using connection = aedis::connection; +using connection = aedis::connection<>; using node_type = aedis::resp3::node; using error_code = boost::system::error_code; @@ -44,7 +44,7 @@ int main() net::io_context ioc; connection db{ioc}; - request req; + request req; req.push(command::ping); req.push(command::incr, "some-key"); req.push(command::quit); diff --git a/examples/chat_room.cpp b/examples/chat_room.cpp index f0132637..a2c60d5b 100644 --- a/examples/chat_room.cpp +++ b/examples/chat_room.cpp @@ -19,7 +19,7 @@ using aedis::command; using aedis::resp3::request; using tcp_socket = net::use_awaitable_t<>::as_default_on_t; using tcp_acceptor = net::use_awaitable_t<>::as_default_on_t; -using connection = aedis::connection; +using connection = aedis::connection; using response_type = std::vector>; class user_session: @@ -52,7 +52,7 @@ private: { try { std::string msg; - request req; + request req; auto dbuffer = net::dynamic_buffer(msg, 1024); for (;;) { auto const n = co_await net::async_read_until(socket_, dbuffer, "\n"); @@ -101,7 +101,7 @@ reader( std::shared_ptr db, std::shared_ptr sessions) { - request req; + request req; req.push(command::subscribe, "channel"); co_await db->async_exec(req); diff --git a/examples/containers.cpp b/examples/containers.cpp index 753e9f6e..df65ba80 100644 --- a/examples/containers.cpp +++ b/examples/containers.cpp @@ -19,7 +19,7 @@ using boost::optional; using aedis::adapt; using aedis::command; using aedis::resp3::request; -using connection = aedis::connection; +using connection = aedis::connection<>; // Response used in this example. using C1 = std::vector; @@ -39,13 +39,13 @@ int main() C2 set {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}}; C3 map {{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}}; - request req1; + request req1; req1.push_range(command::rpush, "rpush-key", vec); req1.push_range(command::sadd, "sadd-key", set); req1.push_range(command::hset, "hset-key", map); // Request that retrieves the containers. - request req2; + request req2; req2.push(command::multi); req2.push(command::lrange, "rpush-key", 0, -1); req2.push(command::smembers, "sadd-key"); diff --git a/examples/echo_server.cpp b/examples/echo_server.cpp index a3fa2f4c..2b3030ea 100644 --- a/examples/echo_server.cpp +++ b/examples/echo_server.cpp @@ -16,14 +16,14 @@ using aedis::command; using aedis::resp3::request; using tcp_socket = net::use_awaitable_t<>::as_default_on_t; using tcp_acceptor = net::use_awaitable_t<>::as_default_on_t; -using connection = aedis::connection; +using connection = aedis::connection; net::awaitable echo_loop(tcp_socket socket, std::shared_ptr db) { try { for (std::string buffer;;) { auto n = co_await net::async_read_until(socket, net::dynamic_buffer(buffer, 1024), "\n"); - request req; + request req; req.push(command::ping, buffer); std::tuple resp; co_await db->async_exec(req, adapt(resp)); diff --git a/examples/intro.cpp b/examples/intro.cpp index a68b685e..9e0c489a 100644 --- a/examples/intro.cpp +++ b/examples/intro.cpp @@ -15,14 +15,14 @@ namespace net = boost::asio; using aedis::adapt; using aedis::command; using aedis::resp3::request; -using connection = aedis::connection; +using connection = aedis::connection<>; auto handler =[](auto ec, auto...) { std::cout << ec.message() << std::endl; }; int main() { - request req; + request req; req.push(command::ping, "Ping example"); req.push(command::quit); diff --git a/examples/subscriber.cpp b/examples/subscriber.cpp index b2a21e28..5d48befa 100644 --- a/examples/subscriber.cpp +++ b/examples/subscriber.cpp @@ -17,7 +17,7 @@ using aedis::adapt; using aedis::command; using aedis::resp3::request; using tcp_socket = net::use_awaitable_t<>::as_default_on_t; -using connection = aedis::connection; +using connection = aedis::connection; using response_type = std::vector>; /* In this example we send a subscription to a channel and start @@ -36,7 +36,7 @@ using response_type = std::vector>; */ net::awaitable reader(std::shared_ptr db) { - request req; + request req; req.push(command::subscribe, "channel"); co_await db->async_exec(req); diff --git a/tests/high_level.cpp b/tests/high_level.cpp index 86d74f37..abd87cb2 100644 --- a/tests/high_level.cpp +++ b/tests/high_level.cpp @@ -23,7 +23,7 @@ namespace resp3 = aedis::resp3; using aedis::command; using aedis::resp3::request; -using connection = aedis::connection; +using connection = aedis::connection<>; using error_code = boost::system::error_code; using net::experimental::as_tuple; using tcp = net::ip::tcp; @@ -72,7 +72,7 @@ void test_quit() net::io_context ioc; auto db = std::make_shared(ioc); - request req; + request req; req.push(command::quit); db->async_exec(req, aedis::adapt(), [](auto ec, auto r){ expect_no_error(ec); @@ -108,7 +108,7 @@ void test_push() net::io_context ioc; auto db = std::make_shared(ioc); - request req; + request req; req.push(command::subscribe, "channel"); req.push(command::quit); @@ -132,7 +132,7 @@ void test_push() net::awaitable run5(std::shared_ptr db) { { - request req; + request req; req.push(command::quit); db->async_exec(req, aedis::adapt(), [](auto ec, auto){ expect_no_error(ec); @@ -143,7 +143,7 @@ net::awaitable run5(std::shared_ptr db) } { - request req; + request req; req.push(command::quit); db->async_exec(req, aedis::adapt(), [](auto ec, auto){ expect_no_error(ec); @@ -176,7 +176,7 @@ void test_idle() net::io_context ioc; auto db = std::make_shared(ioc, cfg); - request req; + request req; req.push(command::client, "PAUSE", 5000); db->async_exec(req, aedis::adapt(), [](auto ec, auto r){ diff --git a/tests/intro_sync.cpp b/tests/intro_sync.cpp index 002e225e..74cb564e 100644 --- a/tests/intro_sync.cpp +++ b/tests/intro_sync.cpp @@ -30,7 +30,7 @@ int main() net::connect(socket, res); // Creates the request and writes to the socket. - request req; + request req; req.push(command::hello, 3); req.push(command::ping); req.push(command::quit); diff --git a/tools/commands.cpp b/tools/commands.cpp index ad4ba55f..b2340a54 100644 --- a/tools/commands.cpp +++ b/tools/commands.cpp @@ -81,7 +81,7 @@ int main() tcp::socket socket{ioc}; net::connect(socket, res); - request req; + request req; req.push(command::hello, 3); req.push(command::command); req.push(command::quit);