From 98b6f456917409e29dbbbe38d3074942eea231ea Mon Sep 17 00:00:00 2001 From: Marcelo Zimbres Date: Sat, 25 Sep 2021 21:45:18 +0200 Subject: [PATCH] More refactoring. --- examples/async_basic.cpp | 5 +- include/aedis/impl/request.ipp | 41 +++++++++++ include/aedis/impl/response.ipp | 73 ++++++++++++------- include/aedis/impl/response_adapter.ipp | 63 ---------------- include/aedis/impl/src.hpp | 3 +- include/aedis/read.hpp | 22 ++---- include/aedis/resp3/array_adapter.hpp | 2 +- .../aedis/resp3/basic_flat_array_adapter.hpp | 2 +- include/aedis/resp3/blob_error_adapter.hpp | 2 +- include/aedis/resp3/blob_string_adapter.hpp | 2 +- include/aedis/resp3/flat_map_adapter.hpp | 2 +- include/aedis/resp3/flat_set_adapter.hpp | 2 +- include/aedis/resp3/number_adapter.hpp | 2 +- include/aedis/resp3/simple_error_adapter.hpp | 2 +- include/aedis/resp3/simple_string_adapter.hpp | 2 +- include/aedis/response.hpp | 39 +++++++++- include/aedis/response_adapter.hpp | 56 -------------- tests/general.cpp | 12 +-- 18 files changed, 152 insertions(+), 180 deletions(-) create mode 100644 include/aedis/impl/request.ipp delete mode 100644 include/aedis/impl/response_adapter.ipp delete mode 100644 include/aedis/response_adapter.hpp diff --git a/examples/async_basic.cpp b/examples/async_basic.cpp index e361a4aa..8f10fe23 100644 --- a/examples/async_basic.cpp +++ b/examples/async_basic.cpp @@ -16,11 +16,10 @@ example(net::ip::tcp::socket& socket, std::queue& requests) requests.back().hello("3"); response resp; - consumer_state cs{resp}; + consumer_state cs; for (;;) { - auto const type = - co_await async_consume(socket, requests, cs, net::use_awaitable); + auto const type = co_await async_consume(socket, requests, resp, cs, net::use_awaitable); if (type == resp3::type::flat_push) { std::cout << "Event: " << "(" << type << ")" << std::endl; diff --git a/include/aedis/impl/request.ipp b/include/aedis/impl/request.ipp new file mode 100644 index 00000000..bfc1455e --- /dev/null +++ b/include/aedis/impl/request.ipp @@ -0,0 +1,41 @@ +/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include + +namespace aedis { namespace resp3 { + +void add_bulk(std::string& to, std::string_view param) +{ + to += "$"; + to += std::to_string(std::size(param)); + to += "\r\n"; + to += param; + to += "\r\n"; +} + +void add_header(std::string& to, int size) +{ + to += "*"; + to += std::to_string(size); + to += "\r\n"; +} + +void assemble(std::string& ret, std::string_view cmd) +{ + add_header(ret, 1); + add_bulk(ret, cmd); +} + +void assemble(std::string& ret, std::string_view cmd, std::string_view key) +{ + std::initializer_list dummy; + assemble(ret, cmd, {key}, std::cbegin(dummy), std::cend(dummy)); +} + +} // resp3 +} // aedis diff --git a/include/aedis/impl/response.ipp b/include/aedis/impl/response.ipp index bfc1455e..3e6bdd22 100644 --- a/include/aedis/impl/response.ipp +++ b/include/aedis/impl/response.ipp @@ -5,37 +5,58 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include #include -namespace aedis { namespace resp3 { +namespace aedis { -void add_bulk(std::string& to, std::string_view param) +response::response() +: array_{&array} +, flat_array_{&flat_array} +, flat_push_{&flat_push} +, flat_set_{&flat_set} +, flat_map_{&flat_map} +, flat_attribute_{&flat_attribute} +, simple_string_{&simple_string} +, simple_error_{&simple_error} +, number_{&number} +, doublean_{&doublean} +, boolean_{&boolean} +, big_number_{&big_number} +, blob_string_{&blob_string} +, blob_error_{&blob_error} +, verbatim_string_{&verbatim_string} +, streamed_string_part_{&streamed_string_part} +{ } + +response_adapter_base* response::select_adapter(resp3::type type, command cmd) { - to += "$"; - to += std::to_string(std::size(param)); - to += "\r\n"; - to += param; - to += "\r\n"; -} + if (type == resp3::type::flat_push) + return &flat_push_; -void add_header(std::string& to, int size) -{ - to += "*"; - to += std::to_string(size); - to += "\r\n"; -} + if (cmd == command::exec) + return &array_; -void assemble(std::string& ret, std::string_view cmd) -{ - add_header(ret, 1); - add_bulk(ret, cmd); + switch (type) { + case resp3::type::flat_set: return &flat_set_; + case resp3::type::flat_map: return &flat_map_; + case resp3::type::flat_attribute: return &flat_attribute_; + case resp3::type::flat_array: return &flat_array_; + case resp3::type::simple_error: return &simple_error_; + case resp3::type::simple_string: return &simple_string_; + case resp3::type::number: return &number_; + case resp3::type::doublean: return &doublean_; + case resp3::type::big_number: return &big_number_; + case resp3::type::boolean: return &boolean_; + case resp3::type::blob_error: return &blob_error_; + case resp3::type::blob_string: return &blob_string_; + case resp3::type::verbatim_string: return &verbatim_string_; + case resp3::type::streamed_string_part: return &streamed_string_part_; + case resp3::type::null: return &ignore_; + default: { + assert(false); + return nullptr; + } + } } - -void assemble(std::string& ret, std::string_view cmd, std::string_view key) -{ - std::initializer_list dummy; - assemble(ret, cmd, {key}, std::cbegin(dummy), std::cend(dummy)); -} - -} // resp3 } // aedis diff --git a/include/aedis/impl/response_adapter.ipp b/include/aedis/impl/response_adapter.ipp deleted file mode 100644 index 294dd66a..00000000 --- a/include/aedis/impl/response_adapter.ipp +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com) - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#include - -namespace aedis { - -response_adapter::response_adapter(response& resp) -: array_{&resp.array} -, flat_array_{&resp.flat_array} -, flat_push_{&resp.flat_push} -, flat_set_{&resp.flat_set} -, flat_map_{&resp.flat_map} -, flat_attribute_{&resp.flat_attribute} -, simple_string_{&resp.simple_string} -, simple_error_{&resp.simple_error} -, number_{&resp.number} -, doublean_{&resp.doublean} -, boolean_{&resp.boolean} -, big_number_{&resp.big_number} -, blob_string_{&resp.blob_string} -, blob_error_{&resp.blob_error} -, verbatim_string_{&resp.verbatim_string} -, streamed_string_part_{&resp.streamed_string_part} -{ } - -response_adapter_base* response_adapter:: -select(resp3::type type, command cmd) -{ - if (type == resp3::type::flat_push) - return &flat_push_; - - if (cmd == command::exec) - return &array_; - - switch (type) { - case resp3::type::flat_set: return &flat_set_; - case resp3::type::flat_map: return &flat_map_; - case resp3::type::flat_attribute: return &flat_attribute_; - case resp3::type::flat_array: return &flat_array_; - case resp3::type::simple_error: return &simple_error_; - case resp3::type::simple_string: return &simple_string_; - case resp3::type::number: return &number_; - case resp3::type::doublean: return &doublean_; - case resp3::type::big_number: return &big_number_; - case resp3::type::boolean: return &boolean_; - case resp3::type::blob_error: return &blob_error_; - case resp3::type::blob_string: return &blob_string_; - case resp3::type::verbatim_string: return &verbatim_string_; - case resp3::type::streamed_string_part: return &streamed_string_part_; - case resp3::type::null: return &ignore_; - default: { - assert(false); - return nullptr; - } - } -} - -} // aedis diff --git a/include/aedis/impl/src.hpp b/include/aedis/impl/src.hpp index c9cc9fa2..1e533df8 100644 --- a/include/aedis/impl/src.hpp +++ b/include/aedis/impl/src.hpp @@ -7,8 +7,7 @@ #include #include -#include #include +#include #include - #include diff --git a/include/aedis/read.hpp b/include/aedis/read.hpp index 91413e58..3f9a0947 100644 --- a/include/aedis/read.hpp +++ b/include/aedis/read.hpp @@ -16,7 +16,6 @@ #include #include -#include #include @@ -230,7 +229,7 @@ struct consume_op { net::ip::tcp::socket& socket; std::string& buffer; std::queue& requests; - response_adapter& adapter; + response& resp; resp3::type& m_type; net::coroutine& coro; @@ -264,8 +263,8 @@ struct consume_op { if (m_type != resp3::type::flat_push) cmd = requests.front().commands.front(); - auto* p = adapter.select(m_type, cmd); - async_read_one(socket, buffer, *p, std::move(self)); + auto* adapter = resp.select_adapter(m_type, cmd); + async_read_one(socket, buffer, *adapter, std::move(self)); } if (ec) { @@ -287,28 +286,23 @@ struct consume_op { struct consumer_state { std::string buffer; - response_adapter adapter; - net::coroutine coro; - resp3::type type; - - consumer_state(response& resp) - : adapter{resp} - , coro{net::coroutine()} - , type {resp3::type::invalid} - { } + response resp; + net::coroutine coro = net::coroutine(); + resp3::type type = resp3::type::invalid; }; template auto async_consume( net::ip::tcp::socket& socket, std::queue& requests, + response& resp, consumer_state& cs, CompletionToken&& token) { return net::async_compose< CompletionToken, void(boost::system::error_code, resp3::type)>( - consume_op{socket, cs.buffer, requests, cs.adapter, cs.type, cs.coro}, token, socket); + consume_op{socket, cs.buffer, requests, resp, cs.type, cs.coro}, token, socket); } } // aedis diff --git a/include/aedis/resp3/array_adapter.hpp b/include/aedis/resp3/array_adapter.hpp index d1e87274..daa7fe51 100644 --- a/include/aedis/resp3/array_adapter.hpp +++ b/include/aedis/resp3/array_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include namespace aedis { namespace resp3 { diff --git a/include/aedis/resp3/basic_flat_array_adapter.hpp b/include/aedis/resp3/basic_flat_array_adapter.hpp index bc5791b5..9adfd4a5 100644 --- a/include/aedis/resp3/basic_flat_array_adapter.hpp +++ b/include/aedis/resp3/basic_flat_array_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include diff --git a/include/aedis/resp3/blob_error_adapter.hpp b/include/aedis/resp3/blob_error_adapter.hpp index ffacd2df..c9eab1eb 100644 --- a/include/aedis/resp3/blob_error_adapter.hpp +++ b/include/aedis/resp3/blob_error_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include diff --git a/include/aedis/resp3/blob_string_adapter.hpp b/include/aedis/resp3/blob_string_adapter.hpp index 3f620c4f..3b8319cf 100644 --- a/include/aedis/resp3/blob_string_adapter.hpp +++ b/include/aedis/resp3/blob_string_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include diff --git a/include/aedis/resp3/flat_map_adapter.hpp b/include/aedis/resp3/flat_map_adapter.hpp index be51b455..457027c2 100644 --- a/include/aedis/resp3/flat_map_adapter.hpp +++ b/include/aedis/resp3/flat_map_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include namespace aedis { namespace resp3 { diff --git a/include/aedis/resp3/flat_set_adapter.hpp b/include/aedis/resp3/flat_set_adapter.hpp index c7c93b7a..c00c2a00 100644 --- a/include/aedis/resp3/flat_set_adapter.hpp +++ b/include/aedis/resp3/flat_set_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include diff --git a/include/aedis/resp3/number_adapter.hpp b/include/aedis/resp3/number_adapter.hpp index f4018f22..d4b0771a 100644 --- a/include/aedis/resp3/number_adapter.hpp +++ b/include/aedis/resp3/number_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include diff --git a/include/aedis/resp3/simple_error_adapter.hpp b/include/aedis/resp3/simple_error_adapter.hpp index 4f45eaca..b536ef56 100644 --- a/include/aedis/resp3/simple_error_adapter.hpp +++ b/include/aedis/resp3/simple_error_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include namespace aedis { namespace resp3 { diff --git a/include/aedis/resp3/simple_string_adapter.hpp b/include/aedis/resp3/simple_string_adapter.hpp index 60bd0a75..b6990c62 100644 --- a/include/aedis/resp3/simple_string_adapter.hpp +++ b/include/aedis/resp3/simple_string_adapter.hpp @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include diff --git a/include/aedis/response.hpp b/include/aedis/response.hpp index 13da7efa..de1279c9 100644 --- a/include/aedis/response.hpp +++ b/include/aedis/response.hpp @@ -8,11 +8,46 @@ #pragma once #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace aedis { class response { private: + resp3::array_adapter array_; + resp3::basic_flat_array_adapter flat_array_; + resp3::basic_flat_array_adapter flat_push_; + resp3::flat_set_adapter flat_set_; + resp3::flat_map_adapter flat_map_; + resp3::basic_flat_array_adapter flat_attribute_; + resp3::simple_string_adapter simple_string_; + resp3::simple_error_adapter simple_error_; + resp3::number_adapter number_; + resp3::doublean_adapter doublean_; + resp3::boolean_adapter boolean_; + resp3::big_number_adapter big_number_; + resp3::blob_string_adapter blob_string_; + resp3::blob_error_adapter blob_error_; + resp3::verbatim_string_adapter verbatim_string_; + resp3::streamed_string_part_adapter streamed_string_part_; + resp3::ignore_adapter ignore_; + public: resp3::array array; resp3::flat_array flat_array; @@ -30,7 +65,9 @@ public: resp3::blob_error blob_error; resp3::verbatim_string verbatim_string; resp3::streamed_string_part streamed_string_part; + + response(); + response_adapter_base* select_adapter(resp3::type type, command cmd); }; } // aedis - diff --git a/include/aedis/response_adapter.hpp b/include/aedis/response_adapter.hpp deleted file mode 100644 index 190a267b..00000000 --- a/include/aedis/response_adapter.hpp +++ /dev/null @@ -1,56 +0,0 @@ -/* Copyright (c) 2019 - 2021 Marcelo Zimbres Silva (mzimbres at gmail dot com) - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace aedis { - -class response_adapter { -private: - resp3::array_adapter array_; - resp3::basic_flat_array_adapter flat_array_; - resp3::basic_flat_array_adapter flat_push_; - resp3::flat_set_adapter flat_set_; - resp3::flat_map_adapter flat_map_; - resp3::basic_flat_array_adapter flat_attribute_; - resp3::simple_string_adapter simple_string_; - resp3::simple_error_adapter simple_error_; - resp3::number_adapter number_; - resp3::doublean_adapter doublean_; - resp3::boolean_adapter boolean_; - resp3::big_number_adapter big_number_; - resp3::blob_string_adapter blob_string_; - resp3::blob_error_adapter blob_error_; - resp3::verbatim_string_adapter verbatim_string_; - resp3::streamed_string_part_adapter streamed_string_part_; - resp3::ignore_adapter ignore_; - -public: - response_adapter(response& resp); - response_adapter_base* select(resp3::type t, command cmd); -}; - -} // aedis diff --git a/tests/general.cpp b/tests/general.cpp index aa830a27..7091484c 100644 --- a/tests/general.cpp +++ b/tests/general.cpp @@ -121,12 +121,12 @@ test_general(net::ip::tcp::resolver::results_type const& res) test_general_fill filler; response resp; - consumer_state cs{resp}; + consumer_state cs; int push_counter = 0; for (;;) { auto const type = - co_await async_consume(socket, requests, cs, net::use_awaitable); + co_await async_consume(socket, requests, resp, cs, net::use_awaitable); if (type == resp3::type::flat_push) { switch (push_counter) { @@ -702,10 +702,10 @@ net::awaitable test_streamed_string() { std::string cmd {"$?\r\n;0\r\n"}; test_tcp_socket ts {cmd}; - resp3::flat_array buffer; - resp3::flat_array_adapter res{&buffer}; - co_await async_read_one(ts, buf, res); - check_equal(buffer, {}, "streamed string (empty)"); + response resp; + auto* adapter = resp.select_adapter(resp3::type::streamed_string_part, command::unknown); + co_await async_read_one(ts, buf, *adapter); + check_equal(resp.streamed_string_part, {}, "streamed string (empty)"); } }