Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more compression algos #7

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion bincache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bincache"
version = "0.3.3"
version = "0.3.4"
edition = "2021"
description = "ZitaneLabs binary cache."
license = "MIT"
Expand All @@ -12,6 +12,8 @@ blocking = ["dep:futures-util", "async-compression/futures-io"]
rt_tokio_1 = ["dep:tokio", "async-compression/tokio"]
rt_async-std_1 = ["dep:async-std", "async-compression/futures-io"]
comp_zstd = ["async-compression/zstd"]
comp_brotli = ["async-compression/brotli"]
comp_gzip = ["async-compression/gzip"]

[dependencies]
paste = "1"
Expand Down
13 changes: 10 additions & 3 deletions bincache/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ pub use compression_level::CompressionLevel;
/// A no-op compression strategy.
pub const NO_COMPRESSION: Option<crate::noop::Noop> = None;

// zstd

#[cfg(feature = "comp_zstd")]
mod zstd_compressor;

#[cfg(feature = "comp_zstd")]
pub use zstd_compressor::Zstd;

#[cfg(feature = "comp_brotli")]
mod brotli_compressor;
#[cfg(feature = "comp_brotli")]
pub use brotli_compressor::Brotli;

#[cfg(feature = "comp_gzip")]
mod gzip_compressor;
#[cfg(feature = "comp_gzip")]
pub use gzip_compressor::Gzip;
114 changes: 114 additions & 0 deletions bincache/src/compression/brotli_compressor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use super::compression_level::CompressionLevel;
use crate::traits::CompressionStrategy;
use crate::Result;
use async_trait::async_trait;
use std::borrow::Cow;

#[derive(Debug)]
pub struct Brotli {
level: CompressionLevel,
}

impl Brotli {
/// Creates a new Brotli Compressor with the given compression level
pub fn new(level: CompressionLevel) -> Self {
Self { level }
}
}

impl Default for Brotli {
/// Creates a new Brotli Compressor with the default compression level
fn default() -> Self {
Self {
level: CompressionLevel::Default,
}
}
}

#[async_trait]
impl CompressionStrategy for Brotli {
async fn compress<'a>(&self, data: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>> {
#[cfg(feature = "rt_tokio_1")]
{
use async_compression::tokio::write;
use tokio::io::AsyncWriteExt;
let mut encoder = write::BrotliEncoder::with_quality(
Vec::with_capacity(data.len()),
self.level.into(),
);
encoder.write_all(data.as_ref()).await?;
encoder.shutdown().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(any(feature = "blocking", feature = "implicit-blocking"))]
{
use async_compression::futures::write;
use futures_util::AsyncWriteExt;
let mut encoder = write::BrotliEncoder::with_quality(
Vec::with_capacity(data.len()),
self.level.into(),
);
encoder.write_all(data.as_ref()).await?;
encoder.close().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(feature = "rt_async-std_1")]
{
use async_compression::futures::write;
use async_std::io::WriteExt;
let mut encoder = write::BrotliEncoder::with_quality(
Vec::with_capacity(data.len()),
self.level.into(),
);
encoder.write_all(data.as_ref()).await?;
encoder.flush().await?;
return Ok(encoder.into_inner().into());
}
}

async fn decompress<'a>(&self, data: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>> {
#[cfg(feature = "rt_tokio_1")]
{
use async_compression::tokio::write;
use tokio::io::AsyncWriteExt;
let mut encoder = write::BrotliDecoder::new(Vec::with_capacity(data.len()));
encoder.write_all(data.as_ref()).await?;
encoder.shutdown().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(any(feature = "blocking", feature = "implicit-blocking"))]
{
use async_compression::futures::write;
use futures_util::AsyncWriteExt;
let mut encoder = write::BrotliDecoder::new(Vec::with_capacity(data.len()));
encoder.write_all(data.as_ref()).await?;
encoder.close().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(feature = "rt_async-std_1")]
{
use async_compression::futures::write;
use async_std::io::WriteExt;
let mut encoder = write::BrotliDecoder::new(Vec::with_capacity(data.len()));
encoder.write_all(data.as_ref()).await?;
encoder.flush().await?;
return Ok(encoder.into_inner().into());
}
}
}

