Skip to content

Commit

Permalink
feat(lib)!: change compression from lz4 to brotli (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickersoft committed Dec 27, 2024
1 parent 52e92f7 commit 6dc0b9a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 51 deletions.
34 changes: 13 additions & 21 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ name = "lib"
harness = false

[dependencies]
lz4_flex = { version = "0.11.3", default-features = false, features = [
"frame",
] }
byteorder = "1.5.0"
quick-xml = { version = "0.37.0", features = ["serialize"] }
rayon = "1.10.0"
Expand All @@ -46,6 +43,7 @@ dirs = { version = "5.0.1", optional = true }
sea-query = { version = "0.32.0", optional = true }
thiserror = "2.0.7"
anyhow = "1.0.94"
brotli = "7.0.0"

[dev-dependencies]
insta = "1.38.0"
Expand Down
25 changes: 25 additions & 0 deletions lib/src/compression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::io::{Read, Write};

const LG_WINDOW_SIZE: u32 = 21;
const QUALITY: u32 = 11;

pub fn compress(bytes: &[u8]) -> crate::Result<Vec<u8>> {
let mut buf = Vec::new();
let mut writer = brotli::CompressorWriter::new(&mut buf, 4096, QUALITY, LG_WINDOW_SIZE);

writer.write_all(bytes).expect("Failed to write data");
writer.flush().expect("Failed to flush data");

drop(writer);

Ok(buf)
}

pub fn decompress(bytes: &[u8]) -> crate::Result<Vec<u8>> {
let mut buf = Vec::new();
let mut reader = brotli::Decompressor::new(bytes, 4096);

reader.read_to_end(&mut buf).expect("Failed to read data");

Ok(buf)
}
2 changes: 1 addition & 1 deletion lib/src/core/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use byteorder::{LittleEndian, ReadBytesExt};
use rkyv::access_unchecked;

use crate::{
compression::decompress,
constants::{SIGNATURE, VERSION},
error::Error,
lz4::decompress,
ArchivedDictionary, Dictionary,
};

Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::read_to_string;
use std::path::Path;
use std::{fs::File, io::Write};

use crate::compression::compress;
use crate::error::Error;
use crate::lz4::compress;
use crate::{Dictionary, ToDictionary};

use super::constants::*;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum Error {
FromUtf8(#[from] std::string::FromUtf8Error),

#[error("Failed to compress: {0}")]
Compression(#[from] lz4_flex::frame::Error),
Compression(String),

#[error("Failed to write the current dictionary: {0}")]
Write(String),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub mod json;
#[cfg(feature = "sql")]
pub mod sql;

mod compression;
mod core;
mod error;
mod ext;
mod lz4;
mod md;
mod models;

Expand Down
23 changes: 0 additions & 23 deletions lib/src/lz4.rs

This file was deleted.

0 comments on commit 6dc0b9a

Please sign in to comment.