MySQL 26.7.0
Source Code Documentation
binlog_reader.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2026, 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_READER_INCLUDED
25#define BINLOG_READER_INCLUDED
26#include "sql/binlog_istream.h"
27#include "sql/log_event.h"
28
29/// @brief Represents basic event metadata: type and length
31 public:
33 Event_metadata() = default;
34 /// @brief Construct
35 Event_metadata(const Type &type, std::size_t len, bool ignorable)
36 : m_type(type),
37 m_event_length(len),
38 m_ignorable(ignorable),
39 m_valid(true) {}
40 /// @brief Event length accessor
41 /// @return Event length
42 std::size_t get_length() const { return m_event_length; }
43 /// @brief Event type accessor
44 /// @return Event type
45 Type get_type() const { return m_type; }
46 /// @brief Returns information on whether this event is ignorable
47 /// @return True if event may be ignored
48 bool is_ignorable() const { return m_ignorable; }
49 /// @brief We allow to construct empty objects, this function returns true
50 /// in case object was initialized with data
51 /// @return True in case object was initialized with data, false otherwise
52 bool is_valid() const { return m_valid; }
53
54 private:
55 /// Log event type
57 /// Log event length
58 std::size_t m_event_length;
59 /// Flag: is ignorable event
60 bool m_ignorable{false};
61 bool m_valid{false};
62};
63
64/**
65 Deserialize a binlog event from event_data. event_data is serialized
66 event object. It is a chunk of data in buffer.
67
68 @param[in] event_data The event data used for deserialization.
69 @param[in] event_data_len Length of event_data
70 @param[in] fde The format_description_event of the event
71 @param[in] verify_checksum Verify event_data's checksum if it is true.
72 @param[out] event the event object generated.
73
74 @retval Binlog_read_error::SUCCESS Success
75 @retval Other than Binlog_read_error::SUCCESS Error
76*/
78 const unsigned char *event_data, unsigned int event_data_len,
80 bool verify_checksum, Log_event **event);
81
83 public:
85 unsigned char *allocate(size_t);
86 void deallocate(unsigned char *ptr);
87};
88
89/// @brief Helper structure used by the reader to aggregate payload and
90/// information needed to decode an event
92 /// @brief Construct from payload data and metadata
93 /// @param data Allocated dynamic array containing payload raw bytes
94 /// @param length Length of the event
95 /// @param verify_checksum Parameter used by the reader to identify if
96 /// checksum of the event should be checked after event decoding
97 Event_payload(uint8_t *data, std::size_t length, bool verify_checksum)
98 : m_data(data), m_length(length), m_verify_checksum(verify_checksum) {}
99
100 /// @brief Default constructor
101 Event_payload() = default;
102
103 /// Allocated dynamic array containing payload raw bytes. This is
104 /// owning pointer of the data
105 uint8_t *m_data;
106 /// length Length of the event
107 std::size_t m_length;
108 /// Parameter used by the reader to identify if
109 /// checksum of the event should be checked after event decoding
110 bool m_verify_checksum{false};
111};
112
113/**
114 Binlog_event_data_istream fetches byte data from Basic_istream and
115 divides them into event_data chunk according to the format. Event_data is a
116 serialized event object. It is a chunk of data in buffer.
117*/
119 public:
121 unsigned int max_event_size);
125 delete;
126 virtual ~Binlog_event_data_istream() = default;
127
128 /**
129 Read an event data from the stream and verify its checksum if
130 verify_checksum is true.
131
132 @param[out] data The pointer of the event data
133 @param[out] length The length of the event data
134 @param[in] allocator It is used to allocate memory for the event data.
135 @param[in] verify_checksum Verify the event data's checksum if it is true.
136 @param[in] checksum_alg Checksum algorithm for verifying the event data. It
137 is used only when verify_checksum is true.
138 @retval false Success
139 @retval true Error
140 */
141 template <class ALLOCATOR>
143 unsigned char **data, unsigned int *length, ALLOCATOR *allocator,
144 bool verify_checksum,
147 if (read_event_header() || check_event_header()) return true;
148 return read_payload_data(data, length, allocator, verify_checksum,
149 checksum_alg);
150 }
151
152 /// @brief Reads event header metadata
153 /// @return Optional event metadata
154 std::optional<Event_metadata> read_event_metadata() {
156 m_event_length = 0;
157 return {};
158 }
159
160 uint16_t flags{0};
161 memcpy(&flags, m_header + FLAGS_OFFSET, sizeof(flags));
162 bool flag_ignorable = le16toh(flags) & LOG_EVENT_IGNORABLE_F;
165 return Event_metadata(type, m_event_length, flag_ignorable);
166 }
167
168 /// @brief Reads event payload (read event metadata has to be called
169 /// before to read header) and fills data with event header (read before)
170 /// and event payload (read here)
171 /// @copydoc read_event_data
172 template <class ALLOCATOR>
174 unsigned char **data, unsigned int *length, ALLOCATOR *allocator,
175 bool verify_checksum,
177 assert(m_event_length);
178 unsigned char *event_data = allocator->allocate(m_event_length);
179 if (event_data == nullptr)
181
182 if (fill_event_data(event_data, verify_checksum, checksum_alg)) {
183 allocator->deallocate(event_data);
184 return true;
185 }
186 *data = event_data;
188 return false;
189 }
190
191 protected:
193 /**
194 Read the event header from the Basic_istream
195
196 @retval false Success
197 @retval true Error
198 */
199 virtual bool read_event_header();
200 /**
201 Check if it is a valid event header
202
203 @retval false Success
204 @retval true Error
205 */
206 bool check_event_header();
207 /**
208 Read fixed length of data from Basic_istream. It sets error to
209 - Binlog_read_error::SYTEM_IO if Basic_istream returns error.
210 - Binlog_read_error::TRUNC_EVENT if less than length is read.
211 - ERROR_TYPE if zero byte is read. ERROR_TYPE is either TRUNC_EVENT or
212 READ_EOF.
213
214 @param[in] data The buffer where the data stored.
215 @param[in] length Bytes of the data to be read.
216 @retval false Success
217 @retval true Error
218 */
219 template <Binlog_read_error::Error_type ERROR_TYPE>
220 bool read_fixed_length(unsigned char *data, unsigned int length) {
222 if (length == 0) return false;
223
224 longlong ret = m_istream->read(data, length);
225 if (ret == length) return false;
226 switch (ret) {
227 case -1:
229 case 0:
230 return m_error->set_type(ERROR_TYPE);
231 default:
233 }
234 }
235
236 /**
237 It is convenient for caller to share a Binlog_read_error object between
238 streams. So Binlog_read_error pointer is defined here. It should be
239 initialized in constructor by caller.
240 */
242
243 private:
245 unsigned int m_max_event_size;
246 unsigned int m_event_length = 0;
247
248 /**
249 Fill the event data into the given buffer and verify checksum if
250 'verify_checksum' is true.
251
252 @param[in] event_data The buffer where the event data will be stored.
253 @param[in] verify_checksum Verify the event data's checksum if it is true.
254 @param[in] checksum_alg Checksum algorithm for verifying the event data. It
255 is used only when verify_checksum is true.
256 @retval false Success
257 @retval true Error
258 */
259 bool fill_event_data(
260 unsigned char *event_data, bool verify_checksum,
262};
263
264/**
265 It reads event_data from an event_data stream and deserialize them to event
266 object.
267*/
268template <class EVENT_DATA_ISTREAM>
270 public:
272 EVENT_DATA_ISTREAM *istream)
273 : m_error(error), m_data_istream(istream) {}
274
278 delete;
279
280 /**
281 Read an event object from the stream
282
283 @param[in] fde The Format_description_event for deserialization.
284 @param[in] verify_checksum Verify the checksum of the event_data before
285 @param[in] allocator It is used to allocate memory for the event data.
286 @return An valid event object if success.
287 @retval nullptr Error
288 */
289 template <class ALLOCATOR>
292 bool verify_checksum, ALLOCATOR *allocator) {
294 unsigned char *data = nullptr;
295 unsigned int length = 0;
296
297 if (m_data_istream->read_event_data(&data, &length, allocator, false,
298 fde.footer()->checksum_alg))
299 return nullptr;
300
301 Log_event *event = nullptr;
303 verify_checksum, &event))) {
304 allocator->deallocate(data);
305 return nullptr;
306 }
307
308 event->register_temp_buf(reinterpret_cast<char *>(data),
309 ALLOCATOR::DELEGATE_MEMORY_TO_EVENT_OBJECT);
310 return event;
311 }
312
313 template <class ALLOCATOR>
316 bool verify_checksum, ALLOCATOR *allocator) {
318 unsigned char *data = nullptr;
319 unsigned int length = 0;
320
321 if (m_data_istream->read_payload_data(&data, &length, allocator, false,
322 fde.footer()->checksum_alg))
323 return nullptr;
324
325 Log_event *event = nullptr;
327 verify_checksum, &event))) {
328 allocator->deallocate(data);
329 return nullptr;
330 }
331
332 event->register_temp_buf(reinterpret_cast<char *>(data),
333 ALLOCATOR::DELEGATE_MEMORY_TO_EVENT_OBJECT);
334 return event;
335 }
336
337 template <class ALLOCATOR>
338 std::optional<Event_payload> read_payload(
340 bool verify_checksum, ALLOCATOR *allocator) {
342 unsigned char *data = nullptr;
343 unsigned int length = 0;
344
345 if (m_data_istream->read_payload_data(&data, &length, allocator, false,
346 fde.footer()->checksum_alg))
347 return {};
348
349 return Event_payload(data, length, verify_checksum);
350 }
351
352 private:
353 /**
354 It is convenient for caller to share a Binlog_read_error object between
355 streams. So Binlog_read_error pointer is defined here. It should be
356 initialized in constructor by caller.
357 */
359 EVENT_DATA_ISTREAM *m_data_istream = nullptr;
360};
361
362/// Interface class that all specializations of
363/// template <...> Basic_binlog_file_reader inherit from.
365 public:
371 virtual ~IBasic_binlog_file_reader() = default;
372
373 /// Open a binlog file and set read position to offset. It will read and store
374 /// Format_description_event automatically if offset is bigger than current
375 /// position and fde is nullptr. Otherwise fde is use instead of finding fde
376 /// from the file if fde is not null.
377 /// @param[in] file_name name of the binlog file which will be opened.
378 /// @param[in] offset The position where it starts to read.
379 /// @param[out] fdle For returning an Format_description_log_event object.
380 /// @retval false Success
381 /// @retval true Error
382 virtual bool open(const char *file_name, my_off_t offset = 0,
383 Format_description_log_event **fdle = nullptr) = 0;
384
385 /// @brief Closes the reader
386 virtual void close() = 0;
387
388 /// Return true if there was a previous error.
389 virtual bool has_fatal_error() const = 0;
390
391 /// Return the type of error.
393
394 /// Return a string representing the error.
395 ///
396 /// The return value is static memory that is never deallocated.
397 virtual const char *get_error_str() const = 0;
398
399 /// Return the current position in bytes, relative to the beginning
400 /// of the file.
401 virtual my_off_t position() const = 0;
402
403 /// Seek to a given position
404 /// @param pos Position to seek to
405 /// @retval false Success
406 /// @retval true Seek not possible
407 virtual bool seek(my_off_t pos) = 0;
408
409 /// Read the next event from the stream.
410 ///
411 /// This function creates the object with "new". It is the caller's
412 /// responsibility to delete it.
413 ///
414 /// @return Log_event on success, nullptr on error. More
415 /// information about the error can be found using `get_error_type`
416 /// and `get_error_str`.
418
419 /// Return a pointer the currently used
420 /// Format_description_log_event.
421 ///
422 /// The event is a member by this reader, so the caller must not use
423 /// the returned reference after this reader has been deleted.
426
427 /// @brief Reads event header metadata without updating position to the
428 /// next header (see skip_event_payload)
429 /// @return Optional event metadata
430 virtual std::optional<Event_metadata> read_event_metadata() = 0;
431
432 /// @brief Reads event payload after header metadata has been read.
433 /// Fills event data and decodes the event
434 /// @param metadata Metadata previously read (read_event_metadata)
435 /// @return Log event read and decoded from the stream
436 virtual Log_event *read_event_payload(const Event_metadata &metadata) = 0;
437
438 /// @brief Reads raw payload after header metadata has been read.
439 /// Fills event data, skips decoding part
440 /// @param metadata Metadata previously read (read_event_metadata)
441 /// @return Log event read and decoded from the stream
442 virtual std::optional<Event_payload> read_payload(
443 const Event_metadata &metadata) = 0;
444
445 /// @brief Skips event payload data. Used to move reader position to the
446 /// next header
447 /// @param metadata Metadata previously read (read_event_metadata)
448 /// @retval false Success
449 /// @retval true Error
450 virtual bool skip_event_payload(const Event_metadata &metadata) = 0;
451};
452
453/**
454 It owns an allocator, a byte stream, an event_data stream and an event object
455 stream. The stream pipeline is setup in the constructor. All the objects
456 required for reading a binlog file is initialized in reader class.
457
458 It maintains the Format_description_event which is needed for reading the
459 following binlog events. A default format_description_event is initialized
460 at the beginning. Then it will be replaced by the one read from the binlog
461 file.
462
463 Some convenient functions is added to encapsulate the access of IFILE,
464 EVENT_DATA_ISTREAM, EVENT_OBJECT_ISTREAM. It makes the code
465 simpler for reading a binlog file.
466*/
467template <class IFILE, class EVENT_DATA_ISTREAM,
468 template <class> class EVENT_OBJECT_ISTREAM, class ALLOCATOR>
470 public:
471 typedef EVENT_DATA_ISTREAM Event_data_istream;
472 typedef EVENT_OBJECT_ISTREAM<Event_data_istream> Event_object_istream;
473
474 Basic_binlog_file_reader(bool verify_checksum,
475 unsigned int max_event_size = UINT_MAX)
476 : m_ifile(&m_error),
477 m_data_istream(&m_error, &m_ifile, max_event_size),
480 m_verify_checksum(verify_checksum),
481 m_file_name("") {}
482
486 delete;
488 delete;
490
491 /**
492 Open a binlog file and set read position to offset. It will read and store
493 Format_description_event automatically if offset is bigger than current
494 position and fde is nullptr. Otherwise fde is use instead of finding fde
495 from the file if fde is not null.
496
497 @param[in] file_name name of the binlog file which will be opened.
498 @param[in] offset The position where it starts to read.
499 @param[out] fdle For returning an Format_description_log_event object.
500 @retval false Success
501 @retval true Error
502 */
503 bool open(const char *file_name, my_off_t offset = 0,
504 Format_description_log_event **fdle = nullptr) override {
506 if (m_ifile.open(file_name)) return true;
507
509 if (!fd) return has_fatal_error();
510
511 if (position() < offset && seek(offset)) {
512 delete fd;
513 return true;
514 }
515 if (fdle)
516 *fdle = fd;
517 else
518 delete fd;
520 return false;
521 }
522 /**
523 Close the binlog file.
524 */
525 void close() override {
526 m_ifile.close();
529 }
530
531 bool is_open() const { return m_ifile.is_open(); }
532 my_off_t position() const override { return m_ifile.position(); }
533 bool seek(my_off_t pos) override { return m_ifile.seek(pos); }
534
535 /**
536 Wrapper of EVENT_DATA_ISTREAM::read_event_data.
537 */
538 bool read_event_data(unsigned char **data, unsigned int *length) {
540 return m_data_istream.read_event_data(data, length, &m_allocator,
543 }
544
545 bool read_payload_data(unsigned char **data, unsigned int *length) {
546 return m_data_istream.read_payload_data(data, length, &m_allocator,
549 }
550
551 /// @brief Reads event header metadata without updating position to the
552 /// next header (see skip_event_payload)
553 /// @return Optional event metadata
554 std::optional<Event_metadata> read_event_metadata() override {
556 return m_data_istream.read_event_metadata();
557 }
558
560 // m_event_start_pos is updated in the read_event_metadata
561 Log_event *ev = m_object_istream.read_event_payload(
563 if (ev != nullptr &&
565 m_fde =
567 return ev;
568 }
569
570 std::optional<Event_payload> read_payload(const Event_metadata &) override {
571 // m_event_start_pos is updated in the read_event_metadata
572 return m_object_istream.read_payload(m_fde, m_verify_checksum,
573 &m_allocator);
574 }
575
576 bool skip_event_payload(const Event_metadata &metadata) override {
577 return seek(position() + metadata.get_length() -
579 }
580
581 /**
582 wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
583 */
586 Log_event *ev = m_object_istream.read_event_object(m_fde, m_verify_checksum,
587 &m_allocator);
588 if (ev != nullptr &&
590 m_fde =
592 return ev;
593 }
594
595 bool has_fatal_error() const override { return m_error.has_fatal_error(); }
596 /**
597 Return the error happened in the stream pipeline.
598 */
600 return m_error.get_type();
601 }
602 /**
603 Return the error message of the error happened in the stream pipeline.
604 */
605 const char *get_error_str() const override { return m_error.get_str(); }
606
607 IFILE *ifile() { return &m_ifile; }
610 ALLOCATOR *allocator() { return &m_allocator; }
611
614 m_fde = fde;
615 }
617 format_description_event() const override {
618 return m_fde;
619 }
621
622 /**
623 @brief Resets the error. Sets it to Binlog_read_error::SUCCESS.
624 */
626 const char *get_file_name() const { return m_file_name; }
627
628 private:
630
631 IFILE m_ifile;
634 ALLOCATOR m_allocator;
635
637 bool m_verify_checksum = false;
638 const char *m_file_name{""};
640
641 /**
642 Read the Format_description_log_events before 'offset'.
643
644 @param[in] offset The position where the read should stop.
645 @return A valid Format_description_log_event pointer or nullptr.
646 */
650 Format_description_log_event *fdle = nullptr;
651 /*
652 Format_description_event is skipped, so we initialize m_fde here. For
653 relay log, it need to find master's Format_description_event. master's
654 Format_description_event is the 3rd or 4th event of a relay log file.
655 Relay log's format looks like:
656 Format_description_event : relay log's Format_description_event
657 Previous_gtid_event
658 [Rotate_event] : In the case rotating relaylog, no Rotate here
659 Format_description_event : master's Format_description_event
660 */
661 while (position() < offset) {
663
664 Log_event *ev = m_object_istream.read_event_object(
666
667 if (ev == nullptr) break;
668 if (ev->get_type_code() ==
670 delete fdle;
671 fdle = dynamic_cast<Format_description_log_event *>(ev);
672 m_fde = *fdle;
673 } else {
675 delete ev;
678 break;
679 }
680 }
681 if (has_fatal_error()) {
682 delete fdle;
683 return nullptr;
684 }
685 return fdle;
686 }
687};
688
689#ifdef MYSQL_SERVER
698#endif // MYSQL_SERVER
699#endif // BINLOG_READER_INCLUDED
#define EVENT_TYPE_OFFSET
Definition: binlog_event.h:424
#define LOG_EVENT_MINIMAL_HEADER_LEN
Fixed header length, where 4.x and 5.0 agree.
Definition: binlog_event.h:450
#define FLAGS_OFFSET
Definition: binlog_event.h:428
Binlog_read_error::Error_type binlog_event_deserialize(const unsigned char *event_data, unsigned int event_data_len, const mysql::binlog::event::Format_description_event *fde, bool verify_checksum, Log_event **event)
Deserialize a binlog event from event_data.
Definition: binlog_reader.cc:125
Basic_binlog_file_reader< Binlog_ifile, Binlog_event_data_istream, Binlog_event_object_istream, Default_binlog_event_allocator > Binlog_file_reader
Definition: binlog_reader.h:693
Basic_binlog_file_reader< Relaylog_ifile, Binlog_event_data_istream, Binlog_event_object_istream, Default_binlog_event_allocator > Relaylog_file_reader
Definition: binlog_reader.h:697
It owns an allocator, a byte stream, an event_data stream and an event object stream.
Definition: binlog_reader.h:469
Event_data_istream m_data_istream
Definition: binlog_reader.h:632
Event_data_istream * event_data_istream()
Definition: binlog_reader.h:608
EVENT_DATA_ISTREAM Event_data_istream
Definition: binlog_reader.h:471
Format_description_log_event * read_fdle(my_off_t offset)
Read the Format_description_log_events before 'offset'.
Definition: binlog_reader.h:647
void set_format_description_event(const mysql::binlog::event::Format_description_event &fde)
Definition: binlog_reader.h:612
~Basic_binlog_file_reader() override
Definition: binlog_reader.h:489
const char * m_file_name
Definition: binlog_reader.h:638
const char * get_error_str() const override
Return the error message of the error happened in the stream pipeline.
Definition: binlog_reader.h:605
bool seek(my_off_t pos) override
Seek to a given position.
Definition: binlog_reader.h:533
Log_event * read_event_payload(const Event_metadata &) override
Reads event payload after header metadata has been read.
Definition: binlog_reader.h:559
bool m_verify_checksum
Definition: binlog_reader.h:637
Binlog_read_error::Error_type get_error_type() const override
Return the error happened in the stream pipeline.
Definition: binlog_reader.h:599
ALLOCATOR m_allocator
Definition: binlog_reader.h:634
Event_object_istream * event_object_istream()
Definition: binlog_reader.h:609
bool is_open() const
Definition: binlog_reader.h:531
bool has_fatal_error() const override
Return true if there was a previous error.
Definition: binlog_reader.h:595
void close() override
Close the binlog file.
Definition: binlog_reader.h:525
std::optional< Event_payload > read_payload(const Event_metadata &) override
Reads raw payload after header metadata has been read.
Definition: binlog_reader.h:570
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &&)=delete
std::optional< Event_metadata > read_event_metadata() override
Reads event header metadata without updating position to the next header (see skip_event_payload)
Definition: binlog_reader.h:554
my_off_t event_start_pos()
Definition: binlog_reader.h:620
Binlog_read_error m_error
Definition: binlog_reader.h:629
Basic_binlog_file_reader & operator=(const Basic_binlog_file_reader &)=delete
EVENT_OBJECT_ISTREAM< Event_data_istream > Event_object_istream
Definition: binlog_reader.h:472
Event_object_istream m_object_istream
Definition: binlog_reader.h:633
ALLOCATOR * allocator()
Definition: binlog_reader.h:610
IFILE m_ifile
Definition: binlog_reader.h:631
mysql::binlog::event::Format_description_event m_fde
Definition: binlog_reader.h:636
bool read_event_data(unsigned char **data, unsigned int *length)
Wrapper of EVENT_DATA_ISTREAM::read_event_data.
Definition: binlog_reader.h:538
my_off_t m_event_start_pos
Definition: binlog_reader.h:639
Basic_binlog_file_reader(bool verify_checksum, unsigned int max_event_size=UINT_MAX)
Definition: binlog_reader.h:474
my_off_t position() const override
Return the current position in bytes, relative to the beginning of the file.
Definition: binlog_reader.h:532
Basic_binlog_file_reader(const Basic_binlog_file_reader &&)=delete
const mysql::binlog::event::Format_description_event & format_description_event() const override
Return a pointer the currently used Format_description_log_event.
Definition: binlog_reader.h:617
IFILE * ifile()
Definition: binlog_reader.h:607
Basic_binlog_file_reader(const Basic_binlog_file_reader &)=delete
bool skip_event_payload(const Event_metadata &metadata) override
Skips event payload data.
Definition: binlog_reader.h:576
Log_event * read_event_object() override
wrapper of EVENT_OBJECT_ISTREAM::read_event_object.
Definition: binlog_reader.h:584
bool open(const char *file_name, my_off_t offset=0, Format_description_log_event **fdle=nullptr) override
Open a binlog file and set read position to offset.
Definition: binlog_reader.h:503
const char * get_file_name() const
Definition: binlog_reader.h:626
bool read_payload_data(unsigned char **data, unsigned int *length)
Definition: binlog_reader.h:545
void reset_error()
Resets the error.
Definition: binlog_reader.h:625
The abstract class for basic byte input streams which provides read operations.
Definition: basic_istream.h:35
virtual ssize_t read(unsigned char *buffer, size_t length)=0
Read some bytes from the input stream.
Binlog_event_data_istream fetches byte data from Basic_istream and divides them into event_data chunk...
Definition: binlog_reader.h:118
unsigned int m_max_event_size
Definition: binlog_reader.h:245
bool read_event_data(unsigned char **data, unsigned int *length, ALLOCATOR *allocator, bool verify_checksum, mysql::binlog::event::enum_binlog_checksum_alg checksum_alg)
Read an event data from the stream and verify its checksum if verify_checksum is true.
Definition: binlog_reader.h:142
Basic_istream * m_istream
Definition: binlog_reader.h:244
bool read_fixed_length(unsigned char *data, unsigned int length)
Read fixed length of data from Basic_istream.
Definition: binlog_reader.h:220
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:241
unsigned int m_event_length
Definition: binlog_reader.h:246
Binlog_event_data_istream & operator=(const Binlog_event_data_istream &)=delete
std::optional< Event_metadata > read_event_metadata()
Reads event header metadata.
Definition: binlog_reader.h:154
bool read_payload_data(unsigned char **data, unsigned int *length, ALLOCATOR *allocator, bool verify_checksum, mysql::binlog::event::enum_binlog_checksum_alg checksum_alg)
Reads event payload (read event metadata has to be called before to read header) and fills data with ...
Definition: binlog_reader.h:173
bool check_event_header()
Check if it is a valid event header.
Definition: binlog_reader.cc:108
virtual bool read_event_header()
Read the event header from the Basic_istream.
Definition: binlog_reader.cc:74
virtual ~Binlog_event_data_istream()=default
Binlog_event_data_istream(const Binlog_event_data_istream &)=delete
bool fill_event_data(unsigned char *event_data, bool verify_checksum, mysql::binlog::event::enum_binlog_checksum_alg checksum_alg)
Fill the event data into the given buffer and verify checksum if 'verify_checksum' is true.
Definition: binlog_reader.cc:79
unsigned char m_header[LOG_EVENT_MINIMAL_HEADER_LEN]
Definition: binlog_reader.h:192
It reads event_data from an event_data stream and deserialize them to event object.
Definition: binlog_reader.h:269
Binlog_read_error * m_error
It is convenient for caller to share a Binlog_read_error object between streams.
Definition: binlog_reader.h:358
EVENT_DATA_ISTREAM * m_data_istream
Definition: binlog_reader.h:359
std::optional< Event_payload > read_payload(const mysql::binlog::event::Format_description_event &fde, bool verify_checksum, ALLOCATOR *allocator)
Definition: binlog_reader.h:338
Binlog_event_object_istream(const Binlog_event_object_istream &)=delete
Log_event * read_event_payload(const mysql::binlog::event::Format_description_event &fde, bool verify_checksum, ALLOCATOR *allocator)
Definition: binlog_reader.h:314
Binlog_event_object_istream & operator=(const Binlog_event_object_istream &)=delete
Binlog_event_object_istream(Binlog_read_error *error, EVENT_DATA_ISTREAM *istream)
Definition: binlog_reader.h:271
Log_event * read_event_object(const mysql::binlog::event::Format_description_event &fde, bool verify_checksum, ALLOCATOR *allocator)
Read an event object from the stream.
Definition: binlog_reader.h:290
Binlog input file.
Definition: binlog_istream.h:241
It defines the error types which could happen when reading binlog files or deserializing binlog event...
Definition: binlog_istream.h:37
Error_type get_type() const
Return the error encountered when reading events.
Definition: binlog_istream.h:88
Error_type
Possible errors which happens when reading an event.
Definition: binlog_istream.h:42
@ MEM_ALLOCATE
Definition: binlog_istream.h:56
@ SUCCESS
Definition: binlog_istream.h:44
@ SYSTEM_IO
Definition: binlog_istream.h:54
@ TRUNC_EVENT
Definition: binlog_istream.h:58
bool set_type(Error_type type)
Set m_error to error.
Definition: binlog_istream.h:106
const char * get_str() const
Return error message of the error type.
Definition: binlog_istream.cc:34
bool has_fatal_error() const
Definition: binlog_istream.h:83
Definition: binlog_reader.h:82
unsigned char * allocate(size_t)
Definition: binlog_reader.cc:33
void deallocate(unsigned char *ptr)
Definition: binlog_reader.cc:39
@ DELEGATE_MEMORY_TO_EVENT_OBJECT
Definition: binlog_reader.h:84
Represents basic event metadata: type and length.
Definition: binlog_reader.h:30
Type get_type() const
Event type accessor.
Definition: binlog_reader.h:45
bool m_valid
Definition: binlog_reader.h:61
std::size_t m_event_length
Log event length.
Definition: binlog_reader.h:58
Event_metadata(const Type &type, std::size_t len, bool ignorable)
Construct.
Definition: binlog_reader.h:35
std::size_t get_length() const
Event length accessor.
Definition: binlog_reader.h:42
Type m_type
Log event type.
Definition: binlog_reader.h:56
bool is_valid() const
We allow to construct empty objects, this function returns true in case object was initialized with d...
Definition: binlog_reader.h:52
bool m_ignorable
Flag: is ignorable event.
Definition: binlog_reader.h:60
bool is_ignorable() const
Returns information on whether this event is ignorable.
Definition: binlog_reader.h:48
Event_metadata()=default
For binlog version 4.
Definition: log_event.h:1558
Interface class that all specializations of template <...> Basic_binlog_file_reader inherit from.
Definition: binlog_reader.h:364
virtual Log_event * read_event_payload(const Event_metadata &metadata)=0
Reads event payload after header metadata has been read.
IBasic_binlog_file_reader(IBasic_binlog_file_reader &)=delete
virtual std::optional< Event_payload > read_payload(const Event_metadata &metadata)=0
Reads raw payload after header metadata has been read.
virtual bool skip_event_payload(const Event_metadata &metadata)=0
Skips event payload data.
IBasic_binlog_file_reader & operator=(IBasic_binlog_file_reader &&)=delete
virtual ~IBasic_binlog_file_reader()=default
IBasic_binlog_file_reader & operator=(IBasic_binlog_file_reader &)=delete
virtual std::optional< Event_metadata > read_event_metadata()=0
Reads event header metadata without updating position to the next header (see skip_event_payload)
virtual const char * get_error_str() const =0
Return a string representing the error.
virtual bool open(const char *file_name, my_off_t offset=0, Format_description_log_event **fdle=nullptr)=0
Open a binlog file and set read position to offset.
virtual my_off_t position() const =0
Return the current position in bytes, relative to the beginning of the file.
virtual void close()=0
Closes the reader.
virtual bool seek(my_off_t pos)=0
Seek to a given position.
IBasic_binlog_file_reader(IBasic_binlog_file_reader &&)=delete
IBasic_binlog_file_reader()=default
virtual Binlog_read_error::Error_type get_error_type() const =0
Return the type of error.
virtual const mysql::binlog::event::Format_description_event & format_description_event() const =0
Return a pointer the currently used Format_description_log_event.
virtual Log_event * read_event_object()=0
Read the next event from the stream.
virtual bool has_fatal_error() const =0
Return true if there was a previous error.
This is the abstract base class for binary log events.
Definition: log_event.h:539
virtual mysql::binlog::event::Log_event_type get_type_code() const
Definition: log_event.h:804
void register_temp_buf(char *buf, bool free_in_destructor=true)
Definition: log_event.h:882
Relaylog input file.
Definition: binlog_istream.h:253
const Log_event_footer * footer() const
Return a const pointer to the footer of the log event.
Definition: binlog_event.h:960
For binlog version 4.
Definition: control_events.h:240
mysql::binlog::event::Format_description_event Format_description_event
Definition: file_storage.cc:40
#define LOG_EVENT_IGNORABLE_F
Setting this flag will mark an event as Ignorable.
Definition: binlog_event.h:165
#define BINLOG_VERSION
binlog_version 3 is MySQL 4.x; 4 is MySQL 5.0.0.
Definition: binlog_event.h:98
static int flags[50]
Definition: hp_test1.cc:40
uint16_t le16toh(uint16_t x)
Converting a 16 bit integer from little-endian byte order to host byteorder.
Definition: byteorder.h:62
Binary log event definitions.
#define DBUG_TRACE
Definition: my_dbug.h:146
ulonglong my_off_t
Definition: my_inttypes.h:72
long long int longlong
Definition: my_inttypes.h:55
static char * server_version
Definition: mysql.cc:120
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
std::string file_name(Log_file_id file_id)
Provides name of the log file with the given file id, e.g.
Definition: log0pre_8_0_30.cc:45
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:279
@ FORMAT_DESCRIPTION_EVENT
Definition: binlog_event.h:305
@ PREVIOUS_GTIDS_LOG_EVENT
Definition: binlog_event.h:348
@ ROTATE_EVENT
Definition: binlog_event.h:295
enum_binlog_checksum_alg
Enumeration spcifying checksum algorithm used to encode a binary log event.
Definition: binlog_event.h:455
required string type
Definition: replication_group_member_actions.proto:34
required string event
Definition: replication_group_member_actions.proto:32
Helper structure used by the reader to aggregate payload and information needed to decode an event.
Definition: binlog_reader.h:91
Event_payload()=default
Default constructor.
Event_payload(uint8_t *data, std::size_t length, bool verify_checksum)
Construct from payload data and metadata.
Definition: binlog_reader.h:97
bool m_verify_checksum
Parameter used by the reader to identify if checksum of the event should be checked after event decod...
Definition: binlog_reader.h:110
uint8_t * m_data
Allocated dynamic array containing payload raw bytes.
Definition: binlog_reader.h:105
std::size_t m_length
length Length of the event
Definition: binlog_reader.h:107