| 1 |
// Copyright (c) 2020-present, Gregory Szorc |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This software may be modified and distributed under the terms |
| 5 |
// of the BSD license. See the LICENSE file for details. |
| 6 |
|
| 7 |
use { |
| 8 |
crate::{ |
| 9 |
constants::{COMPRESSOBJ_FLUSH_BLOCK, COMPRESSOBJ_FLUSH_FINISH}, |
| 10 |
zstd_safe::CCtx, |
| 11 |
ZstdError, |
| 12 |
}, |
| 13 |
pyo3::{buffer::PyBuffer, exceptions::PyValueError, prelude::*, types::PyBytes}, |
| 14 |
std::sync::Arc, |
| 15 |
}; |
| 16 |
|
| 17 |
#[pyclass(module = "zstandard.backend_rust")] |
| 18 |
pub struct ZstdCompressionObj { |
| 19 |
cctx: Arc<CCtx<'static>>, |
| 20 |
finished: bool, |
| 21 |
} |
| 22 |
|
| 23 |
unsafe impl Sync for ZstdCompressionObj {} |
| 24 |
|
| 25 |
impl ZstdCompressionObj { |
| 26 |
pub fn new(cctx: Arc<CCtx<'static>>) -> PyResult<Self> { |
| 27 |
Ok(ZstdCompressionObj { |
| 28 |
cctx, |
| 29 |
finished: false, |
| 30 |
}) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
#[pymethods] |
| 35 |
impl ZstdCompressionObj { |
| 36 |
fn compress<'p>(&self, py: Python<'p>, buffer: PyBuffer<u8>) -> PyResult<Bound<'p, PyBytes>> { |
| 37 |
if self.finished { |
| 38 |
return Err(ZstdError::new_err( |
| 39 |
"cannot call compress() after compressor finished", |
| 40 |
)); |
| 41 |
} |
| 42 |
|
| 43 |
let mut source = unsafe { |
| 44 |
std::slice::from_raw_parts::<u8>(buffer.buf_ptr() as *const _, buffer.len_bytes()) |
| 45 |
}; |
| 46 |
|
| 47 |
// TODO consider collecting chunks and joining |
| 48 |
// TODO try to use zero copy into return value. |
| 49 |
let mut compressed = Vec::new(); |
| 50 |
let write_size = zstd_safe::CCtx::out_size(); |
| 51 |
|
| 52 |
let cctx = &self.cctx; |
| 53 |
while !source.is_empty() { |
| 54 |
let result = py |
| 55 |
.allow_threads(|| { |
| 56 |
cctx.compress_chunk( |
| 57 |
source, |
| 58 |
zstd_sys::ZSTD_EndDirective::ZSTD_e_continue, |
| 59 |
write_size, |
| 60 |
) |
| 61 |
}) |
| 62 |
.or_else(|msg| Err(ZstdError::new_err(format!("zstd compress error: {}", msg))))?; |
| 63 |
|
| 64 |
compressed.extend(result.0); |
| 65 |
source = result.1; |
| 66 |
} |
| 67 |
|
| 68 |
Ok(PyBytes::new(py, &compressed)) |
| 69 |
} |
| 70 |
|
| 71 |
#[pyo3(signature = (flush_mode=None))] |
| 72 |
fn flush<'p>( |
| 73 |
&mut self, |
| 74 |
py: Python<'p>, |
| 75 |
flush_mode: Option<i32>, |
| 76 |
) -> PyResult<Bound<'p, PyBytes>> { |
| 77 |
let flush_mode = if let Some(flush_mode) = flush_mode { |
| 78 |
match flush_mode { |
| 79 |
COMPRESSOBJ_FLUSH_FINISH => Ok(zstd_sys::ZSTD_EndDirective::ZSTD_e_end), |
| 80 |
COMPRESSOBJ_FLUSH_BLOCK => Ok(zstd_sys::ZSTD_EndDirective::ZSTD_e_flush), |
| 81 |
_ => Err(PyValueError::new_err("flush mode not recognized")), |
| 82 |
} |
| 83 |
} else { |
| 84 |
Ok(zstd_sys::ZSTD_EndDirective::ZSTD_e_end) |
| 85 |
}?; |
| 86 |
|
| 87 |
if self.finished { |
| 88 |
return Err(ZstdError::new_err("compressor object already finished")); |
| 89 |
} |
| 90 |
|
| 91 |
if flush_mode == zstd_sys::ZSTD_EndDirective::ZSTD_e_end { |
| 92 |
self.finished = true; |
| 93 |
} |
| 94 |
|
| 95 |
let write_size = zstd_safe::CCtx::out_size(); |
| 96 |
let cctx = &self.cctx; |
| 97 |
|
| 98 |
// TODO avoid extra buffer copy. |
| 99 |
let mut result = Vec::new(); |
| 100 |
|
| 101 |
loop { |
| 102 |
let (chunk, _, call_again) = py |
| 103 |
.allow_threads(|| cctx.compress_chunk(&[], flush_mode, write_size)) |
| 104 |
.or_else(|msg| { |
| 105 |
Err(ZstdError::new_err(format!( |
| 106 |
"error ending compression stream: {}", |
| 107 |
msg |
| 108 |
))) |
| 109 |
})?; |
| 110 |
|
| 111 |
result.extend(&chunk); |
| 112 |
|
| 113 |
if !call_again { |
| 114 |
return Ok(PyBytes::new(py, &result)); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |