Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/LibZstd_clang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions src/libzstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down