diff --git a/src/JLD.jl b/src/JLD.jl index 09bcbb5f6..1ce9fd1c5 100644 --- a/src/JLD.jl +++ b/src/JLD.jl @@ -295,7 +295,6 @@ function read(parent::Union(JldFile, JldGroup), name::ByteString) end read(parent::Union(JldFile,JldGroup), name::Symbol) = read(parent,bytestring(string(name))) -# read and readsafely differ only in how they handle CompositeKind function read(obj::JldDataset) dtype = datatype(obj.plain) dspace_id = HDF5.h5d_get_space(obj.plain) @@ -410,12 +409,17 @@ end # Read a reference function read_ref(f::JldFile, ref::HDF5ReferenceObj) + haskey(f.jlref, ref) && return f.jlref[ref].value + dset = f[ref] - try - return read(dset) + data = try + read(dset) finally close(dset) end + + f.jlref[ref] = WeakRef(data) + data end function getrefs{T}(obj::JldDataset, ::Type{T}) refs = read(obj.plain, Array{HDF5ReferenceObj}) diff --git a/test/jld.jl b/test/jld.jl index 5964a5691..cf33d55b5 100644 --- a/test/jld.jl +++ b/test/jld.jl @@ -123,6 +123,19 @@ immutable MyImmutable2 MyImmutable2() = new() end nonpointerfree_immutable_3 = MyImmutable2() +# Array references +arr_contained = [1, 2, 3] +arr_ref = typeof(arr_contained)[] +push!(arr_ref, arr_contained, arr_contained) +# Object references +type ObjRefType + x::ObjRefType + y::ObjRefType + ObjRefType() = new() + ObjRefType(x, y) = new(x, y) +end +ref1 = ObjRefType() +obj_ref = ObjRefType(ObjRefType(ref1, ref1), ObjRefType(ref1, ref1)) iseq(x,y) = isequal(x,y) iseq(x::MyStruct, y::MyStruct) = (x.len == y.len && x.data == y.data) @@ -247,6 +260,8 @@ end @write fid nonpointerfree_immutable_1 @write fid nonpointerfree_immutable_2 @write fid nonpointerfree_immutable_3 +@write fid arr_ref +@write fid obj_ref # Make sure we can create groups (i.e., use HDF5 features) g = g_create(fid, "mygroup") i = 7 @@ -331,6 +346,12 @@ for mmap = (true, false) @check fidr nonpointerfree_immutable_1 @check fidr nonpointerfree_immutable_2 @check fidr nonpointerfree_immutable_3 + arr = read(fidr, "arr_ref") + @test arr == arr_ref + @test arr[1] === arr[2] + obj = read(fidr, "obj_ref") + @test obj.x.x === obj.x.y == obj.y.x === obj.y.y + @test obj.x !== obj.y x1 = read(fidr, "group1/x") @assert x1 == {1}