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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (c) 2023 Anatoly Ikorsky
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::{
    borrow::Cow,
    cmp::min,
    convert::TryFrom,
    fmt,
    io::{self, BufRead, BufReader},
};

use saturating::Saturating as S;

#[allow(unused)]
use crate::binlog::EventStreamReader;

use super::BinlogEventHeader;
use crate::{
    binlog::{
        consts::{
            BinlogVersion, EventType, TransactionPayloadCompressionType, TransactionPayloadFields,
        },
        BinlogCtx, BinlogEvent, BinlogStruct,
    },
    io::{BufMutExt, ParseBuf, ReadMysqlExt},
    misc::raw::{bytes::EofBytes, int::*, RawBytes},
    proto::{MyDeserialize, MySerialize},
};

/// This structure implements [`io::BufRead`] and represents
/// the payload of a [`TransactionPayloadEvent`].
#[derive(Debug)]
pub struct TransactionPayloadReader<'a> {
    inner: TransactionPayloadInner<'a>,
}

impl<'a> TransactionPayloadReader<'a> {
    /// Creates new instance (`data` is not compressed).
    pub fn new_uncompressed(data: &'a [u8]) -> Self {
        Self {
            inner: TransactionPayloadInner::Uncompressed(data),
        }
    }

    /// Creates new instance (`data` is ZSTD-compressed).
    pub fn new_zstd(data: &'a [u8]) -> io::Result<Self> {
        let decoder = zstd::Decoder::with_buffer(data)?;
        Ok(Self {
            inner: TransactionPayloadInner::ZstdCompressed(BufReader::new(decoder)),
        })
    }

    /// Returns `false` if the reader is exhausted.
    ///
    /// Some io might be necessary to check for data,
    /// so this functions returns `Result<bool>`, not `bool`.
    pub fn has_data_left(&mut self) -> io::Result<bool> {
        self.inner.has_data_left()
    }
}

impl io::Read for TransactionPayloadReader<'_> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl io::BufRead for TransactionPayloadReader<'_> {
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        self.inner.fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        self.inner.consume(amt)
    }
}

/// Wraps the data providing [`io::BufRead`] implementaion.
enum TransactionPayloadInner<'a> {
    Uncompressed(&'a [u8]),
    ZstdCompressed(BufReader<zstd::Decoder<'a, &'a [u8]>>),
}

impl TransactionPayloadInner<'_> {
    fn has_data_left(&mut self) -> io::Result<bool> {
        match self {
            TransactionPayloadInner::Uncompressed(x) => Ok(!x.is_empty()),
            TransactionPayloadInner::ZstdCompressed(ref mut x) => {
                x.fill_buf().map(|b| !b.is_empty())
            }
        }
    }
}

impl io::BufRead for TransactionPayloadInner<'_> {
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        match self {
            TransactionPayloadInner::Uncompressed(ref mut x) => x.fill_buf(),
            TransactionPayloadInner::ZstdCompressed(ref mut x) => x.fill_buf(),
        }
    }

    fn consume(&mut self, amt: usize) {
        match self {
            TransactionPayloadInner::Uncompressed(ref mut x) => x.consume(amt),
            TransactionPayloadInner::ZstdCompressed(ref mut x) => x.consume(amt),
        }
    }
}

impl io::Read for TransactionPayloadInner<'_> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self {
            TransactionPayloadInner::Uncompressed(ref mut x) => x.read(buf),
            TransactionPayloadInner::ZstdCompressed(ref mut x) => x.read(buf),
        }
    }
}

impl fmt::Debug for TransactionPayloadInner<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Uncompressed(arg0) => f.debug_tuple("Uncompressed").field(arg0).finish(),
            Self::ZstdCompressed(_) => f.debug_tuple("ZstdCompressed").field(&"..").finish(),
        }
    }
}

