|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Marcelo Zimbres Silva ([email protected]), |
| 3 | +// Nikolai Vladimirov (TODO) |
| 4 | +// |
| 5 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | +// |
| 8 | + |
| 9 | +#ifndef BOOST_REDIS_FLAT_RESPONSE_HPP |
| 10 | +#define BOOST_REDIS_FLAT_RESPONSE_HPP |
| 11 | + |
| 12 | +#include <boost/redis/adapter/result.hpp> |
| 13 | +#include <boost/redis/resp3/node.hpp> |
| 14 | + |
| 15 | +#include <boost/system/error_code.hpp> |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <tuple> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +namespace boost::redis { |
| 22 | + |
| 23 | +// Similar to the generic_response but its data is stored flat |
| 24 | +// internally. |
| 25 | +struct generic_flat_response_value { |
| 26 | +public: |
| 27 | + /** @brief Reserve capacity |
| 28 | + * |
| 29 | + * Reserve memory for incoming data. |
| 30 | + * |
| 31 | + * @param bytes Number of bytes to reserve for data. |
| 32 | + * @param nodes Number of nodes to reserver. |
| 33 | + */ |
| 34 | + void reserve(std::size_t bytes, std::size_t nodes); |
| 35 | + |
| 36 | + /** @brief Clear both the data and the node buffers |
| 37 | + * |
| 38 | + * @Note: A `boost::redis:.generic_flat_response` can contain the |
| 39 | + * response to multiple Redis commands and server pushes. Calling |
| 40 | + * this function will erase everything contianed in it. To clear |
| 41 | + * only one response/push use `boost::redis::consume_one`. |
| 42 | + */ |
| 43 | + void clear(); |
| 44 | + |
| 45 | + /// Returns the size of the data buffer |
| 46 | + auto data_size() const noexcept -> std::size_t |
| 47 | + { return data_.size(); } |
| 48 | + |
| 49 | + /** @brief Return the RESP3 response |
| 50 | + * |
| 51 | + * The data member in each `boost::redis::offset_string` are all |
| 52 | + * set and therefore safe to use. |
| 53 | + */ |
| 54 | + auto get_view() const -> resp3::offset_response const& |
| 55 | + { return view_; } |
| 56 | + |
| 57 | + /** @brief Return the RESP3 response |
| 58 | + * |
| 59 | + * The data member in each `boost::redis::offset_string` are all |
| 60 | + * set and therefore safe to use. |
| 61 | + */ |
| 62 | + auto get_view() -> resp3::offset_response& |
| 63 | + { return view_; } |
| 64 | + |
| 65 | + /// Push a new node to the response |
| 66 | + void push(resp3::node_view const& nd); |
| 67 | + |
| 68 | + /** @brief Returns the number of times reallocations |
| 69 | + * |
| 70 | + * Each call to the push might result in a memory reallocation. |
| 71 | + * This number function returns how many reallocations were |
| 72 | + * detected and can be useful to determine how much memory to |
| 73 | + * reserve upfront. |
| 74 | + */ |
| 75 | + auto get_reallocs() const noexcept |
| 76 | + { return reallocs_; } |
| 77 | + |
| 78 | + /** @brief Notify the object that all nodes were pushed. |
| 79 | + * |
| 80 | + * This function is called automativally by the library. |
| 81 | + */ |
| 82 | + void notify_done(); |
| 83 | + |
| 84 | + /// Returns the number of complete RESP3 messages contained in this object. |
| 85 | + std::size_t get_total_msgs() const noexcept |
| 86 | + { return total_msgs_; } |
| 87 | + |
| 88 | +private: |
| 89 | + void add_node_impl(resp3::node_view const& nd); |
| 90 | + |
| 91 | + std::string data_; |
| 92 | + resp3::offset_response view_; |
| 93 | + std::size_t pos_ = 0u; |
| 94 | + std::size_t reallocs_ = 0u; |
| 95 | + |
| 96 | + // The number of messages contained in this object. |
| 97 | + std::size_t total_msgs_ = 0u; |
| 98 | +}; |
| 99 | + |
| 100 | +} // namespace boost::redis |
| 101 | + |
| 102 | +#endif // BOOST_REDIS_FLAT_RESPONSE_HPP |
0 commit comments