| 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::ZstdError, |
| 9 |
libc::c_int, |
| 10 |
pyo3::{ |
| 11 |
exceptions::{PyMemoryError, PyTypeError, PyValueError}, |
| 12 |
prelude::*, |
| 13 |
types::{PyDict, PyTuple, PyType}, |
| 14 |
}, |
| 15 |
std::marker::PhantomData, |
| 16 |
}; |
| 17 |
|
| 18 |
/// Safe wrapper for ZSTD_CCtx_params instances. |
| 19 |
pub struct CCtxParams<'a>(*mut zstd_sys::ZSTD_CCtx_params, PhantomData<&'a ()>); |
| 20 |
|
| 21 |
impl<'a> Drop for CCtxParams<'a> { |
| 22 |
fn drop(&mut self) { |
| 23 |
unsafe { |
| 24 |
zstd_sys::ZSTD_freeCCtxParams(self.0); |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
unsafe impl<'a> Send for CCtxParams<'a> {} |
| 30 |
|
| 31 |
unsafe impl<'a> Sync for CCtxParams<'a> {} |
| 32 |
|
| 33 |
impl<'a> CCtxParams<'a> { |
| 34 |
pub(crate) unsafe fn get_raw_ptr(&self) -> *mut zstd_sys::ZSTD_CCtx_params { |
| 35 |
self.0 |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
impl<'a> CCtxParams<'a> { |
| 40 |
pub fn create() -> Result<Self, PyErr> { |
| 41 |
let params = unsafe { zstd_sys::ZSTD_createCCtxParams() }; |
| 42 |
if params.is_null() { |
| 43 |
return Err(PyMemoryError::new_err("unable to create ZSTD_CCtx_params")); |
| 44 |
} |
| 45 |
Ok(CCtxParams(params, PhantomData)) |
| 46 |
} |
| 47 |
|
| 48 |
pub fn set_parameter(&self, param: zstd_sys::ZSTD_cParameter, value: i32) -> PyResult<()> { |
| 49 |
let zresult = unsafe { zstd_sys::ZSTD_CCtxParams_setParameter(self.0, param, value) }; |
| 50 |
if unsafe { zstd_sys::ZSTD_isError(zresult) } != 0 { |
| 51 |
Err(ZstdError::new_err(format!( |
| 52 |
"unable to set compression context parameter: {}", |
| 53 |
zstd_safe::get_error_name(zresult) |
| 54 |
))) |
| 55 |
} else { |
| 56 |
Ok(()) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
fn apply_compression_parameter( |
| 61 |
&self, |
| 62 |
py: Python, |
| 63 |
params: &Py<ZstdCompressionParameters>, |
| 64 |
param: zstd_sys::ZSTD_cParameter, |
| 65 |
) -> PyResult<()> { |
| 66 |
let value = params.borrow(py).get_parameter(param)?; |
| 67 |
self.set_parameter(param, value) |
| 68 |
} |
| 69 |
|
| 70 |
pub fn apply_compression_parameters( |
| 71 |
&self, |
| 72 |
py: Python, |
| 73 |
params: &Py<ZstdCompressionParameters>, |
| 74 |
) -> PyResult<()> { |
| 75 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_nbWorkers)?; |
| 76 |
// ZSTD_c_format. |
| 77 |
self.apply_compression_parameter( |
| 78 |
py, |
| 79 |
¶ms, |
| 80 |
zstd_sys::ZSTD_cParameter::ZSTD_c_experimentalParam2, |
| 81 |
)?; |
| 82 |
self.apply_compression_parameter( |
| 83 |
py, |
| 84 |
¶ms, |
| 85 |
zstd_sys::ZSTD_cParameter::ZSTD_c_compressionLevel, |
| 86 |
)?; |
| 87 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_windowLog)?; |
| 88 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_hashLog)?; |
| 89 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_chainLog)?; |
| 90 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_searchLog)?; |
| 91 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_minMatch)?; |
| 92 |
self.apply_compression_parameter( |
| 93 |
py, |
| 94 |
¶ms, |
| 95 |
zstd_sys::ZSTD_cParameter::ZSTD_c_targetLength, |
| 96 |
)?; |
| 97 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_strategy)?; |
| 98 |
self.apply_compression_parameter( |
| 99 |
py, |
| 100 |
¶ms, |
| 101 |
zstd_sys::ZSTD_cParameter::ZSTD_c_contentSizeFlag, |
| 102 |
)?; |
| 103 |
self.apply_compression_parameter( |
| 104 |
py, |
| 105 |
¶ms, |
| 106 |
zstd_sys::ZSTD_cParameter::ZSTD_c_checksumFlag, |
| 107 |
)?; |
| 108 |
self.apply_compression_parameter( |
| 109 |
py, |
| 110 |
¶ms, |
| 111 |
zstd_sys::ZSTD_cParameter::ZSTD_c_dictIDFlag, |
| 112 |
)?; |
| 113 |
self.apply_compression_parameter(py, ¶ms, zstd_sys::ZSTD_cParameter::ZSTD_c_jobSize)?; |
| 114 |
self.apply_compression_parameter( |
| 115 |
py, |
| 116 |
¶ms, |
| 117 |
zstd_sys::ZSTD_cParameter::ZSTD_c_overlapLog, |
| 118 |
)?; |
| 119 |
// ZSTD_c_forceMaxWindow |
| 120 |
self.apply_compression_parameter( |
| 121 |
py, |
| 122 |
¶ms, |
| 123 |
zstd_sys::ZSTD_cParameter::ZSTD_c_experimentalParam3, |
| 124 |
)?; |
| 125 |
self.apply_compression_parameter( |
| 126 |
py, |
| 127 |
¶ms, |
| 128 |
zstd_sys::ZSTD_cParameter::ZSTD_c_overlapLog, |
| 129 |
)?; |
| 130 |
self.apply_compression_parameter( |
| 131 |
py, |
| 132 |
¶ms, |
| 133 |
zstd_sys::ZSTD_cParameter::ZSTD_c_enableLongDistanceMatching, |
| 134 |
)?; |
| 135 |
self.apply_compression_parameter( |
| 136 |
py, |
| 137 |
¶ms, |
| 138 |
zstd_sys::ZSTD_cParameter::ZSTD_c_ldmHashLog, |
| 139 |
)?; |
| 140 |
self.apply_compression_parameter( |
| 141 |
py, |
| 142 |
¶ms, |
| 143 |
zstd_sys::ZSTD_cParameter::ZSTD_c_ldmMinMatch, |
| 144 |
)?; |
| 145 |
self.apply_compression_parameter( |
| 146 |
py, |
| 147 |
¶ms, |
| 148 |
zstd_sys::ZSTD_cParameter::ZSTD_c_ldmBucketSizeLog, |
| 149 |
)?; |
| 150 |
self.apply_compression_parameter( |
| 151 |
py, |
| 152 |
¶ms, |
| 153 |
zstd_sys::ZSTD_cParameter::ZSTD_c_ldmHashRateLog, |
| 154 |
)?; |
| 155 |
|
| 156 |
Ok(()) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// Resolve the value of a compression context parameter. |
| 161 |
pub(crate) fn get_cctx_parameter( |
| 162 |
params: *mut zstd_sys::ZSTD_CCtx_params, |
| 163 |
param: zstd_sys::ZSTD_cParameter, |
| 164 |
) -> Result<libc::c_int, PyErr> { |
| 165 |
let mut value: libc::c_int = 0; |
| 166 |
|
| 167 |
let zresult = |
| 168 |
unsafe { zstd_sys::ZSTD_CCtxParams_getParameter(params, param, &mut value as *mut _) }; |
| 169 |
|
| 170 |
if unsafe { zstd_sys::ZSTD_isError(zresult) } != 0 { |
| 171 |
Err(ZstdError::new_err(format!( |
| 172 |
"unable to retrieve parameter: {}", |
| 173 |
zstd_safe::get_error_name(zresult) |
| 174 |
))) |
| 175 |
} else { |
| 176 |
Ok(value) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// Surely there is a better way... |
| 181 |
pub(crate) fn int_to_strategy(value: u32) -> Result<zstd_sys::ZSTD_strategy, PyErr> { |
| 182 |
if zstd_sys::ZSTD_strategy::ZSTD_fast as u32 == value { |
| 183 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_fast) |
| 184 |
} else if zstd_sys::ZSTD_strategy::ZSTD_dfast as u32 == value { |
| 185 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_dfast) |
| 186 |
} else if zstd_sys::ZSTD_strategy::ZSTD_greedy as u32 == value { |
| 187 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_greedy) |
| 188 |
} else if zstd_sys::ZSTD_strategy::ZSTD_lazy as u32 == value { |
| 189 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_lazy) |
| 190 |
} else if zstd_sys::ZSTD_strategy::ZSTD_lazy2 as u32 == value { |
| 191 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_lazy2) |
| 192 |
} else if zstd_sys::ZSTD_strategy::ZSTD_btlazy2 as u32 == value { |
| 193 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_btlazy2) |
| 194 |
} else if zstd_sys::ZSTD_strategy::ZSTD_btopt as u32 == value { |
| 195 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_btopt) |
| 196 |
} else if zstd_sys::ZSTD_strategy::ZSTD_btultra as u32 == value { |
| 197 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_btultra) |
| 198 |
} else if zstd_sys::ZSTD_strategy::ZSTD_btultra2 as u32 == value { |
| 199 |
Ok(zstd_sys::ZSTD_strategy::ZSTD_btultra2) |
| 200 |
} else { |
| 201 |
Err(PyValueError::new_err("unknown compression strategy")) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
#[pyclass(module = "zstandard.backend_rust")] |
| 206 |
pub struct ZstdCompressionParameters { |
| 207 |
pub(crate) params: *mut zstd_sys::ZSTD_CCtx_params, |
| 208 |
} |
| 209 |
|
| 210 |
unsafe impl Sync for ZstdCompressionParameters {} |
| 211 |
|
| 212 |
impl Drop for ZstdCompressionParameters { |
| 213 |
fn drop(&mut self) { |
| 214 |
unsafe { |
| 215 |
zstd_sys::ZSTD_freeCCtxParams(self.params); |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
unsafe impl Send for ZstdCompressionParameters {} |
| 221 |
|
| 222 |
impl ZstdCompressionParameters { |
| 223 |
pub(crate) fn get_parameter(&self, param: zstd_sys::ZSTD_cParameter) -> PyResult<c_int> { |
| 224 |
let mut value: c_int = 0; |
| 225 |
|
| 226 |
let zresult = unsafe { |
| 227 |
zstd_sys::ZSTD_CCtxParams_getParameter(self.params, param, &mut value as *mut _) |
| 228 |
}; |
| 229 |
|
| 230 |
if unsafe { zstd_sys::ZSTD_isError(zresult) } != 0 { |
| 231 |
return Err(ZstdError::new_err(format!( |
| 232 |
"unable to retrieve parameter: {}", |
| 233 |
zstd_safe::get_error_name(zresult) |
| 234 |
))); |
| 235 |
} |
| 236 |
|
| 237 |
Ok(value) |
| 238 |
} |
| 239 |
|
| 240 |
fn set_parameter(&self, param: zstd_sys::ZSTD_cParameter, value: i32) -> PyResult<()> { |
| 241 |
let zresult = unsafe { zstd_sys::ZSTD_CCtxParams_setParameter(self.params, param, value) }; |
| 242 |
|
| 243 |
if unsafe { zstd_sys::ZSTD_isError(zresult) } != 0 { |
| 244 |
return Err(ZstdError::new_err(format!( |
| 245 |
"unable to set compression context parameter: {}", |
| 246 |
zstd_safe::get_error_name(zresult) |
| 247 |
))); |
| 248 |
} |
| 249 |
|
| 250 |
Ok(()) |
| 251 |
} |
| 252 |
|
| 253 |
/// Set parameters from a dictionary of options. |
| 254 |
fn set_parameters(&self, kwargs: &Bound<'_, PyDict>) -> PyResult<()> { |
| 255 |
unsafe { |
| 256 |
zstd_sys::ZSTD_CCtxParams_reset(self.params); |
| 257 |
} |
| 258 |
|
| 259 |
let mut format = 0; |
| 260 |
let mut compression_level = 0; |
| 261 |
let mut window_log = 0; |
| 262 |
let mut hash_log = 0; |
| 263 |
let mut chain_log = 0; |
| 264 |
let mut search_log = 0; |
| 265 |
let mut min_match = 0; |
| 266 |
let mut target_length = 0; |
| 267 |
let mut strategy = -1; |
| 268 |
let mut write_content_size = 1; |
| 269 |
let mut write_checksum = 0; |
| 270 |
let mut write_dict_id = 0; |
| 271 |
let mut job_size = 0; |
| 272 |
let mut overlap_log = -1; |
| 273 |
let mut force_max_window = 0; |
| 274 |
let mut enable_ldm = 0; |
| 275 |
let mut ldm_hash_log = 0; |
| 276 |
let mut ldm_min_match = 0; |
| 277 |
let mut ldm_bucket_size_log = 0; |
| 278 |
let mut ldm_hash_rate_log = -1; |
| 279 |
let mut threads = 0; |
| 280 |
|
| 281 |
for (key, value) in kwargs.iter() { |
| 282 |
let key = key.extract::<String>()?; |
| 283 |
|
| 284 |
match key.as_ref() { |
| 285 |
"format" => format = value.extract::<_>()?, |
| 286 |
"compression_level" => compression_level = value.extract::<_>()?, |
| 287 |
"window_log" => window_log = value.extract::<_>()?, |
| 288 |
"hash_log" => hash_log = value.extract::<_>()?, |
| 289 |
"chain_log" => chain_log = value.extract::<_>()?, |
| 290 |
"search_log" => search_log = value.extract::<_>()?, |
| 291 |
"min_match" => min_match = value.extract::<_>()?, |
| 292 |
"target_length" => target_length = value.extract::<_>()?, |
| 293 |
"strategy" => strategy = value.extract::<_>()?, |
| 294 |
"write_content_size" => write_content_size = value.extract::<_>()?, |
| 295 |
"write_checksum" => write_checksum = value.extract::<_>()?, |
| 296 |
"write_dict_id" => write_dict_id = value.extract::<_>()?, |
| 297 |
"job_size" => job_size = value.extract::<_>()?, |
| 298 |
"overlap_log" => overlap_log = value.extract::<_>()?, |
| 299 |
"force_max_window" => force_max_window = value.extract::<_>()?, |
| 300 |
"enable_ldm" => enable_ldm = value.extract::<_>()?, |
| 301 |
"ldm_hash_log" => ldm_hash_log = value.extract::<_>()?, |
| 302 |
"ldm_min_match" => ldm_min_match = value.extract::<_>()?, |
| 303 |
"ldm_bucket_size_log" => ldm_bucket_size_log = value.extract::<_>()?, |
| 304 |
"ldm_hash_rate_log" => ldm_hash_rate_log = value.extract::<_>()?, |
| 305 |
"threads" => threads = value.extract::<_>()?, |
| 306 |
key => { |
| 307 |
return Err(PyTypeError::new_err(format!( |
| 308 |
"'{}' is an invalid keyword argument", |
| 309 |
key |
| 310 |
))); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if threads < 0 { |
| 316 |
threads = num_cpus::get() as _; |
| 317 |
} |
| 318 |
|
| 319 |
// We need to set ZSTD_c_nbWorkers before ZSTD_c_jobSize and ZSTD_c_overlapLog |
| 320 |
// because setting ZSTD_c_nbWorkers resets the other parameters. |
| 321 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_nbWorkers, threads)?; |
| 322 |
|
| 323 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_experimentalParam2, format)?; |
| 324 |
self.set_parameter( |
| 325 |
zstd_sys::ZSTD_cParameter::ZSTD_c_compressionLevel, |
| 326 |
compression_level, |
| 327 |
)?; |
| 328 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_windowLog, window_log)?; |
| 329 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_hashLog, hash_log)?; |
| 330 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_chainLog, chain_log)?; |
| 331 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_searchLog, search_log)?; |
| 332 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_minMatch, min_match)?; |
| 333 |
self.set_parameter( |
| 334 |
zstd_sys::ZSTD_cParameter::ZSTD_c_targetLength, |
| 335 |
target_length, |
| 336 |
)?; |
| 337 |
|
| 338 |
if strategy == -1 { |
| 339 |
strategy = 0; |
| 340 |
} |
| 341 |
|
| 342 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_strategy, strategy)?; |
| 343 |
self.set_parameter( |
| 344 |
zstd_sys::ZSTD_cParameter::ZSTD_c_contentSizeFlag, |
| 345 |
write_content_size, |
| 346 |
)?; |
| 347 |
self.set_parameter( |
| 348 |
zstd_sys::ZSTD_cParameter::ZSTD_c_checksumFlag, |
| 349 |
write_checksum, |
| 350 |
)?; |
| 351 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_dictIDFlag, write_dict_id)?; |
| 352 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_jobSize, job_size)?; |
| 353 |
|
| 354 |
if overlap_log == -1 { |
| 355 |
overlap_log = 0; |
| 356 |
} |
| 357 |
|
| 358 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_overlapLog, overlap_log)?; |
| 359 |
self.set_parameter( |
| 360 |
zstd_sys::ZSTD_cParameter::ZSTD_c_experimentalParam3, |
| 361 |
force_max_window, |
| 362 |
)?; |
| 363 |
self.set_parameter( |
| 364 |
zstd_sys::ZSTD_cParameter::ZSTD_c_enableLongDistanceMatching, |
| 365 |
enable_ldm, |
| 366 |
)?; |
| 367 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_ldmHashLog, ldm_hash_log)?; |
| 368 |
self.set_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_ldmMinMatch, ldm_min_match)?; |
| 369 |
self.set_parameter( |
| 370 |
zstd_sys::ZSTD_cParameter::ZSTD_c_ldmBucketSizeLog, |
| 371 |
ldm_bucket_size_log, |
| 372 |
)?; |
| 373 |
|
| 374 |
if ldm_hash_rate_log == -1 { |
| 375 |
ldm_hash_rate_log = 0; |
| 376 |
} |
| 377 |
|
| 378 |
self.set_parameter( |
| 379 |
zstd_sys::ZSTD_cParameter::ZSTD_c_ldmHashRateLog, |
| 380 |
ldm_hash_rate_log, |
| 381 |
)?; |
| 382 |
|
| 383 |
Ok(()) |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
#[pymethods] |
| 388 |
impl ZstdCompressionParameters { |
| 389 |
#[classmethod] |
| 390 |
#[pyo3(signature = (* args, * * kwargs))] |
| 391 |
fn from_level( |
| 392 |
_cls: Bound<'_, PyType>, |
| 393 |
py: Python, |
| 394 |
args: &Bound<'_, PyTuple>, |
| 395 |
kwargs: Option<&Bound<'_, PyDict>>, |
| 396 |
) -> PyResult<Self> { |
| 397 |
if args.len() != 1 { |
| 398 |
return Err(PyTypeError::new_err(format!( |
| 399 |
"from_level() takes exactly 1 argument ({} given)", |
| 400 |
args.len() |
| 401 |
))); |
| 402 |
} |
| 403 |
|
| 404 |
let kwargs = if let Some(v) = kwargs { |
| 405 |
v.copy()? |
| 406 |
} else { |
| 407 |
PyDict::new(py) |
| 408 |
}; |
| 409 |
|
| 410 |
let level = args.get_item(0)?.extract::<i32>()?; |
| 411 |
|
| 412 |
let source_size = if let Some(value) = kwargs.get_item("source_size")? { |
| 413 |
kwargs.del_item("source_size")?; |
| 414 |
value.extract::<u64>()? |
| 415 |
} else { |
| 416 |
0 |
| 417 |
}; |
| 418 |
|
| 419 |
let dict_size = if let Some(value) = kwargs.get_item("dict_size")? { |
| 420 |
kwargs.del_item("dict_size")?; |
| 421 |
value.extract::<usize>()? |
| 422 |
} else { |
| 423 |
0 |
| 424 |
}; |
| 425 |
|
| 426 |
let compression_params = |
| 427 |
unsafe { zstd_sys::ZSTD_getCParams(level, source_size, dict_size) }; |
| 428 |
|
| 429 |
if !kwargs.contains("window_log")? { |
| 430 |
kwargs.set_item("window_log", compression_params.windowLog)?; |
| 431 |
} |
| 432 |
if !kwargs.contains("chain_log")? { |
| 433 |
kwargs.set_item("chain_log", compression_params.chainLog)?; |
| 434 |
} |
| 435 |
if !kwargs.contains("hash_log")? { |
| 436 |
kwargs.set_item("hash_log", compression_params.hashLog)?; |
| 437 |
} |
| 438 |
if !kwargs.contains("search_log")? { |
| 439 |
kwargs.set_item("search_log", compression_params.searchLog)?; |
| 440 |
} |
| 441 |
if !kwargs.contains("min_match")? { |
| 442 |
kwargs.set_item("min_match", compression_params.minMatch)?; |
| 443 |
} |
| 444 |
if !kwargs.contains("target_length")? { |
| 445 |
kwargs.set_item("target_length", compression_params.targetLength)?; |
| 446 |
} |
| 447 |
if !kwargs.contains("strategy")? { |
| 448 |
kwargs.set_item("strategy", compression_params.strategy as u32)?; |
| 449 |
} |
| 450 |
|
| 451 |
Self::new(py, &PyTuple::empty(py), Some(&kwargs)) |
| 452 |
} |
| 453 |
|
| 454 |
#[new] |
| 455 |
#[pyo3(signature = (* _args, * * kwargs))] |
| 456 |
fn new( |
| 457 |
py: Python, |
| 458 |
_args: &Bound<'_, PyTuple>, |
| 459 |
kwargs: Option<&Bound<'_, PyDict>>, |
| 460 |
) -> PyResult<Self> { |
| 461 |
let params = unsafe { zstd_sys::ZSTD_createCCtxParams() }; |
| 462 |
if params.is_null() { |
| 463 |
return Err(PyMemoryError::new_err("unable to create ZSTD_CCtx_params")); |
| 464 |
} |
| 465 |
|
| 466 |
let instance = ZstdCompressionParameters { params }; |
| 467 |
|
| 468 |
let kwargs = if let Some(v) = kwargs { |
| 469 |
v.copy()? |
| 470 |
} else { |
| 471 |
PyDict::new(py) |
| 472 |
}; |
| 473 |
|
| 474 |
instance.set_parameters(&kwargs)?; |
| 475 |
|
| 476 |
Ok(instance) |
| 477 |
} |
| 478 |
|
| 479 |
#[getter] |
| 480 |
fn format(&self) -> PyResult<c_int> { |
| 481 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_experimentalParam2) |
| 482 |
} |
| 483 |
|
| 484 |
#[getter] |
| 485 |
fn compression_level(&self) -> PyResult<c_int> { |
| 486 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_compressionLevel) |
| 487 |
} |
| 488 |
|
| 489 |
#[getter] |
| 490 |
fn window_log(&self) -> PyResult<c_int> { |
| 491 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_windowLog) |
| 492 |
} |
| 493 |
|
| 494 |
#[getter] |
| 495 |
fn hash_log(&self) -> PyResult<c_int> { |
| 496 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_hashLog) |
| 497 |
} |
| 498 |
|
| 499 |
#[getter] |
| 500 |
fn chain_log(&self) -> PyResult<c_int> { |
| 501 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_chainLog) |
| 502 |
} |
| 503 |
|
| 504 |
#[getter] |
| 505 |
fn search_log(&self) -> PyResult<c_int> { |
| 506 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_searchLog) |
| 507 |
} |
| 508 |
|
| 509 |
#[getter] |
| 510 |
fn min_match(&self) -> PyResult<c_int> { |
| 511 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_minMatch) |
| 512 |
} |
| 513 |
|
| 514 |
#[getter] |
| 515 |
fn target_length(&self) -> PyResult<c_int> { |
| 516 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_targetLength) |
| 517 |
} |
| 518 |
|
| 519 |
#[getter] |
| 520 |
fn strategy(&self) -> PyResult<c_int> { |
| 521 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_strategy) |
| 522 |
} |
| 523 |
|
| 524 |
#[getter] |
| 525 |
fn write_content_size(&self) -> PyResult<c_int> { |
| 526 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_contentSizeFlag) |
| 527 |
} |
| 528 |
|
| 529 |
#[getter] |
| 530 |
fn write_checksum(&self) -> PyResult<c_int> { |
| 531 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_checksumFlag) |
| 532 |
} |
| 533 |
|
| 534 |
#[getter] |
| 535 |
fn write_dict_id(&self) -> PyResult<c_int> { |
| 536 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_dictIDFlag) |
| 537 |
} |
| 538 |
|
| 539 |
#[getter] |
| 540 |
fn overlap_log(&self) -> PyResult<c_int> { |
| 541 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_overlapLog) |
| 542 |
} |
| 543 |
|
| 544 |
#[getter] |
| 545 |
fn force_max_window(&self) -> PyResult<c_int> { |
| 546 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_experimentalParam3) |
| 547 |
} |
| 548 |
|
| 549 |
#[getter] |
| 550 |
fn enable_ldm(&self) -> PyResult<c_int> { |
| 551 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_enableLongDistanceMatching) |
| 552 |
} |
| 553 |
|
| 554 |
#[getter] |
| 555 |
fn ldm_hash_log(&self) -> PyResult<c_int> { |
| 556 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_ldmHashLog) |
| 557 |
} |
| 558 |
|
| 559 |
#[getter] |
| 560 |
fn ldm_min_match(&self) -> PyResult<c_int> { |
| 561 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_ldmMinMatch) |
| 562 |
} |
| 563 |
|
| 564 |
#[getter] |
| 565 |
fn ldm_bucket_size_log(&self) -> PyResult<c_int> { |
| 566 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_ldmBucketSizeLog) |
| 567 |
} |
| 568 |
|
| 569 |
#[getter] |
| 570 |
fn ldm_hash_rate_log(&self) -> PyResult<c_int> { |
| 571 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_ldmHashRateLog) |
| 572 |
} |
| 573 |
|
| 574 |
#[getter] |
| 575 |
fn threads(&self) -> PyResult<c_int> { |
| 576 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_nbWorkers) |
| 577 |
} |
| 578 |
|
| 579 |
#[getter] |
| 580 |
fn job_size(&self) -> PyResult<c_int> { |
| 581 |
self.get_parameter(zstd_sys::ZSTD_cParameter::ZSTD_c_jobSize) |
| 582 |
} |
| 583 |
|
| 584 |
fn estimated_compression_context_size(&self) -> PyResult<usize> { |
| 585 |
let size = unsafe { zstd_sys::ZSTD_estimateCCtxSize_usingCCtxParams(self.params) }; |
| 586 |
|
| 587 |
Ok(size) |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
pub(crate) fn init_module(module: &Bound<'_, PyModule>) -> PyResult<()> { |
| 592 |
module.add_class::<ZstdCompressionParameters>()?; |
| 593 |
|
| 594 |
Ok(()) |
| 595 |
} |