2
0
mirror of https://github.com/boostorg/redis.git synced 2026-01-19 04:42:09 +00:00

Some code improvements.

This commit is contained in:
Marcelo Zimbres
2022-04-04 22:38:02 +02:00
parent bcf13c03c0
commit 379da7a340
11 changed files with 91 additions and 59 deletions

View File

@@ -66,6 +66,7 @@ nobase_include_HEADERS =\
$(top_srcdir)/aedis/redis/receiver_base.hpp\
$(top_srcdir)/aedis/generic/client.hpp\
$(top_srcdir)/aedis/generic/receiver_base.hpp\
$(top_srcdir)/aedis/generic/serializer.hpp\
$(top_srcdir)/aedis/generic/detail/client_ops.hpp\
$(top_srcdir)/aedis/sentinel/command.hpp\
$(top_srcdir)/aedis/sentinel/client.hpp\
@@ -76,10 +77,9 @@ nobase_include_HEADERS =\
$(top_srcdir)/aedis/adapter/adapt.hpp\
$(top_srcdir)/aedis/adapter/response_traits.hpp\
$(top_srcdir)/aedis/resp3/node.hpp\
$(top_srcdir)/aedis/resp3/detail/composer.hpp\
$(top_srcdir)/aedis/resp3/compose.hpp\
$(top_srcdir)/aedis/resp3/detail/read_ops.hpp\
$(top_srcdir)/aedis/resp3/detail/parser.hpp\
$(top_srcdir)/aedis/resp3/serializer.hpp\
$(top_srcdir)/aedis/resp3/error.hpp\
$(top_srcdir)/aedis/resp3/type.hpp\
$(top_srcdir)/aedis/resp3/read.hpp\

View File

@@ -21,7 +21,7 @@
#include <array>
#include <aedis/resp3/type.hpp>
#include <aedis/resp3/serializer.hpp>
#include <aedis/generic/serializer.hpp>
#include <aedis/resp3/node.hpp>
#include <aedis/adapter/error.hpp>

View File

@@ -145,7 +145,7 @@
\subsubsection Serialization
In general the \c send and \c send_range functions above work with integers
and \c std::string. To send your own data type defined the \c to_string
and \c std::string. To send your own data type defined the \c to_bulk
function like this
@code
@@ -154,9 +154,10 @@
// ...
};
std::string to_string(mystruct const& obj)
void to_bulk(std::string& to, mystruct const& obj)
{
// Convert to obj string
// Convert to obj string and call
aedis::resp3::to_bulk(to, "Dummy serializaiton string.");
}
std::map<std::string, mystruct> map

View File

