Skip to content

Commit 196c34d

Browse files
authored
Add ZstdCompressor (#180)
* Add ZstdCompressor * fix typo * Use decode!
1 parent 8d65246 commit 196c34d

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ version = "0.9.4"
66
[deps]
77
AWSS3 = "1c724243-ef5b-51ab-93f4-b0a88ac62a95"
88
Blosc = "a74b3585-a348-5f62-a45c-50e91977d574"
9+
ChunkCodecCore = "0b6fb165-00bc-4d37-ab8b-79f91016dbe1"
10+
ChunkCodecLibZstd = "55437552-ac27-4d47-9aa3-63184e8fd398"
911
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
1012
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1113
DateTimes64 = "b342263e-b350-472a-b1a9-8dfd21b51589"
@@ -22,6 +24,8 @@ ZipArchives = "49080126-0e18-4c2a-b176-c102e4b3760c"
2224
[compat]
2325
AWSS3 = "0.10, 0.11"
2426
Blosc = "0.5, 0.6, 0.7"
27+
ChunkCodecCore = "0.4.2"
28+
ChunkCodecLibZstd = "0.1.2"
2529
CodecZlib = "0.6, 0.7"
2630
DataStructures = "0.17, 0.18"
2731
DateTimes64 = "1"
@@ -32,4 +36,4 @@ OffsetArrays = "0.11, 1.0"
3236
OpenSSL = "1"
3337
URIs = "1"
3438
ZipArchives = "2"
35-
julia = "1.2"
39+
julia = "1.10"

docs/src/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Pages = ["ZGroup.jl"]
1818

1919
```@autodocs
2020
Modules = [Zarr]
21-
Pages = ["Compressors/Compressors.jl", "Compressors/blosc.jl", "Compressors/zlib.jl"]
21+
Pages = ["Compressors/Compressors.jl", "Compressors/blosc.jl", "Compressors/zlib.jl", "Compressors/zstd.jl"]
2222
```

src/Compressors/Compressors.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const compressortypes = Dict{Union{String,Nothing}, Type{<: Compressor}}()
4848
# Include the compressor implementations
4949
include("blosc.jl")
5050
include("zlib.jl")
51+
include("zstd.jl")
5152

5253
# ## Fallback definitions for the compressor interface
5354
# Define fallbacks and generic methods for the compressor interface

src/Compressors/zstd.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#=
2+
# Zstd compression
3+
4+
This file implements a Zstd compressor via ChunkCodecLibZstd.jl.
5+
6+
=#
7+
using ChunkCodecLibZstd: ZstdEncodeOptions
8+
using ChunkCodecCore: encode, decode, decode!
9+
10+
"""
11+
ZstdCompressor(;level=0, checksum=false)
12+
Returns a `ZstdCompressor` struct that can serve as a Zarr array compressor. Keyword arguments are:
13+
* `level=0`: the compression level, regular levels are 1 to 22, 0 is a special value for default, there are also even faster negative levels.
14+
* `checksum=false`: flag to enable saving checksums.
15+
"""
16+
struct ZstdCompressor <: Compressor
17+
config::ZstdEncodeOptions
18+
end
19+
20+
ZstdCompressor(;level=0, checksum::Bool=false) = ZstdCompressor(ZstdEncodeOptions(;compressionLevel=level, checksum))
21+
22+
function getCompressor(::Type{ZstdCompressor}, d::Dict)
23+
ZstdCompressor(;
24+
level=get(Returns(0), d, "level"),
25+
checksum=Bool(get(Returns(false), d, "checksum")),
26+
)
27+
end
28+
29+
function zuncompress(a, z::ZstdCompressor, T)
30+
result = decode(z.config.codec, a)
31+
_reinterpret(Base.nonmissingtype(T),result)
32+
end
33+
34+
function zuncompress!(data::DenseArray, compressed, z::ZstdCompressor)
35+
decode!(z.config.codec, reinterpret(UInt8, vec(data)), compressed)
36+
data
37+
end
38+
39+
function zcompress(a, z::ZstdCompressor)
40+
encode(z.config, reinterpret(UInt8, vec(a)))
41+
end
42+
43+
JSON.lower(z::ZstdCompressor) = Dict("id"=>"zstd", "level" => z.config.compressionLevel, "checksum" => z.config.checksum)
44+
45+
Zarr.compressortypes["zstd"] = ZstdCompressor

test/python.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ groupattrs = Dict("String attribute"=>"One", "Int attribute"=>5, "Float attribut
2121
g = zgroup(pjulia,attrs=groupattrs)
2222

2323
# Test all supported data types and compressors
24-
import Zarr: NoCompressor, BloscCompressor, ZlibCompressor, MaxLengthString,
24+
import Zarr: NoCompressor, BloscCompressor, ZlibCompressor, ZstdCompressor, MaxLengthString,
2525
Fletcher32Filter, FixedScaleOffsetFilter, ShuffleFilter, QuantizeFilter, DeltaFilter
2626
using Random: randstring
2727
numeric_dtypes = (UInt8, UInt16, UInt32, UInt64,
@@ -38,7 +38,9 @@ compressors = (
3838
"blosc_autoshuffle"=>BloscCompressor(cname="zstd",shuffle=-1),
3939
"blosc_noshuffle"=>BloscCompressor(cname="zstd",shuffle=0),
4040
"blosc_bitshuffle"=>BloscCompressor(cname="zstd",shuffle=2),
41-
"zlib"=>ZlibCompressor())
41+
"zlib"=>ZlibCompressor(),
42+
"zstd"=>ZstdCompressor(),
43+
)
4244
filters = (
4345
"fletcher32"=>Fletcher32Filter(),
4446
"scale_offset"=>FixedScaleOffsetFilter(offset=1000, scale=10^6, T=Float64, Tenc=Int32),

0 commit comments

Comments
 (0)