Module: HDF5::Hierarchy

Included in:
File, Group
Defined in:
lib/hdf5/hierarchy.rb

Instance Method Summary collapse

Instance Method Details

Raises:



65
66
67
68
69
70
71
# File 'lib/hdf5/hierarchy.rb', line 65

def create_hard_link(target, name)
  status = HDF5::FFI.H5Lcreate_hard(hdf5_id, target, hdf5_id, name, HDF5::DEFAULT_PROPERTY_LIST,
                                    HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to create hard link: #{name}" if status < 0

  self
end

Raises:



73
74
75
76
77
78
79
# File 'lib/hdf5/hierarchy.rb', line 73

def create_soft_link(target, name)
  status = HDF5::FFI.H5Lcreate_soft(target, hdf5_id, name, HDF5::DEFAULT_PROPERTY_LIST,
                                    HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to create soft link: #{name}" if status < 0

  self
end

#delete(name) ⇒ Object

Raises:



58
59
60
61
62
63
# File 'lib/hdf5/hierarchy.rb', line 58

def delete(name)
  status = HDF5::FFI.H5Ldelete(hdf5_id, name, HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to delete link: #{name}" if status < 0

  self
end

#each_key(&block) ⇒ Object



7
8
9
10
11
12
# File 'lib/hdf5/hierarchy.rb', line 7

def each_key(&block)
  return enum_for(__method__) unless block

  keys.each(&block)
  self
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



51
52
53
54
55
56
# File 'lib/hdf5/hierarchy.rb', line 51

def key?(name)
  exists = HDF5::FFI.H5Lexists(hdf5_id, name, HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to check link existence: #{name}" if exists.negative?

  exists.positive?
end

#keysObject



3
4
5
# File 'lib/hdf5/hierarchy.rb', line 3

def keys
  list_entries
end

Raises:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hdf5/hierarchy.rb', line 81

def link_info(name)
  info = HDF5::FFI::MiV == 10 ? HDF5::FFI::H5LInfoT.new : HDF5::FFI::H5LInfo2T.new
  status = if HDF5::FFI::MiV == 10
             HDF5::FFI.H5Lget_info(hdf5_id, name, info, HDF5::DEFAULT_PROPERTY_LIST)
           else
             HDF5::FFI.H5Lget_info2(hdf5_id, name, info, HDF5::DEFAULT_PROPERTY_LIST)
           end
  raise NativeError, "Failed to get link information: #{name}" if status < 0

  type = info[:type]
  return { type: :hard } if type == :H5L_TYPE_HARD

  size = info[:u][:val_size]
  buffer = ::FFI::MemoryPointer.new(:char, size)
  status = HDF5::FFI.H5Lget_val(hdf5_id, name, buffer, size, HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to get link value: #{name}" if status < 0

  raw = buffer.read_bytes(size)
  return { type: :soft, target: raw.delete_suffix("\0") } if type == :H5L_TYPE_SOFT

  if type == :H5L_TYPE_EXTERNAL
    filename, path = raw.byteslice(1..).split("\0", 2)
    return { type: :external, filename:, path: path&.delete_suffix("\0") }
  end

  raise UnsupportedTypeError, "Unsupported link type: #{type}"
end

#move(source, destination) ⇒ Object

Raises:



109
110
111
112
113
114
115
# File 'lib/hdf5/hierarchy.rb', line 109

def move(source, destination)
  status = HDF5::FFI.H5Lmove(hdf5_id, source, hdf5_id, destination, HDF5::DEFAULT_PROPERTY_LIST,
                             HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to move link: #{source}" if status < 0

  self
end

#open_dataset(name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hdf5/hierarchy.rb', line 33

def open_dataset(name)
  object = self[name]
  unless object.is_a?(Dataset)
    begin
      raise UnsupportedTypeError, "Object is not a dataset: #{name}"
    ensure
      Native.close_object(object)
    end
  end
  return object unless block_given?

  begin
    yield object
  ensure
    Native.close_object(object)
  end
end

#require_group(path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hdf5/hierarchy.rb', line 14

def require_group(path)
  path.split('/').reject(&:empty?).inject(self) do |parent, name|
    if parent.key?(name)
      child = parent[name]
      unless child.is_a?(Group)
        begin
          raise UnsupportedTypeError, "Existing object is not a group: #{name}"
        ensure
          Native.close_object(child)
        end
      end

      child
    else
      parent.create_group(name)
    end
  end
end