@@ -190,7 +190,7 @@ public:
{
auto const can_write = prepare_next();
resp3::serializer<std::string> sr(requests_);
serializer<std::string> sr(requests_);
auto const before = std::size(requests_);
sr.push(cmd, args...);
auto const after = std::size(requests_);
@@ -216,7 +216,7 @@ public:
auto const can_write = prepare_next();
resp3::serializer<std::string> sr(requests_);
serializer<std::string> sr(requests_);
auto const before = std::size(requests_);
sr.push_range2(cmd, key, begin, end);
auto const after = std::size(requests_);
@@ -242,7 +242,7 @@ public:
auto const can_write = prepare_next();
resp3::serializer<std::string> sr(requests_);
serializer<std::string> sr(requests_);
auto const before = std::size(requests_);
sr.push_range2(cmd, begin, end);
auto const after = std::size(requests_);

View File

@@ -7,10 +7,10 @@
#pragma once
#include <aedis/resp3/detail/composer.hpp>
#include <aedis/resp3/compose.hpp>
namespace aedis {
namespace resp3 {
namespace generic {
/** @brief Creates a Redis request from user data.
* \ingroup any
@@ -78,10 +78,10 @@ public:
void push(Command cmd, Ts const&... args)
{
auto constexpr pack_size = sizeof...(Ts);
detail::add_header(*request_, 1 + pack_size);
resp3::add_header(*request_, 1 + pack_size);
detail::add_bulk(*request_, to_string(cmd));
(detail::add_bulk(*request_, args), ...);
resp3::add_bulk(*request_, to_string(cmd));
(resp3::add_bulk(*request_, args), ...);
}
/** @brief Appends a new command to the end of the request.
@@ -112,14 +112,14 @@ public:
if (begin == end)
return;
auto constexpr size = detail::value_type_size<value_type>::size;
auto constexpr size = resp3::bulk_counter<value_type>::size;
auto const distance = std::distance(begin, end);
detail::add_header(*request_, 2 + size * distance);
detail::add_bulk(*request_, to_string(cmd));
detail::add_bulk(*request_, key);
resp3::add_header(*request_, 2 + size * distance);
resp3::add_bulk(*request_, to_string(cmd));
resp3::add_bulk(*request_, key);
for (; begin != end; ++begin)
detail::add_bulk(*request_, *begin);
resp3::add_bulk(*request_, *begin);
}
/** @brief Appends a new command to the end of the request.
@@ -147,13 +147,13 @@ public:
if (begin == end)
return;
auto constexpr size = detail::value_type_size<value_type>::size;
auto constexpr size = resp3::bulk_counter<value_type>::size;
auto const distance = std::distance(begin, end);
detail::add_header(*request_, 1 + size * distance);
detail::add_bulk(*request_, to_string(cmd));
resp3::add_header(*request_, 1 + size * distance);
resp3::add_bulk(*request_, to_string(cmd));
for (; begin != end; ++begin)
detail::add_bulk(*request_, *begin);
resp3::add_bulk(*request_, *begin);
}
/** \brief Sends a range.
@@ -188,5 +188,5 @@ make_serializer(std::basic_string<CharT, Traits, Allocator>& storage)
return serializer<std::basic_string<CharT, Traits, Allocator>>(storage);
}
} // resp3
} // generic
} // aedis

View File

@@ -10,8 +10,6 @@
#include <ostream>
#include <string>
#include <aedis/resp3/serializer.hpp>
namespace aedis {
namespace redis {

View File

@@ -13,6 +13,29 @@
namespace aedis {
namespace resp3 {
/** @brief Adds data to stora.
* @ingroup any
*/
template <class Storage>
void to_bulk(Storage& to, std::string_view data)
{
auto const str = std::to_string(std::size(data));
to += "$";
to.append(std::cbegin(str), std::cend(str));
to += "\r\n";
to += data;
to += "\r\n";
}
template <class Storage, class T, typename = typename std::enable_if<std::is_integral<T>::value>::type>
void to_bulk(Storage& to, T n)
{
auto const s = std::to_string(n);
to_bulk(to, std::string_view{s});
}
namespace detail {
template <class>
@@ -29,6 +52,30 @@ struct needs_to_string<char[N]> : std::false_type {};
template <std::size_t N>
struct needs_to_string<char const[N]> : std::false_type {};
template <class Storage, class T>
struct add_bulk_impl {
static void add(Storage& to, T const& from)
{
using namespace aedis::resp3;
to_bulk(to, from);
}
};
template <class Storage, class U, class V>
struct add_bulk_impl<Storage, std::pair<U, V>> {
static void add(Storage& to, std::pair<U, V> const& from)
{
using namespace aedis::resp3;
to_bulk(to, from.first);
to_bulk(to, from.second);
}
};
} // detail
/** @brief Adds a resp3 header to the store to.
* @ingroup any
*/
template <class Storage>
void add_header(Storage& to, std::size_t size)
{
@@ -39,45 +86,32 @@ void add_header(Storage& to, std::size_t size)
to += "\r\n";
}
template <class Storage>
void add_bulk(Storage& to, std::string_view data)
{
auto const str = std::to_string(std::size(data));
to += "$";
to.append(std::cbegin(str), std::cend(str));
to += "\r\n";
to += data;
to += "\r\n";
}
/** @brief Adds a rep3 bulk to the storage.
* @ingroup any
*
* This function adds \c data as a bulk string to the storage \c to.
*/
template <class Storage, class T>
void add_bulk(Storage& to, T const& data, typename std::enable_if<needs_to_string<T>::value, bool>::type = false)
void add_bulk(Storage& to, T const& data)
{
using std::to_string;
auto const s = to_string(data);
add_bulk(to, s);
detail::add_bulk_impl<Storage, T>::add(to, data);
}
// Consider adding overload for std::tuple.
// Overload for pairs.
template <class Storage, class T1, class T2>
void add_bulk(Storage& to, std::pair<T1, T2> const& pair)
{
add_bulk(to, pair.first);
add_bulk(to, pair.second);
}
/** @brief Counts the number of bulks required by a given type.
* @ingroup any
*/
template <class>
struct bulk_counter;
template <class>
struct value_type_size {
struct bulk_counter {
static constexpr auto size = 1U;
};
template <class T, class U>
struct value_type_size<std::pair<T, U>> {
struct bulk_counter<std::pair<T, U>> {
static constexpr auto size = 2U;
};
} // detail
} // resp3
} // aedis

View File

@@ -21,8 +21,7 @@ AC_CHECK_TYPES([ptrdiff_t])
AX_CXX_COMPILE_STDCXX(17, , mandatory)
AX_CXX_COMPILE_STDCXX(20, , optional)
AM_CONDITIONAL(HAVE_CXX17,[test $HAVE_CXX17])
AM_CONDITIONAL(HAVE_CXX20,[test $HAVE_CXX20])
AM_CONDITIONAL(HAVE_CXX20,[test x$HAVE_CXX20 == x1])
AC_CONFIG_FILES([Makefile doc/Doxyfile])
AC_OUTPUT

View File

@@ -34,9 +34,9 @@ bool operator<(mystruct const& a, mystruct const& b)
}
// Dumy serialization.
std::string to_string(mystruct const& obj)
void to_bulk(std::string& to, mystruct const& obj)
{
return "Dummy serializaiton string.";
aedis::resp3::to_bulk(to, "Dummy serializaiton string.");
}
// Dummy deserialization.

View File

@@ -17,7 +17,7 @@ namespace net = boost::asio;
namespace resp3 = aedis::resp3;
using aedis::redis::command;
using aedis::resp3::make_serializer;
using aedis::generic::make_serializer;
using aedis::adapter::adapt;
using net::dynamic_buffer;
using net::ip::tcp;

View File

@@ -18,7 +18,7 @@ namespace net = boost::asio;
namespace resp3 = aedis::resp3;
using aedis::redis::command;
using aedis::resp3::make_serializer;
using aedis::generic::make_serializer;
using aedis::resp3::node;
using aedis::adapter::adapt;
using net::ip::tcp;