Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a property interface to ZstdCompressor, add ZstdError type #51

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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,8 +3,41 @@

struct ZstdCompressor <: TranscodingStreams.Codec
cstream::CStream
level::Int
endOp::LibZstd.ZSTD_EndDirective
function ZstdCompressor(cstream, level, endOp)
self = new(cstream, endOp)
setParameter!(self.cstream, :compressionLevel, level)
return self
end
end

function Base.propertynames(::Type{ZstdCompressor})
(fieldnames(ZstdCompressor)..., :level, keys(_parameters_dict)...)

Check warning on line 15 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L14-L15

Added lines #L14 - L15 were not covered by tests
end
Base.propertynames(::ZstdCompressor) = propertynames(ZstdCompressor)

Check warning on line 17 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L17

Added line #L17 was not covered by tests
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"))

Check warning on line 28 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L28

Added line #L28 was not covered by tests
end
function Base.setproperty!(c::ZstdCompressor, p::Symbol, v::Integer)
if p == :level
p = :compressionLevel

Check warning on line 32 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L30-L32

Added lines #L30 - L32 were not covered by tests
end
if p in fieldnames(ZstdCompressor)
return setfield!(c, p, v)

Check warning on line 35 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L34-L35

Added lines #L34 - L35 were not covered by tests
end
if p in keys(_parameters_dict)
return setParameter!(c.cstream, p, v)

Check warning on line 38 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L37-L38

Added lines #L37 - L38 were not covered by tests
end
throw(ArgumentError("ZstdCompressor has no property $p"))

Check warning on line 40 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L40

Added line #L40 was not covered by tests
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 @@ -120,6 +120,53 @@
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))

Check warning on line 126 in src/libzstd.jl

View check run for this annotation

Codecov / codecov/patch

src/libzstd.jl#L126

Added line #L126 was not covered by tests
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