mysql_common/binlog/events/
rand_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 crate::{
12    binlog::{
13        consts::{BinlogVersion, EventType},
14        BinlogCtx, BinlogEvent, BinlogStruct,
15    },
16    io::ParseBuf,
17    misc::raw::{int::LeU64, RawInt},
18    proto::{MyDeserialize, MySerialize},
19};
20
21/// Rand event.
22///
23/// Logs random seed used by the next `RAND()`, and by `PASSWORD()` in 4.1.0. 4.1.1 does not need
24/// it (it's repeatable again) so this event needn't be written in 4.1.1 for `PASSWORD()`
25/// (but the fact that it is written is just a waste, it does not cause bugs).
26///
27/// The state of the random number generation consists of 128 bits, which are stored internally
28/// as two 64-bit numbers.
29#[derive(Debug, Clone, Eq, PartialEq, Hash)]
30pub struct RandEvent {
31    pub seed1: RawInt<LeU64>,
32    pub seed2: RawInt<LeU64>,
33}
34
35impl<'de> MyDeserialize<'de> for RandEvent {
36    const SIZE: Option<usize> = Some(16);
37    type Ctx = BinlogCtx<'de>;
38
39    fn deserialize(_: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
40        Ok(Self {
41            seed1: buf.parse_unchecked(())?,
42            seed2: buf.parse_unchecked(())?,
43        })
44    }
45}
46
47impl MySerialize for RandEvent {
48    fn serialize(&self, buf: &mut Vec<u8>) {
49        self.seed1.serialize(&mut *buf);
50        self.seed2.serialize(&mut *buf);
51    }
52}
53
54impl<'a> BinlogEvent<'a> for RandEvent {
55    const EVENT_TYPE: EventType = EventType::RAND_EVENT;
56}
57
58impl<'a> BinlogStruct<'a> for RandEvent {
59    fn len(&self, _version: BinlogVersion) -> usize {
60        8
61    }
62}