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

Use ZSTD_compressStream2 with endOp argument #49

Merged
merged 3 commits into from
May 19, 2024
Merged
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
33 changes: 31 additions & 2 deletions src/libzstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,37 @@ function reset!(cstream::CStream, srcsize::Integer)

end

function compress!(cstream::CStream)
return LibZstd.ZSTD_compressStream(cstream, cstream.obuffer, cstream.ibuffer)
"""
compress!(cstream::CStream; endOp=:continue)

Compress a Zstandard `CStream`. Optionally, provide one one of the following end directives
* `:continue` - (default) collect more data, encoder decides when to output compressed result, for optimal compression ratio
* `:flush` - flush any data provided so far
* `:end` - flush any remaining data _and_ close current frame

If `:end` is provided on the first call to `compress!`, the size of the input data will be recorded in the frame header.
This is equivalent to calling [`CodecZstd.LibZstd.ZSTD_compress2`](@ref).
See also [`CodecZstd.LibZstd.ZSTD_CCtx_setPledgedSrcSize`](@ref).

See the Zstd manual for additional details:
https://facebook.github.io/zstd/zstd_manual.html
"""
function compress!(cstream::CStream; endOp::Union{Symbol,LibZstd.ZSTD_EndDirective}=LibZstd.ZSTD_e_continue)
return LibZstd.ZSTD_compressStream2(cstream, cstream.obuffer, cstream.ibuffer, endOp)
end

# Support endOp keyword of compress! above when providing a Symbol
function Base.convert(::Type{LibZstd.ZSTD_EndDirective}, endOp::Symbol)
endOp = if endOp == :continue
LibZstd.ZSTD_e_continue
elseif endOp == :flush
LibZstd.ZSTD_e_flush
elseif endOp == :end
LibZstd.ZSTD_e_end
else
throw(ArgumentError("Received value `:$endOp` for `endOp`, but only :continue, :flush, or :end are allowed values."))
end
return endOp
end

function finish!(cstream::CStream)
Expand Down
61 changes: 61 additions & 0 deletions test/compress_endOp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using CodecZstd
using Test

@testset "compress! endOp = :continue" begin
data = rand(1:100, 1024*1024)
cstream = CodecZstd.CStream()
cstream.ibuffer.src = pointer(data)
cstream.ibuffer.size = sizeof(data)
cstream.ibuffer.pos = 0
cstream.obuffer.dst = Base.Libc.malloc(sizeof(data)*2)
cstream.obuffer.size = sizeof(data)*2
cstream.obuffer.pos = 0
try
GC.@preserve data begin
# default endOp
@test CodecZstd.compress!(cstream; endOp=:continue) == 0
@test CodecZstd.find_decompressed_size(cstream.obuffer.dst, cstream.obuffer.pos) == CodecZstd.ZSTD_CONTENTSIZE_UNKNOWN
end
finally
Base.Libc.free(cstream.obuffer.dst)
end
end

@testset "compress! endOp = :flush" begin
data = rand(1:100, 1024*1024)
cstream = CodecZstd.CStream()
cstream.ibuffer.src = pointer(data)
cstream.ibuffer.size = sizeof(data)
cstream.ibuffer.pos = 0
cstream.obuffer.dst = Base.Libc.malloc(sizeof(data)*2)
cstream.obuffer.size = sizeof(data)*2
cstream.obuffer.pos = 0
try
GC.@preserve data begin
@test CodecZstd.compress!(cstream; endOp=:flush) == 0
@test CodecZstd.find_decompressed_size(cstream.obuffer.dst, cstream.obuffer.pos) == CodecZstd.ZSTD_CONTENTSIZE_UNKNOWN
end
finally
Base.Libc.free(cstream.obuffer.dst)
end
end

@testset "compress! endOp = :end" begin
data = rand(1:100, 1024*1024)
cstream = CodecZstd.CStream()
cstream.ibuffer.src = pointer(data)
cstream.ibuffer.size = sizeof(data)
cstream.ibuffer.pos = 0
cstream.obuffer.dst = Base.Libc.malloc(sizeof(data)*2)
cstream.obuffer.size = sizeof(data)*2
cstream.obuffer.pos = 0
try
GC.@preserve data begin
# The frame should contain the decompressed size
@test CodecZstd.compress!(cstream; endOp=:end) == 0
@test CodecZstd.find_decompressed_size(cstream.obuffer.dst, cstream.obuffer.pos) == sizeof(data)
end
finally
Base.Libc.free(cstream.obuffer.dst)
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ using Test
TranscodingStreams.test_roundtrip_write(ZstdCompressorStream, ZstdDecompressorStream)
TranscodingStreams.test_roundtrip_lines(ZstdCompressorStream, ZstdDecompressorStream)
TranscodingStreams.test_roundtrip_transcode(ZstdCompressor, ZstdDecompressor)

include("compress_endOp.jl")
end