MySQL 8.0.40
Source Code Documentation
decompressing_event_object_istream.h
Go to the documentation of this file.
1/* Copyright (c) 2019, 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#ifndef BINLOG_DECOMPRESSING_EVENT_OBJECT_ISTREAM_H_
25#define BINLOG_DECOMPRESSING_EVENT_OBJECT_ISTREAM_H_
26
27#include <queue>
28
31#include "sql/binlog_reader.h"
33
34/// @addtogroup Replication
35/// @{
36///
37/// @file decompressing_event_object_istream.h
38///
39/// Stream class that yields Log_event objects, including events
40/// contained in Transaction_payload_log_events.
41
42namespace binlog {
43
44/// Stream class that yields Log_event objects from a source. The
45/// source can be a Transaction_payload_log_event, in which case it will
46/// produce the contained events. Or it can be a file, in which case it
47/// will yield all events in the file, and if there is a
48/// Transaction_payload_log_event, it will yield first that and then all
49/// the contained events.
50///
51/// The recommended use pattern is:
52///
53/// @code
54/// while (stream >> event) {
55/// // handle event
56/// }
57/// if (stream.has_error()) {
58/// // handle error
59/// }
60/// @endcode
61///
62/// This class actually enforces that you call `has_error` after the
63/// loop; failure to do so will result in an assertion in debug mode.
64/// In the unlikely case that your code doesn't need to check for
65/// errors, you can get rid of the assert by calling has_error() and
66/// discarding the return value.
68 public:
73 using Event_ptr_t = std::shared_ptr<Log_event>;
74 using Tple_ptr_t = std::shared_ptr<const Transaction_payload_log_event>;
78
79 /// Construct stream over a file, decompressing payload events.
80 ///
81 /// This will produce all events in the file, and in addition, each
82 /// Transaction_payload_log_event is followed by the contained
83 /// events.
84 ///
85 /// @param reader The source file to read from.
88
89 /// Construct stream over a Transaction_payload_log_event.
90 ///
91 /// This will produce all events contained in the event, but not the
92 /// event itself.
93 ///
94 /// This Decompressing_event_object_istream will, during its entire
95 /// life time, hold shared ownership of the Transaction_payload_log_event.
96 ///
97 /// @param transaction_payload_log_event The source file to read from.
98 ///
99 /// @param format_description_event The FD event used to parse events.
101 const Tple_ptr_t &transaction_payload_log_event,
102 Fde_ref_t format_description_event);
103
104 /// Construct stream over a Transaction_payload_log_event.
105 ///
106 /// This will produce all events contained in the event, but not the
107 /// event itself.
108 ///
109 /// This Decompressing_event_object_istream will, during its entire
110 /// life time, hold a pointer to the Transaction_payload_log_event,
111 /// and the caller must ensure that the event outlives the stream.
112 ///
113 /// @param transaction_payload_log_event The source file to read from.
114 ///
115 /// @param format_description_event The FD event used to parse events.
117 const Transaction_payload_log_event &transaction_payload_log_event,
118 Fde_ref_t format_description_event);
119
120#ifdef NDEBUG
122#else
124#endif
125
127 delete;
129 delete;
134
135 /// Specify whether checksums shall be verified or not.
136 ///
137 /// @param verify_checksum If true, verify checksums; otherwise
138 /// don't.
139 void set_verify_checksum(bool verify_checksum = true);
140
141 /// Read an event from the stream.
142 ///
143 /// @param out Shared pointer to the produced event. If this is a
144 /// Transaction_payload_log_event, the stream will keep a shared
145 /// pointer to it, until it has produced all the contained events.
146 ///
147 /// @return This object.
149
150 /// Indicate if EOF or error has not happened.
151 ///
152 /// @retval true last read was successful (or there was no last
153 /// read).
154 ///
155 /// @retval false last read resulted in end-of-stream or error.
156 explicit operator bool() const;
157
158 /// Indicate if EOF or error has happened. This is the negation of
159 /// `operator bool`.
160 ///
161 /// @retval false last read was successful, or there was no last
162 /// read.
163 ///
164 /// @retval true last read resulted in end-of-stream or error.
165 bool operator!() const;
166
167 /// Return true if an error has happened.
168 bool has_error() const;
169
170 /// Return a message describing the last error.
171 ///
172 /// @retval "" No error
173 ///
174 /// @retval string Error
175 std::string get_error_str() const;
176
177 /// Return the status
178 Status_t get_status() const;
179
180 /// Return const reference to Grow_calculator to the internal event buffer.
182
183 /// Set the Grow_calculator for the internal event buffer.
184 void set_grow_calculator(const Grow_calculator_t &grow_calculator);
185
186 private:
187 /// Stream of events to read from
189 /// Whether we should verify checksum. Unused!
190 bool m_verify_checksum{false};
191 /// Error from last operation
192 std::string m_error_str;
193 /// True if we have reached EOF, false otherwise.
194 bool m_end{false};
195 /// Status
197 /// Position of last event
199#ifndef NDEBUG
200 /// True if a read has failed but neither `get_error_str` nor
201 /// `has_error` has been called.
202 mutable bool m_outstanding_error{false};
203#endif
204
205 /// Policy for growing buffers in the decompressing stream
207 /// The decompression stream; non-null while we are positioned in a TPLE.
208 std::unique_ptr<Buffer_stream_t> m_buffer_istream{nullptr};
209 /// end_log_pos for the currently processed TPLE, if any
211 /// 0 when not processing a TPLE; N>0 when positioned before the Nth
212 /// embedded event of a TPLE.
214
215 /// Return the current FDE.
217
218 /// Report an error
219 ///
220 /// This sets the status as specified, and returns a stringstream to
221 /// which the caller shall write a message.
222 ///
223 /// @param status The status
224 ///
225 /// @returns Targeted_stringstream object; the error for this stream
226 /// will be set to the given status.
228
229 /// Prepare to unfold a given Transaction_payload_log_event by
230 /// setting state variables and creating the
231 /// Payload_event_buffer_istream object.
232 ///
233 /// This object will not hold ownership of the event. The caller
234 /// must ensure that the event outlives the stream.
235 ///
236 /// @param tple Event to unfold
238 do_begin_payload_event(tple, tple);
239 }
240
241 /// Prepare to unfold a given Transaction_payload_log_event by
242 /// setting state variables and creating the
243 /// Payload_event_buffer_istream object.
244 ///
245 /// This object will hold shared ownership of the event.
246 ///
247 /// @param tple Event to unfold
248 void begin_payload_event(const Tple_ptr_t &tple) {
249 do_begin_payload_event(*tple, tple);
250 }
251
252 /// Worker function implementing both forms of begin_payload_event.
253 ///
254 /// @tparam Event_ref_or_ptr Either reference-to-event or
255 /// shared-pointer-to-event.
256 ///
257 /// @param tple Event to unfold.
258 ///
259 /// @param ownership_tple Ownership handle for the event. This can
260 /// be a shared_ptr if this object should hold shared ownership, or
261 /// a reference otherwise.
262 template <class Event_ref_or_ptr>
264 const Event_ref_or_ptr &ownership_tple) {
268 assert(!m_buffer_istream);
269 try {
270 m_buffer_istream = std::make_unique<Buffer_stream_t>(ownership_tple);
271 } catch (...) {
272 // Leave m_buffer_istream empty.
273 // Report error on next invocation of `operator>>`.
274 }
275 }
276
277 /// Worker that deserializes an event from the buffer.
278 ///
279 /// @param[in] buffer Input byte buffer.
280 ///
281 /// @param[out] out Pointer to output event.
282 ///
283 /// @retval false success
284 /// @retval true error
286 Event_ptr_t &out);
287
288 /// Status from read_from_payload_stream
289 enum class Read_status { success, eof, error };
290
291 /// Read and decode next event from the Payload_log_event stream
292 ///
293 /// @param[out] out Pointer to output event.
294 ///
295 /// @retval success The event was successfully read and decoded
296 ///
297 /// @retval error An error occurred. `get_error_str` will contain
298 /// the reason.
299 ///
300 /// @retval eof Nothing was read because the read position was at
301 /// the end of the event.
303
304 /// Read and decode the next event from the binlog stream.
305 ///
306 /// @param[out] out Pointer to the output event.
307 ///
308 /// @retval false The event was successfully read and decoded.
309 ///
310 /// @retval true Error or EOF occurred. In case of error,
311 /// `get_error_str` will contain the reason.
313};
314
315} // namespace binlog
316
317/// @} (end of group Replication)
318
319#endif // ifdef BINLOG_DECOMPRESSING_EVENT_OBJECT_ISTREAM_H_
Interface class that all specializations of template <...> Basic_binlog_file_reader inherit from.
Definition: binlog_reader.h:233
Definition: log_event.h:3699
const Log_event_header * header() const
Return a const pointer to the header of the log event.
Definition: binlog_event.h:926
For binlog version 4.
Definition: control_events.h:231
unsigned long long log_pos
Definition: binlog_event.h:694
Stream class that yields a stream of byte buffers, each holding the raw decompressed data of one even...
Definition: payload_event_buffer_istream.h:59
Managed_buffer_t::Buffer_view_t Buffer_view_t
Definition: payload_event_buffer_istream.h:68
mysqlns::buffer::Grow_calculator Grow_calculator_t
Definition: payload_event_buffer_istream.h:63
std::shared_ptr< Buffer_view_t > Buffer_ptr_t
Definition: payload_event_buffer_istream.h:69
Stream class that yields Log_event objects from a source.
Definition: decompressing_event_object_istream.h:67
~Decompressing_event_object_istream()
Definition: decompressing_event_object_istream.cc:59
Buffer_stream_t::Buffer_view_t Buffer_view_t
Definition: decompressing_event_object_istream.h:71
bool decode_from_buffer(Buffer_view_t &buffer, Event_ptr_t &out)
Worker that deserializes an event from the buffer.
Definition: decompressing_event_object_istream.cc:156
Status_t get_status() const
Return the status.
Definition: decompressing_event_object_istream.cc:92
Status_t m_status
Status.
Definition: decompressing_event_object_istream.h:196
void begin_payload_event(const Transaction_payload_log_event &tple)
Prepare to unfold a given Transaction_payload_log_event by setting state variables and creating the P...
Definition: decompressing_event_object_istream.h:237
Read_status read_from_payload_stream(Event_ptr_t &out)
Read and decode next event from the Payload_log_event stream.
Definition: decompressing_event_object_istream.cc:186
void do_begin_payload_event(const Transaction_payload_log_event &tple, const Event_ref_or_ptr &ownership_tple)
Worker function implementing both forms of begin_payload_event.
Definition: decompressing_event_object_istream.h:263
my_off_t m_transaction_payload_event_offset
end_log_pos for the currently processed TPLE, if any
Definition: decompressing_event_object_istream.h:210
std::shared_ptr< const Transaction_payload_log_event > Tple_ptr_t
Definition: decompressing_event_object_istream.h:74
raii::Targeted_stringstream error_stream(Status_t status)
Report an error.
Definition: decompressing_event_object_istream.cc:129
my_off_t m_event_position
Position of last event.
Definition: decompressing_event_object_istream.h:198
bool m_verify_checksum
Whether we should verify checksum. Unused!
Definition: decompressing_event_object_istream.h:190
std::shared_ptr< Log_event > Event_ptr_t
Definition: decompressing_event_object_istream.h:73
Decompressing_event_object_istream & operator=(Decompressing_event_object_istream &)=delete
Decompressing_event_object_istream(IBasic_binlog_file_reader &reader)
Construct stream over a file, decompressing payload events.
Definition: decompressing_event_object_istream.cc:33
Grow_calculator_t m_grow_calculator
Policy for growing buffers in the decompressing stream.
Definition: decompressing_event_object_istream.h:206
int m_embedded_event_number
0 when not processing a TPLE; N>0 when positioned before the Nth embedded event of a TPLE.
Definition: decompressing_event_object_istream.h:213
void set_verify_checksum(bool verify_checksum=true)
Specify whether checksums shall be verified or not.
Definition: decompressing_event_object_istream.cc:68
std::unique_ptr< Buffer_stream_t > m_buffer_istream
The decompression stream; non-null while we are positioned in a TPLE.
Definition: decompressing_event_object_istream.h:208
IBasic_binlog_file_reader * m_binlog_reader
Stream of events to read from.
Definition: decompressing_event_object_istream.h:188
bool operator!() const
Indicate if EOF or error has happened.
Definition: decompressing_event_object_istream.cc:75
void begin_payload_event(const Tple_ptr_t &tple)
Prepare to unfold a given Transaction_payload_log_event by setting state variables and creating the P...
Definition: decompressing_event_object_istream.h:248
Read_status
Status from read_from_payload_stream.
Definition: decompressing_event_object_istream.h:289
bool m_end
True if we have reached EOF, false otherwise.
Definition: decompressing_event_object_istream.h:194
void set_grow_calculator(const Grow_calculator_t &grow_calculator)
Set the Grow_calculator for the internal event buffer.
Definition: decompressing_event_object_istream.cc:151
std::function< Fde_ref_t()> m_get_format_description_event
Return the current FDE.
Definition: decompressing_event_object_istream.h:216
Decompressing_event_object_istream & operator>>(Event_ptr_t &out)
Read an event from the stream.
Definition: decompressing_event_object_istream.cc:258
Buffer_stream_t::Buffer_ptr_t Buffer_ptr_t
Definition: decompressing_event_object_istream.h:72
Decompressing_event_object_istream(Decompressing_event_object_istream &&)=delete
bool has_error() const
Return true if an error has happened.
Definition: decompressing_event_object_istream.cc:84
const Grow_calculator_t & get_grow_calculator() const
Return const reference to Grow_calculator to the internal event buffer.
Definition: decompressing_event_object_istream.cc:147
std::string m_error_str
Error from last operation.
Definition: decompressing_event_object_istream.h:192
Decompressing_event_object_istream(Decompressing_event_object_istream &)=delete
bool m_outstanding_error
True if a read has failed but neither get_error_str nor has_error has been called.
Definition: decompressing_event_object_istream.h:202
bool read_from_binlog_stream(Event_ptr_t &out)
Read and decode the next event from the binlog stream.
Definition: decompressing_event_object_istream.cc:222
const Format_description_event & Fde_ref_t
Definition: decompressing_event_object_istream.h:75
std::string get_error_str() const
Return a message describing the last error.
Definition: decompressing_event_object_istream.cc:77
Decompressing_event_object_istream & operator=(Decompressing_event_object_istream &&)=delete
Description of a heuristic to determine how much memory to allocate.
Definition: grow_calculator.h:67
Like std::stringstream, copying to a given target string at destruction.
Definition: targeted_stringstream.h:48
#define DBUG_TRACE
Definition: my_dbug.h:146
ulonglong my_off_t
Definition: my_inttypes.h:72
Decompress_status
Definition: decompress_status.h:32
Definition: decompressing_event_object_istream.cc:31
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
#define NODISCARD
The function attribute [[NODISCARD]] is a replacement for [[nodiscard]] to workaround a gcc bug.
Definition: nodiscard.h:47
Stream class that yields decompressed event byte buffers from a Transaction_payload_log_event.
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61