1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use std::{cmp::min, fmt, io};
use bitvec::prelude::*;
use bytes::BufMut;
use saturating::Saturating as S;
use crate::{
binlog::{
consts::{BinlogVersion, EventType, RowsEventFlags},
row::BinlogRow,
BinlogCtx,
},
io::ParseBuf,
misc::{
raw::{
bytes::{BareBytes, EofBytes},
int::*,
RawBytes, RawFlags,
},
unexpected_buf_eof,
},
proto::{MyDeserialize, MySerialize},
};
use super::{BinlogEventHeader, TableMapEvent};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct RowsEvent<'a> {
event_type: EventType,
table_id: RawInt<LeU48>,
flags: RawFlags<RowsEventFlags, LeU16>,
extra_data: RawBytes<'a, BareBytes<{ u16::MAX as usize - 2 }>>,
num_columns: RawInt<LenEnc>,
columns_before_image: Option<RawBytes<'a, BareBytes<0x2000000000000000>>>,
columns_after_image: Option<RawBytes<'a, BareBytes<0x2000000000000000>>>,
rows_data: RawBytes<'a, EofBytes>,
}
impl<'a> RowsEvent<'a> {
pub fn event_type(&self) -> EventType {
self.event_type
}
pub fn table_id(&self) -> u64 {
self.table_id.0
}
pub fn num_columns(&self) -> u64 {
self.num_columns.0
}
pub fn columns_before_image(&'a self) -> Option<&'a BitSlice<Lsb0, u8>> {
match self.columns_before_image {
Some(ref bytes) => {
let slice = BitSlice::from_slice(bytes.as_bytes()).expect("the slice is too big");
Some(&slice[..self.num_columns() as usize])
}
None => None,
}
}
pub fn columns_after_image(&'a self) -> Option<&'a BitSlice<Lsb0, u8>> {
match self.columns_after_image {
Some(ref bytes) => {
let slice = BitSlice::from_slice(bytes.as_bytes()).expect("the slice is too big");
Some(&slice[..self.num_columns() as usize])
}
None => None,
}
}
pub fn rows_data(&'a self) -> &'a [u8] {
self.rows_data.as_bytes()
}
pub fn len(&self, _version: BinlogVersion) -> usize {
let mut len = S(0);
len += S(6);
len += S(2);
len += S(2);
len += S(min(self.extra_data.len(), u16::MAX as usize - 2));
len += S(crate::misc::lenenc_int_len(self.num_columns()) as usize);
let bitmap_len = (self.num_columns() as usize + 7) / 8;
if self.columns_before_image.is_some() {
len += S(bitmap_len);
}
if self.columns_after_image.is_some() {
len += S(bitmap_len);
}
len += S(self.rows_data.len());
min(len.0, u32::MAX as usize - BinlogEventHeader::LEN)
}
pub fn rows<'b>(&'b self, table_map_event: &'b TableMapEvent<'b>) -> RowsEventRows<'b> {
RowsEventRows {
rows_event: self,
table_map_event,
rows_data: ParseBuf(self.rows_data.as_bytes()),
}
}
pub fn into_owned(self) -> RowsEvent<'static> {
RowsEvent {
event_type: self.event_type,
table_id: self.table_id,
flags: self.flags,
extra_data: self.extra_data.into_owned(),
num_columns: self.num_columns,
columns_before_image: self.columns_before_image.map(|x| x.into_owned()),
columns_after_image: self.columns_after_image.map(|x| x.into_owned()),
rows_data: self.rows_data.into_owned(),
}
}
}
pub struct RowsEventCtx<'a> {
pub event_type: EventType,
pub binlog_ctx: BinlogCtx<'a>,
}
impl<'de> MyDeserialize<'de> for RowsEvent<'de> {
const SIZE: Option<usize> = None;
type Ctx = RowsEventCtx<'de>;
fn deserialize(ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
let post_header_len = ctx
.binlog_ctx
.fde
.get_event_type_header_length(ctx.event_type);
let is_delete_event = ctx.event_type == EventType::DELETE_ROWS_EVENT
|| ctx.event_type == EventType::DELETE_ROWS_EVENT_V1;
let is_update_event = ctx.event_type == EventType::UPDATE_ROWS_EVENT
|| ctx.event_type == EventType::UPDATE_ROWS_EVENT_V1
|| ctx.event_type == EventType::PARTIAL_UPDATE_ROWS_EVENT;
let table_id = if post_header_len == 6 {
let value = buf.parse::<RawInt<LeU32>>(())?;
RawInt::new(value.0 as u64)
} else {
buf.parse(())?
};
let flags = buf.parse(())?;
let extra_data = if post_header_len
== ctx
.binlog_ctx
.fde
.get_event_type_header_length(EventType::WRITE_ROWS_EVENT)
{
let extra_data_len = buf.checked_eat_u16_le().ok_or_else(unexpected_buf_eof)? as usize;
buf.parse(extra_data_len.saturating_sub(2))?
} else {
RawBytes::new(&[][..])
};
let num_columns: RawInt<LenEnc> = buf.parse(())?;
let bitmap_len = (num_columns.0 as usize + 7) / 8;
let mut columns_before_image = None;
let mut columns_after_image = None;
if is_update_event {
columns_before_image = Some(buf.parse(bitmap_len)?);
columns_after_image = Some(buf.parse(bitmap_len)?);
} else if is_delete_event {
columns_before_image = Some(buf.parse(bitmap_len)?);
} else {
columns_after_image = Some(buf.parse(bitmap_len)?);
}
let rows_data = buf.parse(())?;
Ok(Self {
event_type: ctx.event_type,
table_id,
flags,
extra_data,
num_columns,
columns_before_image,
columns_after_image,
rows_data,
})
}
}
impl MySerialize for RowsEvent<'_> {
fn serialize(&self, buf: &mut Vec<u8>) {
self.table_id.serialize(&mut *buf);
self.flags.serialize(&mut *buf);
if self.event_type == EventType::WRITE_ROWS_EVENT
|| self.event_type == EventType::UPDATE_ROWS_EVENT
|| self.event_type == EventType::DELETE_ROWS_EVENT
|| self.event_type == EventType::PARTIAL_UPDATE_ROWS_EVENT
{
let len = min(self.extra_data.len().saturating_add(2), u16::MAX as usize) as u16;
buf.put_u16_le(len);
self.extra_data.serialize(&mut *buf);
}
self.num_columns.serialize(&mut *buf);
if let Some(bitmap) = &self.columns_before_image {
bitmap.serialize(&mut *buf);
}
if let Some(bitmap) = &self.columns_after_image {
bitmap.serialize(&mut *buf);
}
self.rows_data.serialize(buf);
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct RowsEventRows<'a> {
rows_event: &'a RowsEvent<'a>,
table_map_event: &'a TableMapEvent<'a>,
rows_data: ParseBuf<'a>,
}
impl<'a> RowsEventRows<'a> {
pub(crate) fn new(
rows_event: &'a RowsEvent<'a>,
table_map_event: &'a TableMapEvent<'a>,
rows_data: ParseBuf<'a>,
) -> Self {
Self {
rows_event,
table_map_event,
rows_data,
}
}
}
impl<'a> Iterator for RowsEventRows<'a> {
type Item = io::Result<(Option<BinlogRow>, Option<BinlogRow>)>;
fn next(&mut self) -> Option<Self::Item> {
let mut row_before = None;
let mut row_after = None;
if self.rows_data.is_empty() {
return None;
}
if let Some(cols) = self.rows_event.columns_before_image() {
let ctx = (
self.rows_event.num_columns(),
cols,
false,
self.table_map_event,
);
row_before = match self.rows_data.parse(ctx) {
Ok(row_before) => Some(row_before),
Err(err) => return Some(Err(err)),
};
}
if let Some(cols) = self.rows_event.columns_after_image() {
let ctx = (
self.rows_event.num_columns(),
cols,
self.rows_event.event_type == EventType::PARTIAL_UPDATE_ROWS_EVENT,
self.table_map_event,
);
row_after = match self.rows_data.parse(ctx) {
Ok(row_after) => Some(row_after),
Err(err) => return Some(Err(err)),
};
}
Some(Ok((row_before, row_after)))
}
}
impl fmt::Debug for RowsEventRows<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}