-
Notifications
You must be signed in to change notification settings - Fork 143
Add bzip2, lz4, and zstd independent compression filter packages #880
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
Merged
musm
merged 30 commits into
JuliaIO:master
from
JaneliaSciComp:janelia/h5z_subdir_packages
Dec 17, 2021
+1,411
−103
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
d7bcada
Add bzip2, lz4, and zstd filters from HDF5Plugins.jl
mkitti 3561115
Create Filters interface def, add tests
mkitti b7b7f24
Filters return 0 rather than rethrowing error
mkitti a5100e5
Move blosc.jl to H5Zblosc.jl
mkitti 424de91
Optimize method inference, init, precompile
mkitti cbfb653
Register filters as when dep packages are loaded
mkitti 58f8a7c
Fix tests for lazy loaded filters
mkitti 55bb1c5
Move plugin licenses to licenses folder
mkitti 9a8f3e9
Add HHMI copyright to LICENSE.txt
mkitti b215ded
Remove generic register_filter method
mkitti 26dbde0
Apply suggestions from code review for license notices
mkitti 0e011dd
Implement generic register_filter on filter type
mkitti b253de1
Update test/filter.jl
mkitti 84085b4
Define filters API on the type alone
mkitti d6d7fd4
H5Zblosc, H5Zbzip2, H5Zlz4, H5Zzstd as subdir pkgs
mkitti b46117a
Fix typos in H5Zlz4.jl
mkitti bc9af91
Use PackageSpec to make dev_embedded_filters Julia 1.3 compatible
mkitti 12a613b
Add Licenses to subpackages
mkitti 4aca896
H5Zbzip2: Add 32-bit Windows support
mkitti 0bee6b9
Reorganized licenses, create root THIRDPARTY.md
mkitti fd1eccd
Debug tests for Julia 1.8
mkitti 73ceacc
Revert GC.@preserve due to https://github.com/JuliaLang/julia/pull/43408
mkitti d4cf627
Remove debug for not deleted file in test/external.jl
mkitti e344882
Start H5Z* pkgs at 0.1.0
mkitti e23d739
Update Project.toml
mkitti ed1b342
Update Project.toml
musm 55afa4f
Add filters as test targets
musm 393711b
Update Project.toml
musm b2e16d3
Cosmetic changes and instantiate filters packages directly in test file
musm 82fba8e
Update docs remove external exports
musm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Third Party Licenses | ||
|
||
HDF5.jl contains several derivative works of open source software. | ||
|
||
In particular, the following submodules are licensed as derivative works from third-parties. | ||
Original and derivative code in HDF5.jl is licensed according to [LICENSE.txt](LICENSE.txt) | ||
as permitted by licenses for the original software from which they may be derived. | ||
See the files indicated below for the copyright notices and the licenses of the original | ||
software from which individual submodules are derived. | ||
|
||
## Filter Plugins | ||
* [H5Zbzip2](src/filters/H5Zbzip2/src/H5Zbzip2.jl): See [src/filters/H5Zbzip2/THIRDPARTY.txt](src/filters/H5Zbzip2/THIRDPARTY.txt) | ||
* [H5Zlz4](src/filters/H5Zlz4/src/H5Zlz4.jl): See [src/filters/H5Zlz4/THIRDPARTY.txt](src/filters/H5Zlz4/THIRDPARTY.txt) | ||
* [H5Zzstd](src/filters/H5Zzstd/src/H5Zzstd.jl): See [src/filters/H5Zzstd/THIRDPARTY.txt](src/filters/H5Zzstd/THIRDPARTY.txt) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2012-2021: Timothy E. Holy, Simon Kornblith, and contributors: https://github.com/JuliaIO/HDF5.jl/contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software | ||
and associated documentation files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or | ||
substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | ||
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name = "H5Zblosc" | ||
uuid = "c8ec2601-a99c-407f-b158-e79c03c2f5f7" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
Blosc = "a74b3585-a348-5f62-a45c-50e91977d574" | ||
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" | ||
|
||
[compat] | ||
HDF5 = "0.16" | ||
Blosc = "0.7.1" | ||
julia = "1.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2012-2021: Timothy E. Holy, Simon Kornblith, and contributors: https://github.com/JuliaIO/HDF5.jl/contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software | ||
and associated documentation files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or | ||
substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | ||
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name = "H5Zbzip2" | ||
uuid = "094576f2-1e46-4c84-8e32-c46c042eaaa2" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd" | ||
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" | ||
|
||
[compat] | ||
HDF5 = "0.16" | ||
CodecBzip2 = "0.7" | ||
julia = "1.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
H5Z_filter_bzip2 in H5Zbzip2.jl was derived from H5Zbzip2.c from PyTables: | ||
|
||
Copyright Notice and Statement for PyTables Software Library and Utilities: | ||
Copyright (c) 2002-2004 by Francesc Alted | ||
Copyright (c) 2005-2007 by Carabos Coop. V. | ||
Copyright (c) 2008-2010 by Francesc Alted | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
a. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
b. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the | ||
distribution. | ||
c. Neither the name of Francesc Alted nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
#= | ||
The code below has been ported to Julia from the original C source: | ||
https://github.com/nexusformat/HDF5-External-Filter-Plugins/blob/master/BZIP2/src/H5Zbzip2.c | ||
The filter function H5Z_filter_bzip2 was adopted from: | ||
PyTables http://www.pytables.org. | ||
The plugin can be used with the HDF5 library version 1.8.11+ to read HDF5 datasets compressed with bzip2 created by PyTables. | ||
License: licenses/H5Zbzip2_LICENSE.txt | ||
The following license applies to the Julia port. | ||
Copyright (c) 2021 Mark Kittisopikul and Howard Hughes Medical Institute. License MIT, see LICENSE.txt | ||
=# | ||
module H5Zbzip2 | ||
|
||
using CodecBzip2 | ||
import CodecBzip2: libbzip2 | ||
using HDF5.API | ||
import HDF5.Filters: Filter, filterid, register_filter, filtername, filter_func, filter_cfunc | ||
|
||
export H5Z_FILTER_BZIP2, H5Z_filter_bzip2, Bzip2Filter | ||
|
||
|
||
const H5Z_FILTER_BZIP2 = API.H5Z_filter_t(307) | ||
const bzip2_name = "HDF5 bzip2 filter; see http://www.hdfgroup.org/services/contributions.html" | ||
|
||
function H5Z_filter_bzip2(flags::Cuint, cd_nelmts::Csize_t, | ||
cd_values::Ptr{Cuint}, nbytes::Csize_t, | ||
buf_size::Ptr{Csize_t}, buf::Ptr{Ptr{Cvoid}})::Csize_t | ||
outbuf = C_NULL | ||
outdatalen = Cuint(0) | ||
|
||
# Prepare the output buffer | ||
|
||
try | ||
|
||
if flags & API.H5Z_FLAG_REVERSE != 0 | ||
# Decompress | ||
|
||
outbuflen = nbytes * 3 + 1 | ||
outbuf = Libc.malloc(outbuflen) | ||
if outbuf == C_NULL | ||
error("H5Zbzip2: memory allocation failed for bzip2 decompression.") | ||
end | ||
|
||
stream = CodecBzip2.BZStream() | ||
# Just use default malloc and free | ||
stream.bzalloc = C_NULL | ||
stream.bzfree = C_NULL | ||
# BZ2_bzDecompressInit | ||
ret = CodecBzip2.decompress_init!(stream, 0, false) | ||
if ret != CodecBzip2.BZ_OK | ||
errror("H5Zbzip2: bzip2 decompress start failed with error $ret.") | ||
end | ||
|
||
stream.next_out = outbuf | ||
stream.avail_out = outbuflen | ||
stream.next_in = unsafe_load(buf) | ||
stream.avail_in = nbytes | ||
|
||
cont = true | ||
|
||
while cont | ||
# BZ2_bzDecompress | ||
ret = CodecBzip2.decompress!(stream) | ||
if ret < 0 | ||
error("H5Zbzip2: bzip2 decompression failed with error $ret.") | ||
end | ||
cont = ret != CodecBzip2.BZ_STREAM_END | ||
if cont && stream.avail_out == 0 | ||
# Grow the output buffer | ||
newbuflen = outbuflen * 2 | ||
newbuf = Libc.realloc(outbuf, newbuflen) | ||
if newbuf == C_NULL | ||
error("H5Zbzip2: memory allocation failed for bzip2 decompression.") | ||
end | ||
stream.next_out = newbuf + outbuflen | ||
stream.avail_out = outbuflen | ||
outbuf = newbuf | ||
outbuflen = newbuflen | ||
end | ||
end | ||
|
||
outdatalen = stream.total_out_lo32 | ||
# BZ2_bzDecompressEnd | ||
ret = CodecBzip2.decompress_end!(stream) | ||
if ret != CodecBzip2.BZ_OK | ||
error("H5Zbzip2: bzip2 compression end failed with error $ret.") | ||
end | ||
else | ||
# Compress data | ||
|
||
# Maybe not the same size as outdatalen | ||
odatalen = Cuint(0) | ||
blockSize100k = 9 | ||
|
||
# Get compression blocksize if present | ||
if cd_nelmts > 0 | ||
blockSize100k = unsafe_load(cd_values) | ||
if blockSize100k < 1 || blockSize100k > 9 | ||
error("H5Zbzip2: Invalid compression blocksize: $blockSize100k") | ||
end | ||
end | ||
|
||
# Prepare the output buffer | ||
outbuflen = nbytes + nbytes ÷ 100 + 600 # worse case (bzip2 docs) | ||
outbuf = Libc.malloc(outbuflen) | ||
@debug "Allocated" outbuflen outbuf | ||
if outbuf == C_NULL | ||
error("H5Zbzip2: Memory allocation failed for bzip2 compression") | ||
end | ||
|
||
# Compress data | ||
odatalen = outbuflen | ||
r_odatalen = Ref{Cuint}(odatalen) | ||
ret = BZ2_bzBuffToBuffCompress(outbuf, r_odatalen, unsafe_load(buf), nbytes, | ||
blockSize100k, 0, 0) | ||
outdatalen = r_odatalen[] | ||
if ret != CodecBzip2.BZ_OK | ||
error("H5Zbzip2: bzip2 compression failed with error $ret.") | ||
end | ||
end # if flags & API.H5Z_FLAG_REVERSE != 0 | ||
Libc.free(unsafe_load(buf)) | ||
unsafe_store!(buf, outbuf) | ||
unsafe_store!(buf_size, outbuflen) | ||
|
||
catch err | ||
# "In the case of failure, the return value is 0 (zero) and all pointer arguments are left unchanged." | ||
outdatalen = Csize_t(0) | ||
if outbuf != C_NULL | ||
Libc.free(outbuf) | ||
end | ||
@error "H5Zbzip2.jl Non-Fatal ERROR: " err | ||
display(stacktrace(catch_backtrace())) | ||
end # try - catch | ||
|
||
return Csize_t(outdatalen) | ||
end # function H5Z_filter_bzip2 | ||
|
||
# Need stdcall for 32-bit Windows? | ||
function BZ2_bzBuffToBuffCompress(dest, destLen, source, sourceLen, blockSize100k, verbosity, workFactor) | ||
@static if CodecBzip2.WIN32 | ||
return ccall( | ||
("BZ2_bzBuffToBuffCompress@28", libbzip2), | ||
stdcall, | ||
Cint, | ||
(Ptr{Cchar}, Ptr{Cuint}, Ptr{Cchar}, Cuint, Cint, Cint, Cint), | ||
dest, | ||
destLen, | ||
source, | ||
sourceLen, | ||
blockSize100k, | ||
verbosity, | ||
workFactor | ||
) | ||
else | ||
return ccall( | ||
(:BZ2_bzBuffToBuffCompress, libbzip2), | ||
Cint, | ||
(Ptr{Cchar}, Ptr{Cuint}, Ptr{Cchar}, Cuint, Cint, Cint, Cint), | ||
dest, | ||
destLen, | ||
source, | ||
sourceLen, | ||
blockSize100k, | ||
verbosity, | ||
workFactor | ||
) | ||
end | ||
end | ||
|
||
function BZ2_bzBuffToBuffDecompress(dest, destLen, source, sourceLen, small, verbosity) | ||
@static if CodecBzip2.WIN32 | ||
return ccall( | ||
("BZ2_bzBuffToBuffDecompress@24", libbzip2), | ||
stdcall, | ||
Cint, | ||
(Ptr{Cchar}, Ptr{Cuint}, Ptr{Cchar}, Cuint, Cint, Cint), | ||
dest, | ||
destLen, | ||
source, | ||
sourceLen, | ||
small, | ||
verbosity | ||
) | ||
else | ||
return ccall( | ||
(:BZ2_bzBuffToBuffDecompress, libbzip2), | ||
Cint, | ||
(Ptr{Cchar}, Ptr{Cuint}, Ptr{Cchar}, Cuint, Cint, Cint), | ||
dest, | ||
destLen, | ||
source, | ||
sourceLen, | ||
small, | ||
verbosity | ||
) | ||
end | ||
end | ||
|
||
# Filters Module | ||
|
||
struct Bzip2Filter <: Filter | ||
blockSize100k::Cuint | ||
end | ||
Bzip2Filter() = Bzip2Filter(9) | ||
|
||
filterid(::Type{Bzip2Filter}) = H5Z_FILTER_BZIP2 | ||
filtername(::Type{Bzip2Filter}) = bzip2_name | ||
filter_func(::Type{Bzip2Filter}) = H5Z_filter_bzip2 | ||
filter_cfunc(::Type{Bzip2Filter}) = @cfunction(H5Z_filter_bzip2, Csize_t, | ||
(Cuint, Csize_t, Ptr{Cuint}, Csize_t, | ||
Ptr{Csize_t}, Ptr{Ptr{Cvoid}})) | ||
|
||
function __init__() | ||
register_filter(Bzip2Filter) | ||
end | ||
|
||
end # module H5Zbzip2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2012-2021: Timothy E. Holy, Simon Kornblith, and contributors: https://github.com/JuliaIO/HDF5.jl/contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software | ||
and associated documentation files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or | ||
substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | ||
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name = "H5Zlz4" | ||
uuid = "eb20ec05-5464-47b5-ba41-098e3c1068a3" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
CodecLz4 = "5ba52731-8f18-5e0d-9241-30f10d1ec561" | ||
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" | ||
|
||
[compat] | ||
HDF5 = "0.16" | ||
CodecLz4 = "0.4" | ||
julia = "1.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
H5Zlz4.jl is derived from H5Zlz4.c from HDF5 Group | ||
|
||
================================================================================ | ||
LZ4 filter plugin license | ||
================================================================================ | ||
|
||
Copyright Notice and License Terms for | ||
HDF5 LZ4 compression filter plugin | ||
----------------------------------------------------------------------------- | ||
|
||
HDF5 LZ4 compression filter plugin | ||
Copyright 2013-2015 by The HDF Group. | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted for any purpose (including commercial purposes) | ||
provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, | ||
this list of conditions, and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions, and the following disclaimer in the documentation | ||
and/or materials provided with the distribution. | ||
|
||
3. In addition, redistributions of modified forms of the source or binary | ||
code must carry prominent notices stating that the original code was | ||
changed and the date of the change. | ||
|
||
4. All publications or advertising materials mentioning features or use of | ||
this software are asked, but not required, to acknowledge that it was | ||
developed by The HDF Group and credit the contributors. | ||
|
||
5. Neither the name of The HDF Group, nor the name of any Contributor may | ||
be used to endorse or promote products derived from this software | ||
without specific prior written permission from The HDF Group or the | ||
Contributor, respectively. | ||
|
||
DISCLAIMER: | ||
THIS SOFTWARE IS PROVIDED BY THE HDF GROUP AND THE CONTRIBUTORS | ||
"AS IS" WITH NO WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. In no | ||
event shall The HDF Group or the Contributors be liable for any damages | ||
suffered by the users arising out of the use of this software, even if | ||
advised of the possibility of such damage. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
#= | ||
This is a port of H5Zlz4.c to Julia | ||
https://github.com/HDFGroup/hdf5_plugins/blob/master/LZ4/src/H5Zlz4.c | ||
https://github.com/nexusformat/HDF5-External-Filter-Plugins/blob/master/LZ4/src/H5Zlz4.c | ||
https://github.com/silx-kit/hdf5plugin/blob/main/src/LZ4/H5Zlz4.c | ||
H5Zlz4 is originally a copyright of HDF Group. License: licenses/H5Zlz4_LICENSE.txt | ||
The following license applies to the Julia port. | ||
Copyright (c) 2021 Mark Kittisopikul and Howard Hughes Medical Institute. License MIT, see LICENSE.txt | ||
=# | ||
module H5Zlz4 | ||
|
||
using CodecLz4 | ||
using HDF5.API | ||
import HDF5.Filters: Filter, filterid, register_filter, filtername, filter_func, filter_cfunc | ||
|
||
|
||
export H5Z_FILTER_LZ4, H5Z_filter_lz4, Lz4Filter | ||
|
||
const H5Z_FILTER_LZ4 = API.H5Z_filter_t(32004) | ||
|
||
const DEFAULT_BLOCK_SIZE = 1 << 30; | ||
const lz4_name = "HDF5 lz4 filter; see http://www.hdfgroup.org/services/contributions.html" | ||
|
||
const LZ4_AGGRESSION = Ref(1) | ||
|
||
|
||
|
||
# flags H5Z_FLAG_REVERSE or H5Z_FLAG_OPTIONAL | ||
# cd_nelmts number of elements in cd_values (0 or 1) | ||
# cd_values the first optional element must be the blockSize | ||
# nbytes - number of valid bytes of data | ||
# buf_size - total size of buffer | ||
# buf - pointer to pointer of data | ||
function H5Z_filter_lz4(flags::Cuint, cd_nelmts::Csize_t, | ||
cd_values::Ptr{Cuint}, nbytes::Csize_t, | ||
buf_size::Ptr{Csize_t}, buf::Ptr{Ptr{Cvoid}})::Csize_t | ||
|
||
outBuf = C_NULL | ||
ret_value = Csize_t(0) | ||
|
||
try | ||
|
||
if (flags & API.H5Z_FLAG_REVERSE) != 0 # reverse filter, decompressing | ||
#i32Buf = Ref{UInt32}() | ||
blockSize = UInt32(0) | ||
roBuf = Ref{UInt8}() | ||
rpos = Ptr{UInt8}(unsafe_load(buf)) | ||
#i64Buf = Ptr{UInt64}(rpos) | ||
# Load the first 8 bytes from buffer as a big endian UInt64 | ||
# This is the original size of the buffer | ||
origSize = ntoh(unsafe_load(Ptr{UInt64}(rpos))) | ||
rpos += 8 # advance the pointer | ||
|
||
# Next read the next four bytes from the buffer as a big endian UInt32 | ||
# This is the blocksize | ||
#i32Buf[] = rpos | ||
blockSize = ntoh(unsafe_load(Ptr{UInt32}(rpos))) | ||
rpos += 4 | ||
if blockSize > origSize | ||
blockSize = origSize | ||
end | ||
|
||
# malloc a byte buffer of origSize | ||
# outBuf = Vector{UInt8}(undef, origSize) | ||
@debug "OrigSize" origSize | ||
outBuf = Libc.malloc(origSize) | ||
# Julia should throw an error if it cannot allocate this | ||
roBuf = Ptr{UInt8}(outBuf) | ||
decompSize = 0 | ||
# Start with the first blockSize | ||
while decompSize < origSize | ||
# compressedBlockSize = UInt32(0) | ||
if origSize - decompSize < blockSize # the last block can be smaller than block size | ||
blockSize = origSize - decompSize | ||
end | ||
|
||
#i32Buf[] = rpos | ||
compressedBlockSize = ntoh(unsafe_load(Ptr{UInt32}(rpos))) | ||
rpos += 4 | ||
|
||
if compressedBlockSize == blockSize | ||
# There was no compression | ||
# memcpy(roBuf, rpos, blockSize) | ||
unsafe_copyto!(roBuf, rpos, blockSize) | ||
decompressedBytes = blockSize | ||
else | ||
# do the compression | ||
# LZ4_decompress_fast, version number 10300 ? | ||
@debug "decompress_safe" rpos roBuf compressedBlockSize (origSize - decompSize) | ||
decompressedBytes = CodecLz4.LZ4_decompress_safe(rpos, roBuf, compressedBlockSize, origSize -decompSize) | ||
@debug "decompressedBytes" decompressedBytes | ||
end | ||
|
||
rpos += compressedBlockSize | ||
roBuf += blockSize | ||
decompSize += decompressedBytes | ||
end | ||
Libc.free(unsafe_load(buf)) | ||
unsafe_store!(buf, outBuf) | ||
outBuf = C_NULL | ||
ret_value = Csize_t(origSize) | ||
else | ||
# forward filter | ||
# compressing | ||
#i64Buf = Ref{UInt64}() | ||
#i32Buf = Ref{UInt32}() | ||
|
||
if nbytes > typemax(Int32) | ||
error("Can only compress chunks up to 2GB") | ||
end | ||
blockSize = unsafe_load(cd_values) | ||
if cd_nelmts > 0 && blockSize > 0 | ||
else | ||
blockSize = DEFAULT_BLOCK_SIZE | ||
end | ||
if blockSize > nbytes | ||
blockSize = nbytes | ||
end | ||
nBlocks = (nbytes-1) ÷ blockSize + 1 | ||
maxDestSize = nBlocks * CodecLz4.LZ4_compressBound(blockSize) + 4 + 8 + nBlocks*4 | ||
outBuf = Libc.malloc(maxDestSize) | ||
|
||
rpos = Ptr{UInt8}(unsafe_load(buf)) | ||
roBuf = Ptr{UInt8}(outBuf) | ||
|
||
# Header | ||
unsafe_store!(Ptr{UInt64}(roBuf), hton(UInt64(nbytes))) | ||
roBuf += 8 | ||
|
||
unsafe_store!(Ptr{UInt32}(roBuf), hton(UInt32(blockSize))) | ||
roBuf += 4 | ||
|
||
outSize = 12 | ||
|
||
for block = 0:nBlocks-1 | ||
# compBlockSize::UInt32 | ||
origWritten = Csize_t(block*blockSize) | ||
if nbytes - origWritten < blockSize # the last block may be < blockSize | ||
blockSize = nbytes - origWritten | ||
end | ||
|
||
# aggression = 1 is the same LZ4_compress_default | ||
@debug "LZ4_compress_fast args" rpos outBuf roBuf roBuf+4 blockSize nBlocks CodecLz4.LZ4_compressBound(blockSize) | ||
compBlockSize = UInt32(CodecLz4.LZ4_compress_fast(rpos, roBuf+4, blockSize, CodecLz4.LZ4_compressBound(blockSize), LZ4_AGGRESSION[])) | ||
@debug "Compressed block size" compBlockSize | ||
|
||
if compBlockSize == 0 | ||
error("Could not compress block $block") | ||
end | ||
|
||
if compBlockSize >= blockSize # compression did not save any space, do a memcpy instead | ||
compBlockSize = blockSize | ||
unsafe_copyto!(roBuf+4, rpos, blockSize) | ||
end | ||
|
||
unsafe_store!(Ptr{UInt32}(roBuf), hton(UInt32(compBlockSize))) # write blocksize | ||
roBuf += 4 | ||
|
||
rpos += blockSize | ||
roBuf += compBlockSize | ||
outSize += compBlockSize + 4 | ||
end | ||
|
||
Libc.free(unsafe_load(buf)) | ||
unsafe_store!(buf, outBuf) | ||
unsafe_store!(buf_size, outSize) | ||
outBuf = C_NULL | ||
ret_value = Csize_t(outSize) | ||
end # (flags & API.H5Z_FLAG_REVERSE) != 0 | ||
|
||
catch err | ||
# "In the case of failure, the return value is 0 (zero) and all pointer arguments are left unchanged." | ||
ret_value = Csize_t(0) | ||
@error "H5Zlz4.jl Non-Fatal ERROR: " err | ||
display(stacktrace(catch_backtrace())) | ||
finally | ||
if outBuf != C_NULL | ||
Libc.free(outBuf) | ||
end | ||
end | ||
return Csize_t(ret_value) | ||
end | ||
|
||
# Filters Module | ||
|
||
struct Lz4Filter <: Filter | ||
blockSize::Cuint | ||
end | ||
Lz4Filter() = Lz4Filter(DEFAULT_BLOCK_SIZE) | ||
|
||
filterid(::Type{Lz4Filter}) = H5Z_FILTER_LZ4 | ||
filtername(::Type{Lz4Filter}) = lz4_name | ||
filter_func(::Type{Lz4Filter}) = H5Z_filter_lz4 | ||
filter_cfunc(::Type{Lz4Filter}) = @cfunction(H5Z_filter_lz4, Csize_t, | ||
(Cuint, Csize_t, Ptr{Cuint}, Csize_t, | ||
Ptr{Csize_t}, Ptr{Ptr{Cvoid}})) | ||
|
||
function __init__() | ||
register_filter(Lz4Filter) | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2012-2021: Timothy E. Holy, Simon Kornblith, and contributors: https://github.com/JuliaIO/HDF5.jl/contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software | ||
and associated documentation files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or | ||
substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | ||
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name = "H5Zzstd" | ||
uuid = "f6f2d980-1ec6-471c-a70d-0270e22f1103" | ||
version = "0.1.0" | ||
|
||
[deps] | ||
CodecZstd = "6b39b394-51ab-5f42-8807-6242bab2b4c2" | ||
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" | ||
|
||
[compat] | ||
HDF5 = "0.16" | ||
CodecZstd = "0.7" | ||
julia = "1.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
H5Zzstd.jl is derived from | ||
https://github.com/aparamon/HDF5Plugin-Zstandard, zstd_h5plugin.c | ||
under Apache License Version 2.0: | ||
|
||
Apache License | ||
Version 2.0, January 2004 | ||
http://www.apache.org/licenses/ | ||
|
||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
|
||
1. Definitions. | ||
|
||
"License" shall mean the terms and conditions for use, reproduction, | ||
and distribution as defined by Sections 1 through 9 of this document. | ||
|
||
"Licensor" shall mean the copyright owner or entity authorized by | ||
the copyright owner that is granting the License. | ||
|
||
"Legal Entity" shall mean the union of the acting entity and all | ||
other entities that control, are controlled by, or are under common | ||
control with that entity. For the purposes of this definition, | ||
"control" means (i) the power, direct or indirect, to cause the | ||
direction or management of such entity, whether by contract or | ||
otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||
outstanding shares, or (iii) beneficial ownership of such entity. | ||
|
||
"You" (or "Your") shall mean an individual or Legal Entity | ||
exercising permissions granted by this License. | ||
|
||
"Source" form shall mean the preferred form for making modifications, | ||
including but not limited to software source code, documentation | ||
source, and configuration files. | ||
|
||
"Object" form shall mean any form resulting from mechanical | ||
transformation or translation of a Source form, including but | ||
not limited to compiled object code, generated documentation, | ||
and conversions to other media types. | ||
|
||
"Work" shall mean the work of authorship, whether in Source or | ||
Object form, made available under the License, as indicated by a | ||
copyright notice that is included in or attached to the work | ||
(an example is provided in the Appendix below). | ||
|
||
"Derivative Works" shall mean any work, whether in Source or Object | ||
form, that is based on (or derived from) the Work and for which the | ||
editorial revisions, annotations, elaborations, or other modifications | ||
represent, as a whole, an original work of authorship. For the purposes | ||
of this License, Derivative Works shall not include works that remain | ||
separable from, or merely link (or bind by name) to the interfaces of, | ||
the Work and Derivative Works thereof. | ||
|
||
"Contribution" shall mean any work of authorship, including | ||
the original version of the Work and any modifications or additions | ||
to that Work or Derivative Works thereof, that is intentionally | ||
submitted to Licensor for inclusion in the Work by the copyright owner | ||
or by an individual or Legal Entity authorized to submit on behalf of | ||
the copyright owner. For the purposes of this definition, "submitted" | ||
means any form of electronic, verbal, or written communication sent | ||
to the Licensor or its representatives, including but not limited to | ||
communication on electronic mailing lists, source code control systems, | ||
and issue tracking systems that are managed by, or on behalf of, the | ||
Licensor for the purpose of discussing and improving the Work, but | ||
excluding communication that is conspicuously marked or otherwise | ||
designated in writing by the copyright owner as "Not a Contribution." | ||
|
||
"Contributor" shall mean Licensor and any individual or Legal Entity | ||
on behalf of whom a Contribution has been received by Licensor and | ||
subsequently incorporated within the Work. | ||
|
||
2. Grant of Copyright License. Subject to the terms and conditions of | ||
this License, each Contributor hereby grants to You a perpetual, | ||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
copyright license to reproduce, prepare Derivative Works of, | ||
publicly display, publicly perform, sublicense, and distribute the | ||
Work and such Derivative Works in Source or Object form. | ||
|
||
3. Grant of Patent License. Subject to the terms and conditions of | ||
this License, each Contributor hereby grants to You a perpetual, | ||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
(except as stated in this section) patent license to make, have made, | ||
use, offer to sell, sell, import, and otherwise transfer the Work, | ||
where such license applies only to those patent claims licensable | ||
by such Contributor that are necessarily infringed by their | ||
Contribution(s) alone or by combination of their Contribution(s) | ||
with the Work to which such Contribution(s) was submitted. If You | ||
institute patent litigation against any entity (including a | ||
cross-claim or counterclaim in a lawsuit) alleging that the Work | ||
or a Contribution incorporated within the Work constitutes direct | ||
or contributory patent infringement, then any patent licenses | ||
granted to You under this License for that Work shall terminate | ||
as of the date such litigation is filed. | ||
|
||
4. Redistribution. You may reproduce and distribute copies of the | ||
Work or Derivative Works thereof in any medium, with or without | ||
modifications, and in Source or Object form, provided that You | ||
meet the following conditions: | ||
|
||
(a) You must give any other recipients of the Work or | ||
Derivative Works a copy of this License; and | ||
|
||
(b) You must cause any modified files to carry prominent notices | ||
stating that You changed the files; and | ||
|
||
(c) You must retain, in the Source form of any Derivative Works | ||
that You distribute, all copyright, patent, trademark, and | ||
attribution notices from the Source form of the Work, | ||
excluding those notices that do not pertain to any part of | ||
the Derivative Works; and | ||
|
||
(d) If the Work includes a "NOTICE" text file as part of its | ||
distribution, then any Derivative Works that You distribute must | ||
include a readable copy of the attribution notices contained | ||
within such NOTICE file, excluding those notices that do not | ||
pertain to any part of the Derivative Works, in at least one | ||
of the following places: within a NOTICE text file distributed | ||
as part of the Derivative Works; within the Source form or | ||
documentation, if provided along with the Derivative Works; or, | ||
within a display generated by the Derivative Works, if and | ||
wherever such third-party notices normally appear. The contents | ||
of the NOTICE file are for informational purposes only and | ||
do not modify the License. You may add Your own attribution | ||
notices within Derivative Works that You distribute, alongside | ||
or as an addendum to the NOTICE text from the Work, provided | ||
that such additional attribution notices cannot be construed | ||
as modifying the License. | ||
|
||
You may add Your own copyright statement to Your modifications and | ||
may provide additional or different license terms and conditions | ||
for use, reproduction, or distribution of Your modifications, or | ||
for any such Derivative Works as a whole, provided Your use, | ||
reproduction, and distribution of the Work otherwise complies with | ||
the conditions stated in this License. | ||
|
||
5. Submission of Contributions. Unless You explicitly state otherwise, | ||
any Contribution intentionally submitted for inclusion in the Work | ||
by You to the Licensor shall be under the terms and conditions of | ||
this License, without any additional terms or conditions. | ||
Notwithstanding the above, nothing herein shall supersede or modify | ||
the terms of any separate license agreement you may have executed | ||
with Licensor regarding such Contributions. | ||
|
||
6. Trademarks. This License does not grant permission to use the trade | ||
names, trademarks, service marks, or product names of the Licensor, | ||
except as required for reasonable and customary use in describing the | ||
origin of the Work and reproducing the content of the NOTICE file. | ||
|
||
7. Disclaimer of Warranty. Unless required by applicable law or | ||
agreed to in writing, Licensor provides the Work (and each | ||
Contributor provides its Contributions) on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
implied, including, without limitation, any warranties or conditions | ||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||
PARTICULAR PURPOSE. You are solely responsible for determining the | ||
appropriateness of using or redistributing the Work and assume any | ||
risks associated with Your exercise of permissions under this License. | ||
|
||
8. Limitation of Liability. In no event and under no legal theory, | ||
whether in tort (including negligence), contract, or otherwise, | ||
unless required by applicable law (such as deliberate and grossly | ||
negligent acts) or agreed to in writing, shall any Contributor be | ||
liable to You for damages, including any direct, indirect, special, | ||
incidental, or consequential damages of any character arising as a | ||
result of this License or out of the use or inability to use the | ||
Work (including but not limited to damages for loss of goodwill, | ||
work stoppage, computer failure or malfunction, or any and all | ||
other commercial damages or losses), even if such Contributor | ||
has been advised of the possibility of such damages. | ||
|
||
9. Accepting Warranty or Additional Liability. While redistributing | ||
the Work or Derivative Works thereof, You may choose to offer, | ||
and charge a fee for, acceptance of support, warranty, indemnity, | ||
or other liability obligations and/or rights consistent with this | ||
License. However, in accepting such obligations, You may act only | ||
on Your own behalf and on Your sole responsibility, not on behalf | ||
of any other Contributor, and only if You agree to indemnify, | ||
defend, and hold each Contributor harmless for any liability | ||
incurred by, or claims asserted against, such Contributor by reason | ||
of your accepting any such warranty or additional liability. | ||
|
||
END OF TERMS AND CONDITIONS | ||
|
||
APPENDIX: How to apply the Apache License to your work. | ||
|
||
To apply the Apache License to your work, attach the following | ||
boilerplate notice, with the fields enclosed by brackets "{}" | ||
replaced with your own identifying information. (Don't include | ||
the brackets!) The text should be enclosed in the appropriate | ||
comment syntax for the file format. We also recommend that a | ||
file or class name and description of purpose be included on the | ||
same "printed page" as the copyright notice for easier | ||
identification within third-party archives. | ||
|
||
Copyright {yyyy} {name of copyright owner} | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#= | ||
Derived from https://github.com/aparamon/HDF5Plugin-Zstandard, zstd_h5plugin.c | ||
Licensed under Apache License Version 2.0, see licenses/H5Zzstd_LICENSE.txt | ||
The following license applies to the Julia port. | ||
Copyright (c) 2021 Mark Kittisopikul and Howard Hughes Medical Institute. License MIT, see LICENSE.txt | ||
=# | ||
module H5Zzstd | ||
|
||
using CodecZstd | ||
import CodecZstd.LibZstd | ||
using HDF5.API | ||
import HDF5.Filters: Filter, filterid, register_filter, filterid, filtername, filter_func, filter_cfunc | ||
|
||
|
||
const H5Z_FILTER_ZSTD = API.H5Z_filter_t(32015) | ||
const zstd_name = "Zstandard compression: http://www.zstd.net" | ||
|
||
export H5Z_filter_zstd, H5Z_FILTER_ZSTD, ZstdFilter | ||
|
||
# cd_values First optional value is the compressor aggression | ||
# Default is CodecZstd.LibZstd.ZSTD_CLEVEL_DEFAULT | ||
function H5Z_filter_zstd(flags::Cuint, cd_nelmts::Csize_t, | ||
cd_values::Ptr{Cuint}, nbytes::Csize_t, | ||
buf_size::Ptr{Csize_t}, buf::Ptr{Ptr{Cvoid}})::Csize_t | ||
inbuf = unsafe_load(buf) | ||
outbuf = C_NULL | ||
origSize = nbytes | ||
ret_value = Csize_t(0) | ||
|
||
try | ||
|
||
if flags & API.H5Z_FLAG_REVERSE != 0 | ||
#decompresssion | ||
|
||
decompSize = LibZstd.ZSTD_getDecompressedSize(inbuf, origSize) | ||
outbuf = Libc.malloc(decompSize) | ||
if outbuf == C_NULL | ||
error("zstd_h5plugin: Cannot allocate memory for outbuf during decompression.") | ||
end | ||
decompSize = LibZstd.ZSTD_decompress(outbuf, decompSize, inbuf, origSize) | ||
Libc.free(inbuf) | ||
unsafe_store!(buf, outbuf) | ||
outbuf = C_NULL | ||
ret_value = Csize_t(decompSize) | ||
else | ||
# compression | ||
|
||
if cd_nelmts > 0 | ||
aggression = Cint(unsafe_load(cd_values)) | ||
else | ||
aggression = CodecZstd.LibZstd.ZSTD_CLEVEL_DEFAULT | ||
end | ||
|
||
if aggression < 1 | ||
aggression = 1 # ZSTD_minCLevel() | ||
elseif aggression > LibZstd.ZSTD_maxCLevel() | ||
aggression = LibZstd.ZSTD_maxCLevel() | ||
end | ||
|
||
compSize = LibZstd.ZSTD_compressBound(origSize) | ||
outbuf = Libc.malloc(compSize) | ||
if outbuf == C_NULL | ||
error("zstd_h5plugin: Cannot allocate memory for outbuf during compression.") | ||
end | ||
|
||
compSize = LibZstd.ZSTD_compress(outbuf, compSize, inbuf, origSize, aggression) | ||
|
||
Libc.free(unsafe_load(buf)) | ||
unsafe_store!(buf, outbuf) | ||
unsafe_store!(buf_size, compSize) | ||
outbuf = C_NULL | ||
ret_value = compSize | ||
end | ||
catch e | ||
# "In the case of failure, the return value is 0 (zero) and all pointer arguments are left unchanged." | ||
ret_value = Csize_t(0) | ||
@error "H5Zzstd Non-Fatal ERROR: " err | ||
display(stacktrace(catch_backtrace())) | ||
finally | ||
|
||
if outbuf != C_NULL | ||
free(outbuf) | ||
end | ||
|
||
end # try catch finally | ||
return Csize_t(ret_value) | ||
end | ||
|
||
# Filters Module | ||
|
||
struct ZstdFilter <: Filter | ||
clevel::Cuint | ||
end | ||
ZstdFilter() = ZstdFilter(CodecZstd.LibZstd.ZSTD_CLEVEL_DEFAULT) | ||
|
||
filterid(::Type{ZstdFilter}) = H5Z_FILTER_ZSTD | ||
filtername(::Type{ZstdFilter}) = zstd_name | ||
filter_func(::Type{ZstdFilter}) = H5Z_filter_zstd | ||
filter_cfunc(::Type{ZstdFilter}) = @cfunction(H5Z_filter_zstd, Csize_t, | ||
(Cuint, Csize_t, Ptr{Cuint}, Csize_t, | ||
Ptr{Csize_t}, Ptr{Ptr{Cvoid}})) | ||
|
||
function __init__() | ||
register_filter(ZstdFilter) | ||
end | ||
|
||
end # module H5Zzstd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
""" | ||
module FilterTestUtils | ||
This module contains utilities for evaluating and debugging HDF5 Filters. | ||
""" | ||
module FilterTestUtils | ||
|
||
import HDF5.API | ||
import H5Zlz4: H5Z_filter_lz4 | ||
import H5Zzstd: H5Z_filter_zstd | ||
import H5Zbzip2: H5Z_filter_bzip2 | ||
using Test | ||
|
||
export test_filter | ||
|
||
function test_filter_init(; cd_values = Cuint[], data = ones(UInt8, 1024)) | ||
flags = Cuint(0) | ||
nbytes = sizeof(data) | ||
buf_size = Ref(Csize_t(sizeof(data))) | ||
databuf = Libc.malloc(sizeof(data)) | ||
data = reinterpret(UInt8, data) | ||
unsafe_copyto!(Ptr{UInt8}(databuf), pointer(data), sizeof(data)) | ||
buf = Ref(Ptr{Cvoid}(databuf)) | ||
return flags, cd_values, nbytes, buf_size, buf | ||
end | ||
|
||
function test_filter_compress!(filter_func, flags::Cuint, cd_values::Vector{Cuint}, nbytes::Integer, buf_size::Ref{Csize_t}, buf::Ref{Ptr{Cvoid}}) | ||
nbytes = Csize_t(nbytes) | ||
cd_nelmts = Csize_t(length(cd_values)) | ||
GC.@preserve flags cd_nelmts cd_values nbytes buf_size buf begin | ||
ret_code = filter_func( | ||
flags, | ||
cd_nelmts, | ||
pointer(cd_values), | ||
Csize_t(nbytes), | ||
Base.unsafe_convert(Ptr{Csize_t}, buf_size), | ||
Base.unsafe_convert(Ptr{Ptr{Cvoid}}, buf) | ||
) | ||
@debug "Compression:" ret_code buf_size[] | ||
if ret_code <= 0 | ||
error("Test compression failed: $ret_code.") | ||
end | ||
end | ||
return ret_code | ||
end | ||
|
||
function test_filter_decompress!(filter_func, flags::Cuint, cd_values::Vector{Cuint}, nbytes::Integer, buf_size::Ref{Csize_t}, buf::Ref{Ptr{Cvoid}}) | ||
nbytes = Csize_t(nbytes) | ||
cd_nelmts = Csize_t(length(cd_values)) | ||
flags |= UInt32(API.H5Z_FLAG_REVERSE) | ||
GC.@preserve flags cd_nelmts cd_values nbytes buf_size buf begin | ||
ret_code = filter_func( | ||
flags, | ||
cd_nelmts, | ||
pointer(cd_values), | ||
Csize_t(nbytes), | ||
Base.unsafe_convert(Ptr{Csize_t},buf_size), | ||
Base.unsafe_convert(Ptr{Ptr{Cvoid}}, buf) | ||
) | ||
@debug "Decompression:" ret_code buf_size[] | ||
end | ||
return ret_code | ||
end | ||
|
||
function test_filter_cleanup!(buf::Ref{Ptr{Cvoid}}) | ||
Libc.free(buf[]) | ||
end | ||
|
||
function test_filter(filter_func; cd_values::Vector{Cuint} = Cuint[], data = ones(UInt8, 1024)) | ||
flags, cd_values, nbytes, buf_size, buf = test_filter_init(; cd_values = cd_values, data = data) | ||
nbytes_compressed, nbytes_decompressed = 0, 0 | ||
try | ||
nbytes_compressed = test_filter_compress!(filter_func, flags, cd_values, nbytes, buf_size, buf) | ||
nbytes_decompressed = test_filter_decompress!(filter_func, flags, cd_values, nbytes_compressed, buf_size, buf) | ||
if nbytes_decompressed > 0 | ||
# ret_code is the number of bytes out | ||
round_trip_data = unsafe_wrap(Array,Ptr{UInt8}(buf[]), nbytes_decompressed) | ||
@debug "Is the data the same after a roundtrip?" data == round_trip_data | ||
end | ||
catch err | ||
rethrow(err) | ||
finally | ||
test_filter_cleanup!(buf) | ||
end | ||
@debug "Compression Ratio" nbytes_compressed / nbytes_decompressed | ||
return nbytes_compressed, nbytes_decompressed | ||
end | ||
|
||
function test_bzip2_filter(data = ones(UInt8, 1024)) | ||
cd_values = Cuint[8] | ||
test_filter(H5Z_filter_bzip2; cd_values = cd_values, data = data) | ||
end | ||
|
||
function test_lz4_filter(data = ones(UInt8, 1024)) | ||
cd_values = Cuint[1024] | ||
test_filter(H5Z_filter_lz4; cd_values = cd_values, data = data) | ||
end | ||
|
||
function test_zstd_filter(data = ones(UInt8, 1024)) | ||
cd_values = Cuint[3] # aggression | ||
test_filter(H5Z_filter_zstd; cd_values = cd_values, data = data) | ||
end | ||
|
||
function __init__() | ||
@testset "Compression Filter Unit Tests" begin | ||
@test argmin(test_bzip2_filter()) == 1 | ||
@test argmin(test_lz4_filter()) == 1 | ||
@test argmin(test_zstd_filter()) == 1 | ||
str = codeunits(repeat("foobar", 1000)) | ||
@test argmin(test_bzip2_filter(str)) == 1 | ||
@test argmin(test_lz4_filter(str)) == 1 | ||
@test argmin(test_zstd_filter(str)) == 1 | ||
end | ||
end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,63 @@ | ||
using HDF5 | ||
using Test | ||
using Pkg | ||
|
||
@info "libhdf5 v$(HDF5.API.h5_get_libversion())" | ||
|
||
# To debug HDF5.jl tests, uncomment the next line | ||
# ENV["JULIA_DEBUG"] = "Main" | ||
|
||
@testset "HDF5.jl" begin | ||
|
||
@debug "plain" | ||
include("plain.jl") | ||
@debug "compound" | ||
include("compound.jl") | ||
@debug "custom" | ||
include("custom.jl") | ||
@debug "reference" | ||
include("reference.jl") | ||
@debug "dataspace" | ||
include("dataspace.jl") | ||
@debug "hyperslab" | ||
include("hyperslab.jl") | ||
@debug "readremote" | ||
include("readremote.jl") | ||
@debug "extend_test" | ||
include("extend_test.jl") | ||
@debug "gc" | ||
include("gc.jl") | ||
@debug "external" | ||
include("external.jl") | ||
@debug "swmr" | ||
include("swmr.jl") | ||
@debug "mmap" | ||
include("mmap.jl") | ||
@debug "properties" | ||
include("properties.jl") | ||
@debug "table" | ||
include("table.jl") | ||
@debug "filter" | ||
include("filter.jl") | ||
@debug "chunkstorage" | ||
include("chunkstorage.jl") | ||
@debug "fileio" | ||
include("fileio.jl") | ||
|
||
using MPI | ||
if HDF5.has_parallel() | ||
# basic MPI tests, for actual parallel tests we need to run in MPI mode | ||
include("mpio.jl") | ||
end | ||
|
||
# Clean up after all resources | ||
HDF5.API.h5_close() | ||
|
||
end | ||
using HDF5 | ||
using Test | ||
using Pkg | ||
filter_path = joinpath(dirname(pathof(HDF5)), "filters") | ||
Pkg.develop(PackageSpec(path=joinpath(filter_path, "H5Zblosc"))) | ||
Pkg.develop(PackageSpec(path=joinpath(filter_path, "H5Zbzip2"))) | ||
Pkg.develop(PackageSpec(path=joinpath(filter_path, "H5Zlz4"))) | ||
Pkg.develop(PackageSpec(path=joinpath(filter_path, "H5Zzstd"))) | ||
Comment on lines
+4
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did like the idea of having this in some kind of callable closure. I anticipate downstream projects may want to do something similar via CI. Perhaps though it could it its own file under |
||
|
||
@info "libhdf5 v$(HDF5.API.h5_get_libversion())" | ||
|
||
# To debug HDF5.jl tests, uncomment the next line | ||
# ENV["JULIA_DEBUG"] = "Main" | ||
|
||
@testset "HDF5.jl" begin | ||
|
||
@debug "plain" | ||
include("plain.jl") | ||
@debug "compound" | ||
include("compound.jl") | ||
@debug "custom" | ||
include("custom.jl") | ||
@debug "reference" | ||
include("reference.jl") | ||
@debug "dataspace" | ||
include("dataspace.jl") | ||
@debug "hyperslab" | ||
include("hyperslab.jl") | ||
@debug "readremote" | ||
include("readremote.jl") | ||
@debug "extend_test" | ||
include("extend_test.jl") | ||
@debug "gc" | ||
include("gc.jl") | ||
@debug "external" | ||
include("external.jl") | ||
@debug "swmr" | ||
include("swmr.jl") | ||
@debug "mmap" | ||
include("mmap.jl") | ||
@debug "properties" | ||
include("properties.jl") | ||
@debug "table" | ||
include("table.jl") | ||
@debug "filter" | ||
include("filter.jl") | ||
@debug "chunkstorage" | ||
include("chunkstorage.jl") | ||
@debug "fileio" | ||
include("fileio.jl") | ||
@debug "filter test utils" | ||
include("filters/FilterTestUtils.jl") | ||
|
||
using MPI | ||
if HDF5.has_parallel() | ||
# basic MPI tests, for actual parallel tests we need to run in MPI mode | ||
include("mpio.jl") | ||
end | ||
|
||
# Clean up after all resources | ||
HDF5.API.h5_close() | ||
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.