adapters.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #ifndef BOOST_REDIS_ADAPTER_ADAPTERS_HPP
  7. #define BOOST_REDIS_ADAPTER_ADAPTERS_HPP
  8. #include <boost/redis/error.hpp>
  9. #include <boost/redis/resp3/type.hpp>
  10. #include <boost/redis/resp3/serialization.hpp>
  11. #include <boost/redis/resp3/node.hpp>
  12. #include <boost/redis/adapter/result.hpp>
  13. #include <boost/assert.hpp>
  14. #include <set>
  15. #include <optional>
  16. #include <unordered_set>
  17. #include <forward_list>
  18. #include <system_error>
  19. #include <map>
  20. #include <unordered_map>
  21. #include <list>
  22. #include <deque>
  23. #include <vector>
  24. #include <array>
  25. #include <string_view>
  26. #include <charconv>
  27. // See https://stackoverflow.com/a/31658120/1077832
  28. #include<ciso646>
  29. #ifdef _LIBCPP_VERSION
  30. #else
  31. #include <cstdlib>
  32. #endif
  33. namespace boost::redis::adapter::detail
  34. {
  35. // Serialization.
  36. template <class T>
  37. auto boost_redis_from_bulk(T& i, std::string_view sv, system::error_code& ec) -> typename std::enable_if<std::is_integral<T>::value, void>::type
  38. {
  39. auto const res = std::from_chars(sv.data(), sv.data() + std::size(sv), i);
  40. if (res.ec != std::errc())
  41. ec = redis::error::not_a_number;
  42. }
  43. inline
  44. void boost_redis_from_bulk(bool& t, std::string_view sv, system::error_code&)
  45. {
  46. t = *sv.data() == 't';
  47. }
  48. inline
  49. void boost_redis_from_bulk(double& d, std::string_view sv, system::error_code& ec)
  50. {
  51. #ifdef _LIBCPP_VERSION
  52. // The string in sv is not null terminated and we also don't know
  53. // if there is enough space at the end for a null char. The easiest
  54. // thing to do is to create a temporary.
  55. std::string const tmp{sv.data(), sv.data() + std::size(sv)};
  56. char* end{};
  57. d = std::strtod(tmp.data(), &end);
  58. if (d == HUGE_VAL || d == 0)
  59. ec = redis::error::not_a_double;
  60. #else
  61. auto const res = std::from_chars(sv.data(), sv.data() + std::size(sv), d);
  62. if (res.ec != std::errc())
  63. ec = redis::error::not_a_double;
  64. #endif // _LIBCPP_VERSION
  65. }
  66. template <class CharT, class Traits, class Allocator>
  67. void
  68. boost_redis_from_bulk(
  69. std::basic_string<CharT, Traits, Allocator>& s,
  70. std::string_view sv,
  71. system::error_code&)
  72. {
  73. s.append(sv.data(), sv.size());
  74. }
  75. //================================================
  76. template <class Result>
  77. class general_aggregate {
  78. private:
  79. Result* result_;
  80. public:
  81. explicit general_aggregate(Result* c = nullptr): result_(c) {}
  82. template <class String>
  83. void operator()(resp3::basic_node<String> const& nd, system::error_code&)
  84. {
  85. BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
  86. switch (nd.data_type) {
  87. case resp3::type::blob_error:
  88. case resp3::type::simple_error:
  89. *result_ = error{nd.data_type, std::string{std::cbegin(nd.value), std::cend(nd.value)}};
  90. break;
  91. default:
  92. result_->value().push_back({nd.data_type, nd.aggregate_size, nd.depth, std::string{std::cbegin(nd.value), std::cend(nd.value)}});
  93. }
  94. }
  95. };
  96. template <class Node>
  97. class general_simple {
  98. private:
  99. Node* result_;
  100. public:
  101. explicit general_simple(Node* t = nullptr) : result_(t) {}
  102. template <class String>
  103. void operator()(resp3::basic_node<String> const& nd, system::error_code&)
  104. {
  105. BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
  106. switch (nd.data_type) {
  107. case resp3::type::blob_error:
  108. case resp3::type::simple_error:
  109. *result_ = error{nd.data_type, std::string{std::cbegin(nd.value), std::cend(nd.value)}};
  110. break;
  111. default:
  112. result_->value().data_type = nd.data_type;
  113. result_->value().aggregate_size = nd.aggregate_size;
  114. result_->value().depth = nd.depth;
  115. result_->value().value.assign(nd.value.data(), nd.value.size());
  116. }
  117. }
  118. };
  119. template <class Result>
  120. class simple_impl {
  121. public:
  122. void on_value_available(Result&) {}
  123. template <class String>
  124. void operator()(Result& result, resp3::basic_node<String> const& n, system::error_code& ec)
  125. {
  126. if (is_aggregate(n.data_type)) {
  127. ec = redis::error::expects_resp3_simple_type;
  128. return;
  129. }
  130. boost_redis_from_bulk(result, n.value, ec);
  131. }
  132. };
  133. template <class Result>
  134. class set_impl {
  135. private:
  136. typename Result::iterator hint_;
  137. public:
  138. void on_value_available(Result& result)
  139. { hint_ = std::end(result); }
  140. template <class String>
  141. void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
  142. {
  143. if (is_aggregate(nd.data_type)) {
  144. if (nd.data_type != resp3::type::set)
  145. ec = redis::error::expects_resp3_set;
  146. return;
  147. }
  148. BOOST_ASSERT(nd.aggregate_size == 1);
  149. if (nd.depth < 1) {
  150. ec = redis::error::expects_resp3_set;
  151. return;
  152. }
  153. typename Result::key_type obj;
  154. boost_redis_from_bulk(obj, nd.value, ec);
  155. hint_ = result.insert(hint_, std::move(obj));
  156. }
  157. };
  158. template <class Result>
  159. class map_impl {
  160. private:
  161. typename Result::iterator current_;
  162. bool on_key_ = true;
  163. public:
  164. void on_value_available(Result& result)
  165. { current_ = std::end(result); }
  166. template <class String>
  167. void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
  168. {
  169. if (is_aggregate(nd.data_type)) {
  170. if (element_multiplicity(nd.data_type) != 2)
  171. ec = redis::error::expects_resp3_map;
  172. return;
  173. }
  174. BOOST_ASSERT(nd.aggregate_size == 1);
  175. if (nd.depth < 1) {
  176. ec = redis::error::expects_resp3_map;
  177. return;
  178. }
  179. if (on_key_) {
  180. typename Result::key_type obj;
  181. boost_redis_from_bulk(obj, nd.value, ec);
  182. current_ = result.insert(current_, {std::move(obj), {}});
  183. } else {
  184. typename Result::mapped_type obj;
  185. boost_redis_from_bulk(obj, nd.value, ec);
  186. current_->second = std::move(obj);
  187. }
  188. on_key_ = !on_key_;
  189. }
  190. };
  191. template <class Result>
  192. class vector_impl {
  193. public:
  194. void on_value_available(Result& ) { }
  195. template <class String>
  196. void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
  197. {
  198. if (is_aggregate(nd.data_type)) {
  199. auto const m = element_multiplicity(nd.data_type);
  200. result.reserve(result.size() + m * nd.aggregate_size);
  201. } else {
  202. result.push_back({});
  203. boost_redis_from_bulk(result.back(), nd.value, ec);
  204. }
  205. }
  206. };
  207. template <class Result>
  208. class array_impl {
  209. private:
  210. int i_ = -1;
  211. public:
  212. void on_value_available(Result& ) { }
  213. template <class String>
  214. void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
  215. {
  216. if (is_aggregate(nd.data_type)) {
  217. if (i_ != -1) {
  218. ec = redis::error::nested_aggregate_not_supported;
  219. return;
  220. }
  221. if (result.size() != nd.aggregate_size * element_multiplicity(nd.data_type)) {
  222. ec = redis::error::incompatible_size;
  223. return;
  224. }
  225. } else {
  226. if (i_ == -1) {
  227. ec = redis::error::expects_resp3_aggregate;
  228. return;
  229. }
  230. BOOST_ASSERT(nd.aggregate_size == 1);
  231. boost_redis_from_bulk(result.at(i_), nd.value, ec);
  232. }
  233. ++i_;
  234. }
  235. };
  236. template <class Result>
  237. struct list_impl {
  238. void on_value_available(Result& ) { }
  239. template <class String>
  240. void operator()(Result& result, resp3::basic_node<String> const& nd, system::error_code& ec)
  241. {
  242. if (!is_aggregate(nd.data_type)) {
  243. BOOST_ASSERT(nd.aggregate_size == 1);
  244. if (nd.depth < 1) {
  245. ec = redis::error::expects_resp3_aggregate;
  246. return;
  247. }
  248. result.push_back({});
  249. boost_redis_from_bulk(result.back(), nd.value, ec);
  250. }
  251. }
  252. };
  253. //---------------------------------------------------
  254. template <class T>
  255. struct impl_map { using type = simple_impl<T>; };
  256. template <class Key, class Compare, class Allocator>
  257. struct impl_map<std::set<Key, Compare, Allocator>> { using type = set_impl<std::set<Key, Compare, Allocator>>; };
  258. template <class Key, class Compare, class Allocator>
  259. struct impl_map<std::multiset<Key, Compare, Allocator>> { using type = set_impl<std::multiset<Key, Compare, Allocator>>; };
  260. template <class Key, class Hash, class KeyEqual, class Allocator>
  261. struct impl_map<std::unordered_set<Key, Hash, KeyEqual, Allocator>> { using type = set_impl<std::unordered_set<Key, Hash, KeyEqual, Allocator>>; };
  262. template <class Key, class Hash, class KeyEqual, class Allocator>
  263. struct impl_map<std::unordered_multiset<Key, Hash, KeyEqual, Allocator>> { using type = set_impl<std::unordered_multiset<Key, Hash, KeyEqual, Allocator>>; };
  264. template <class Key, class T, class Compare, class Allocator>
  265. struct impl_map<std::map<Key, T, Compare, Allocator>> { using type = map_impl<std::map<Key, T, Compare, Allocator>>; };
  266. template <class Key, class T, class Compare, class Allocator>
  267. struct impl_map<std::multimap<Key, T, Compare, Allocator>> { using type = map_impl<std::multimap<Key, T, Compare, Allocator>>; };
  268. template <class Key, class Hash, class KeyEqual, class Allocator>
  269. struct impl_map<std::unordered_map<Key, Hash, KeyEqual, Allocator>> { using type = map_impl<std::unordered_map<Key, Hash, KeyEqual, Allocator>>; };
  270. template <class Key, class Hash, class KeyEqual, class Allocator>
  271. struct impl_map<std::unordered_multimap<Key, Hash, KeyEqual, Allocator>> { using type = map_impl<std::unordered_multimap<Key, Hash, KeyEqual, Allocator>>; };
  272. template <class T, class Allocator>
  273. struct impl_map<std::vector<T, Allocator>> { using type = vector_impl<std::vector<T, Allocator>>; };
  274. template <class T, std::size_t N>
  275. struct impl_map<std::array<T, N>> { using type = array_impl<std::array<T, N>>; };
  276. template <class T, class Allocator>
  277. struct impl_map<std::list<T, Allocator>> { using type = list_impl<std::list<T, Allocator>>; };
  278. template <class T, class Allocator>
  279. struct impl_map<std::deque<T, Allocator>> { using type = list_impl<std::deque<T, Allocator>>; };
  280. //---------------------------------------------------
  281. template <class>
  282. class wrapper;
  283. template <class Result>
  284. class wrapper<result<Result>> {
  285. public:
  286. using response_type = result<Result>;
  287. private:
  288. response_type* result_;
  289. typename impl_map<Result>::type impl_;
  290. template <class String>
  291. bool set_if_resp3_error(resp3::basic_node<String> const& nd) noexcept
  292. {
  293. switch (nd.data_type) {
  294. case resp3::type::null:
  295. case resp3::type::simple_error:
  296. case resp3::type::blob_error:
  297. *result_ = error{nd.data_type, {std::cbegin(nd.value), std::cend(nd.value)}};
  298. return true;
  299. default:
  300. return false;
  301. }
  302. }
  303. public:
  304. explicit wrapper(response_type* t = nullptr) : result_(t)
  305. {
  306. if (result_) {
  307. result_->value() = Result{};
  308. impl_.on_value_available(result_->value());
  309. }
  310. }
  311. template <class String>
  312. void operator()(resp3::basic_node<String> const& nd, system::error_code& ec)
  313. {
  314. BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
  315. if (result_->has_error())
  316. return;
  317. if (set_if_resp3_error(nd))
  318. return;
  319. BOOST_ASSERT(result_);
  320. impl_(result_->value(), nd, ec);
  321. }
  322. };
  323. template <class T>
  324. class wrapper<result<std::optional<T>>> {
  325. public:
  326. using response_type = result<std::optional<T>>;
  327. private:
  328. response_type* result_;
  329. typename impl_map<T>::type impl_{};
  330. template <class String>
  331. bool set_if_resp3_error(resp3::basic_node<String> const& nd) noexcept
  332. {
  333. switch (nd.data_type) {
  334. case resp3::type::blob_error:
  335. case resp3::type::simple_error:
  336. *result_ = error{nd.data_type, {std::cbegin(nd.value), std::cend(nd.value)}};
  337. return true;
  338. default:
  339. return false;
  340. }
  341. }
  342. public:
  343. explicit wrapper(response_type* o = nullptr) : result_(o) {}
  344. template <class String>
  345. void
  346. operator()(
  347. resp3::basic_node<String> const& nd,
  348. system::error_code& ec)
  349. {
  350. BOOST_ASSERT_MSG(!!result_, "Unexpected null pointer");
  351. if (result_->has_error())
  352. return;
  353. if (set_if_resp3_error(nd))
  354. return;
  355. if (nd.data_type == resp3::type::null)
  356. return;
  357. if (!result_->value().has_value()) {
  358. result_->value() = T{};
  359. impl_.on_value_available(result_->value().value());
  360. }
  361. impl_(result_->value().value(), nd, ec);
  362. }
  363. };
  364. } // boost::redis::adapter::detail
  365. #endif // BOOST_REDIS_ADAPTER_ADAPTERS_HPP