cassandra_protocol/
compression.rs

1/// CDRS support traffic compression as it is described in [Apache
2/// Cassandra protocol](
3/// https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v4.spec#L790)
4///
5/// Before being used, client and server must agree on a compression algorithm to
6/// use, which is done in the STARTUP message. As a consequence, a STARTUP message
7/// must never be compressed.  However, once the STARTUP envelope has been received
8/// by the server, messages can be compressed (including the response to the STARTUP
9/// request).
10use derive_more::Display;
11use snap::raw::{Decoder, Encoder};
12use std::convert::{From, TryInto};
13use std::error::Error;
14use std::fmt;
15use std::io;
16use std::result;
17
18type Result<T> = result::Result<T, CompressionError>;
19
20pub const LZ4: &str = "lz4";
21pub const SNAPPY: &str = "snappy";
22
23/// An error which may occur during encoding or decoding frame body. As there are only two types
24/// of compressors it contains two related enum options.
25#[derive(Debug)]
26pub enum CompressionError {
27    /// Snappy error.
28    Snappy(snap::Error),
29    /// Lz4 error.
30    Lz4(io::Error),
31}
32
33impl fmt::Display for CompressionError {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match *self {
36            CompressionError::Snappy(ref err) => write!(f, "Snappy Error: {err:?}"),
37            CompressionError::Lz4(ref err) => write!(f, "Lz4 Error: {err:?}"),
38        }
39    }
40}
41
42impl Error for CompressionError {
43    fn source(&self) -> Option<&(dyn Error + 'static)> {
44        match *self {
45            CompressionError::Snappy(ref err) => Some(err),
46            CompressionError::Lz4(ref err) => Some(err),
47        }
48    }
49}
50
51impl Clone for CompressionError {
52    fn clone(&self) -> Self {
53        match self {
54            CompressionError::Snappy(error) => CompressionError::Snappy(error.clone()),
55            CompressionError::Lz4(error) => CompressionError::Lz4(io::Error::new(
56                error.kind(),
57                error
58                    .get_ref()
59                    .map(|error| error.to_string())
60                    .unwrap_or_default(),
61            )),
62        }
63    }
64}
65
66/// Enum which represents a type of compression. Only non-startup frame's body can be compressed.
67#[derive(Debug, PartialEq, Clone, Copy, Eq, Ord, PartialOrd, Hash, Display)]
68pub enum Compression {
69    /// [lz4](https://code.google.com/p/lz4/) compression
70    Lz4,
71    /// [snappy](https://code.google.com/p/snappy/) compression
72    Snappy,
73    /// No compression
74    None,
75}
76
77impl Compression {
78    /// It encodes `bytes` basing on type of `Compression`..
79    ///
80    /// # Examples
81    ///
82    /// ```
83    ///    use cassandra_protocol::compression::Compression;
84    ///
85    ///   let snappy_compression = Compression::Snappy;
86    ///   let bytes = String::from("Hello World").into_bytes().to_vec();
87    ///   let encoded = snappy_compression.encode(&bytes).unwrap();
88    ///   assert_eq!(snappy_compression.decode(encoded).unwrap(), bytes);
89    ///
90    /// ```
91    pub fn encode(&self, bytes: &[u8]) -> Result<Vec<u8>> {
92        match *self {
93            Compression::Lz4 => Compression::encode_lz4(bytes),
94            Compression::Snappy => Compression::encode_snappy(bytes),
95            Compression::None => Ok(bytes.into()),
96        }
97    }
98
99    /// Checks if current compression actually compresses data.
100    #[inline]
101    pub fn is_compressed(self) -> bool {
102        self != Compression::None
103    }
104
105    /// It decodes `bytes` basing on type of compression.
106    pub fn decode(&self, bytes: Vec<u8>) -> Result<Vec<u8>> {
107        match *self {
108            Compression::Lz4 => Compression::decode_lz4(bytes),
109            Compression::Snappy => Compression::decode_snappy(bytes),
110            Compression::None => Ok(bytes),
111        }
112    }
113
114    /// It transforms compression method into a `&str`.
115    pub fn as_str(&self) -> Option<&'static str> {
116        match *self {
117            Compression::Lz4 => Some(LZ4),
118            Compression::Snappy => Some(SNAPPY),
119            Compression::None => None,
120        }
121    }
122
123    fn encode_snappy(bytes: &[u8]) -> Result<Vec<u8>> {
124        let mut encoder = Encoder::new();
125        encoder
126            .compress_vec(bytes)
127            .map_err(CompressionError::Snappy)
128    }
129
130    fn decode_snappy(bytes: Vec<u8>) -> Result<Vec<u8>> {
131        let mut decoder = Decoder::new();
132        decoder
133            .decompress_vec(bytes.as_slice())
134            .map_err(CompressionError::Snappy)
135    }
136
137    fn encode_lz4(bytes: &[u8]) -> Result<Vec<u8>> {
138        let len = 4 + lz4_flex::block::get_maximum_output_size(bytes.len());
139        assert!(len <= i32::MAX as usize);
140
141        let mut result = vec![0; len];
142
143        let len = bytes.len() as i32;
144        result[..4].copy_from_slice(&len.to_be_bytes());
145
146        let compressed_len = lz4_flex::compress_into(bytes, &mut result[4..])
147            .map_err(|error| CompressionError::Lz4(io::Error::other(error)))?;
148
149        result.truncate(4 + compressed_len);
150        Ok(result)
151    }
152
153    fn decode_lz4(bytes: Vec<u8>) -> Result<Vec<u8>> {
154        let uncompressed_size = i32::from_be_bytes(
155            bytes[..4]
156                .try_into()
157                .map_err(|error| CompressionError::Lz4(io::Error::other(error)))?,
158        );
159
160        lz4_flex::decompress(&bytes[4..], uncompressed_size as usize)
161            .map_err(|error| CompressionError::Lz4(io::Error::other(error)))
162    }
163}
164
165impl From<String> for Compression {
166    /// It converts `String` into `Compression`. If string is neither `lz4` nor `snappy` then
167    /// `Compression::None` will be returned
168    fn from(compression_string: String) -> Compression {
169        Compression::from(compression_string.as_str())
170    }
171}
172
173impl Compression {
174    /// It converts `Compression` into `String`. If compression is `None` then empty string will be
175    /// returned
176    pub fn to_protocol_string(self) -> String {
177        match self {
178            Compression::Lz4 => "LZ4".to_string(),
179            Compression::Snappy => "SNAPPY".to_string(),
180            Compression::None => "NONE".to_string(),
181        }
182    }
183
184    pub fn from_protocol_string(protocol_string: &str) -> std::result::Result<Self, String> {
185        match protocol_string {
186            "lz4" | "LZ4" => Ok(Compression::Lz4),
187            "snappy" | "SNAPPY" => Ok(Compression::Snappy),
188            "none" | "NONE" => Ok(Compression::None),
189            _ => Err("Unknown compression".to_string()),
190        }
191    }
192}
193
194impl<'a> From<&'a str> for Compression {
195    /// It converts `str` into `Compression`. If string is neither `lz4` nor `snappy` then
196    /// `Compression::None` will be returned
197    fn from(compression_str: &'a str) -> Compression {
198        match compression_str {
199            LZ4 => Compression::Lz4,
200            SNAPPY => Compression::Snappy,
201            _ => Compression::None,
202        }
203    }
204}
205
206#[cfg(test)]
207mod tests {
208    use super::*;
209
210    #[test]
211    fn test_compression_to_protocol_string() {
212        let lz4 = Compression::Lz4;
213        assert_eq!("LZ4", lz4.to_protocol_string());
214
215        let snappy = Compression::Snappy;
216        assert_eq!("SNAPPY", snappy.to_protocol_string());
217
218        let none = Compression::None;
219        assert_eq!("NONE", none.to_protocol_string());
220    }
221
222    #[test]
223    fn test_compression_from_protocol_str() {
224        let lz4 = "lz4";
225        assert_eq!(
226            Compression::from_protocol_string(lz4).unwrap(),
227            Compression::Lz4
228        );
229
230        let lz4 = "LZ4";
231        assert_eq!(
232            Compression::from_protocol_string(lz4).unwrap(),
233            Compression::Lz4
234        );
235
236        let snappy = "snappy";
237        assert_eq!(
238            Compression::from_protocol_string(snappy).unwrap(),
239            Compression::Snappy
240        );
241
242        let snappy = "SNAPPY";
243        assert_eq!(
244            Compression::from_protocol_string(snappy).unwrap(),
245            Compression::Snappy
246        );
247
248        let none = "none";
249        assert_eq!(
250            Compression::from_protocol_string(none).unwrap(),
251            Compression::None
252        );
253
254        let none = "NONE";
255        assert_eq!(
256            Compression::from_protocol_string(none).unwrap(),
257            Compression::None
258        );
259    }
260
261    #[test]
262    fn test_compression_from_string() {
263        let lz4 = "lz4".to_string();
264        assert_eq!(Compression::from(lz4), Compression::Lz4);
265        let snappy = "snappy".to_string();
266        assert_eq!(Compression::from(snappy), Compression::Snappy);
267        let none = "x".to_string();
268        assert_eq!(Compression::from(none), Compression::None);
269    }
270
271    #[test]
272    fn test_compression_encode_snappy() {
273        let snappy_compression = Compression::Snappy;
274        let bytes = String::from("Hello World").into_bytes().to_vec();
275        snappy_compression
276            .encode(&bytes)
277            .expect("Should work without exceptions");
278    }
279
280    #[test]
281    fn test_compression_decode_snappy() {
282        let snappy_compression = Compression::Snappy;
283        let bytes = String::from("Hello World").into_bytes().to_vec();
284        let encoded = snappy_compression.encode(&bytes).unwrap();
285        assert_eq!(snappy_compression.decode(encoded).unwrap(), bytes);
286    }
287
288    #[test]
289    fn test_compression_encode_lz4() {
290        let snappy_compression = Compression::Lz4;
291        let bytes = String::from("Hello World").into_bytes().to_vec();
292        snappy_compression
293            .encode(&bytes)
294            .expect("Should work without exceptions");
295    }
296
297    #[test]
298    fn test_compression_decode_lz4() {
299        let lz4_compression = Compression::Lz4;
300        let bytes = String::from("Hello World").into_bytes().to_vec();
301        let encoded = lz4_compression.encode(&bytes).unwrap();
302        assert_eq!(lz4_compression.decode(encoded).unwrap(), bytes);
303    }
304
305    #[test]
306    fn test_compression_encode_none() {
307        let none_compression = Compression::None;
308        let bytes = String::from("Hello World").into_bytes().to_vec();
309        none_compression
310            .encode(&bytes)
311            .expect("Should work without exceptions");
312    }
313
314    #[test]
315    fn test_compression_decode_none() {
316        let none_compression = Compression::None;
317        let bytes = String::from("Hello World").into_bytes().to_vec();
318        let encoded = none_compression.encode(&bytes).unwrap();
319        assert_eq!(none_compression.decode(encoded).unwrap(), bytes);
320    }
321
322    #[test]
323    fn test_compression_decode_lz4_with_invalid_input() {
324        let lz4_compression = Compression::Lz4;
325        let decode = lz4_compression.decode(vec![0, 0, 0, 0x7f]);
326        assert!(decode.is_err());
327    }
328
329    #[test]
330    fn test_compression_encode_snappy_with_non_utf8() {
331        let snappy_compression = Compression::Snappy;
332        let v = vec![0xff, 0xff];
333        let encoded = snappy_compression
334            .encode(&v)
335            .expect("Should work without exceptions");
336        assert_eq!(snappy_compression.decode(encoded).unwrap(), v);
337    }
338}