Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code to highlevel.jl #1002

Merged
merged 4 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 2 additions & 119 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ module HDF5

using Base: unsafe_convert
using Requires: @require
using Mmap: Mmap
# needed for filter(f, tuple) in julia 1.3
using Compat

import Mmap

### PUBLIC API ###

export @read,
Expand Down Expand Up @@ -79,128 +78,12 @@ include("attributes.jl")
include("readwrite.jl")
include("references.jl")
include("show.jl")

### High-level interface ###

function h5write(filename, name::AbstractString, data; pv...)
file = h5open(filename, "cw"; pv...)
try
write(file, name, data)
finally
close(file)
end
end

function h5read(filename, name::AbstractString; pv...)
local dat
fapl = FileAccessProperties(; fclose_degree=:strong)
pv = setproperties!(fapl; pv...)
file = h5open(filename, "r", fapl)
try
obj = getindex(file, name; pv...)
dat = read(obj)
close(obj)
finally
close(file)
end
dat
end

function h5read(filename, name_type_pair::Pair{<:AbstractString,DataType}; pv...)
local dat
fapl = FileAccessProperties(; fclose_degree=:strong)
pv = setproperties!(fapl; pv...)
file = h5open(filename, "r", fapl)
try
obj = getindex(file, name_type_pair[1]; pv...)
dat = read(obj, name_type_pair[2])
close(obj)
finally
close(file)
end
dat
end

function h5read(
filename,
name::AbstractString,
indices::Tuple{Vararg{Union{AbstractRange{Int},Int,Colon}}};
pv...
)
local dat
fapl = FileAccessProperties(; fclose_degree=:strong)
pv = setproperties!(fapl; pv...)
file = h5open(filename, "r", fapl)
try
dset = getindex(file, name; pv...)
dat = dset[indices...]
close(dset)
finally
close(file)
end
dat
end

function Base.getindex(parent::Union{File,Group}, path::AbstractString; pv...)
haskey(parent, path) || throw(KeyError(path))
# Faster than below if defaults are OK
isempty(pv) && return open_object(parent, path)
obj_type = gettype(parent, path)
if obj_type == API.H5I_DATASET
dapl = DatasetAccessProperties()
dxpl = DatasetTransferProperties()
pv = setproperties!(dapl, dxpl; pv...)
isempty(pv) || error("invalid keyword options $pv")
return open_dataset(parent, path, dapl, dxpl)
elseif obj_type == API.H5I_GROUP
gapl = GroupAccessProperties(; pv...)
return open_group(parent, path, gapl)
else#if obj_type == API.H5I_DATATYPE # only remaining choice
tapl = DatatypeAccessProperties(; pv...)
return open_datatype(parent, path, tapl)
end
end

# Assign syntax: obj[path] = value
# Create a dataset with properties: obj[path, prop = val, ...] = val
function Base.setindex!(
parent::Union{File,Group}, val, path::Union{AbstractString,Nothing}; pv...
)
need_chunks = any(k in keys(chunked_props) for k in keys(pv))
have_chunks = any(k == :chunk for k in keys(pv))

chunk = need_chunks ? heuristic_chunk(val) : Int[]

# ignore chunked_props (== compression) for empty datasets (issue #246):
discard_chunks = need_chunks && isempty(chunk)
if discard_chunks
pv = pairs(Base.structdiff((; pv...), chunked_props))
else
if need_chunks && !have_chunks
pv = pairs((; chunk=chunk, pv...))
end
end
write(parent, path, val; pv...)
end

# end of high-level interface

include("api_midlevel.jl")

#API.h5s_get_simple_extent_ndims(space_id::API.hid_t) = API.h5s_get_simple_extent_ndims(space_id, C_NULL, C_NULL)
include("highlevel.jl")

# Functions that require special handling

const libversion = API.h5_get_libversion()

### Property manipulation ###
get_access_properties(d::Dataset) = DatasetAccessProperties(API.h5d_get_access_plist(d))
get_access_properties(f::File) = FileAccessProperties(API.h5f_get_access_plist(f))
get_create_properties(d::Dataset) = DatasetCreateProperties(API.h5d_get_create_plist(d))
get_create_properties(g::Group) = GroupCreateProperties(API.h5g_get_create_plist(g))
get_create_properties(f::File) = FileCreateProperties(API.h5f_get_create_plist(f))
get_create_properties(a::Attribute) = AttributeCreateProperties(API.h5a_get_create_plist(a))

