Skip to content

Commit 7afb99d

Browse files
committed
Use H5Lexists to be more efficient in lookup
1 parent 184f2a6 commit 7afb99d

1 file changed

Lines changed: 17 additions & 27 deletions

File tree

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -442,39 +442,29 @@ void HDF5IOHandlerImpl::createDataset(
442442
// The dataset might already exist in the file from a previous run
443443
// We delete it, otherwise we could not create it again with
444444
// possibly different parameters.
445-
// This is inefficient, as only the link to the dataset will be
446-
// removed, but not the actual dataset
447-
// But such is life if overwriting an iteration in Append mode
448-
H5G_info_t group_info;
449-
herr_t status = H5Gget_info(node_id, &group_info);
450-
VERIFY(
451-
status == 0,
452-
"[HDF5] Internal error: Failed to get HDF5 group info for " +
453-
concrete_h5_file_position(writable) +
454-
" during dataset creation");
455-
for (hsize_t i = 0; i < group_info.nlinks; ++i)
445+
if (htri_t link_id = H5Lexists(node_id, name.c_str(), H5P_DEFAULT);
446+
link_id > 0)
456447
{
457-
if (H5G_DATASET != H5Gget_objtype_by_idx(node_id, i))
458-
{
459-
continue;
460-
}
461-
ssize_t name_length =
462-
H5Gget_objname_by_idx(node_id, i, nullptr, 0);
463-
std::vector<char> charbuffer(name_length + 1);
464-
H5Gget_objname_by_idx(
465-
node_id, i, charbuffer.data(), name_length + 1);
466-
if (std::strncmp(
467-
name.c_str(), charbuffer.data(), name_length + 1) != 0)
468-
{
469-
continue;
470-
}
471-
status = H5Ldelete(node_id, name.c_str(), H5P_DEFAULT);
448+
// This only unlinks, but does not delete the dataset
449+
// Deleting the actual dataset physically is now up to HDF5:
450+
// > when removing an object with H5Ldelete, the HDF5 library
451+
// > should be able to detect and recycle the file space when no
452+
// > other reference to the deleted object exists
453+
// https://github.com/openPMD/openPMD-api/pull/1007#discussion_r867223316
454+
herr_t status = H5Ldelete(node_id, name.c_str(), H5P_DEFAULT);
472455
VERIFY(
473456
status == 0,
474457
"[HDF5] Internal error: Failed to delete old dataset '" +
475458
name + "' from group for overwriting.");
476-
break;
477459
}
460+
else if (link_id < 0)
461+
{
462+
throw std::runtime_error(
463+
"[HDF5] Internal error: Failed to check for link existence "
464+
"of '" +
465+
name + "' inside group for overwriting.");
466+
}
467+
// else: link_id == 0: Link does not exist, nothing to do
478468
}
479469

480470
Datatype d = parameters.dtype;

0 commit comments

Comments
 (0)