MySQL 8.4.2
Source Code Documentation
buffer_sequence_view.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/// @file buffer_sequence_view.h
25///
26/// Container class that provides a sequence of buffers to the caller.
27/// This is intended for capturing the output from compressors.
28
29#ifndef MYSQL_BINLOG_EVENT_COMPRESSION_BUFFER_BUFFER_SEQUENCE_VIEW_H
30#define MYSQL_BINLOG_EVENT_COMPRESSION_BUFFER_BUFFER_SEQUENCE_VIEW_H
31
32#include <algorithm> // std::min
33#include <cassert> // assert
34#include <cstring> // std::memcpy
35#include <limits> // std::numeric_limits
36#include <memory> // std::allocator
37#include <type_traits> // std::conditional
38#include <vector> // std::vector
39#include "mysql/binlog/event/resource/allocator.h" // mysql::binlog::event::resource::Allocator
40
41#include "mysql/binlog/event/compression/buffer/buffer_view.h" // buffer::Buffer_view
42#include "mysql/binlog/event/compression/buffer/grow_calculator.h" // buffer::Grow_calculator
43#include "mysql/binlog/event/compression/buffer/grow_status.h" // buffer::Grow_status
44#include "mysql/binlog/event/nodiscard.h" // NODISCARD
45
46#include "mysql/binlog/event/wrapper_functions.h" // BAPI_TRACE
47
48/// @addtogroup GroupLibsMysqlBinlogEvent
49/// @{
50
52
53/// Sequence of memory buffers.
54///
55/// This is a minimal class with just a sequence of buffers. It does
56/// not have a read/write position (@see Rw_buffer_sequence). It does
57/// not have methods to grow the buffer sequence (@see
58/// Managed_buffer_sequence).
59///
60/// @tparam Char_tp The type of elements stored in the buffer:
61/// typically unsigned char.
62///
63/// @tparam Container_tp The type of container to hold the buffers.
64/// This defaults to std::vector, but std::list is also possible.
65///
66/// @tparam const_tp If true, use const iterators instead of non-const
67/// iterators to represent the beginning and end of the container.
68template <class Char_tp = unsigned char,
69 template <class Element_tp, class Allocator_tp> class Container_tp =
71 bool const_tp = false>
73 public:
74 using Char_t = Char_tp;
75 using Size_t = std::size_t;
79 using Container_t = Container_tp<Buffer_view_t, Buffer_allocator_t>;
80 using Const_iterator_t = typename Container_t::const_iterator;
81 using Iterator_t =
82 typename std::conditional<const_tp, Const_iterator_t,
83 typename Container_t::iterator>::type;
84
85 private:
86 /// Indicates that @c m_size has not yet been computed.
87 static constexpr Size_t uninitialized_size =
88 std::numeric_limits<Size_t>::max();
89
90 public:
91 /// Construct a Buffer_sequence_view with buffers in the range given by
92 /// the iterators.
93 ///
94 /// This copies only the iterators; the underlying container and the
95 /// buffers contained in the container are not copied.
96 ///
97 /// @param begin_arg Iterator to the first buffer.
98 ///
99 /// @param end_arg Iterator to one-past-the-last buffer.
100 ///
101 /// @param size_arg The total size of all buffers from begin_arg to
102 /// end_arg. This is an optimization only: if the parameter is
103 /// omitted, it will be computed the next time it is needed.
105 Size_t size_arg = uninitialized_size)
106 : m_begin(begin_arg), m_end(end_arg), m_size(size_arg) {}
107
108 // Disallow copy, implement move.
110 Buffer_sequence_view(Buffer_sequence_view &&other) noexcept = default;
113
114 virtual ~Buffer_sequence_view() = default;
115
116 /// Iterator to the first buffer.
118
119 /// Iterator to the last buffer.
120 Iterator_t end() { return m_end; }
121
122 /// Iterator to the first buffer.
123 Const_iterator_t begin() const { return m_begin; }
124
125 /// Iterator to the last buffer.
126 Const_iterator_t end() const { return m_end; }
127
128 /// Const iterator pointing to the first buffer.
129 Const_iterator_t cbegin() const { return m_begin; }
130
131 /// Const iterator pointing to the last buffer.
132 Const_iterator_t cend() const { return m_end; }
133
134 /// Copy all data to the given, contiguous output buffer.
135 ///
136 /// The caller is responsible for providing a buffer of at
137 /// least @c size() bytes.
138 ///
139 /// @param destination The target buffer.
140 template <class Destination_char_t>
141 void copy(Destination_char_t *destination) const {
143 Size_t position = 0;
144 for (const auto &buffer : *this) {
145 std::memcpy(destination + position, buffer.data(), buffer.size());
146 position += buffer.size();
147 }
148 }
149
150 /// Return a copy of all the data in this object, as a `std::string`
151 /// object.
152 template <class Str_char_t = char,
153 class Str_traits_t = std::char_traits<Str_char_t>,
154 class Str_allocator_t = std::allocator<Str_char_t>>
155 std::basic_string<Str_char_t, Str_traits_t, Str_allocator_t> str(
156 const Str_allocator_t &allocator = Str_allocator_t()) {
157 std::basic_string<Str_char_t, Str_traits_t, Str_allocator_t> ret(
158 this->size(), '\0', allocator);
159 copy(ret.data());
160 return ret;
161 }
162
163 /// Return the total size of all buffers.
164 Size_t size() const {
165 if (m_size == uninitialized_size) {
166 Size_t size = 0;
167 for (const auto &buffer : *this) size += buffer.size();
168 m_size = size;
169 }
170 return m_size;
171 }
172
173 /// In debug mode, return a string that describes the internal
174 /// structure of this object, to use for debugging.
175 ///
176 /// @param show_contents If true, includes the buffer contents.
177 /// Otherwise, just pointers and sizes.
178 ///
179 /// @param indent If 0, put all info on one line. Otherwise, put
180 /// each field on its own line and indent the given number of
181 /// two-space levels.
182 ///
183 /// @return String that describes the internal structure of this
184 /// Buffer_sequence_view.
185 std::string debug_string([[maybe_unused]] bool show_contents = false,
186 [[maybe_unused]] int indent = 0) const {
187#ifdef NDEBUG
188 return "";
189#else
191 // whitespace following the comma: newline + indentation, or just space
192 std::string ws;
193 if (indent != 0)
194 ws = std::string("\n") +
195 std::string(static_cast<std::string::size_type>(indent * 2), ' ');
196 else
197 ws = " ";
198 // separator = comma + whitespace
199 std::string sep = "," + ws;
200 // whitespace / separator with one level deeper indentation
201 std::string ws2 = (indent != 0) ? (ws + " ") : ws;
202 std::string sep2 = (indent != 0) ? (sep + " ") : sep;
203 // clang-format off
204 ss << "Buffer_sequence_view(ptr=" << (const void *)this
205 << sep << "size=" << size()
206 << sep << "buffers.ptr=" << (const void *)&*this->begin()
207 << sep << "buffers=[";
208 // clang-format on
209 bool first = true;
210 for (auto &buffer : *this) {
211 if (first) {
212 if (indent != 0) ss << ws2;
213 first = false;
214 } else {
215 ss << sep2;
216 }
217 ss << buffer.debug_string(show_contents);
218 }
219 ss << "])";
220 return ss.str();
221#endif
222 }
223
224 private:
225 /// Iterator to beginning of buffer.
227
228 /// Iterator to end of buffer.
230
231 /// Total size of all buffers, cached.
232 mutable Size_t m_size;
233};
234
235} // namespace mysql::binlog::event::compression::buffer
236
237/// @}
238
239#endif // MYSQL_BINLOG_EVENT_COMPRESSION_BUFFER_BUFFER_SEQUENCE_VIEW_H
Class that groups a pointer+size as one object, without managing the memory for it.
Sequence of memory buffers.
Definition: buffer_sequence_view.h:72
Container_tp< Buffer_view_t, Buffer_allocator_t > Container_t
Definition: buffer_sequence_view.h:79
Const_iterator_t cend() const
Const iterator pointing to the last buffer.
Definition: buffer_sequence_view.h:132
Iterator_t begin()
Iterator to the first buffer.
Definition: buffer_sequence_view.h:117
Size_t size() const
Return the total size of all buffers.
Definition: buffer_sequence_view.h:164
Buffer_sequence_view & operator=(Buffer_sequence_view &)=delete
typename std::conditional< const_tp, Const_iterator_t, typename Container_t::iterator >::type Iterator_t
Definition: buffer_sequence_view.h:83
std::string debug_string(bool show_contents=false, int indent=0) const
In debug mode, return a string that describes the internal structure of this object,...
Definition: buffer_sequence_view.h:185
Iterator_t end()
Iterator to the last buffer.
Definition: buffer_sequence_view.h:120
std::size_t Size_t
Definition: buffer_sequence_view.h:75
Iterator_t m_begin
Iterator to beginning of buffer.
Definition: buffer_sequence_view.h:226
Const_iterator_t end() const
Iterator to the last buffer.
Definition: buffer_sequence_view.h:126
Buffer_sequence_view(Buffer_sequence_view &&other) noexcept=default
Const_iterator_t cbegin() const
Const iterator pointing to the first buffer.
Definition: buffer_sequence_view.h:129
Buffer_sequence_view & operator=(Buffer_sequence_view &&) noexcept=default
Char_tp Char_t
Definition: buffer_sequence_view.h:74
Iterator_t m_end
Iterator to end of buffer.
Definition: buffer_sequence_view.h:229
std::basic_string< Str_char_t, Str_traits_t, Str_allocator_t > str(const Str_allocator_t &allocator=Str_allocator_t())
Return a copy of all the data in this object, as a std::string object.
Definition: buffer_sequence_view.h:155
Const_iterator_t begin() const
Iterator to the first buffer.
Definition: buffer_sequence_view.h:123
typename Container_t::const_iterator Const_iterator_t
Definition: buffer_sequence_view.h:80
void copy(Destination_char_t *destination) const
Copy all data to the given, contiguous output buffer.
Definition: buffer_sequence_view.h:141
static constexpr Size_t uninitialized_size
Indicates that m_size has not yet been computed.
Definition: buffer_sequence_view.h:87
Buffer_sequence_view(Iterator_t begin_arg, Iterator_t end_arg, Size_t size_arg=uninitialized_size)
Construct a Buffer_sequence_view with buffers in the range given by the iterators.
Definition: buffer_sequence_view.h:104
Size_t m_size
Total size of all buffers, cached.
Definition: buffer_sequence_view.h:232
Allocator using a Memory_resource to do the allocator.
Definition: allocator.h:54
void * data() const noexcept
Definition: buffer.h:119
size_t size() const noexcept
Definition: buffer.h:120
Allocator class that uses a polymorphic Memory_resource to allocate memory.
Definition: buffer_sequence_view.h:51
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2874
static task_arg end_arg()
Definition: task.h:207
Contains wrapper functions for memory allocation and deallocation.
#define BAPI_TRACE
Definition: wrapper_functions.h:65