Skip to content

Commit

Permalink
Rename Compression to Compressor and so on. (#8)
Browse files Browse the repository at this point in the history
* rename compressors and decompressors

* add deprecations

* fix README.md

* fix test

* allow failures on nightly
  • Loading branch information
bicycle1885 authored Oct 17, 2017
1 parent ef15c7d commit a5dfdc2
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 154 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ os:
julia:
- 0.6
- nightly
matrix:
allow_failures:
- julia: nightly
notifications:
email: false
# uncomment the following lines to override the default test script
Expand All @@ -14,4 +17,4 @@ notifications:
# - julia -e 'Pkg.clone(pwd()); Pkg.build("CodecZlib"); Pkg.test("CodecZlib"; coverage=true)'
after_success:
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("CodecZlib")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- if [ $TRAVIS_JULIA_VERSION != "nightly" ]; then julia -e 'cd(Pkg.dir("CodecZlib")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Pkg.add("CodecZlib")

## Usage

**Note**: `GzipCompression`, `GzipDecompressionStream`, etc. are deprecated. Use
`GzipCompressor`, `GzipDecompressorStream`, etc. instead.

```julia
using CodecZlib

Expand All @@ -27,28 +30,28 @@ erat ex bibendum ipsum, sed varius ipsum ipsum vitae dui.
"""

# Streaming API.
stream = GzipCompressionStream(IOBuffer(text))
for line in eachline(GzipDecompressionStream(stream))
stream = GzipCompressorStream(IOBuffer(text))
for line in eachline(GzipDecompressorStream(stream))
println(line)
end
close(stream)

# Array API.
compressed = transcode(GzipCompression(), text)
compressed = transcode(GzipCompressor, text)
@assert sizeof(compressed) < sizeof(text)
@assert transcode(GzipDecompression(), compressed) == Vector{UInt8}(text)
@assert transcode(GzipDecompressor, compressed) == Vector{UInt8}(text)
```

This package exports following codecs and streams:

| Codec | Stream |
| ---------------------- | ---------------------------- |
| `GzipCompression` | `GzipCompressionStream` |
| `GzipDecompression` | `GzipDecompressionStream` |
| `ZlibCompression` | `ZlibCompressionStream` |
| `ZlibDecompression` | `ZlibDecompressionStream` |
| `DeflateCompression` | `DeflateCompressionStream` |
| `DeflateDecompression` | `DeflateDecompressionStream` |
| `GzipCompressor` | `GzipCompressorStream` |
| `GzipDecompressor` | `GzipDecompressorStream` |
| `ZlibCompressor` | `ZlibCompressorStream` |
| `ZlibDecompressor` | `ZlibDecompressorStream` |
| `DeflateCompressor` | `DeflateCompressorStream` |
| `DeflateDecompressor` | `DeflateDecompressorStream` |

See docstrings and [TranscodingStreams.jl](https://github.com/bicycle1885/TranscodingStreams.jl) for details.

Expand Down
40 changes: 28 additions & 12 deletions src/CodecZlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ module CodecZlib

export
# gzip
GzipCompression,
GzipCompressionStream,
GzipDecompression,
GzipDecompressionStream,
GzipCompressor,
GzipCompressorStream,
GzipDecompressor,
GzipDecompressorStream,

# zlib
ZlibCompression,
ZlibCompressionStream,
ZlibDecompression,
ZlibDecompressionStream,
ZlibCompressor,
ZlibCompressorStream,
ZlibDecompressor,
ZlibDecompressorStream,

# deflate
DeflateCompression,
DeflateCompressionStream,
DeflateDecompression,
DeflateDecompressionStream
DeflateCompressor,
DeflateCompressorStream,
DeflateDecompressor,
DeflateDecompressorStream

import TranscodingStreams:
TranscodingStreams,
Expand All @@ -43,4 +43,20 @@ include("libz.jl")
include("compression.jl")
include("decompression.jl")

# Deprecations
# ------------

@deprecate GzipCompression GzipCompressor
@deprecate GzipCompressionStream GzipCompressorStream
@deprecate GzipDecompression GzipDecompressor
@deprecate GzipDecompressionStream GzipDecompressorStream
@deprecate ZlibCompression ZlibCompressor
@deprecate ZlibCompressionStream ZlibCompressorStream
@deprecate ZlibDecompression ZlibDecompressor
@deprecate ZlibDecompressionStream ZlibDecompressorStream
@deprecate DeflateCompression DeflateCompressor
@deprecate DeflateCompressionStream DeflateCompressorStream
@deprecate DeflateDecompression DeflateDecompressor
@deprecate DeflateDecompressionStream DeflateDecompressorStream

end # module
68 changes: 34 additions & 34 deletions src/compression.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Compression Codecs
# Compressor Codecs
# ==================

abstract type CompressionCodec <: TranscodingStreams.Codec end
abstract type CompressorCodec <: TranscodingStreams.Codec end

function Base.show(io::IO, codec::CompressionCodec)
function Base.show(io::IO, codec::CompressorCodec)
print(io, summary(codec), "(level=$(codec.level), windowbits=$(codec.windowbits))")
end


# Gzip
# ----

struct GzipCompression <: CompressionCodec
struct GzipCompressor <: CompressorCodec
zstream::ZStream
level::Int
windowbits::Int
end

"""
GzipCompression(;level=$(Z_DEFAULT_COMPRESSION), windowbits=$(Z_DEFAULT_WINDOWBITS))
GzipCompressor(;level=$(Z_DEFAULT_COMPRESSION), windowbits=$(Z_DEFAULT_WINDOWBITS))
Create a gzip compression codec.
Expand All @@ -27,40 +27,40 @@ Arguments
- `level`: compression level (-1..9)
- `windowbits`: size of history buffer (8..15)
"""
function GzipCompression(;level::Integer=Z_DEFAULT_COMPRESSION,
function GzipCompressor(;level::Integer=Z_DEFAULT_COMPRESSION,
windowbits::Integer=Z_DEFAULT_WINDOWBITS)
if !(-1 level 9)
throw(ArgumentError("compression level must be within -1..9"))
elseif !(8 windowbits 15)
throw(ArgumentError("windowbits must be within 8..15"))
end
return GzipCompression(ZStream(), level, windowbits+16)
return GzipCompressor(ZStream(), level, windowbits+16)
end

const GzipCompressionStream{S} = TranscodingStream{GzipCompression,S} where S<:IO
const GzipCompressorStream{S} = TranscodingStream{GzipCompressor,S} where S<:IO

"""
GzipCompressionStream(stream::IO; kwargs...)
GzipCompressorStream(stream::IO; kwargs...)
Create a gzip compression stream (see `GzipCompression` for `kwargs`).
Create a gzip compression stream (see `GzipCompressor` for `kwargs`).
"""
function GzipCompressionStream(stream::IO; kwargs...)
function GzipCompressorStream(stream::IO; kwargs...)
x, y = splitkwargs(kwargs, (:level, :windowbits))
return TranscodingStream(GzipCompression(;x...), stream; y...)
return TranscodingStream(GzipCompressor(;x...), stream; y...)
end


# Zlib
# ----

struct ZlibCompression <: CompressionCodec
struct ZlibCompressor <: CompressorCodec
zstream::ZStream
level::Int
windowbits::Int
end

"""
ZlibCompression(;level=$(Z_DEFAULT_COMPRESSION), windowbits=$(Z_DEFAULT_WINDOWBITS))
ZlibCompressor(;level=$(Z_DEFAULT_COMPRESSION), windowbits=$(Z_DEFAULT_WINDOWBITS))
Create a zlib compression codec.
Expand All @@ -69,40 +69,40 @@ Arguments
- `level`: compression level (-1..9)
- `windowbits`: size of history buffer (8..15)
"""
function ZlibCompression(;level::Integer=Z_DEFAULT_COMPRESSION,
function ZlibCompressor(;level::Integer=Z_DEFAULT_COMPRESSION,
windowbits::Integer=Z_DEFAULT_WINDOWBITS)
if !(-1 level 9)
throw(ArgumentError("compression level must be within -1..9"))
elseif !(8 windowbits 15)
throw(ArgumentError("windowbits must be within 8..15"))
end
return ZlibCompression(ZStream(), level, windowbits)
return ZlibCompressor(ZStream(), level, windowbits)
end

const ZlibCompressionStream{S} = TranscodingStream{ZlibCompression,S} where S<:IO
const ZlibCompressorStream{S} = TranscodingStream{ZlibCompressor,S} where S<:IO

"""
ZlibCompressionStream(stream::IO)
ZlibCompressorStream(stream::IO)
Create a zlib compression stream (see `ZlibCompression` for `kwargs`).
Create a zlib compression stream (see `ZlibCompressor` for `kwargs`).
"""
function ZlibCompressionStream(stream::IO; kwargs...)
function ZlibCompressorStream(stream::IO; kwargs...)
x, y = splitkwargs(kwargs, (:level, :windowbits))
return TranscodingStream(ZlibCompression(;x...), stream; y...)
return TranscodingStream(ZlibCompressor(;x...), stream; y...)
end


# Deflate
# -------

struct DeflateCompression <: CompressionCodec
struct DeflateCompressor <: CompressorCodec
zstream::ZStream
level::Int
windowbits::Int
end

"""
DeflateCompression(;level=$(Z_DEFAULT_COMPRESSION), windowbits=$(Z_DEFAULT_COMPRESSION))
DeflateCompressor(;level=$(Z_DEFAULT_COMPRESSION), windowbits=$(Z_DEFAULT_COMPRESSION))
Create a deflate compression codec.
Expand All @@ -111,41 +111,41 @@ Arguments
- `level`: compression level (-1..9)
- `windowbits`: size of history buffer (8..15)
"""
function DeflateCompression(;level::Integer=Z_DEFAULT_COMPRESSION,
function DeflateCompressor(;level::Integer=Z_DEFAULT_COMPRESSION,
windowbits::Integer=Z_DEFAULT_WINDOWBITS)
if !(-1 level 9)
throw(ArgumentError("compression level must be within -1..9"))
elseif !(8 windowbits 15)
throw(ArgumentError("windowbits must be within 8..15"))
end
return DeflateCompression(ZStream(), level, -Int(windowbits))
return DeflateCompressor(ZStream(), level, -Int(windowbits))
end

const DeflateCompressionStream{S} = TranscodingStream{DeflateCompression,S} where S<:IO
const DeflateCompressorStream{S} = TranscodingStream{DeflateCompressor,S} where S<:IO

"""
DeflateCompressionStream(stream::IO; kwargs...)
DeflateCompressorStream(stream::IO; kwargs...)
Create a deflate compression stream (see `DeflateCompression` for `kwargs`).
Create a deflate compression stream (see `DeflateCompressor` for `kwargs`).
"""
function DeflateCompressionStream(stream::IO; kwargs...)
function DeflateCompressorStream(stream::IO; kwargs...)
x, y = splitkwargs(kwargs, (:level, :windowbits))
return TranscodingStream(DeflateCompression(;x...), stream; y...)
return TranscodingStream(DeflateCompressor(;x...), stream; y...)
end


# Methods
# -------

function TranscodingStreams.initialize(codec::CompressionCodec)
function TranscodingStreams.initialize(codec::CompressorCodec)
code = deflate_init!(codec.zstream, codec.level, codec.windowbits)
if code != Z_OK
zerror(codec.zstream, code)
end
return
end

function TranscodingStreams.finalize(codec::CompressionCodec)
function TranscodingStreams.finalize(codec::CompressorCodec)
zstream = codec.zstream
if zstream.state != C_NULL
code = deflate_end!(zstream)
Expand All @@ -156,7 +156,7 @@ function TranscodingStreams.finalize(codec::CompressionCodec)
return
end

function TranscodingStreams.startproc(codec::CompressionCodec, state::Symbol, error::Error)
function TranscodingStreams.startproc(codec::CompressorCodec, state::Symbol, error::Error)
code = deflate_reset!(codec.zstream)
if code == Z_OK
return :ok
Expand All @@ -166,7 +166,7 @@ function TranscodingStreams.startproc(codec::CompressionCodec, state::Symbol, er
end
end

function TranscodingStreams.process(codec::CompressionCodec, input::Memory, output::Memory, error::Error)
function TranscodingStreams.process(codec::CompressorCodec, input::Memory, output::Memory, error::Error)
zstream = codec.zstream
zstream.next_in = input.ptr
zstream.avail_in = input.size
Expand Down
Loading

0 comments on commit a5dfdc2

Please sign in to comment.