Skip to content

Commit

Permalink
Fix handling of object references
Browse files Browse the repository at this point in the history
Turns out I never implemented the reading part of this
  • Loading branch information
simonster committed Aug 21, 2014
1 parent ac11825 commit 58b3f55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/JLD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand Down
21 changes: 21 additions & 0 deletions test/jld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 58b3f55

Please sign in to comment.