Skip to content

Commit

Permalink
Initial impl of capi
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger committed Dec 22, 2023
1 parent 0bea714 commit 9a56423
Show file tree
Hide file tree
Showing 13 changed files with 2,610 additions and 72 deletions.
608 changes: 584 additions & 24 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
homepage = "https://github.com/milesgranger/pyrus-cramjam"

[profile.release]
# strip = true
lto = "fat"
codegen-units = 1
opt-level = 3
41 changes: 11 additions & 30 deletions cramjam-python/src/lz4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::exceptions::{CompressionError, DecompressionError};
use crate::io::{AsBytes, RustyBuffer};
use crate::BytesType;
use libcramjam::lz4::lz4::{block, block::CompressionMode};
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use pyo3::PyResult;
Expand Down Expand Up @@ -84,11 +83,15 @@ pub fn decompress_into(py: Python, input: BytesType, mut output: BytesType) -> P
/// >>> cramjam.lz4.decompress_block(compressed_bytes, output_len=Optional[int])
/// ```
#[pyfunction]
#[allow(unused_variables)]
pub fn decompress_block(py: Python, data: BytesType, output_len: Option<usize>) -> PyResult<RustyBuffer> {
let bytes = data.as_bytes();
py.allow_threads(|| block::decompress(bytes, output_len.map(|v| v as i32)))
.map_err(DecompressionError::from_err)
.map(RustyBuffer::from)

py.allow_threads(|| {
libcramjam::lz4::block::compress_vec(bytes, None, None, Some(true))
.map_err(DecompressionError::from_err)
.map(RustyBuffer::from)
})
}

/// LZ4 _block_ compression.
Expand Down Expand Up @@ -120,9 +123,7 @@ pub fn compress_block(
) -> PyResult<RustyBuffer> {
let bytes = data.as_bytes();
py.allow_threads(|| {
let store_size = store_size.unwrap_or(true);
let mode = compression_mode(mode, compression, acceleration)?;
block::compress(bytes, Some(mode), store_size)
libcramjam::lz4::block::compress_vec(bytes, compression.map(|v| v as _), acceleration, store_size)
})
.map_err(CompressionError::from_err)
.map(RustyBuffer::from)
Expand All @@ -139,7 +140,7 @@ pub fn compress_block(
pub fn decompress_block_into(py: Python, input: BytesType, mut output: BytesType) -> PyResult<usize> {
let bytes = input.as_bytes();
let out_bytes = output.as_bytes_mut();
py.allow_threads(|| block::decompress_to_buffer(bytes, None, out_bytes))
py.allow_threads(|| libcramjam::lz4::block::decompress_into(bytes, out_bytes))
.map_err(DecompressionError::from_err)
.map(|v| v as _)
}
Expand Down Expand Up @@ -174,32 +175,12 @@ pub fn compress_block_into(
let bytes = data.as_bytes();
let out_bytes = output.as_bytes_mut();
py.allow_threads(|| {
let store_size = store_size.unwrap_or(true);
let mode = compression_mode(mode, compression, acceleration)?;
block::compress_to_buffer(bytes, Some(mode), store_size, out_bytes)
libcramjam::lz4::block::compress_into(bytes, out_bytes, compression.map(|v| v as _), acceleration, store_size)
})
.map_err(CompressionError::from_err)
.map(|v| v as _)
}

#[inline]
fn compression_mode(
mode: Option<&str>,
compression: Option<i32>,
acceleration: Option<i32>,
) -> PyResult<CompressionMode> {
let m = match mode {
Some(m) => match m {
"default" => CompressionMode::DEFAULT,
"fast" => CompressionMode::FAST(acceleration.unwrap_or(1)),
"high_compression" => CompressionMode::HIGHCOMPRESSION(compression.unwrap_or(9)),
_ => return Err(DecompressionError::new_err(format!("Unrecognized mode '{}'", m))),
},
None => CompressionMode::DEFAULT,
};
Ok(m)
}

/// Determine the size of a buffer which is guaranteed to hold the result of block compression, will error if
/// data is too long to be compressed by LZ4.
///
Expand All @@ -210,7 +191,7 @@ fn compression_mode(
/// ```
#[pyfunction]
pub fn compress_block_bound(src: BytesType) -> PyResult<usize> {
block::compress_bound(src.len()).map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))
Ok(libcramjam::lz4::block::compress_bound(src.len(), Some(true)))
}

/// lz4 Compressor object for streaming compression
Expand Down
Loading

0 comments on commit 9a56423

Please sign in to comment.