Skip to content

Commit 58e137a

Browse files
committed
Allow overwriting old datasets in HDF5
1 parent 4aff022 commit 58e137a

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,47 @@ HDF5IOHandlerImpl::createDataset(Writable* writable,
338338
H5P_DEFAULT);
339339
VERIFY(node_id >= 0, "[HDF5] Internal error: Failed to open HDF5 group during dataset creation");
340340

341+
if( m_handler->m_backendAccess == Access::APPEND )
342+
{
343+
// The dataset might already exist in the file from a previous run
344+
// We delete it, otherwise we could not create it again with
345+
// possibly different parameters.
346+
// This is inefficient, as only the link to the dataset will be
347+
// removed, but not the actual dataset
348+
// But such is life if overwriting an iteration in Append mode
349+
H5G_info_t group_info;
350+
herr_t status = H5Gget_info( node_id, &group_info );
351+
VERIFY(
352+
status == 0,
353+
"[HDF5] Internal error: Failed to get HDF5 group info for " +
354+
concrete_h5_file_position( writable ) +
355+
" during dataset creation" );
356+
for( hsize_t i = 0; i < group_info.nlinks; ++i )
357+
{
358+
if( H5G_DATASET != H5Gget_objtype_by_idx( node_id, i ) )
359+
{
360+
continue;
361+
}
362+
ssize_t name_length =
363+
H5Gget_objname_by_idx( node_id, i, nullptr, 0 );
364+
std::vector< char > charbuffer( name_length + 1 );
365+
H5Gget_objname_by_idx(
366+
node_id, i, charbuffer.data(), name_length + 1 );
367+
if( std::strncmp(
368+
name.c_str(), charbuffer.data(), name_length + 1 ) !=
369+
0 )
370+
{
371+
continue;
372+
}
373+
status = H5Ldelete( node_id, name.c_str(), H5P_DEFAULT );
374+
VERIFY(
375+
status == 0,
376+
"[HDF5] Internal error: Failed to delete old dataset from "
377+
"group for overwriting." );
378+
break;
379+
}
380+
}
381+
341382
Datatype d = parameters.dtype;
342383
if( d == Datatype::UNDEFINED )
343384
{

test/SerialIOTest.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4677,10 +4677,6 @@ TEST_CASE( "append_mode", "[serial]" )
46774677
{
46784678
for( auto const & t : testedFileExtensions() )
46794679
{
4680-
if( t == "h5" )
4681-
{
4682-
continue;
4683-
}
46844680
if( t == "bp" )
46854681
{
46864682
std::string jsonConfigOld = R"END(

0 commit comments

Comments
 (0)