Skip to main content

mysql_replication/protocol/mysql/events/
view.rs

1use crate::{
2    Operation, Result,
3    protocol::mysql::{file::EventView, version::EventAction},
4};
5
6/// A normalized TABLE_MAP envelope. Column metadata remains borrowed and is
7/// compiled only after the schema/table whitelist matches.
8#[derive(Debug, Clone, Copy)]
9pub struct TableMapEvent<'a> {
10    pub table_id: u64,
11    pub flags: u16,
12    pub schema: &'a str,
13    pub table: &'a str,
14    pub column_data: &'a [u8],
15    pub(crate) standard_body: Option<&'a [u8]>,
16}
17impl<'a> TableMapEvent<'a> {
18    pub fn new(
19        table_id: u64,
20        flags: u16,
21        schema: &'a str,
22        table: &'a str,
23        column_data: &'a [u8],
24    ) -> Self {
25        Self {
26            table_id,
27            flags,
28            schema,
29            table,
30            column_data,
31            standard_body: None,
32        }
33    }
34}
35
36#[derive(Debug, Clone, Copy)]
37pub struct RowImages<'a> {
38    pub column_count: usize,
39    pub before_columns: &'a [u8],
40    pub after_columns: &'a [u8],
41    pub rows: &'a [u8],
42}
43pub type RowImagesParser = for<'a> fn(Operation, &'a [u8]) -> Result<RowImages<'a>>;
44
45/// Image parsing is delayed until after table-id lookup. Excluded tables never
46/// parse extra-data, bitmaps or row values, even through an adapter.
47#[derive(Debug, Clone, Copy)]
48pub struct RowsEvent<'a> {
49    pub table_id: u64,
50    pub flags: u16,
51    pub operation: Operation,
52    encoded_images: &'a [u8],
53    parse_images: RowImagesParser,
54    pub(crate) standard_body: Option<(u8, &'a [u8])>,
55}
56impl<'a> RowsEvent<'a> {
57    pub fn new(
58        table_id: u64,
59        flags: u16,
60        operation: Operation,
61        encoded_images: &'a [u8],
62        parse_images: RowImagesParser,
63    ) -> Self {
64        Self {
65            table_id,
66            flags,
67            operation,
68            encoded_images,
69            parse_images,
70            standard_body: None,
71        }
72    }
73    pub fn images(self) -> Result<RowImages<'a>> {
74        (self.parse_images)(self.operation, self.encoded_images)
75    }
76}
77
78#[derive(Debug)]
79pub enum ParsedEvent<'a> {
80    TableMap(TableMapEvent<'a>),
81    Rows(RowsEvent<'a>),
82    Control(EventAction<'a>),
83    /// Lets existing adapters classify additional control event codes. New row
84    /// layouts must return a typed TableMap/Rows variant from their parser.
85    Unclassified(EventView<'a>),
86}
87pub trait EventParser: Send {
88    fn parse<'a>(&self, event: EventView<'a>) -> Result<ParsedEvent<'a>>;
89}