Skip to content

Commit 9263751

Browse files
committed
Implement attribute overwrite functionality
1 parent 98fc4a9 commit 9263751

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/hdf5/attribute.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ def write(attr_name, value)
7171
values = normalize_data(value)
7272
datatype_id = datatype_id_for(values)
7373

74+
exists = HDF5::FFI.H5Aexists(@dataset_id, attr_name)
75+
raise HDF5::Error, "Failed to check attribute existence: #{attr_name}" if exists.negative?
76+
77+
if exists.positive?
78+
status = HDF5::FFI.H5Adelete(@dataset_id, attr_name)
79+
raise HDF5::Error, "Failed to replace attribute: #{attr_name}" if status < 0
80+
end
81+
7482
dims = ::FFI::MemoryPointer.new(:ulong_long, 1)
7583
dims.write_array_of_ulong_long([values.length])
7684
dataspace_id = HDF5::FFI.H5Screate_simple(1, dims, nil)

test/hdf5_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ class HDF5Test < Test::Unit::TestCase
105105
end
106106
end
107107

108+
test 'dataset attrs overwrite existing value' do
109+
Dir.mktmpdir do |dir|
110+
path = File.join(dir, 'attribute-overwrite.h5')
111+
112+
HDF5::File.create(path) do |file|
113+
dataset = file.create_dataset('values', [1, 2, 3])
114+
dataset.attrs['scale'] = 42
115+
dataset.attrs['scale'] = 100
116+
end
117+
118+
HDF5::File.open(path) do |file|
119+
assert_equal([100], file['values'].attrs['scale'])
120+
end
121+
end
122+
end
123+
108124
test 'group list_datasets returns only datasets' do
109125
Dir.mktmpdir do |dir|
110126
path = File.join(dir, 'group-list.h5')

0 commit comments

Comments
 (0)