Skip to content

Commit

Permalink
Draft: experimental plain reconstruction (#522)
Browse files Browse the repository at this point in the history
* experimental plain reconstruction

* Upgrade

* add test
  • Loading branch information
JonasIsensee authored Aug 25, 2024
1 parent 131c688 commit 3c8884f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/JLD2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mutable struct JLDFile{T<:IO}
path::String
writable::Bool
written::Bool
plain::Bool
compress#::Union{Bool,Symbol}
mmaparrays::Bool
n_times_opened::Int
Expand All @@ -125,11 +126,13 @@ mutable struct JLDFile{T<:IO}
root_group::Group{JLDFile{T}}
types_group::Group{JLDFile{T}}
base_address::UInt64


function JLDFile{T}(io::IO, path::AbstractString, writable::Bool, written::Bool,
plain::Bool,
compress,#::Union{Bool,Symbol},
mmaparrays::Bool) where T
f = new(io, path, writable, written, compress, mmaparrays, 1, false,
f = new(io, path, writable, written, plain, compress, mmaparrays, 1, false,
OrderedDict{RelOffset,CommittedDatatype}(), H5Datatype[],
JLDWriteSession(), Dict{String,Any}(), IdDict(), IdDict(), Dict{RelOffset,WeakRef}(),
DATA_START, Dict{RelOffset,GlobalHeap}(),
Expand All @@ -138,8 +141,8 @@ mutable struct JLDFile{T<:IO}
f
end
end
JLDFile(io::IO, path::AbstractString, writable::Bool, written::Bool, compress, mmaparrays::Bool) =
JLDFile{typeof(io)}(io, path, writable, written, compress, mmaparrays)
JLDFile(io::IO, path::AbstractString, writable::Bool, written::Bool, plain::Bool, compress, mmaparrays::Bool) =
JLDFile{typeof(io)}(io, path, writable, written, plain, compress, mmaparrays)

"""
fileoffset(f::JLDFile, x::RelOffset)
Expand Down Expand Up @@ -189,6 +192,7 @@ function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool,
mmaparrays::Bool=false,
typemap::Dict{String}=Dict{String,Any}(),
parallel_read::Bool=false,
plain::Bool=false
) where T<:Union{Type{IOStream},Type{MmapIO}}
mmaparrays && @warn "mmaparrays keyword is currently ignored" maxlog=1
verify_compressor(compress)
Expand Down Expand Up @@ -240,7 +244,7 @@ function jldopen(fname::AbstractString, wr::Bool, create::Bool, truncate::Bool,
io = openfile(iotype, fname, wr, create, truncate, fallback)
created = !exists || truncate
rname = realpath(fname)
f = JLDFile(io, rname, wr, created, compress, mmaparrays)
f = JLDFile(io, rname, wr, created, plain, compress, mmaparrays)

if !parallel_read
OPEN_FILES[rname] = WeakRef(f)
Expand Down
8 changes: 8 additions & 0 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ function jltype(f::JLDFile, cdt::CommittedDatatype)
end

datatype = read_attr_data(f, julia_type_attr)
if f.plain && !(datatype isa Upgrade) && !(datatype <: Tuple)
rr = jltype(f, dt)
return f.h5jltype[cdt] = rr
end

if written_type_attr !== nothing
# Custom serialization
custom_datatype = read_attr_data(f, written_type_attr)
Expand Down Expand Up @@ -415,6 +420,9 @@ function jlconvert(rr::ReadRepresentation{T,DataTypeODR()},
isunknowntype(m) && return m
unknown_params && return UnknownType{m, Tuple{params...}}
if hasparams
if f.plain && !(m === Tuple)
return Any
end
try
m = m{params...}
catch e
Expand Down
12 changes: 12 additions & 0 deletions test/test_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,16 @@ end
@test getfoo("readas_foo_n_sin.jld2") isa Readas.FooN{typeof(sin)}
@test getfoo("readas_foo_a.jld2") isa Readas.Foo{Readas.UndefinedFunction}
@test getfoo("readas_foo_n_a.jld2") isa Readas.FooNSerialization
end


@testset "plain reconstruction" begin
fn = joinpath(testfiles,"struct_reconstruction.jld2")
data = load(fn; plain=true)
# This is somewhat broken: Tuples are committed with field names "1", "2",...
# these are valid names but break most of the common API incl. the @NamedTuple macro
#@test data["tms"] == @NamedTuple{1::Int64, 2}((1, (a = 1,)))
@test getproperty(data["tms"], Symbol(1)) == 1
@test data["s"] == (a = 1,)
@test data["ds"].kvvec[1] == (; first = "a", second = (a = 1,))
end

0 comments on commit 3c8884f

Please sign in to comment.