Skip to main content

mysql_replication/source/
mod.rs

1//! Source plugin interfaces and registry; concrete transports live in submodules.
2pub mod mysql;
3
4use crate::{
5    Error, Result, TableMeta,
6    config::Config,
7    protocol::mysql::{
8        file::EventView,
9        version::{ServerVersion, VersionRegistry},
10    },
11};
12use std::sync::Arc;
13
14use crate::engine::checkpoint::State;
15pub use mysql::MysqlSource;
16
17#[derive(Debug, Clone)]
18pub struct ServerInfo {
19    pub uuid: String,
20    pub version: ServerVersion,
21    pub adapter: String,
22}
23pub trait EventStream: Send {
24    /// Borrowed until the next call; no event-body copy at the trait boundary.
25    fn next_event(&mut self) -> Result<Option<EventView<'_>>>;
26}
27pub trait SourceSession: Send {
28    fn info(&self) -> &ServerInfo;
29    fn load_tables(&mut self, config: &Config) -> Result<Vec<TableMeta>>;
30    /// Capture a checkpoint with this ID, the configured plugin and the server UUID.
31    fn initialize(&mut self, source_id: &str, config: &Config) -> Result<State>;
32    fn start(
33        self: Box<Self>,
34        config: &Config,
35        state: &State,
36        once: bool,
37    ) -> Result<Box<dyn EventStream>>;
38}
39pub trait SourcePlugin: Send + Sync {
40    fn name(&self) -> &str;
41    fn connect(
42        &self,
43        config: &Config,
44        versions: &VersionRegistry,
45    ) -> Result<Box<dyn SourceSession>>;
46}
47#[derive(Clone)]
48pub struct SourceRegistry {
49    plugins: Vec<Arc<dyn SourcePlugin>>,
50}
51impl SourceRegistry {
52    pub fn empty() -> Self {
53        Self {
54            plugins: Vec::new(),
55        }
56    }
57    pub fn register(&mut self, plugin: Arc<dyn SourcePlugin>) -> Result<()> {
58        if plugin.name().is_empty() || self.plugins.iter().any(|p| p.name() == plugin.name()) {
59            return Err(Error::Schema("empty/duplicate source plugin name".into()));
60        }
61        self.plugins.push(plugin);
62        Ok(())
63    }
64    pub fn resolve(&self, name: &str) -> Result<Arc<dyn SourcePlugin>> {
65        self.plugins
66            .iter()
67            .find(|p| p.name() == name)
68            .cloned()
69            .ok_or_else(|| Error::Unsupported(format!("source plugin {name}")))
70    }
71}
72impl Default for SourceRegistry {
73    fn default() -> Self {
74        Self {
75            plugins: vec![Arc::new(MysqlSource)],
76        }
77    }
78}