/// Event that encloses all the events of a transaction.
///
/// It is used for carrying compressed payloads, and contains
/// compression metadata.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct TransactionPayloadEvent<'a> {
    /// payload size
    payload_size: RawInt<LeU64>,

    /// compression algorithm
    algorithm: TransactionPayloadCompressionType,

    /// uncompressed size
    uncompressed_size: RawInt<LeU64>,

    /// payload to be decompressed
    payload: RawBytes<'a, EofBytes>,

    /// size of parsed header
    header_size: usize,
}

impl<'a> TransactionPayloadEvent<'a> {
    pub fn new(
        payload_size: u64,
        algorithm: TransactionPayloadCompressionType,
        uncompressed_size: u64,
        payload: impl Into<Cow<'a, [u8]>>,
    ) -> Self {
        Self {
            payload_size: RawInt::new(payload_size),
            algorithm,
            uncompressed_size: RawInt::new(uncompressed_size),
            payload: RawBytes::new(payload),
            header_size: 0,
        }
    }

    /// Sets the `payload_size` field value.
    pub fn with_payload_size(mut self, payload_size: u64) -> Self {
        self.payload_size = RawInt::new(payload_size);
        self
    }
    /// Sets the `algorithm` field value.
    pub fn with_algorithm(mut self, algorithm: TransactionPayloadCompressionType) -> Self {
        self.algorithm = algorithm;
        self
    }
    /// Sets the `uncompressed_size` field value.
    pub fn with_uncompressed_size(mut self, uncompressed_size: u64) -> Self {
        self.uncompressed_size = RawInt::new(uncompressed_size);
        self
    }

    /// Sets the `payload` field value.
    pub fn with_payload(mut self, payload: impl Into<Cow<'a, [u8]>>) -> Self {
        self.payload = RawBytes::new(payload);
        self
    }

    /// Returns the payload_size.
    pub fn payload_size(&self) -> u64 {
        self.payload_size.0
    }

    /// Returns raw payload of the binlog event.
    pub fn payload_raw(&'a self) -> &'a [u8] {
        self.payload.as_bytes()
    }

    /// Returns decompressed payload in form of a struct that implements [`io::BufRead`].
    ///
    /// See [`EventStreamReader::read_decompressed`].
    pub fn decompressed(&self) -> io::Result<TransactionPayloadReader<'_>> {
        if self.algorithm == TransactionPayloadCompressionType::NONE {
            return Ok(TransactionPayloadReader::new_uncompressed(
                self.payload_raw(),
            ));
        }

        return TransactionPayloadReader::new_zstd(self.payload_raw());
    }

    /// Decompress the whole payload.
    ///
    /// # Danger
    ///
    /// This function may allocate a huge buffer and cause OOM.
    /// Consider using [`TransactionPayloadEvent::decompressed`] instead.
    pub fn danger_decompress(self) -> Vec<u8> {
        if self.algorithm == TransactionPayloadCompressionType::NONE {
            return self.payload_raw().to_vec();
        }
        let mut decode_buf = vec![0_u8; self.uncompressed_size.0 as usize];
        match zstd::stream::copy_decode(self.payload.as_bytes(), &mut decode_buf[..]) {
            Ok(_) => {}
            Err(_) => {
                return Vec::new();
            }
        };
        decode_buf
    }

    /// Returns the algorithm.
    pub fn algorithm(&self) -> TransactionPayloadCompressionType {
        self.algorithm
    }

    /// Returns the uncompressed_size.
    pub fn uncompressed_size(&self) -> u64 {
        self.uncompressed_size.0
    }

    pub fn into_owned(self) -> TransactionPayloadEvent<'static> {
        TransactionPayloadEvent {
            payload_size: self.payload_size,
            algorithm: self.algorithm,
            uncompressed_size: self.uncompressed_size,
            payload: self.payload.into_owned(),
            header_size: self.header_size,
        }
    }
}