#[cfg(test)]
mod tests {
use super::Brotli;
use crate::{async_test, traits::CompressionStrategy, utils::test::create_arb_data};

async_test! {
async fn test_compression() {
let data = create_arb_data(1024);
let brotli = Brotli::default();
let compressed = brotli.compress(data.clone().into()).await.unwrap();
let decompressed = brotli.decompress(compressed).await.unwrap();
assert_eq!(data.as_slice(), decompressed.as_ref());
}
}
}
108 changes: 108 additions & 0 deletions bincache/src/compression/gzip_compressor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use super::compression_level::CompressionLevel;
use crate::traits::CompressionStrategy;
use crate::Result;
use async_trait::async_trait;
use std::borrow::Cow;

#[derive(Debug)]
pub struct Gzip {
level: CompressionLevel,
}

impl Gzip {
/// Creates a new Gzip Compressor with the given compression level
pub fn new(level: CompressionLevel) -> Self {
Self { level }
}
}

impl Default for Gzip {
/// Creates a new Gzip Compressor with the default compression level
fn default() -> Self {
Self {
level: CompressionLevel::Default,
}
}
}

#[async_trait]
impl CompressionStrategy for Gzip {
async fn compress<'a>(&self, data: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>> {
#[cfg(feature = "rt_tokio_1")]
{
use async_compression::tokio::write;
use tokio::io::AsyncWriteExt;
let mut encoder =
write::GzipEncoder::with_quality(Vec::with_capacity(data.len()), self.level.into());
encoder.write_all(data.as_ref()).await?;
encoder.shutdown().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(any(feature = "blocking", feature = "implicit-blocking"))]
{
use async_compression::futures::write;
use futures_util::AsyncWriteExt;
let mut encoder =
write::GzipEncoder::with_quality(Vec::with_capacity(data.len()), self.level.into());
encoder.write_all(data.as_ref()).await?;
encoder.close().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(feature = "rt_async-std_1")]
{
use async_compression::futures::write;
use async_std::io::WriteExt;
let mut encoder =
write::GzipEncoder::with_quality(Vec::with_capacity(data.len()), self.level.into());
encoder.write_all(data.as_ref()).await?;
encoder.flush().await?;
return Ok(encoder.into_inner().into());
}
}

async fn decompress<'a>(&self, data: Cow<'a, [u8]>) -> Result<Cow<'a, [u8]>> {
#[cfg(feature = "rt_tokio_1")]
{
use async_compression::tokio::write;
use tokio::io::AsyncWriteExt;
let mut encoder = write::GzipDecoder::new(Vec::with_capacity(data.len()));
encoder.write_all(data.as_ref()).await?;
encoder.shutdown().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(any(feature = "blocking", feature = "implicit-blocking"))]
{
use async_compression::futures::write;
use futures_util::AsyncWriteExt;
let mut encoder = write::GzipDecoder::new(Vec::with_capacity(data.len()));
encoder.write_all(data.as_ref()).await?;
encoder.close().await?;
return Ok(encoder.into_inner().into());
}
#[cfg(feature = "rt_async-std_1")]
{
use async_compression::futures::write;
use async_std::io::WriteExt;
let mut encoder = write::GzipDecoder::new(Vec::with_capacity(data.len()));
encoder.write_all(data.as_ref()).await?;
encoder.flush().await?;
return Ok(encoder.into_inner().into());
}
}
}

#[cfg(test)]
mod tests {
use super::Gzip;
use crate::{async_test, traits::CompressionStrategy, utils::test::create_arb_data};

async_test! {
async fn test_compression() {
let data = create_arb_data(1024);
let gzip = Gzip::default();
let compressed = gzip.compress(data.clone().into()).await.unwrap();
let decompressed = gzip.decompress(compressed).await.unwrap();
assert_eq!(data.as_slice(), decompressed.as_ref());
}
}
}
Loading