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<M: BinlogMode = Unspecified> {
38 mode: std::marker::PhantomData<M>,
39 config: ParserConfig,
40 headers: Option<[u8; 256]>,
41}
42impl Default for BinlogParser {
43 fn default() -> Self {
44 Self::new(ParserConfig::default())
45 }
46}
47impl BinlogParser {
48 pub fn new(config: ParserConfig) -> Self {
49 Self::for_mode(config)
50 }
51}
52impl<M: BinlogMode> BinlogParser<M> {
53 pub fn for_mode(config: ParserConfig) -> Self {
54 Self {
55 config,
56 headers: None,
57 mode: std::marker::PhantomData,
58 }
59 }
60 pub fn with_mode<N: BinlogMode>(self) -> BinlogParser<N> {
61 BinlogParser {
62 config: self.config,
63 headers: self.headers,
64 mode: std::marker::PhantomData,
65 }
66 }
67 pub fn parse<'a>(&mut self, frame: BinlogFrame<'a>) -> Result<BinlogEvent<'a, M>> {
68 let kind = frame.header.event_type;
69 let mut event = if kind == code::FORMAT_DESCRIPTION_EVENT {
70 let mut format = FormatDescriptionEvent::default();
71 format.decode(frame.body)?;
72 format.checksum_algorithm =
73 match frame.raw_data.get(EventHeader::LEN + frame.body.len()) {
74 Some(0) => ChecksumAlgorithm::None,
75 Some(1) => ChecksumAlgorithm::Crc32,
76 _ => return Err(invalid("missing FDE checksum algorithm")),
77 };
78 let (major, minor, _) = crate::version::parse_server_version(format.server_version)?;
79 if format
80 .server_version
81 .to_ascii_lowercase()
82 .contains("mariadb")
83 || !matches!((major, minor), (5, 7) | (8, 0..=4) | (9, 0 | 7))
84 {
85 return Err(Error::Unsupported(format!(
86 "unregistered producer {}",
87 format.server_version
88 )));
89 }
90 for (kind, length) in [
91 (code::QUERY_EVENT, 13),
92 (code::ROTATE_EVENT, 8),
93 (code::TABLE_MAP_EVENT, 8),
94 (code::WRITE_ROWS_EVENT, 10),
95 (code::UPDATE_ROWS_EVENT, 10),
96 (code::DELETE_ROWS_EVENT, 10),
97 ] {
98 if format.post_header_lengths.get(usize::from(kind) - 1) != Some(&length) {
99 return Err(Error::Unsupported(format!(
100 "unvalidated post-header length for event {kind}"
101 )));
102 }
103 }
104 for kind in [
105 code::WRITE_ROWS_EVENT_V1,
106 code::UPDATE_ROWS_EVENT_V1,
107 code::DELETE_ROWS_EVENT_V1,
108 ] {
109 if !matches!(
110 format.post_header_lengths.get(usize::from(kind) - 1),
111 Some(0 | 8)
112 ) {
113 return Err(Error::Unsupported(format!(
114 "unvalidated legacy ROWS post-header {kind}"
115 )));
116 }
117 }
118 let mut headers = [0; 256];
119 if format.post_header_lengths.len() > 255 {
120 return Err(invalid("too many FDE event types"));
121 }
122 headers[1..1 + format.post_header_lengths.len()]
123 .copy_from_slice(format.post_header_lengths);
124 self.headers = Some(headers);
125 EventData::FormatDescription(format)
126 } else {
127 if self.headers.is_none() && kind != code::ROTATE_EVENT {
128 return Err(invalid("event before FORMAT_DESCRIPTION"));
129 }
130 let post_header = self
131 .headers
132 .as_ref()
133 .map_or(8, |h| usize::from(h[usize::from(kind)]));
134 match kind {
135 code::QUERY_EVENT => EventData::Query(QueryEvent::with_post_header(post_header)),
136 code::ROTATE_EVENT => EventData::Rotate(RotateEvent::with_post_header(post_header)),
137 code::TABLE_MAP_EVENT => {
138 EventData::TableMap(TableMapEvent::with_post_header(post_header))
139 }
140 code::WRITE_ROWS_EVENT_V1
141 | code::UPDATE_ROWS_EVENT_V1
142 | code::DELETE_ROWS_EVENT_V1
143 | code::WRITE_ROWS_EVENT
144 | code::UPDATE_ROWS_EVENT
145 | code::DELETE_ROWS_EVENT => {
146 if post_header == 0 {
147 return Err(Error::Unsupported(format!(
148 "ROWS event {kind} absent from FDE"
149 )));
150 }
151 let operation = match kind {
152 code::WRITE_ROWS_EVENT_V1 | code::WRITE_ROWS_EVENT => RowsKind::Write,
153 code::UPDATE_ROWS_EVENT_V1 | code::UPDATE_ROWS_EVENT => RowsKind::Update,
154 _ => RowsKind::Delete,
155 };
156 let v2 = matches!(
157 kind,
158 code::WRITE_ROWS_EVENT | code::UPDATE_ROWS_EVENT | code::DELETE_ROWS_EVENT
159 );
160 EventData::Rows(RowsEvent::<M>::for_mode(operation, post_header, v2))
161 }
162 code::XID_EVENT => EventData::Xid(Default::default()),
163 code::GTID_EVENT if self.config.decode_gtids => EventData::Gtid(Default::default()),
164 code::ANONYMOUS_GTID_EVENT if self.config.decode_gtids => {
165 EventData::AnonymousGtid(Default::default())
166 }
167 code::GTID_TAGGED_LOG_EVENT if self.config.decode_gtids => {
168 EventData::TaggedGtid(Default::default())
169 }
170 code::PREVIOUS_GTIDS_EVENT if self.config.decode_previous_gtids => {
171 EventData::PreviousGtids(Default::default())
172 }
173 code::HEARTBEAT_EVENT => EventData::Heartbeat(Default::default()),
174 _ => EventData::Raw(Default::default()),
175 }
176 };
177 if kind != code::FORMAT_DESCRIPTION_EVENT {
178 event.decode(frame.body)?;
179 }
180 if let EventData::Rows(rows) = &event
181 && self.config.rows == RowsDecode::ValidateImages
182 {
183 rows.images()?;
184 }
185 if let EventData::Gtid(gtid) = &event
186 && gtid.gno == 0
187 {
188 return Err(invalid("GTID GNO must be positive"));
189 }
190 Ok(BinlogEvent {
191 raw_data: frame.raw_data,
192 header: frame.header,
193 event,
194 body: frame.body,
195 })
196 }
197}