mysql_common/binlog/events/
begin_load_query_event.rs1use std::{
10 borrow::Cow,
11 cmp::min,
12 io::{self},
13};
14
15use saturating::Saturating as S;
16
17use crate::{
18 binlog::{
19 consts::{BinlogVersion, EventType},
20 BinlogCtx, BinlogEvent, BinlogStruct,
21 },
22 io::ParseBuf,
23 misc::raw::{bytes::EofBytes, int::LeU32, RawBytes, RawInt},
24 proto::{MyDeserialize, MySerialize},
25};
26
27use super::BinlogEventHeader;
28
29#[derive(Debug, Clone, Eq, PartialEq, Hash)]
33pub struct BeginLoadQueryEvent<'a> {
34 file_id: RawInt<LeU32>,
35 block_data: RawBytes<'a, EofBytes>,
36}
37
38impl<'a> BeginLoadQueryEvent<'a> {
39 pub fn new(file_id: u32) -> Self {
40 Self {
41 file_id: RawInt::new(file_id),
42 block_data: Default::default(),
43 }
44 }
45
46 pub fn with_file_id(mut self, file_id: u32) -> Self {
48 self.file_id.0 = file_id;
49 self
50 }
51
52 pub fn with_block_data(mut self, block_data: impl Into<Cow<'a, [u8]>>) -> Self {
54 self.block_data = RawBytes::new(block_data);
55 self
56 }
57
58 pub fn file_id(&self) -> u32 {
62 self.file_id.0
63 }
64
65 pub fn block_data(&'a self) -> &'a [u8] {
69 self.block_data.as_bytes()
70 }
71
72 pub fn into_owned(self) -> BeginLoadQueryEvent<'static> {
73 BeginLoadQueryEvent {
74 file_id: self.file_id,
75 block_data: self.block_data.into_owned(),
76 }
77 }
78}
79
80impl<'de> MyDeserialize<'de> for BeginLoadQueryEvent<'de> {
81 const SIZE: Option<usize> = None;
82 type Ctx = BinlogCtx<'de>;
83
84 fn deserialize(_ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
85 Ok(Self {
86 file_id: buf.parse(())?,
87 block_data: buf.parse(())?,
88 })
89 }
90}
91
92impl MySerialize for BeginLoadQueryEvent<'_> {
93 fn serialize(&self, buf: &mut Vec<u8>) {
94 self.file_id.serialize(&mut *buf);
95 self.block_data.serialize(&mut *buf);
96 }
97}
98
99impl<'a> BinlogStruct<'a> for BeginLoadQueryEvent<'a> {
100 fn len(&self, _version: BinlogVersion) -> usize {
102 let mut len = S(0);
103
104 len += S(4);
105 len += S(self.block_data.len());
106
107 min(len.0, u32::MAX as usize - BinlogEventHeader::LEN)
108 }
109}
110
111impl<'a> BinlogEvent<'a> for BeginLoadQueryEvent<'a> {
112 const EVENT_TYPE: EventType = EventType::BEGIN_LOAD_QUERY_EVENT;
113}