mysql_binlog_connector_rust/
binlog_client.rs

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
use std::collections::HashMap;

use crate::{
    binlog_error::BinlogError,
    binlog_parser::BinlogParser,
    binlog_stream::BinlogStream,
    command::{authenticator::Authenticator, command_util::CommandUtil},
};

#[derive(Default)]
pub struct BinlogClient {
    pub url: String,
    pub binlog_filename: String,
    pub binlog_position: u32,
    pub server_id: u64,
    pub gtid_enabled: bool,
    pub gtid_set: String,
}

const MIN_BINLOG_POSITION: u32 = 4;

impl BinlogClient {
    pub async fn connect(&mut self) -> Result<BinlogStream, BinlogError> {
        // init connect
        let mut authenticator = Authenticator::new(&self.url)?;
        let mut channel = authenticator.connect().await?;

        if self.gtid_enabled {
            if self.gtid_set.is_empty() {
                let (_, _, gtid_set) = CommandUtil::fetch_binlog_info(&mut channel).await?;
                self.gtid_set = gtid_set;
            }
        } else {
            // fetch binlog info
            if self.binlog_filename.is_empty() {
                let (binlog_filename, binlog_position, _) =
                    CommandUtil::fetch_binlog_info(&mut channel).await?;
                self.binlog_filename = binlog_filename;
                self.binlog_position = binlog_position;
            }

            if self.binlog_position < MIN_BINLOG_POSITION {
                self.binlog_position = MIN_BINLOG_POSITION;
            }
        }

        // fetch binlog checksum
        let binlog_checksum = CommandUtil::fetch_binlog_checksum(&mut channel).await?;

        // setup connection
        CommandUtil::setup_binlog_connection(&mut channel).await?;

        // dump binlog
        CommandUtil::dump_binlog(&mut channel, self).await?;

        // list for binlog
        let parser = BinlogParser {
            checksum_length: binlog_checksum.get_length(),
            table_map_event_by_table_id: HashMap::new(),
        };

        Ok(BinlogStream { channel, parser })
    }
}