impl<'de> MyDeserialize<'de> for TransactionPayloadEvent<'de> {
    const SIZE: Option<usize> = None;
    type Ctx = BinlogCtx<'de>;
    fn deserialize(_ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self> {
        let mut ob = Self {
            payload_size: RawInt::new(0),
            algorithm: TransactionPayloadCompressionType::NONE,
            uncompressed_size: RawInt::new(0),
            payload: RawBytes::from("".as_bytes()),
            header_size: 0,
        };
        let mut have_payload_size = false;
        let mut have_compression_type = false;
        let original_buf_size = buf.len();
        while !buf.is_empty() {
            /* read the type of the field. */
            let field_type = buf.read_lenenc_int()?;
            match TransactionPayloadFields::try_from(field_type) {
                // we have reached the end of the header
                Ok(TransactionPayloadFields::OTW_PAYLOAD_HEADER_END_MARK) => {
                    if !have_payload_size || !have_compression_type {
                        Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            "Missing field in payload header",
                        ))?;
                    }
                    if ob.payload_size.0 as usize > buf.len() {
                        Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            format!(
                                "Payload size is bigger than the remaining buffer: {} > {}",
                                ob.payload_size.0,
                                buf.len()
                            ),
                        ))?;
                    }
                    ob.header_size = original_buf_size - ob.payload_size.0 as usize;
                    let mut payload_buf: ParseBuf = buf.parse(ob.payload_size.0 as usize)?;
                    ob.payload = RawBytes::from(payload_buf.eat_all());
                    break;
                }

                Ok(TransactionPayloadFields::OTW_PAYLOAD_SIZE_FIELD) => {
                    let _length = buf.read_lenenc_int()?;
                    let val = buf.read_lenenc_int()?;
                    ob.payload_size = RawInt::new(val);
                    have_payload_size = true;
                    continue;
                }
                Ok(TransactionPayloadFields::OTW_PAYLOAD_COMPRESSION_TYPE_FIELD) => {
                    let _length = buf.read_lenenc_int()?;
                    let val = buf.read_lenenc_int()?;
                    ob.algorithm = TransactionPayloadCompressionType::try_from(val).unwrap();
                    have_compression_type = true;
                    continue;
                }
                Ok(TransactionPayloadFields::OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD) => {
                    let _length = buf.read_lenenc_int()?;
                    let val = buf.read_lenenc_int()?;
                    ob.uncompressed_size = RawInt::new(val);
                    continue;
                }
                Err(_) => {
                    let length = buf.eat_lenenc_int();
                    buf.skip(length as usize);
                    continue;
                }
            };
        }

        Ok(ob)
    }
}

impl MySerialize for TransactionPayloadEvent<'_> {
    fn serialize(&self, buf: &mut Vec<u8>) {
        buf.put_lenenc_int(TransactionPayloadFields::OTW_PAYLOAD_COMPRESSION_TYPE_FIELD as u64);
        buf.put_lenenc_int(crate::misc::lenenc_int_len(self.algorithm as u64));
        buf.put_lenenc_int(self.algorithm as u64);

        if self.algorithm != TransactionPayloadCompressionType::NONE {
            buf.put_lenenc_int(
                TransactionPayloadFields::OTW_PAYLOAD_UNCOMPRESSED_SIZE_FIELD as u64,
            );
            buf.put_lenenc_int(crate::misc::lenenc_int_len(self.uncompressed_size.0));
            buf.put_lenenc_int(self.uncompressed_size.0);
        }

        buf.put_lenenc_int(TransactionPayloadFields::OTW_PAYLOAD_SIZE_FIELD as u64);
        buf.put_lenenc_int(crate::misc::lenenc_int_len(self.payload_size.0));
        buf.put_lenenc_int(self.payload_size.0);

        buf.put_lenenc_int(TransactionPayloadFields::OTW_PAYLOAD_HEADER_END_MARK as u64);

        self.payload.serialize(&mut *buf);
    }
}

impl<'a> BinlogEvent<'a> for TransactionPayloadEvent<'a> {
    const EVENT_TYPE: EventType = EventType::TRANSACTION_PAYLOAD_EVENT;
}

impl<'a> BinlogStruct<'a> for TransactionPayloadEvent<'a> {
    fn len(&self, _version: BinlogVersion) -> usize {
        let mut len = S(self.header_size);

        len += S(self.payload.0.len());

        min(len.0, u32::MAX as usize - BinlogEventHeader::LEN)
    }
}