MySQL 8.0.29
Source Code Documentation
rows_event.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2021, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @addtogroup Replication
25 @{
26
27 @file rows_event.h
28
29 @brief Contains the classes representing events which are used for row based
30 replication. In row-based replication, the master writes events to the binary
31 log that indicate how individual table rows are changed.
32*/
33
34#ifndef ROWS_EVENT_INCLUDED
35#define ROWS_EVENT_INCLUDED
36
37#include <vector>
38#include "control_events.h"
39#include "table_id.h"
40
41/**
42 1 byte length, 1 byte format
43 Length is total length in bytes, including 2 byte header
44 Length values 0 and 1 are currently invalid and reserved.
45*/
46#define EXTRA_ROW_INFO_LEN_OFFSET 0
47#define EXTRA_ROW_INFO_FORMAT_OFFSET 1
48#define EXTRA_ROW_INFO_HEADER_LENGTH 2
49#define EXTRA_ROW_INFO_MAX_PAYLOAD (255 - EXTRA_ROW_INFO_HEADER_LENGTH)
50
51#define ROWS_MAPID_OFFSET 0
52#define ROWS_FLAGS_OFFSET 6
53#define ROWS_VHLEN_OFFSET 8
54#define EXTRA_ROW_INFO_TYPECODE_LENGTH 1
55#define EXTRA_ROW_PART_INFO_VALUE_LENGTH 2
56
57/**
58 This is the typecode defined for the different elements present in
59 the container Extra_row_info, this is different from the format information
60 stored inside extra_row_ndb_info at EXTRA_ROW_INFO_FORMAT_OFFSET.
61*/
62enum class enum_extra_row_info_typecode { NDB = 0, PART = 1 };
63
64namespace binary_log {
65/**
66 @class Table_map_event
67
68 In row-based mode, every row operation event is preceded by a
69 Table_map_event which maps a table definition to a number. The
70 table definition consists of database name, table name, and column
71 definitions.
72
73 @section Table_map_event_binary_format Binary Format
74
75 The Post-Header has the following components:
76
77 <table>
78 <caption>Post-Header for Table_map_event</caption>
79
80 <tr>
81 <th>Name</th>
82 <th>Format</th>
83 <th>Description</th>
84 </tr>
85
86 <tr>
87 <td>table_id</td>
88 <td>6 bytes unsigned integer</td>
89 <td>The number that identifies the table.</td>
90 </tr>
91
92 <tr>
93 <td>flags</td>
94 <td>2 byte bitfield</td>
95 <td>Reserved for future use; currently always 0.</td>
96 </tr>
97
98 </table>
99
100 The Body has the following components:
101
102 <table>
103 <caption>Body for Table_map_event</caption>
104
105 <tr>
106 <th>Name</th>
107 <th>Format</th>
108 <th>Description</th>
109 </tr>
110
111 <tr>
112 <td>database_name</td>
113 <td>one byte string length, followed by null-terminated string</td>
114 <td>The name of the database in which the table resides. The name
115 is represented as a packed variable-length integer representing the
116 number of bytes in the name, followed by length bytes containing
117 the database name, followed by a terminating 0 byte. (Note the
118 redundancy in the representation of the length.) </td>
119 </tr>
120
121 <tr>
122 <td>table_name</td>
123 <td> The table name is represented as a packed variable-length integer
124 representing the number of bytes in the name, followed by null-terminated
125 string</td>
126 <td>The name of the table, encoded the same way as the database
127 name above.</td>
128 </tr>
129
130 <tr>
131 <td>column_count</td>
132 <td>@ref packed_integer "Packed Integer"</td>
133 <td>The number of columns in the table, represented as a packed
134 variable-length integer.</td>
135 </tr>
136
137 <tr>
138 <td>column_type</td>
139 <td>List of column_count 1 byte enumeration values</td>
140 <td>The type of each column in the table, listed from left to
141 right. Each byte is mapped to a column type according to the
142 enumeration type enum_field_types defined in mysql_com.h. The
143 mapping of types to numbers is listed in the table @ref
144 Table_table_map_event_column_types "below" (along with
145 description of the associated metadata field). </td>
146 </tr>
147
148 <tr>
149 <td>metadata_length</td>
150 <td>@ref packed_integer "Packed Integer"</td>
151 <td>The length of the following metadata block</td>
152 </tr>
153
154 <tr>
155 <td>metadata</td>
156 <td>list of metadata for each column</td>
157 <td>For each column from left to right, a chunk of data who's
158 length and semantics depends on the type of the column. The
159 length and semantics for the metadata for each column are listed
160 in the table @ref Table_table_map_event_column_types
161 "below".</td>
162 </tr>
163
164 <tr>
165 <td>null_bits</td>
166 <td>column_count bits, rounded up to nearest byte</td>
167 <td>For each column, a bit indicating whether data in the column
168 can be NULL or not. The number of bytes needed for this is
169 int((column_count + 7) / 8). The flag for the first column from the
170 left is in the least-significant bit of the first byte, the second
171 is in the second least significant bit of the first byte, the
172 ninth is in the least significant bit of the second byte, and so
173 on. </td>
174 </tr>
175
176 <tr>
177 <td>optional metadata fields</td>
178 <td>optional metadata fields are stored in Type, Length, Value(TLV) format.
179 Type takes 1 byte. Length is a packed integer value. Values takes
180 Length bytes.
181 </td>
182 <td>There are some optional metadata defined. They are listed in the table
183 @ref Table_table_map_event_optional_metadata. Optional metadata fields
184 follow null_bits. Whether binlogging an optional metadata is decided by the
185 server. The order is not defined, so they can be binlogged in any order.
186 </td>
187 </tr>
188 </table>
189
190 The table below lists all column types, along with the numerical
191 identifier for it and the size and interpretation of meta-data used
192 to describe the type.
193
194 @anchor Table_table_map_event_column_types
195 <table>
196 <caption>Table_map_event column types: numerical identifier and
197 metadata</caption>
198 <tr>
199 <th>Name</th>
200 <th>Identifier</th>
201 <th>Size of metadata in bytes</th>
202 <th>Description of metadata</th>
203 </tr>
204
205 <tr>
206 <td>MYSQL_TYPE_DECIMAL</td><td>0</td>
207 <td>0</td>
208 <td>No column metadata.</td>
209 </tr>
210
211 <tr>
212 <td>MYSQL_TYPE_TINY</td><td>1</td>
213 <td>0</td>
214 <td>No column metadata.</td>
215 </tr>
216
217 <tr>
218 <td>MYSQL_TYPE_SHORT</td><td>2</td>
219 <td>0</td>
220 <td>No column metadata.</td>
221 </tr>
222
223 <tr>
224 <td>MYSQL_TYPE_LONG</td><td>3</td>
225 <td>0</td>
226 <td>No column metadata.</td>
227 </tr>
228
229 <tr>
230 <td>MYSQL_TYPE_FLOAT</td><td>4</td>
231 <td>1 byte</td>
232 <td>1 byte unsigned integer, representing the "pack_length", which
233 is equal to sizeof(float) on the server from which the event
234 originates.</td>
235 </tr>
236
237 <tr>
238 <td>MYSQL_TYPE_DOUBLE</td><td>5</td>
239 <td>1 byte</td>
240 <td>1 byte unsigned integer, representing the "pack_length", which
241 is equal to sizeof(double) on the server from which the event
242 originates.</td>
243 </tr>
244
245 <tr>
246 <td>MYSQL_TYPE_NULL</td><td>6</td>
247 <td>0</td>
248 <td>No column metadata.</td>
249 </tr>
250
251 <tr>
252 <td>MYSQL_TYPE_TIMESTAMP</td><td>7</td>
253 <td>0</td>
254 <td>No column metadata.</td>
255 </tr>
256
257 <tr>
258 <td>MYSQL_TYPE_LONGLONG</td><td>8</td>
259 <td>0</td>
260 <td>No column metadata.</td>
261 </tr>
262
263 <tr>
264 <td>MYSQL_TYPE_INT24</td><td>9</td>
265 <td>0</td>
266 <td>No column metadata.</td>
267 </tr>
268
269 <tr>
270 <td>MYSQL_TYPE_DATE</td><td>10</td>
271 <td>0</td>
272 <td>No column metadata.</td>
273 </tr>
274
275 <tr>
276 <td>MYSQL_TYPE_TIME</td><td>11</td>
277 <td>0</td>
278 <td>No column metadata.</td>
279 </tr>
280
281 <tr>
282 <td>MYSQL_TYPE_DATETIME</td><td>12</td>
283 <td>0</td>
284 <td>No column metadata.</td>
285 </tr>
286
287 <tr>
288 <td>MYSQL_TYPE_YEAR</td><td>13</td>
289 <td>0</td>
290 <td>No column metadata.</td>
291 </tr>
292
293 <tr>
294 <td><i>MYSQL_TYPE_NEWDATE</i></td><td><i>14</i></td>
295 <td>&ndash;</td>
296 <td><i>This enumeration value is only used internally and cannot
297 exist in a binlog.</i></td>
298 </tr>
299
300 <tr>
301 <td>MYSQL_TYPE_VARCHAR</td><td>15</td>
302 <td>2 bytes</td>
303 <td>2 byte unsigned integer representing the maximum length of
304 the string.</td>
305 </tr>
306
307 <tr>
308 <td>MYSQL_TYPE_BIT</td><td>16</td>
309 <td>2 bytes</td>
310 <td>A 1 byte unsigned int representing the length in bits of the
311 bitfield (0 to 64), followed by a 1 byte unsigned int
312 representing the number of bytes occupied by the bitfield. The
313 number of bytes is either int((length + 7) / 8) or int(length / 8).
314 </td>
315 </tr>
316
317 <tr>
318 <td>MYSQL_TYPE_NEWDECIMAL</td><td>246</td>
319 <td>2 bytes</td>
320 <td>A 1 byte unsigned int representing the precision, followed
321 by a 1 byte unsigned int representing the number of decimals.</td>
322 </tr>
323
324 <tr>
325 <td><i>MYSQL_TYPE_ENUM</i></td><td><i>247</i></td>
326 <td>&ndash;</td>
327 <td><i>This enumeration value is only used internally and cannot
328 exist in a binlog.</i></td>
329 </tr>
330
331 <tr>
332 <td><i>MYSQL_TYPE_SET</i></td><td><i>248</i></td>
333 <td>&ndash;</td>
334 <td><i>This enumeration value is only used internally and cannot
335 exist in a binlog.</i></td>
336 </tr>
337
338 <tr>
339 <td>MYSQL_TYPE_TINY_BLOB</td><td>249</td>
340 <td>&ndash;</td>
341 <td><i>This enumeration value is only used internally and cannot
342 exist in a binlog.</i></td>
343 </tr>
344
345 <tr>
346 <td><i>MYSQL_TYPE_MEDIUM_BLOB</i></td><td><i>250</i></td>
347 <td>&ndash;</td>
348 <td><i>This enumeration value is only used internally and cannot
349 exist in a binlog.</i></td>
350 </tr>
351
352 <tr>
353 <td><i>MYSQL_TYPE_LONG_BLOB</i></td><td><i>251</i></td>
354 <td>&ndash;</td>
355 <td><i>This enumeration value is only used internally and cannot
356 exist in a binlog.</i></td>
357 </tr>
358
359 <tr>
360 <td>MYSQL_TYPE_BLOB</td><td>252</td>
361 <td>1 byte</td>
362 <td>The pack length, i.e., the number of bytes needed to represent
363 the length of the blob: 1, 2, 3, or 4.</td>
364 </tr>
365
366 <tr>
367 <td>MYSQL_TYPE_VAR_STRING</td><td>253</td>
368 <td>2 bytes</td>
369 <td>This is used to store both strings and enumeration values.
370 The first byte is a enumeration value storing the <i>real
371 type</i>, which may be either MYSQL_TYPE_VAR_STRING or
372 MYSQL_TYPE_ENUM. The second byte is a 1 byte unsigned integer
373 representing the field size, i.e., the number of bytes needed to
374 store the length of the string.</td>
375 </tr>
376
377 <tr>
378 <td>MYSQL_TYPE_STRING</td><td>254</td>
379 <td>2 bytes</td>
380 <td>The first byte is always MYSQL_TYPE_VAR_STRING (i.e., 253).
381 The second byte is the field size, i.e., the number of bytes in
382 the representation of size of the string: 3 or 4.</td>
383 </tr>
384
385 <tr>
386 <td>MYSQL_TYPE_GEOMETRY</td><td>255</td>
387 <td>1 byte</td>
388 <td>The pack length, i.e., the number of bytes needed to represent
389 the length of the geometry: 1, 2, 3, or 4.</td>
390 </tr>
391
392 <tr>
393 <td>MYSQL_TYPE_TYPED_ARRAY</td><td>15</td>
394 <td>up to 4 bytes</td>
395 <td>
396 - The first byte holds the MySQL type for the elements.
397 - The following 0, 1, 2, or 3 bytes holds the metadata for the MySQL
398 type for the elements. The contents of these bytes depends on the
399 element type, as described in the other rows of this table.
400 </td>
401 </tr>
402
403 </table>
404
405 The table below lists all optional metadata types, along with the numerical
406 identifier for it and the size and interpretation of meta-data used
407 to describe the type.
408
409 @anchor Table_table_map_event_optional_metadata
410 <table>
411 <caption>Table_map_event optional metadata types: numerical identifier and
412 metadata. Optional metadata fields are stored in TLV fields.
413 Format of values are described in this table. </caption>
414 <tr>
415 <th>Type</th>
416 <th>Description</th>
417 <th>Format</th>
418 </tr>
419 <tr>
420 <td>SIGNEDNESS</td>
421 <td>signedness of numeric colums. This is included for all values of
422 binlog_row_metadata.</td>
423 <td>For each numeric column, a bit indicates whether the numeric
424 colunm has unsigned flag. 1 means it is unsigned. The number of
425 bytes needed for this is int((column_count + 7) / 8). The order is
426 the same as the order of column_type field.</td>
427 </tr>
428 <tr>
429 <td>DEFAULT_CHARSET</td>
430 <td>Charsets of character columns. It has a default charset for
431 the case that most of character columns have same charset and the
432 most used charset is binlogged as default charset.Collation
433 numbers are binlogged for identifying charsets. They are stored in
434 packed length format. Either DEFAULT_CHARSET or COLUMN_CHARSET is
435 included for all values of binlog_row_metadata.</td>
436 <td>Default charset's collation is logged first. The charsets which are not
437 same to default charset are logged following default charset. They are
438 logged as column index and charset collation number pair sequence. The
439 column index is counted only in all character columns. The order is same to
440 the order of column_type
441 field. </td>
442 </tr>
443 <tr>
444 <td>COLUMN_CHARSET</td>
445 <td>Charsets of character columns. For the case that most of columns have
446 different charsets, this field is logged. It is never logged with
447 DEFAULT_CHARSET together. Either DEFAULT_CHARSET or COLUMN_CHARSET is
448 included for all values of binlog_row_metadata.</td>
449 <td>It is a collation number sequence for all character columns.</td>
450 </tr>
451 <tr>
452 <td>COLUMN_NAME</td>
453 <td>Names of columns. This is only included if
454 binlog_row_metadata=FULL.</td>
455 <td>A sequence of column names. For each column name, 1 byte for
456 the string length in bytes is followed by a string without null
457 terminator.</td>
458 </tr>
459 <tr>
460 <td>SET_STR_VALUE</td>
461 <td>The string values of SET columns. This is only included if
462 binlog_row_metadata=FULL.</td>
463 <td>For each SET column, a pack_length representing the value
464 count is followed by a sequence of length and string pairs. length
465 is the byte count in pack_length format. The string has no null
466 terminator.</td>
467 </tr>
468 <tr>
469 <td>ENUM_STR_VALUE</td>
470 <td>The string values is ENUM columns. This is only included
471 if binlog_row_metadata=FULL.</td>
472 <td>The format is the same as SET_STR_VALUE.</td>
473 </tr>
474 <tr>
475 <td>GEOMETRY_TYPE</td>
476 <td>The real type of geometry columns. This is only included
477 if binlog_row_metadata=FULL.</td>
478 <td>A sequence of real type of geometry columns are stored in pack_length
479 format. </td>
480 </tr>
481 <tr>
482 <td>SIMPLE_PRIMARY_KEY</td>
483 <td>The primary key without any prefix. This is only included
484 if binlog_row_metadata=FULL and there is a primary key where every
485 key part covers an entire column.</td>
486 <td>A sequence of column indexes. The indexes are stored in pack_length
487 format.</td>
488 </tr>
489 <tr>
490 <td>PRIMARY_KEY_WITH_PREFIX</td>
491 <td>The primary key with some prefix. It doesn't appear together with
492 SIMPLE_PRIMARY_KEY. This is only included if
493 binlog_row_metadata=FULL and there is a primary key where some key
494 part covers a prefix of the column.</td>
495 <td>A sequence of column index and prefix length pairs. Both
496 column index and prefix length are in pack_length format. Prefix length
497 0 means that the whole column value is used.</td>
498 </tr>
499 <tr>
500 <td>ENUM_AND_SET_DEFAULT_CHARSET</td>
501 <td>Charsets of ENUM and SET columns. It has the same layout as
502 DEFAULT_CHARSET. If there are SET or ENUM columns and
503 binlog_row_metadata=FULL, exactly one of
504 ENUM_AND_SET_DEFAULT_CHARSET and ENUM_AND_SET_COLUMN_CHARSET
505 appears (the encoder chooses the representation that uses the
506 least amount of space). Otherwise, none of them appears.</td>
507 <td>The same format as for DEFAULT_CHARSET, except it counts ENUM
508 and SET columns rather than character columns.</td>
509 </tr>
510 <tr>
511 <td>ENUM_AND_SET_COLUMN_CHARSET</td>
512 <td>Charsets of ENUM and SET columns. It has the same layout as
513 COLUMN_CHARSET. If there are SET or ENUM columns and
514 binlog_row_metadata=FULL, exactly one of
515 ENUM_AND_SET_DEFAULT_CHARSET and ENUM_AND_SET_COLUMN_CHARSET
516 appears (the encoder chooses the representation that uses the
517 least amount of space). Otherwise, none of them appears.</td>
518 <td>The same format as for COLUMN_CHARSET, except it counts ENUM
519 and SET columns rather than character columns.</td>
520 </tr>
521 </table>
522*/
524 public:
525 /** Constants representing offsets */
527 /** TM = "Table Map" */
530 };
531
532 typedef uint16_t flag_set;
533
534 /**
535 DEFAULT_CHARSET and COLUMN_CHARSET don't appear together, and
536 ENUM_AND_SET_DEFAULT_CHARSET and ENUM_AND_SET_COLUMN_CHARSET don't
537 appear together. They are just alternative ways to pack character
538 set information. When binlogging, it logs character sets in the
539 way that occupies least storage.
540
541 SIMPLE_PRIMARY_KEY and PRIMARY_KEY_WITH_PREFIX don't appear together.
542 SIMPLE_PRIMARY_KEY is for the primary keys which only use whole values of
543 pk columns. PRIMARY_KEY_WITH_PREFIX is
544 for the primary keys which just use part value of pk columns.
545 */
547 SIGNEDNESS = 1, // UNSIGNED flag of numeric columns
548 DEFAULT_CHARSET, /* Character set of string columns, optimized to
549 minimize space when many columns have the
550 same charset. */
551 COLUMN_CHARSET, /* Character set of string columns, optimized to
552 minimize space when columns have many
553 different charsets. */
555 SET_STR_VALUE, // String value of SET columns
556 ENUM_STR_VALUE, // String value of ENUM columns
557 GEOMETRY_TYPE, // Real type of geometry columns
558 SIMPLE_PRIMARY_KEY, // Primary key without prefix
559 PRIMARY_KEY_WITH_PREFIX, // Primary key with prefix
560 ENUM_AND_SET_DEFAULT_CHARSET, /* Character set of enum and set
561 columns, optimized to minimize
562 space when many columns have the
563 same charset. */
564 ENUM_AND_SET_COLUMN_CHARSET, /* Character set of enum and set
565 columns, optimized to minimize
566 space when many columns have the
567 same charset. */
568 COLUMN_VISIBILITY /* Flag to indicate column visibility
569 attribute. */
570 };
571
572 /**
573 Metadata_fields organizes m_optional_metadata into a structured format which
574 is easy to access.
575 */
577 typedef std::pair<unsigned int, unsigned int> uint_pair;
578 typedef std::vector<std::string> str_vector;
579
582 bool empty() const { return default_charset == 0; }
583
584 // Default charset for the columns which are not in charset_pairs.
585 unsigned int default_charset;
586
587 /* The uint_pair means <column index, column charset number>. */
588 std::vector<uint_pair> charset_pairs;
589 };
590
591 // Contents of DEFAULT_CHARSET field are converted into Default_charset.
593 // Contents of ENUM_AND_SET_DEFAULT_CHARSET are converted into
594 // Default_charset.
596 std::vector<bool> m_signedness;
597 // Character set number of every string column
598 std::vector<unsigned int> m_column_charset;
599 // Character set number of every ENUM or SET column.
600 std::vector<unsigned int> m_enum_and_set_column_charset;
601 std::vector<std::string> m_column_name;
602 // each str_vector stores values of one enum/set column
603 std::vector<str_vector> m_enum_str_value;
604 std::vector<str_vector> m_set_str_value;
605 std::vector<unsigned int> m_geometry_type;
606 /*
607 The uint_pair means <column index, prefix length>. Prefix length is 0 if
608 whole column value is used.
609 */
610 std::vector<uint_pair> m_primary_key;
611 std::vector<bool> m_column_visibility;
612
613 /*
614 It parses m_optional_metadata and populates into above variables.
615
616 @param[in] optional_metadata points to the begin of optional metadata
617 fields in table_map_event.
618 @param[in] optional_metadata_len length of optional_metadata field.
619 */
620 Optional_metadata_fields(unsigned char *optional_metadata,
621 unsigned int optional_metadata_len);
622 // It is used to specify the validity of the deserialized structure
624 };
625
626 /**
627 <pre>
628 The buffer layout for fixed data part is as follows:
629 +-----------------------------------+
630 | table_id | Reserved for future use|
631 +-----------------------------------+
632 </pre>
633
634 <pre>
635 The buffer layout for variable data part is as follows:
636 +--------------------------------------------------------------------------+
637 | db len| db name | table len| table name | no of cols | array of col types|
638 +--------------------------------------------------------------------------+
639 +---------------------------------------------+
640 | metadata len | metadata block | m_null_bits |
641 +---------------------------------------------+
642 </pre>
643
644 @param buf Contains the serialized event.
645 @param fde An FDE event (see Rotate_event constructor for more info).
646 */
647 Table_map_event(const char *buf, const Format_description_event *fde);
648
649 Table_map_event(const Table_id &tid, unsigned long colcnt, const char *dbnam,
650 size_t dblen, const char *tblnam, size_t tbllen)
652 m_table_id(tid),
653 m_data_size(0),
654 m_dbnam(""),
655 m_dblen(dblen),
656 m_tblnam(""),
657 m_tbllen(tbllen),
658 m_colcnt(colcnt),
664 if (dbnam) m_dbnam = std::string(dbnam, m_dblen);
665 if (tblnam) m_tblnam = std::string(tblnam, m_tbllen);
666 }
667
668 ~Table_map_event() override;
669
670 /** Event post header contents */
673
674 size_t m_data_size; /** event data size */
675
676 /** Event body contents */
677 std::string m_dbnam;
678 unsigned long long int m_dblen;
679 std::string m_tblnam;
680 unsigned long long int m_tbllen;
681 unsigned long m_colcnt;
682 unsigned char *m_coltype;
683
684 /**
685 The size of field metadata buffer set by calling save_field_metadata()
686 */
688 unsigned char *m_field_metadata; /** field metadata */
689 unsigned char *m_null_bits;
691 unsigned char *m_optional_metadata;
692
701
702 unsigned long long get_table_id() { return m_table_id.id(); }
703 std::string get_table_name() { return m_tblnam; }
704 std::string get_db_name() { return m_dbnam; }
705
706#ifndef HAVE_MYSYS
707 void print_event_info(std::ostream &info) override;
708 void print_long_info(std::ostream &info) override;
709#endif
710};
711
712/**
713 @class Rows_event
714
715 Common base class for all row-containing binary log events.
716
717 RESPONSIBILITIES
718
719 - Provide an interface for adding an individual row to the event.
720
721 @section Rows_event_binary_format Binary Format
722
723 The Post-Header has the following components:
724
725 <table>
726 <caption>Post-Header for Rows_event</caption>
727
728 <tr>
729 <th>Name</th>
730 <th>Format</th>
731 <th>Description</th>
732 </tr>
733
734 <tr>
735 <td>table_id</td>
736 <td>6 bytes unsigned integer</td>
737 <td>The number that identifies the table</td>
738 </tr>
739
740 <tr>
741 <td>flags</td>
742 <td>2 byte bitfield</td>
743 <td>Reserved for future use; currently always 0.</td>
744 </tr>
745
746 </table>
747
748 The Body has the following components:
749
750 <table>
751 <caption>Body for Rows_event</caption>
752
753 <tr>
754 <th>Name</th>
755 <th>Format</th>
756 <th>Description</th>
757 </tr>
758
759
760 <tr>
761 <td>width</td>
762 <td>packed integer</td>
763 <td>Represents the number of columns in the table</td>
764 </tr>
765
766 <tr>
767 <td>cols</td>
768 <td>Bitfield, variable sized</td>
769 <td>Indicates whether each column is used, one bit per column.
770 For this field, the amount of storage required is
771 INT((width + 7) / 8) bytes. </td>
772 </tr>
773
774 <tr>
775 <td>extra_row_info</td>
776 <td>An object of class Extra_row_info</td>
777 <td>The class Extra_row_info will be storing the information related
778 to m_extra_row_ndb_info and partition info (partition_id and
779 source_partition_id). At any given time a Rows_event can have both, one
780 or none of ndb_info and partition_info present as part of Rows_event.
781 In case both ndb_info and partition_info are present then below will
782 be the order in which they will be stored.
783
784 @verbatim
785 +----------+--------------------------------------+
786 |type_code | extra_row_ndb_info |
787 +--- ------+--------------------------------------+
788 | NDB |Len of ndb_info |Format |ndb_data |
789 | 1 byte |1 byte |1 byte |len - 2 byte |
790 +----------+----------------+-------+-------------+
791
792 In case of INSERT/DELETE
793 +-----------+----------------+
794 | type_code | partition_info |
795 +-----------+----------------+
796 | PART | partition_id |
797 | (1 byte) | 2 byte |
798 +-----------+----------------+
799
800 In case of UPDATE
801 +-----------+------------------------------------+
802 | type_code | partition_info |
803 +-----------+--------------+---------------------+
804 | PART | partition_id | source_partition_id |
805 | (1 byte) | 2 byte | 2 byte |
806 +-----------+--------------+---------------------+
807
808 source_partition_id is used only in the case of Update_event
809 to log the partition_id of the source partition.
810
811 @endverbatim
812 This is the format for any information stored as extra_row_info.
813 type_code is not a part of the class Extra_row_info as it is a constant
814 values used at the time of serializing and decoding the event.
815 </td>
816 </tr>
817
818 <tr>
819 <td>columns_before_image</td>
820 <td>vector of elements of type uint8_t</td>
821 <td>For DELETE and UPDATE only.
822 Bit-field indicating whether each column is used
823 one bit per column. For this field, the amount of storage
824 required for N columns is INT((N + 7) / 8) bytes.</td>
825 </tr>
826
827 <tr>
828 <td>columns_after_image</td>
829 <td>vector of elements of type uint8_t</td>
830 <td>For WRITE and UPDATE only.
831 Bit-field indicating whether each column is used in the
832 UPDATE_ROWS_EVENT and WRITE_ROWS_EVENT after-image; one bit per column.
833 For this field, the amount of storage required for N columns
834 is INT((N + 7) / 8) bytes.
835
836 @verbatim
837 +-------------------------------------------------------+
838 | Event Type | Cols_before_image | Cols_after_image |
839 +-------------------------------------------------------+
840 | DELETE | Deleted row | NULL |
841 | INSERT | NULL | Inserted row |
842 | UPDATE | Old row | Updated row |
843 +-------------------------------------------------------+
844 @endverbatim
845 </td>
846 </tr>
847
848 <tr>
849 <td>row</td>
850 <td>vector of elements of type uint8_t</td>
851 <td> A sequence of zero or more rows. The end is determined by the size
852 of the event. Each row has the following format:
853 - A Bit-field indicating whether each field in the row is NULL.
854 Only columns that are "used" according to the second field in
855 the variable data part are listed here. If the second field in
856 the variable data part has N one-bits, the amount of storage
857 required for this field is INT((N + 7) / 8) bytes.
858 - The row-image, containing values of all table fields. This only
859 lists table fields that are used (according to the second field
860 of the variable data part) and non-NULL (according to the
861 previous field). In other words, the number of values listed here
862 is equal to the number of zero bits in the previous field.
863 (not counting padding bits in the last byte).
864 @verbatim
865 For example, if a INSERT statement inserts into 4 columns of a
866 table, N= 4 (in the formula above).
867 length of bitmask= (4 + 7) / 8 = 1
868 Number of fields in the row= 4.
869
870 +------------------------------------------------+
871 |Null_bit_mask(4)|field-1|field-2|field-3|field 4|
872 +------------------------------------------------+
873 @endverbatim
874 </td>
875 </tr>
876 </table>
877*/
879 public:
880 /**
881 These definitions allow to combine the flags into an
882 appropriate flag set using the normal bitwise operators. The
883 implicit conversion from an enum-constant to an integer is
884 accepted by the compiler, which is then used to set the real set
885 of flags.
886 */
888 /** Last event of a statement */
889 STMT_END_F = (1U << 0),
890 /** Value of the OPTION_NO_FOREIGN_KEY_CHECKS flag in thd->options */
892 /** Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options */
894 /**
895 Indicates that rows in this event are complete, that is contain
896 values for all columns of the table.
897 */
898 COMPLETE_ROWS_F = (1U << 3)
899 };
900
901 /**
902 Constructs an event directly. The members are assigned default values.
903
904 @param type_arg Type of ROW_EVENT. Expected types are:
905 - WRITE_ROWS_EVENT, WRITE_ROWS_EVENT_V1
906 - UPDATE_ROWS_EVENT, UPDATE_ROWS_EVENT_V1,
907 PARTIAL_UPDATE_ROWS_EVENT
908 - DELETE_ROWS_EVENT, DELETE_ROWS_EVENT_V1
909 */
910 explicit Rows_event(Log_event_type type_arg)
911 : Binary_log_event(type_arg),
912 m_table_id(0),
913 m_width(0),
916 row(0) {}
917 /**
918 The constructor is responsible for decoding the event contained in
919 the buffer.
920
921 <pre>
922 The buffer layout for fixed data part is as follows
923 +------------------------------------+
924 | table_id | reserved for future use |
925 +------------------------------------+
926 </pre>
927
928 <pre>
929 The buffer layout for variable data part is as follows
930 +------------------------------------------------------------------+
931 | var_header_len | column_before_image | columns_after_image | row |
932 +------------------------------------------------------------------+
933 </pre>
934
935 @param buf Contains the serialized event.
936 @param fde An FDE event (see Rotate_event constructor for more info).
937 */
938 Rows_event(const char *buf, const Format_description_event *fde);
939
940 ~Rows_event() override;
941
942 protected:
943 Log_event_type m_type; /** Actual event type */
944
945 /** Post header content */
947 uint16_t m_flags; /** Flags for row-level events */
948
949 /* Body of the event */
950 unsigned long m_width; /** The width of the columns bitmap */
951 uint32_t n_bits_len; /** value determined by (m_width + 7) / 8 */
953
954 std::vector<uint8_t> columns_before_image;
955 std::vector<uint8_t> columns_after_image;
956 std::vector<uint8_t> row;
957
958 public:
960 private:
961 /** partition_id for a row in a partitioned table */
963 /**
964 It is the partition_id of the source partition in case
965 of Update_event, the target's partition_id is m_partition_id.
966 This variable is used only in case of Update_event.
967 */
969 /** The extra row info provided by NDB */
970 unsigned char *m_extra_row_ndb_info;
971
972 public:
977
979
980 int get_partition_id() const { return m_partition_id; }
981 void set_partition_id(int partition_id) {
982 BAPI_ASSERT(partition_id <= 65535);
983 m_partition_id = partition_id;
984 }
985
987 void set_source_partition_id(int source_partition_id) {
988 BAPI_ASSERT(source_partition_id <= 65535);
989 m_source_partition_id = source_partition_id;
990 }
991
992 unsigned char *get_ndb_info() const { return m_extra_row_ndb_info; }
993 void set_ndb_info(const unsigned char *ndb_info, size_t len) {
996 static_cast<unsigned char *>(bapi_malloc(len, 16 /* flags */));
997 std::copy(ndb_info, ndb_info + len, m_extra_row_ndb_info);
998 }
999 /**
1000 Compares the extra_row_info in a Row event, it checks three things
1001 1. The m_extra_row_ndb_info pointers. It compares their significant bytes.
1002 2. Partition_id
1003 3. source_partition_id
1004
1005 @return
1006 true all the above variables are same in the event and the one passed
1007 in parameter.
1008 false Any of the above variable has a different value.
1009 */
1010 bool compare_extra_row_info(const unsigned char *ndb_info_arg,
1011 int part_id_arg, int source_part_id);
1012
1013 bool have_part() const { return m_partition_id != UNDEFINED; }
1014
1015 bool have_ndb_info() const { return m_extra_row_ndb_info != nullptr; }
1016 size_t get_ndb_length();
1017 size_t get_part_length();
1019
1020 static const int UNDEFINED{INT_MAX};
1021 };
1023
1024 unsigned long long get_table_id() const { return m_table_id.id(); }
1025
1026 enum_flag get_flags() const { return static_cast<enum_flag>(m_flags); }
1027
1028 uint32_t get_null_bits_len() const { return n_bits_len; }
1029
1030 unsigned long get_width() const { return m_width; }
1031
1032 static std::string get_flag_string(enum_flag flag) {
1033 std::string str = "";
1034 if (flag & STMT_END_F) str.append(" Last event of the statement");
1035 if (flag & NO_FOREIGN_KEY_CHECKS_F) str.append(" No foreign Key checks");
1036 if (flag & RELAXED_UNIQUE_CHECKS_F) str.append(" No unique key checks");
1037 if (flag & COMPLETE_ROWS_F) str.append(" Complete Rows");
1040 str.append("Unknown Flag");
1041 return str;
1042 }
1043#ifndef HAVE_MYSYS
1044 void print_event_info(std::ostream &info) override;
1045 void print_long_info(std::ostream &info) override;
1046#endif
1047
1048 template <class Iterator_value_type>
1050};
1051
1052/**
1053 @class Write_rows_event
1054
1055 Log row insertions. The event contain several insert/update rows
1056 for a table. Note that each event contains only rows for one table.
1057
1058 @section Write_rows_event_binary_format Binary Format
1059*/
1060class Write_rows_event : public virtual Rows_event {
1061 public:
1062 Write_rows_event(const char *buf, const Format_description_event *fde);
1064};
1065
1066/**
1067 @class Update_rows_event
1068
1069 Log row updates with a before image. The event contain several
1070 update rows for a table. Note that each event contains only rows for
1071 one table.
1072
1073 Also note that the row data consists of pairs of row data: one row
1074 for the old data and one row for the new data.
1075
1076 @section Update_rows_event_binary_format Binary Format
1077*/
1078class Update_rows_event : public virtual Rows_event {
1079 public:
1080 Update_rows_event(const char *buf, const Format_description_event *fde);
1081 Update_rows_event(Log_event_type event_type) : Rows_event(event_type) {}
1082};
1083
1084/**
1085 @class Delete_rows_event
1086
1087 Log row deletions. The event contain several delete rows for a
1088 table. Note that each event contains only rows for one table.
1089
1090 RESPONSIBILITIES
1091
1092 - Act as a container for rows that has been deleted on the master
1093 and should be deleted on the slave.
1094
1095 @section Delete_rows_event_binary_format Binary Format
1096*/
1097class Delete_rows_event : public virtual Rows_event {
1098 public:
1099 Delete_rows_event(const char *buf, const Format_description_event *fde);
1101};
1102
1103/**
1104 @class Rows_query_event
1105
1106 Rows query event type, which is a subclass
1107 of the Ignorable_event, to record the original query for the rows
1108 events in RBR. This event can be used to display the original query as
1109 comments by SHOW BINLOG EVENTS query, or mysqlbinlog client when the
1110 --verbose option is given twice
1111
1112 @section Rows_query_var_event_binary_format Binary Format
1113
1114 The Post-Header for this event type is empty. The Body has one
1115 component:
1116
1117 <table>
1118 <caption>Body for Rows_query_event</caption>
1119
1120 <tr>
1121 <th>Name</th>
1122 <th>Format</th>
1123 <th>Description</th>
1124 </tr>
1125
1126 <tr>
1127 <td>m_rows_query</td>
1128 <td>char array</td>
1129 <td>Records the original query executed in RBR </td>
1130 </tr>
1131 </table>
1132*/
1133class Rows_query_event : public virtual Ignorable_event {
1134 public:
1135 /**
1136 It is used to write the original query in the binlog file in case of RBR
1137 when the session flag binlog_rows_query_log_events is set.
1138
1139 <pre>
1140 The buffer layout is as follows:
1141 +------------------------------------+
1142 | The original query executed in RBR |
1143 +------------------------------------+
1144 </pre>
1145
1146 @param buf Contains the serialized event.
1147 @param fde An FDE event (see Rotate_event constructor for more info).
1148 */
1149
1150 Rows_query_event(const char *buf, const Format_description_event *fde);
1151 /**
1152 It is the minimal constructor, and all it will do is set the type_code as
1153 ROWS_QUERY_LOG_EVENT in the header object in Binary_log_event.
1154 */
1157
1158 ~Rows_query_event() override;
1159
1160 protected:
1162};
1163} // namespace binary_log
1164
1165/**
1166 @} (end of group Replication)
1167*/
1168#endif /* ROWS_EVENT_INCLUDED */
Each table share has a table id, it is mainly used for row based replication.
Definition: table_id.h:39
unsigned long long id() const
Definition: table_id.h:49
This is the abstract base class for binary log events.
Definition: binlog_event.h:798
Log row deletions.
Definition: rows_event.h:1097
Delete_rows_event()
Definition: rows_event.h:1100
For binlog version 4.
Definition: control_events.h:230
Base class for ignorable log events.
Definition: control_events.h:651
Definition: rows_event.h:959
size_t get_part_length()
Definition: rows_event.cpp:509
Extra_row_info(const Extra_row_info &)=delete
int m_source_partition_id
It is the partition_id of the source partition in case of Update_event, the target's partition_id is ...
Definition: rows_event.h:968
bool compare_extra_row_info(const unsigned char *ndb_info_arg, int part_id_arg, int source_part_id)
Compares the extra_row_info in a Row event, it checks three things.
Definition: rows_event.cpp:486
unsigned char * m_extra_row_ndb_info
The extra row info provided by NDB.
Definition: rows_event.h:970
int get_partition_id() const
Definition: rows_event.h:980
void set_ndb_info(const unsigned char *ndb_info, size_t len)
Definition: rows_event.h:993
bool have_part() const
Definition: rows_event.h:1013
int m_partition_id
partition_id for a row in a partitioned table
Definition: rows_event.h:962
int get_source_partition_id() const
Definition: rows_event.h:986
unsigned char * get_ndb_info() const
Definition: rows_event.h:992
void set_source_partition_id(int source_partition_id)
Definition: rows_event.h:987
~Extra_row_info()
Definition: rows_event.cpp:518
static const int UNDEFINED
Definition: rows_event.h:1020
bool have_ndb_info() const
Definition: rows_event.h:1015
size_t get_ndb_length()
Definition: rows_event.cpp:502
Extra_row_info()
Definition: rows_event.h:973
void set_partition_id(int partition_id)
Definition: rows_event.h:981
Common base class for all row-containing binary log events.
Definition: rows_event.h:878
uint16_t m_flags
Definition: rows_event.h:947
unsigned long long get_table_id() const
Definition: rows_event.h:1024
enum_flag get_flags() const
Definition: rows_event.h:1026
enum_flag
These definitions allow to combine the flags into an appropriate flag set using the normal bitwise op...
Definition: rows_event.h:887
@ STMT_END_F
Last event of a statement.
Definition: rows_event.h:889
@ NO_FOREIGN_KEY_CHECKS_F
Value of the OPTION_NO_FOREIGN_KEY_CHECKS flag in thd->options.
Definition: rows_event.h:891
@ COMPLETE_ROWS_F
Indicates that rows in this event are complete, that is contain values for all columns of the table.
Definition: rows_event.h:898
@ RELAXED_UNIQUE_CHECKS_F
Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options.
Definition: rows_event.h:893
uint32_t n_bits_len
The width of the columns bitmap.
Definition: rows_event.h:951
std::vector< uint8_t > columns_after_image
Definition: rows_event.h:955
Log_event_type m_type
Definition: rows_event.h:943
static std::string get_flag_string(enum_flag flag)
Definition: rows_event.h:1032
unsigned long m_width
Flags for row-level events.
Definition: rows_event.h:950
unsigned long get_width() const
Definition: rows_event.h:1030
friend class Row_event_iterator
Definition: rows_event.h:1049
uint16_t var_header_len
value determined by (m_width + 7) / 8
Definition: rows_event.h:952
uint32_t get_null_bits_len() const
Definition: rows_event.h:1028
Rows_event(Log_event_type type_arg)
Constructs an event directly.
Definition: rows_event.h:910
std::vector< uint8_t > columns_before_image
Definition: rows_event.h:954
Table_id m_table_id
Actual event type.
Definition: rows_event.h:946
Extra_row_info m_extra_row_info
Definition: rows_event.h:1022
std::vector< uint8_t > row
Definition: rows_event.h:956
Rows query event type, which is a subclass of the Ignorable_event, to record the original query for t...
Definition: rows_event.h:1133
~Rows_query_event() override
Definition: rows_event.cpp:549
Rows_query_event()
It is the minimal constructor, and all it will do is set the type_code as ROWS_QUERY_LOG_EVENT in the...
Definition: rows_event.h:1155
char * m_rows_query
Definition: rows_event.h:1161
In row-based mode, every row operation event is preceded by a Table_map_event which maps a table defi...
Definition: rows_event.h:523
unsigned char * m_optional_metadata
Definition: rows_event.h:691
Optional_metadata_field_type
DEFAULT_CHARSET and COLUMN_CHARSET don't appear together, and ENUM_AND_SET_DEFAULT_CHARSET and ENUM_A...
Definition: rows_event.h:546
@ ENUM_AND_SET_COLUMN_CHARSET
Definition: rows_event.h:564
@ SIGNEDNESS
Definition: rows_event.h:547
@ DEFAULT_CHARSET
Definition: rows_event.h:548
@ SET_STR_VALUE
Definition: rows_event.h:555
@ COLUMN_NAME
Definition: rows_event.h:554
@ SIMPLE_PRIMARY_KEY
Definition: rows_event.h:558
@ ENUM_AND_SET_DEFAULT_CHARSET
Definition: rows_event.h:560
@ GEOMETRY_TYPE
Definition: rows_event.h:557
@ ENUM_STR_VALUE
Definition: rows_event.h:556
@ COLUMN_CHARSET
Definition: rows_event.h:551
@ COLUMN_VISIBILITY
Definition: rows_event.h:568
@ PRIMARY_KEY_WITH_PREFIX
Definition: rows_event.h:559
unsigned long m_field_metadata_size
The size of field metadata buffer set by calling save_field_metadata()
Definition: rows_event.h:687
~Table_map_event() override
Definition: rows_event.cpp:105
std::string m_dbnam
event data size
Definition: rows_event.h:677
uint16_t flag_set
Definition: rows_event.h:532
unsigned char * m_null_bits
field metadata
Definition: rows_event.h:689
unsigned char * m_field_metadata
Definition: rows_event.h:688
unsigned long long int m_tbllen
Definition: rows_event.h:680
unsigned long m_colcnt
Definition: rows_event.h:681
Table_map_event()
Definition: rows_event.h:693
unsigned int m_optional_metadata_len
Definition: rows_event.h:690
Table_map_event_offset
Constants representing offsets.
Definition: rows_event.h:526
@ TM_MAPID_OFFSET
TM = "Table Map".
Definition: rows_event.h:528
@ TM_FLAGS_OFFSET
Definition: rows_event.h:529
unsigned char * m_coltype
Definition: rows_event.h:682
flag_set m_flags
Definition: rows_event.h:672
std::string m_tblnam
Definition: rows_event.h:679
std::string get_table_name()
Definition: rows_event.h:703
size_t m_data_size
Definition: rows_event.h:674
std::string get_db_name()
Definition: rows_event.h:704
Table_map_event(const Table_id &tid, unsigned long colcnt, const char *dbnam, size_t dblen, const char *tblnam, size_t tbllen)
Definition: rows_event.h:649
unsigned long long int m_dblen
Definition: rows_event.h:678
Table_id m_table_id
Event post header contents.
Definition: rows_event.h:671
unsigned long long get_table_id()
Definition: rows_event.h:702
Log row updates with a before image.
Definition: rows_event.h:1078
Update_rows_event(Log_event_type event_type)
Definition: rows_event.h:1081
Update_rows_event(const char *buf, const Format_description_event *fde)
Definition: rows_event.cpp:562
Log row insertions.
Definition: rows_event.h:1060
Write_rows_event()
Definition: rows_event.h:1063
Contains the classes representing events operating in the replication stream properties.
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
enum_extra_row_info_typecode
This is the typecode defined for the different elements present in the container Extra_row_info,...
Definition: rows_event.h:62
static int flag
Definition: hp_test1.cc:39
Log info(cout, "NOTE")
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1055
The namespace contains classes representing events that can occur in a replication stream.
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:266
@ TABLE_MAP_EVENT
Definition: binlog_event.h:297
@ DELETE_ROWS_EVENT
Definition: binlog_event.h:329
@ ROWS_QUERY_LOG_EVENT
Definition: binlog_event.h:324
@ WRITE_ROWS_EVENT
Version 2 of the Row events.
Definition: binlog_event.h:327
Definition: buf0block_hint.cc:29
std::vector< uint_pair > charset_pairs
Definition: rows_event.h:588
Metadata_fields organizes m_optional_metadata into a structured format which is easy to access.
Definition: rows_event.h:576
Default_charset m_default_charset
Definition: rows_event.h:592
std::vector< std::string > str_vector
Definition: rows_event.h:578
std::vector< str_vector > m_enum_str_value
Definition: rows_event.h:603
std::vector< unsigned int > m_column_charset
Definition: rows_event.h:598
Optional_metadata_fields(unsigned char *optional_metadata, unsigned int optional_metadata_len)
Definition: rows_event.cpp:320
std::vector< unsigned int > m_geometry_type
Definition: rows_event.h:605
std::pair< unsigned int, unsigned int > uint_pair
Definition: rows_event.h:577
std::vector< bool > m_column_visibility
Definition: rows_event.h:611
std::vector< uint_pair > m_primary_key
Definition: rows_event.h:610
std::vector< str_vector > m_set_str_value
Definition: rows_event.h:604
std::vector< bool > m_signedness
Definition: rows_event.h:596
std::vector< std::string > m_column_name
Definition: rows_event.h:601
Default_charset m_enum_and_set_default_charset
Definition: rows_event.h:595
std::vector< unsigned int > m_enum_and_set_column_charset
Definition: rows_event.h:600
Contains the class Table_id, mainly used for row based replication.
#define BAPI_ASSERT(x)
Definition: wrapper_functions.h:61
void * bapi_malloc(size_t size, int flags)
This is a wrapper function in order to allocate memory from the heap in the binlogevent library.
Definition: wrapper_functions.h:169