-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65605e4
commit 73d3791
Showing
5 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use bytes::Bytes; | ||
use lz4_flex::{compress, decompress}; | ||
use std::io; | ||
|
||
use super::{CompressionType, Compressor, Decompressor}; | ||
|
||
/// A compressor that uses the LZ4 algorithm. | ||
#[derive(Default)] | ||
pub struct Lz4Compressor; | ||
|
||
impl Compressor for Lz4Compressor { | ||
fn compression_type(&self) -> CompressionType { | ||
CompressionType::Lz4 | ||
} | ||
|
||
fn compress(&self, data: &[u8]) -> Result<Bytes, io::Error> { | ||
let bytes = compress(data); | ||
|
||
Ok(Bytes::from(bytes)) | ||
} | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Lz4Decompressor; | ||
|
||
impl Decompressor for Lz4Decompressor { | ||
fn decompress(&self, data: &[u8]) -> Result<Bytes, io::Error> { | ||
// Usually the Lz4 compression ratio is 2.1x. So 4x should be plenty. | ||
let min_uncompressed_size = data.len() * 4; | ||
let bytes = decompress(data, min_uncompressed_size).map_err(|e| { | ||
io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
format!("Lz4 decompression failed: {}", e), | ||
) | ||
})?; | ||
|
||
Ok(Bytes::from(bytes)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters