mysql_common/binlog/events/
xid_event.rs

1// Copyright (c) 2021 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::io::{self};
10
11use bytes::BufMut;
12
13use crate::{
14    binlog::{
15        consts::{BinlogVersion, EventType},
16        BinlogCtx, BinlogEvent, BinlogStruct,
17    },
18    io::ParseBuf,
19    misc::unexpected_buf_eof,
20    proto::{MyDeserialize, MySerialize},
21};
22
23/// Xid event.
24///
25/// Generated for a commit of a transaction that modifies one or more tables of an XA-capable
26/// storage engine.
27#[derive(Debug, Clone, Eq, PartialEq, Hash)]
28pub struct XidEvent {
29    pub xid: u64,
30}
31
32impl<'de> MyDeserialize<'de> for XidEvent {
33    const SIZE: Option<usize> = None;
34    type Ctx = BinlogCtx<'de>;
35
36    fn deserialize(ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
37        let post_header_len = ctx.fde.get_event_type_header_length(Self::EVENT_TYPE);
38
39        if !buf.checked_skip(post_header_len as usize) {
40            return Err(unexpected_buf_eof());
41        }
42
43        let xid = buf.checked_eat_u64_le().ok_or_else(unexpected_buf_eof)?;
44
45        Ok(Self { xid })
46    }
47}
48
49impl MySerialize for XidEvent {
50    fn serialize(&self, buf: &mut Vec<u8>) {
51        buf.put_u64_le(self.xid);
52    }
53}
54
55impl<'a> BinlogEvent<'a> for XidEvent {
56    const EVENT_TYPE: EventType = EventType::XID_EVENT;
57}
58
59impl<'a> BinlogStruct<'a> for XidEvent {
60    fn len(&self, _version: BinlogVersion) -> usize {
61        8
62    }
63}