const HAS_PARALLEL = Ref(false)

"""
Expand Down
110 changes: 110 additions & 0 deletions src/highlevel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
### High-level interface ###

function h5write(filename, name::AbstractString, data; pv...)
file = h5open(filename, "cw"; pv...)
try
write(file, name, data)
finally
close(file)
end
end

function h5read(filename, name::AbstractString; pv...)
local dat
fapl = FileAccessProperties(; fclose_degree=:strong)
pv = setproperties!(fapl; pv...)
file = h5open(filename, "r", fapl)
try
obj = getindex(file, name; pv...)
dat = read(obj)
close(obj)
finally
close(file)
end
dat
end

function h5read(filename, name_type_pair::Pair{<:AbstractString,DataType}; pv...)
local dat
fapl = FileAccessProperties(; fclose_degree=:strong)
pv = setproperties!(fapl; pv...)
file = h5open(filename, "r", fapl)
try
obj = getindex(file, name_type_pair[1]; pv...)
dat = read(obj, name_type_pair[2])
close(obj)
finally
close(file)
end
dat
end

function h5read(
filename,
name::AbstractString,
indices::Tuple{Vararg{Union{AbstractRange{Int},Int,Colon}}};
pv...
)
local dat
fapl = FileAccessProperties(; fclose_degree=:strong)
pv = setproperties!(fapl; pv...)
file = h5open(filename, "r", fapl)
try
dset = getindex(file, name; pv...)
dat = dset[indices...]
close(dset)
finally
close(file)
end
dat
end

function Base.getindex(parent::Union{File,Group}, path::AbstractString; pv...)
haskey(parent, path) || throw(KeyError(path))
# Faster than below if defaults are OK
isempty(pv) && return open_object(parent, path)
obj_type = gettype(parent, path)
if obj_type == API.H5I_DATASET
dapl = DatasetAccessProperties()
dxpl = DatasetTransferProperties()
pv = setproperties!(dapl, dxpl; pv...)
isempty(pv) || error("invalid keyword options $pv")
return open_dataset(parent, path, dapl, dxpl)
elseif obj_type == API.H5I_GROUP
gapl = GroupAccessProperties(; pv...)
return open_group(parent, path, gapl)
else#if obj_type == API.H5I_DATATYPE # only remaining choice
tapl = DatatypeAccessProperties(; pv...)
return open_datatype(parent, path, tapl)
end
end

# Assign syntax: obj[path] = value
# Create a dataset with properties: obj[path, prop = val, ...] = val
function Base.setindex!(
parent::Union{File,Group}, val, path::Union{AbstractString,Nothing}; pv...
)
need_chunks = any(k in keys(chunked_props) for k in keys(pv))
have_chunks = any(k == :chunk for k in keys(pv))

chunk = need_chunks ? heuristic_chunk(val) : Int[]

# ignore chunked_props (== compression) for empty datasets (issue #246):
discard_chunks = need_chunks && isempty(chunk)
if discard_chunks
pv = pairs(Base.structdiff((; pv...), chunked_props))
else
if need_chunks && !have_chunks
pv = pairs((; chunk=chunk, pv...))
end
end
write(parent, path, val; pv...)
end

### Property manipulation ###
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought why not at the end of https://github.com/JuliaIO/HDF5.jl/blob/master/src/properties.jl ? Seems more appropriate especially since that's where the *Properties types are defined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that. It does not work. We would need to move the property types up into types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh

get_access_properties(d::Dataset) = DatasetAccessProperties(API.h5d_get_access_plist(d))
get_access_properties(f::File) = FileAccessProperties(API.h5f_get_access_plist(f))
get_create_properties(d::Dataset) = DatasetCreateProperties(API.h5d_get_create_plist(d))
get_create_properties(g::Group) = GroupCreateProperties(API.h5g_get_create_plist(g))
get_create_properties(f::File) = FileCreateProperties(API.h5f_get_create_plist(f))
get_create_properties(a::Attribute) = AttributeCreateProperties(API.h5a_get_create_plist(a))