mysql_replication/
parser.rs1use crate::{BinlogFrame, Error, Result, error::invalid, event_type as code, events::*};
2
3#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
5pub enum RowsDecode {
6 #[default]
7 Deferred,
8 ValidateImages,
9}
10#[derive(Debug, Clone, Copy)]
11pub struct ParserConfig {
12 pub rows: RowsDecode,
13 pub decode_gtids: bool,
14 pub decode_previous_gtids: bool,
15}
16impl Default for ParserConfig {
17 fn default() -> Self {
18 Self {
19 rows: RowsDecode::Deferred,
20 decode_gtids: true,
21 decode_previous_gtids: true,
22 }
23 }
24}
25impl ParserConfig {
26 pub fn minimal() -> Self {
28 Self {
29 decode_gtids: false,
30 decode_previous_gtids: false,
31 ..Self::default()
32 }
33 }
34}
35
36pub struct BinlogParser {
38 config: ParserConfig,
39 headers: Option<[u8; 256]>,
40}
41impl Default for BinlogParser {
42 fn default() -> Self {
43 Self::new(ParserConfig::default())
44 }
45}
46impl BinlogParser {
47 pub fn new(config: ParserConfig) -> Self {
48 Self {
49 config,
50 headers: None,
51 }
52 }
53 pub fn parse<'a>(&mut self, frame: BinlogFrame<'a>) -> Result<BinlogEvent<'a>> {
54 let kind = frame.header.event_type;
55 let mut event = if kind == code::FORMAT_DESCRIPTION_EVENT {
56 let mut format = FormatDescriptionEvent::default();
57 format.decode(frame.body)?;
58 format.checksum_algorithm =
59 match frame.raw_data.get(EventHeader::LEN + frame.body.len()) {
60 Some(0) => ChecksumAlgorithm::None,
61 Some(1) => ChecksumAlgorithm::Crc32,
62 _ => return Err(invalid("missing FDE checksum algorithm")),
63 };
64 let mut version = format.server_version.split('.');
65 let major = version.next().and_then(|s| s.parse::<u32>().ok());
66 let minor = version.next().and_then(|s| s.parse::<u32>().ok());
67 if !matches!(
68 (major, minor),
69 (Some(5), Some(7)) | (Some(8), Some(0..=4)) | (Some(9), Some(0 | 7))
70 ) {
71 return Err(Error::Unsupported(format!(
72 "unregistered producer {}",
73 format.server_version
74 )));
75 }
76 for (kind, length) in [
77 (code::QUERY_EVENT, 13),
78 (code::ROTATE_EVENT, 8),
79 (code::TABLE_MAP_EVENT, 8),
80 (code::WRITE_ROWS_EVENT, 10),
81 (code::UPDATE_ROWS_EVENT, 10),
82 (code::DELETE_ROWS_EVENT, 10),
83 ] {
84 if format.post_header_lengths.get(usize::from(kind) - 1) != Some(&length) {
85 return Err(Error::Unsupported(format!(
86 "unvalidated post-header length for event {kind}"
87 )));
88 }
89 }
90 for kind in [
91 code::WRITE_ROWS_EVENT_V1,
92 code::UPDATE_ROWS_EVENT_V1,
93 code::DELETE_ROWS_EVENT_V1,
94 ] {
95 if !matches!(
96 format.post_header_lengths.get(usize::from(kind) - 1),
97 Some(0 | 8)
98 ) {
99 return Err(Error::Unsupported(format!(
100 "unvalidated legacy ROWS post-header {kind}"
101 )));
102 }
103 }
104 let mut headers = [0; 256];
105 if format.post_header_lengths.len() > 255 {
106 return Err(invalid("too many FDE event types"));
107 }
108 headers[1..1 + format.post_header_lengths.len()]
109 .copy_from_slice(format.post_header_lengths);
110 self.headers = Some(headers);
111 EventData::FormatDescription(format)
112 } else {
113 if self.headers.is_none() && kind != code::ROTATE_EVENT {
114 return Err(invalid("event before FORMAT_DESCRIPTION"));
115 }
116 let post_header = self
117 .headers
118 .as_ref()
119 .map_or(8, |h| usize::from(h[usize::from(kind)]));
120 match kind {
121 code::QUERY_EVENT => EventData::Query(QueryEvent::with_post_header(post_header)),
122 code::ROTATE_EVENT => EventData::Rotate(RotateEvent::with_post_header(post_header)),
123 code::TABLE_MAP_EVENT => {
124 EventData::TableMap(TableMapEvent::with_post_header(post_header))
125 }
126 code::WRITE_ROWS_EVENT_V1
127 | code::UPDATE_ROWS_EVENT_V1
128 | code::DELETE_ROWS_EVENT_V1
129 | code::WRITE_ROWS_EVENT
130 | code::UPDATE_ROWS_EVENT
131 | code::DELETE_ROWS_EVENT => {
132 if post_header == 0 {
133 return Err(Error::Unsupported(format!(
134 "ROWS event {kind} absent from FDE"
135 )));
136 }
137 let operation = match kind {
138 code::WRITE_ROWS_EVENT_V1 | code::WRITE_ROWS_EVENT => RowsKind::Write,
139 code::UPDATE_ROWS_EVENT_V1 | code::UPDATE_ROWS_EVENT => RowsKind::Update,
140 _ => RowsKind::Delete,
141 };
142 let v2 = matches!(
143 kind,
144 code::WRITE_ROWS_EVENT | code::UPDATE_ROWS_EVENT | code::DELETE_ROWS_EVENT
145 );
146 EventData::Rows(RowsEvent::with_layout(operation, post_header, v2))
147 }
148 code::XID_EVENT => EventData::Xid(Default::default()),
149 code::GTID_EVENT if self.config.decode_gtids => EventData::Gtid(Default::default()),
150 code::ANONYMOUS_GTID_EVENT if self.config.decode_gtids => {
151 EventData::AnonymousGtid(Default::default())
152 }
153 code::GTID_TAGGED_LOG_EVENT if self.config.decode_gtids => {
154 EventData::TaggedGtid(Default::default())
155 }
156 code::PREVIOUS_GTIDS_EVENT if self.config.decode_previous_gtids => {
157 EventData::PreviousGtids(Default::default())
158 }
159 code::HEARTBEAT_EVENT => EventData::Heartbeat(Default::default()),
160 _ => EventData::Raw(Default::default()),
161 }
162 };
163 if kind != code::FORMAT_DESCRIPTION_EVENT {
164 event.decode(frame.body)?;
165 }
166 if let EventData::Rows(rows) = &event
167 && self.config.rows == RowsDecode::ValidateImages
168 {
169 rows.images()?;
170 }
171 if let EventData::Gtid(gtid) = &event
172 && gtid.gno == 0
173 {
174 return Err(invalid("GTID GNO must be positive"));
175 }
176 Ok(BinlogEvent {
177 raw_data: frame.raw_data,
178 header: frame.header,
179 event,
180 body: frame.body,
181 })
182 }
183}