Skip to content

Commit

Permalink
Merge pull request #607 from JuliaIO/aquatests
Browse files Browse the repository at this point in the history
Aquatests
  • Loading branch information
JonasIsensee authored Oct 19, 2024
2 parents 16c3448 + 1dcee45 commit e591c10
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 199 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.5.6
- Add Aqua tests and eliminate method ambiguities

## 0.5.5
- Experimental support for writing to and reading from `IO` objects e.g. `jldopen(io, "r")`

Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "JLD2"
uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
version = "0.5.5"
version = "0.5.6"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand All @@ -13,7 +13,7 @@ TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[compat]
FileIO = "1.5"
MacroTools = "0.5"
MacroTools = "0.5.10"
OrderedCollections = "1"
PrecompileTools = "1"
Requires = "1.3"
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# JLD2

| **Documentation** | **Tests** | **CodeCov** | **Downloads** |
|:--------:|:---------------:|:-------:|:---:|
|[![](https://img.shields.io/badge/docs-online-blue.svg)](https://JuliaIO.github.io/JLD2.jl/dev)| [![CI](https://github.com/JuliaIO/JLD2.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/JuliaIO/JLD2.jl/actions/workflows/ci.yml) | [![codecov.io](https://codecov.io/github/JuliaIO/JLD2.jl/coverage.svg?branch=master)](https://codecov.io/github/JuliaIO/JLD2.jl?branch=master) | [![JLD2 Downloads](https://img.shields.io/badge/dynamic/json?url=http%3A%2F%2Fjuliapkgstats.com%2Fapi%2Fv1%2Fmonthly_downloads%2FJLD2&query=total_requests&suffix=%2Fmonth&label=Downloads)](http://juliapkgstats.com/pkg/JLD2) |
[![](https://img.shields.io/badge/docs-online-blue.svg)](https://JuliaIO.github.io/JLD2.jl/dev)
[![CI](https://github.com/JuliaIO/JLD2.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/JuliaIO/JLD2.jl/actions/workflows/ci.yml)
[![codecov.io](https://codecov.io/github/JuliaIO/JLD2.jl/coverage.svg?branch=master)](https://codecov.io/github/JuliaIO/JLD2.jl?branch=master)
[![JLD2 Downloads](https://img.shields.io/badge/dynamic/json?url=http%3A%2F%2Fjuliapkgstats.com%2Fapi%2Fv1%2Fmonthly_downloads%2FJLD2&query=total_requests&suffix=%2Fmonth&label=Downloads)](http://juliapkgstats.com/pkg/JLD2)
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)


JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library.
JLD2 is able to read most HDF5 files created by other HDF5 implementations supporting HDF5 File Format Specification Version 3.0 (i.e. libhdf5 1.10 or later) and similarly those should be able to read the files that JLD2 produces. JLD2 provides read-only support for files created with the JLD package.
Expand Down
8 changes: 4 additions & 4 deletions src/backwards_compatibility.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# The following block allows reading Union types written prior to v0.2
const LEGACY_H5TYPE_UNION = VariableLengthDatatype(H5TYPE_DATATYPE)

function jlconvert(::ReadRepresentation{Union, Vlen{DataTypeODR()}}, f::JLDFile,
function jlconvert(::MappedRepr{Union, Vlen{DataTypeODR}}, f::JLDFile,
ptr::Ptr, header_offset::RelOffset)
v = Union{jlconvert(ReadRepresentation{DataType,Vlen{DataTypeODR()}}(), f, ptr, NULL_REFERENCE)...}
v = Union{jlconvert(MappedRepr{DataType,Vlen{DataTypeODR}}(), f, ptr, NULL_REFERENCE)...}
track_weakref!(f, header_offset, v)
v
end

constructrr(::JLDFile, ::Type{T}, dt::VariableLengthDatatype, ::Vector{ReadAttribute}) where {T<:Union} =
dt == LEGACY_H5TYPE_UNION ? (ReadRepresentation{Union,Vlen{DataTypeODR()}}(), true) :
dt == LEGACY_H5TYPE_UNION ? (MappedRepr{Union,Vlen{DataTypeODR}}(), true) :
throw(UnsupportedFeatureException())

# The following definition is needed to correctly load Strings written
Expand All @@ -18,7 +18,7 @@ function read_array(f::JLDFile, dataspace::ReadDataspace,
rr::FixedLengthString{String}, layout::DataLayout,
filters::FilterPipeline, header_offset::RelOffset,
attributes::Union{Vector{ReadAttribute},Nothing})
rrv = ReadRepresentation{UInt8,odr(UInt8)}()
rrv = SameRepr{UInt8}()
v = read_array(f, dataspace, rrv, layout, filters, NULL_REFERENCE, attributes)
String(v)
end
Expand Down
10 changes: 5 additions & 5 deletions src/committed_datatype_introspection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function stringify_committed_datatype(f, cdt; showfields=false)
else
# These are normal julia types
dtrr = jltype(f, dt.members[i])
fieldtype = string(typeof(dtrr).parameters[1])
fieldtype = string(julia_type(dtrr))
end
push!(field_strs, "$(dt.names[i])::$(fieldtype)")
end
Expand Down Expand Up @@ -97,7 +97,7 @@ function typestring_from_refs(f::JLDFile, ptr::Ptr)
# Test for a potential null pointer indicating an empty array
isinit = jlunsafe_load(convert(Ptr{UInt32}, ptr)) != 0
if isinit
refs = jlconvert(ReadRepresentation{RelOffset, Vlen{RelOffset}}(), f, ptr, NULL_REFERENCE)
refs = jlconvert(MappedRepr{RelOffset, Vlen{RelOffset}}(), f, ptr, NULL_REFERENCE)
#println("datatypes at refs $(Int.(getproperty.(refs,:offset)))")
params = Any[let
# If the reference is to a committed datatype, read the datatype
Expand Down Expand Up @@ -127,10 +127,10 @@ function jlconvert_string_wrap(rr, f, offset)
end
end

function jlconvert_string(rr::ReadRepresentation{T,DataTypeODR()},
function jlconvert_string(::MappedRepr{T,DataTypeODR},
f::JLDFile,
ptr::Ptr) where T
mypath = String(jlconvert(ReadRepresentation{UInt8,Vlen{UInt8}}(), f, ptr, NULL_REFERENCE))
mypath = String(jlconvert(MappedRepr{UInt8,Vlen{UInt8}}(), f, ptr, NULL_REFERENCE))
params = typestring_from_refs(f, ptr+odr_sizeof(Vlen{UInt8}))
if startswith(mypath, r"Core|Main")
mypath = last(split(mypath, "."; limit=2))
Expand All @@ -143,7 +143,7 @@ function jlconvert_string(rr::ReadRepresentation{T,DataTypeODR()},
end
end

function jlconvert_string(::ReadRepresentation{Union, UnionTypeODR()}, f::JLDFile,
function jlconvert_string(::MappedRepr{Union, UnionTypeODR}, f::JLDFile,
ptr::Ptr)#, header_offset::RelOffset)
# Skip union type description in the beginning
ptr += odr_sizeof(Vlen{String})
Expand Down
16 changes: 8 additions & 8 deletions src/data/custom_serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ h5convert!(out::Pointers, odr::Type{CustomSerialization{T,RelOffset}},
h5convert_uninitialized!(out::Pointers, odr::Type{CustomSerialization{T,ODR}}) where {T,ODR} =
h5convert_uninitialized!(out, ODR)

jlconvert_canbeuninitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}) where {T,S,ODR} =
jlconvert_canbeuninitialized(ReadRepresentation{S,ODR}())
jlconvert_isinitialized(::ReadRepresentation{T,CustomSerialization{S,ODR}}, ptr::Ptr) where {T,S,ODR} =
jlconvert_isinitialized(ReadRepresentation{S,ODR}(), ptr)
jlconvert_canbeuninitialized(::MappedRepr{T,CustomSerialization{S,ODR}}) where {T,S,ODR} =
jlconvert_canbeuninitialized(MappedRepr{S,ODR}())
jlconvert_isinitialized(::MappedRepr{T,CustomSerialization{S,ODR}}, ptr::Ptr) where {T,S,ODR} =
jlconvert_isinitialized(MappedRepr{S,ODR}(), ptr)

function jlconvert(::ReadRepresentation{T,CustomSerialization{S,RelOffset}},
function jlconvert(::MappedRepr{T,CustomSerialization{S,RelOffset}},
f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T,S}
# Concerns objects whose custom serialization is itself only referenced by a RelOffset
# This be important when the original object is mutable
offset = jlunsafe_load(pconvert(Ptr{RelOffset}, ptr))
return rconvert(T, load_dataset(f, offset))
end

function jlconvert(::ReadRepresentation{T,CustomSerialization{S,ODR}},
function jlconvert(::MappedRepr{T,CustomSerialization{S,ODR}},
f::JLDFile, ptr::Ptr, header_offset::RelOffset) where {T,S,ODR}

if ismutabletype(T) && !(T <: Core.SimpleVector)
Expand All @@ -70,15 +70,15 @@ function jlconvert(::ReadRepresentation{T,CustomSerialization{S,ODR}},
track_weakref!(f, header_offset, obj)

# actually load the data
v = rconvert(T, jlconvert(ReadRepresentation{S,ODR}(), f, ptr, header_offset))::T
v = rconvert(T, jlconvert(ReadRepresentation(S,ODR), f, ptr, header_offset))::T
# copy fields to initial struct
for i in 0:nfields(obj)-1
fieldval = ccall(:jl_get_nth_field, Any, (Any, Csize_t), v, i)
ccall(:jl_set_nth_field, Nothing, (Any, Csize_t, Any), obj, i, fieldval)
end
return obj
else
v = rconvert(T, jlconvert(ReadRepresentation{S,ODR}(), f, ptr, header_offset))::T
v = rconvert(T, jlconvert(ReadRepresentation(S,ODR), f, ptr, header_offset))::T
track_weakref!(f, header_offset, v)
return v
end
Expand Down
38 changes: 19 additions & 19 deletions src/data/number_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct BENumber{T}
x::T
end

jlconvert(::ReadRepresentation{T,BENumber{T}}, ::JLDFile, ptr::Ptr, ::RelOffset) where {T} =
jlconvert(::MappedRepr{T,BENumber{T}}, ::JLDFile, ptr::Ptr, ::RelOffset) where {T} =
bswap(jlunsafe_load(pconvert(Ptr{T}, ptr)))

function jltype(f::JLDFile, dt::FixedPointDatatype)
Expand All @@ -33,29 +33,29 @@ function jltype(f::JLDFile, dt::FixedPointDatatype)
throw(UnsupportedFeatureException())
if endianness == 0
if dt.size == 8
return signed ? ReadRepresentation{Int64,Int64}() : ReadRepresentation{UInt64,UInt64}()
return signed ? SameRepr{Int64}() : SameRepr{UInt64}()
elseif dt.size == 1
return signed ? ReadRepresentation{Int8,Int8}() : ReadRepresentation{UInt8,UInt8}()
return signed ? SameRepr{Int8}() : SameRepr{UInt8}()
elseif dt.size == 4
return signed ? ReadRepresentation{Int32,Int32}() : ReadRepresentation{UInt32,UInt32}()
return signed ? SameRepr{Int32}() : SameRepr{Int32}()
elseif dt.size == 2
return signed ? ReadRepresentation{Int16,Int16}() : ReadRepresentation{UInt16,UInt16}()
return signed ? SameRepr{Int16}() : SameRepr{UInt16}()
elseif dt.size == 16
return signed ? ReadRepresentation{Int128,Int128}() : ReadRepresentation{UInt128,UInt128}()
return signed ? SameRepr{Int128}() : SameRepr{UInt128}()
else
throw(UnsupportedFeatureException())
end
else
if dt.size == 8
return signed ? ReadRepresentation{Int64,BENumber{Int64}}() : ReadRepresentation{UInt64,BENumber{UInt64}}()
return signed ? MappedRepr{Int64,BENumber{Int64}}() : MappedRepr{UInt64,BENumber{UInt64}}()
elseif dt.size == 1
return signed ? ReadRepresentation{Int8,BENumber{Int8}}() : ReadRepresentation{UInt8,BENumber{UInt8}}()
return signed ? MappedRepr{Int8,BENumber{Int8}}() : MappedRepr{UInt8,BENumber{UInt8}}()
elseif dt.size == 4
return signed ? ReadRepresentation{Int32,BENumber{Int32}}() : ReadRepresentation{UInt32,BENumber{UInt32}}()
return signed ? MappedRepr{Int32,BENumber{Int32}}() : MappedRepr{UInt32,BENumber{UInt32}}()
elseif dt.size == 2
return signed ? ReadRepresentation{Int16,BENumber{Int16}}() : ReadRepresentation{UInt16,BENumber{UInt16}}()
return signed ? MappedRepr{Int16,BENumber{Int16}}() : MappedRepr{UInt16,BENumber{UInt16}}()
elseif dt.size == 16
return signed ? ReadRepresentation{Int128,BENumber{Int128}}() : ReadRepresentation{UInt128,BENumber{UInt128}}()
return signed ? MappedRepr{Int128,BENumber{Int128}}() : MappedRepr{UInt128,BENumber{UInt128}}()
else
throw(UnsupportedFeatureException())
end
Expand All @@ -64,7 +64,7 @@ end

# Special handling for booleans as they are not considered <: Integer in HDF5
h5fieldtype(::JLDFile, ::Type{Bool}, ::Type{Bool}, ::Initialized) =BitFieldDatatype(1)
jltype(::JLDFile, ::BitFieldDatatype) = ReadRepresentation{Bool, Bool}()
jltype(::JLDFile, ::BitFieldDatatype) = SameRepr{Bool}()

h5fieldtype(::JLDFile, ::Type{Float16}, ::Type{Float16}, ::Initialized) =
FloatingPointDatatype(UInt8(DT_FLOATING_POINT) + 0x3<<4, 0x20, 0x0f, 0x00, 2, 0, 16, 10, 5, 0, 10, 0x0000000f)
Expand All @@ -82,17 +82,17 @@ h5fieldtype(::JLDFile, ::Type{BENumber{Float64}}, ::Type{Float64}, ::Initialized

function jltype(f::JLDFile, dt::FloatingPointDatatype)
if dt == h5fieldtype(f, Float64, Float64, Val{true})
return ReadRepresentation{Float64,Float64}()
return SameRepr{Float64}()
elseif dt == h5fieldtype(f, Float32, Float32, Val{true})
return ReadRepresentation{Float32,Float32}()
return SameRepr{Float32}()
elseif dt == h5fieldtype(f, Float16, Float16, Val{true})
return ReadRepresentation{Float16,Float16}()
return SameRepr{Float16}()
elseif dt == h5fieldtype(f, BENumber{Float64}, Float64, Val{true})
return ReadRepresentation{Float32,BENumber{Float32}}()
return MappedRepr{Float32,BENumber{Float32}}()
elseif dt == h5fieldtype(f, BENumber{Float32}, Float32, Val{true})
return ReadRepresentation{Float32,BENumber{Float32}}()
return MappedRepr{Float32,BENumber{Float32}}()
elseif dt == h5fieldtype(f, BENumber{Float16}, Float16, Val{true})
return ReadRepresentation{Float32,BENumber{Float32}}()
return MappedRepr{Float32,BENumber{Float32}}()
else
throw(UnsupportedFeatureException())
end
Expand All @@ -108,5 +108,5 @@ h5type(f::JLDFile, writeas::PrimitiveTypeTypes, x) =
# Used only for custom serialization
constructrr(f::JLDFile, T::PrimitiveTypeTypes, dt::Union{FixedPointDatatype,FloatingPointDatatype},
::Vector{ReadAttribute}) =
dt == h5fieldtype(f, T, T, Val{true}) ? (ReadRepresentation{T,T}(), true) :
dt == h5fieldtype(f, T, T, Val{true}) ? (SameRepr{T}(), true) :
throw(UnsupportedFeatureException())
Loading

0 comments on commit e591c10

Please sign in to comment.