Skip to content

Commit 9568f50

Browse files
committed
cleanup
1 parent b94edf4 commit 9568f50

File tree

4 files changed

+33
-46
lines changed

4 files changed

+33
-46
lines changed

src/JLD2.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ function initialize_fileobject!(f::JLDFile)
267267
f.root_group = load_group(f, f.root_group_offset)
268268

269269
# Use lookup_link directly instead of lookup_offset
270-
types_offset = getoffset(lookup_link(f.root_group, "_types"))
270+
types_offset = getoffset(f.root_group, lookup_link(f.root_group, "_types"))
271271
if types_offset !== UNDEFINED_ADDRESS
272272
f.types_group = f.loaded_groups[types_offset] = load_group(f, types_offset)
273273
for (i, link) in enumerate(values(f.types_group.written_links))

src/explicit_datasets.jl

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -321,36 +321,8 @@ function get_dataset(g::Group, name::String)
321321
return get_dataset(f, group_offset(g), g, name)
322322
end
323323

324-
# Use lookup_link and getoffset for simplified resolution
325324
link = lookup_link(g, name)
326-
link === nothing && throw(KeyError(name))
327-
328-
if is_external_link(link)
329-
# For external links, provide informative error message
330-
throw(ArgumentError(LazyString(
331-
"get_dataset cannot be used on external links.\n",
332-
"Link \"", name, "\" points to:\n",
333-
" • External file: \"", link.external_file, "\"\n",
334-
" • Object path: \"", link.path, "\"\n",
335-
"Use f[\"", name, "\"] to access the external data directly."
336-
)))
337-
end
338-
339-
# Try to get the offset (resolves soft links, returns offset for hard links)
340-
offset = getoffset(g, link; erroroninvalid=false)
341-
if offset == UNDEFINED_ADDRESS
342-
# Either uninitialized hard link or unresolvable soft link
343-
if is_soft_link(link)
344-
throw(ArgumentError(LazyString(
345-
"get_dataset cannot be used on soft links that don't point to regular datasets.\n",
346-
"Link \"", name, "\" points to: \"", link.path, "\"\n",
347-
"Use f[\"", name, "\"] to access the linked data directly."
348-
)))
349-
else
350-
throw(KeyError(name)) # Uninitialized hard link
351-
end
352-
end
353-
325+
offset = getoffset(g, link)
354326
return get_dataset(f, offset, g, name)
355327
end
356328

src/groups.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ function lookup_link(g::Group, name::AbstractString)
3232
return get(g.unwritten_links, name, Link())
3333
end
3434

35+
36+
"""
37+
getoffset(group::Group, link::Link; erroroninvalid::Bool=true) -> RelOffset
38+
39+
Get the offset for a link by resolving it if necessary.
40+
41+
- **Hard links**: Returns `link.offset` directly (may be UNDEFINED_ADDRESS)
42+
- **Soft links**: Recursively resolves the soft link path and returns the target offset
43+
- **External links**: Errors if `erroroninvalid=true`, otherwise returns UNDEFINED_ADDRESS
44+
45+
This centralizes link resolution logic and simplifies calling code.
46+
"""
47+
function getoffset(g::Group, link::Link; erroroninvalid::Bool=true)
48+
if is_hard_link(link)
49+
return link.offset
50+
elseif is_soft_link(link)
51+
# Recursively resolve soft link
52+
target_link = lookup_link(g, link.path)
53+
if !is_set(target_link)
54+
erroroninvalid && throw(ArgumentError("Soft link target not found: $(link.path)"))
55+
return UNDEFINED_ADDRESS
56+
end
57+
# Recursively resolve in case target is also a soft link
58+
return getoffset(g, target_link; erroroninvalid)
59+
else # external link
60+
erroroninvalid && throw(ArgumentError("Cannot get offset for external link to $(link.external_file):$(link.path)"))
61+
return UNDEFINED_ADDRESS
62+
end
63+
end
64+
3565
"""
3666
pathize(g::Group, name::AbstractString, create::Bool) -> Tuple{Group,String}
3767

src/object_headers.jl

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,8 @@ function print_header_messages(g::Group, name::AbstractString)
8585
# This is the group itself
8686
roffset = g.f.root_group_offset
8787
else
88-
# Use lookup_link and getoffset for simplified resolution
8988
link = lookup_link(g, name)
90-
link === nothing && throw(ArgumentError("did not find a group or dataset named \"$name\""))
91-
92-
if is_external_link(link)
93-
throw(ArgumentError("Cannot print header messages for external link pointing to $(link.external_file):$(link.path)"))
94-
end
95-
96-
# Get the offset (resolves soft links)
97-
roffset = getoffset(g, link; erroroninvalid=false)
98-
if roffset == UNDEFINED_ADDRESS
99-
if is_soft_link(link)
100-
throw(ArgumentError("Cannot print header messages for soft link pointing to $(link.path)"))
101-
else
102-
throw(ArgumentError("Cannot print header messages for uninitialized hard link"))
103-
end
104-
end
89+
roffset = getoffset(g, link; erroroninvalid=true)
10590
end
10691
return print_header_messages(g.f, roffset)
10792
end

0 commit comments

Comments
 (0)