Skip to main content

mysql_replication/events/
mode.rs

1//! Compile-time policies for protocol event envelopes.
2use super::{CompleteImage, RowImage, RowImages, RowsEvent, SparseImage};
3use crate::config::RowImageMode;
4use crate::{Error, Result};
5use std::fmt::Debug;
6
7mod sealed {
8    pub trait Sealed {}
9}
10
11/// Sparse images preserve exactly the columns present in the log.
12#[derive(Debug, Clone, Copy)]
13pub struct Minimal;
14/// Every existing image contains every table column.
15#[derive(Debug, Clone, Copy)]
16pub struct Full;
17
18/// A sealed policy carried by the parser, event envelope and ROWS payload.
19/// Common control events retain the same representation for every policy.
20pub trait BinlogMode: sealed::Sealed + Debug + Clone + Copy + Send + Sync + 'static {
21    type Image<'a>: TryFrom<RowImage<'a>, Error = Error>
22        + AsRef<RowImage<'a>>
23        + Into<RowImage<'a>>
24        + Debug
25        + Clone;
26    const ROW_IMAGE: RowImageMode;
27    #[doc(hidden)]
28    fn validate_event(event: &RowsEvent<'_, Self>) -> Result<()>;
29    #[doc(hidden)]
30    fn validate_images(images: &RowImages<'_>) -> Result<()>;
31}
32
33impl sealed::Sealed for Minimal {}
34impl BinlogMode for Minimal {
35    type Image<'a> = SparseImage<'a>;
36    const ROW_IMAGE: RowImageMode = RowImageMode::Minimal;
37    fn validate_event(_: &RowsEvent<'_, Self>) -> Result<()> {
38        Ok(())
39    }
40    fn validate_images(_: &RowImages<'_>) -> Result<()> {
41        Ok(())
42    }
43}
44
45impl sealed::Sealed for Full {}
46impl BinlogMode for Full {
47    type Image<'a> = CompleteImage<'a>;
48    const ROW_IMAGE: RowImageMode = RowImageMode::Full;
49    fn validate_event(event: &RowsEvent<'_, Self>) -> Result<()> {
50        // This sentinel ends a statement and carries no row images.
51        if event.is_statement_end() {
52            return Ok(());
53        }
54        event.images().map(|_| ())
55    }
56    fn validate_images(images: &RowImages<'_>) -> Result<()> {
57        let complete = |bitmap: &[u8]| {
58            (0..images.column_count).all(|index| {
59                bitmap
60                    .get(index / 8)
61                    .is_some_and(|byte| byte & (1 << (index % 8)) != 0)
62            })
63        };
64        if !complete(images.before_columns) || !complete(images.after_columns) {
65            return Err(Error::Unsupported(
66                "FULL row images must contain every table column".into(),
67            ));
68        }
69        Ok(())
70    }
71}