From ab87b589155413ec140eb1aa62e670fbfc8a980b Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Sun, 19 May 2024 13:26:11 -0400 Subject: [PATCH] Add a property interface to ZstdCompressor, add ZstdError type --- src/LibZstd_clang.jl | 3 +-- src/compression.jl | 35 ++++++++++++++++++++++++++++++++- src/libzstd.jl | 47 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/LibZstd_clang.jl b/src/LibZstd_clang.jl index 7e995c4..519385e 100644 --- a/src/LibZstd_clang.jl +++ b/src/LibZstd_clang.jl @@ -144,6 +144,7 @@ end ZSTD_c_minMatch = 105 ZSTD_c_targetLength = 106 ZSTD_c_strategy = 107 + ZSTD_c_targetCBlockSize = 130 ZSTD_c_enableLongDistanceMatching = 160 ZSTD_c_ldmHashLog = 161 ZSTD_c_ldmMinMatch = 162 @@ -1161,8 +1162,6 @@ const ZSTD_c_forceAttachDict = ZSTD_c_experimentalParam4 const ZSTD_c_literalCompressionMode = ZSTD_c_experimentalParam5 -const ZSTD_c_targetCBlockSize = ZSTD_c_experimentalParam6 - const ZSTD_c_srcSizeHint = ZSTD_c_experimentalParam7 const ZSTD_c_enableDedicatedDictSearch = ZSTD_c_experimentalParam8 diff --git a/src/compression.jl b/src/compression.jl index 36b93a4..d1ac367 100644 --- a/src/compression.jl +++ b/src/compression.jl @@ -3,7 +3,40 @@ struct ZstdCompressor <: TranscodingStreams.Codec cstream::CStream - level::Int + function ZstdCompressor(cstream, level) + self = new(cstream) + setParameter!(self.cstream, :compressionLevel, level) + return self + end +end + +function Base.propertynames(::Type{ZstdCompressor}) + (fieldnames(ZstdCompressor)..., :level, keys(_parameters_dict)...) +end +Base.propertynames(::ZstdCompressor) = propertynames(ZstdCompressor) +function Base.getproperty(c::ZstdCompressor, p::Symbol) + if p == :level + p = :compressionLevel + end + if p in fieldnames(ZstdCompressor) + return getfield(c, p) + end + if p in keys(_parameters_dict) + return getParameter(c.cstream, p) + end + throw(ArgumentError("ZstdCompressor has no property $p")) +end +function Base.setproperty!(c::ZstdCompressor, p::Symbol, v::Integer) + if p == :level + p = :compressionLevel + end + if p in fieldnames(ZstdCompressor) + return setfield!(c, p, v) + end + if p in keys(_parameters_dict) + return setParameter!(c.cstream, p, v) + end + throw(ArgumentError("ZstdCompressor has no property $p")) end function Base.show(io::IO, codec::ZstdCompressor) diff --git a/src/libzstd.jl b/src/libzstd.jl index 1f97139..d50a24a 100644 --- a/src/libzstd.jl +++ b/src/libzstd.jl @@ -77,6 +77,53 @@ function free!(cstream::CStream) return LibZstd.ZSTD_freeCStream(cstream) end +function setParameter!(ptr::Ptr{LibZstd.ZSTD_CStream}, param::LibZstd.ZSTD_cParameter, value::Integer) + code = LibZstd.ZSTD_CCtx_setParameter(ptr, param, value) + if iserror(code) + throw(ZstdError(code)) + end + return ptr +end +function setParameter!(cstream::CStream, param::LibZstd.ZSTD_cParameter, value::Integer) + setParameter!(cstream.ptr, param, value) + return cstream +end + +const _parameters_dict = Dict{Symbol, LibZstd.ZSTD_cParameter}( + :compressionLevel => LibZstd.ZSTD_c_compressionLevel, + :windowLog => LibZstd.ZSTD_c_windowLog, + :hashLog => LibZstd.ZSTD_c_hashLog, + :chainLog => LibZstd.ZSTD_c_chainLog, + :searchLog => LibZstd.ZSTD_c_searchLog, + :minMatch => LibZstd.ZSTD_c_minMatch, + :targetLength => LibZstd.ZSTD_c_targetLength, + :strategy => LibZstd.ZSTD_c_strategy, + :targetCBlockSize => LibZstd.ZSTD_c_targetCBlockSize, + :enableLongDistanceMatching => LibZstd.ZSTD_c_enableLongDistanceMatching, + :ldmHashLog => LibZstd.ZSTD_c_ldmHashLog, + :ldmMinMatch => LibZstd.ZSTD_c_ldmMinMatch, + :ldmBucketSizeLog => LibZstd.ZSTD_c_ldmBucketSizeLog, + :ldmHashRateLog => LibZstd.ZSTD_c_ldmHashRateLog, + :contentSizeFlag => LibZstd.ZSTD_c_contentSizeFlag, + :checksumFlag => LibZstd.ZSTD_c_checksumFlag, + :dictIDFlag => LibZstd.ZSTD_c_dictIDFlag, + :nbWorkers => LibZstd.ZSTD_c_nbWorkers, + :jobSize => LibZstd.ZSTD_c_jobSize, + :overlapLog => LibZstd.ZSTD_c_overlapLog +) + +function setParameter!(cstream::CStream, param::Symbol, value::Integer) + return setParameter!(cstream, _parameters_dict[param], value) +end + +function getParameter(ptr::Ptr{LibZstd.ZSTD_CStream}, param::LibZstd.ZSTD_cParameter) + value = Ref{Cint}(0) + LibZstd.ZSTD_CCtx_getParameter(ptr, param, value) + return value[] +end +getParameter(cstream::CStream, param::LibZstd.ZSTD_cParameter) = getParameter(cstream.ptr, param) +getParameter(cstream::CStream, param::Symbol) = getParameter(cstream, _parameters_dict[param]) + # ZSTD_DStream mutable struct DStream ptr::Ptr{LibZstd.ZSTD_DStream}