Skip to content

Commit 3e02c75

Browse files
authored
Merge pull request #250 from psychocoderHPC/topic-attributesForNotExistingGroups
Topic attributes for not existing groups
2 parents f9ebe2e + 1674897 commit 3e02c75

6 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/ParallelDataCollector.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ namespace splash
466466
if (ndims < 1u || ndims > DSP_DIM_MAX)
467467
throw DCException(getExceptionString("writeAttribute", "maximum dimension `ndims` is invalid"));
468468

469+
/* group_path: absolute path to the last inode
470+
* obj_name: last inode, can be a group or a dataset
471+
*/
469472
std::string group_path, obj_name;
470473
std::string dataNameInternal = "";
471474
if (dataName)
@@ -475,6 +478,16 @@ namespace splash
475478
DCParallelGroup group;
476479
if (dataName)
477480
{
481+
/* if the specified inode (obj_name) does not exist
482+
* (as dataset or group), create all missing groups along group_path
483+
* and even create an empty group for obj_name itself
484+
*
485+
* group_path + "/" + obj_name is the absolute path of dataName
486+
*/
487+
std::string pathAndName(group_path + "/" + obj_name);
488+
if(!DCParallelGroup::exists(handles.get(id), pathAndName))
489+
group.create(handles.get(id), pathAndName);
490+
478491
// attach attribute to the dataset or group
479492
group.open(handles.get(id), group_path);
480493
hid_t obj_id = H5Oopen(group.getHandle(), obj_name.c_str(), H5P_DEFAULT);

src/SerialDataCollector.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ namespace splash
382382
if (ndims < 1u || ndims > DSP_DIM_MAX)
383383
throw DCException(getExceptionString("writeAttribute", "maximum dimension `ndims` is invalid"));
384384

385+
/* group_path: absolute path to the last inode
386+
* obj_name: last inode, can be a group or a dataset
387+
*/
385388
std::string group_path, obj_name;
386389
std::string dataNameInternal = "";
387390
if (dataName)
@@ -391,6 +394,16 @@ namespace splash
391394
DCGroup group;
392395
if (dataName)
393396
{
397+
/* if the specified inode (obj_name) does not exist
398+
* (as dataset or group), create all missing groups along group_path
399+
* and even create an empty group for obj_name itself
400+
*
401+
* group_path + "/" + obj_name is the absolute path of dataName
402+
*/
403+
std::string pathAndName(group_path + "/" + obj_name);
404+
if(!DCGroup::exists(handles.get(id), pathAndName))
405+
group.create(handles.get(id), pathAndName);
406+
394407
// attach attribute to the dataset or group
395408
group.open(handles.get(0), group_path);
396409

src/include/splash/DataCollector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ namespace splash
366366
* @param type Type information for data.
367367
* @param dataName Name of the dataset in group \p id to write attribute to.
368368
* If dataName is NULL, the attribute is written for the iteration group.
369+
* If the path dataName does not yet exist, missing groups will be created.
369370
* @param attrName Name of the attribute.
370371
* @param buf Buffer to be written as attribute.
371372
*/

src/include/splash/core/DCGroup.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ namespace splash
5959
H5Handle openCreate(H5Handle base, std::string path) throw (DCException);
6060
void close() throw (DCException);
6161

62+
/** Check if a Group or Dataset Within a Group exist
63+
*
64+
* @param base open file handle
65+
* @param path to either a group or data set
66+
* @return true if either a dataset or a group exists at path
67+
*/
6268
static bool exists(H5Handle base, std::string path);
6369
static void remove(H5Handle base, std::string path) throw (DCException);
6470

tests/AttributesTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ void AttributesTest::testDataAttributes()
7878

7979
dataCollector->writeAttribute(10, ctInt, NULL, "iteration", &sum2);
8080

81+
int groupNotExistsTestValue = 42;
82+
/* check if it is possible to add an attribute to a not existing group */
83+
dataCollector->writeAttribute(0, ctInt, "notExistingGroup/", "magic_number", &groupNotExistsTestValue);
84+
8185
/* variable length string, '\0' terminated */
8286
const char *string_attr = {"My first c-string."};
8387
dataCollector->writeAttribute(10, ctString, NULL, "my_string", &string_attr);
@@ -124,6 +128,10 @@ void AttributesTest::testDataAttributes()
124128

125129
dataCollector->open(TEST_FILE, attr);
126130

131+
int readGroupNotExistsTestValue = 0;
132+
dataCollector->readAttribute(0, "notExistingGroup/", "magic_number", &readGroupNotExistsTestValue);
133+
CPPUNIT_ASSERT(groupNotExistsTestValue == readGroupNotExistsTestValue);
134+
127135
dataCollector->readAttribute(10, NULL, "iteration", &sum2);
128136

129137
char* string_read;

tests/Parallel_AttributesTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ void Parallel_AttributesTest::testDataAttributes()
8787
dummy_data[2 * i + 1] = val_y;
8888
}
8989

90+
int groupNotExistsTestValue = 42;
91+
/* check if it is possible to add an attribute to a not existing group */
92+
dataCollector->writeAttribute(0, ctInt, "notExistingGroup/", "magic_number", &groupNotExistsTestValue);
93+
9094
dataCollector->write(0, ctInt2, 1, Selection(Dimensions(BUF_SIZE, 1, 1)),
9195
"attr/attr2/attr3/data", dummy_data);
9296

@@ -120,6 +124,10 @@ void Parallel_AttributesTest::testDataAttributes()
120124

121125
dataCollector->open(TEST_FILE, attr);
122126

127+
int readGroupNotExistsTestValue = 0;
128+
dataCollector->readAttribute(0, "notExistingGroup/", "magic_number", &readGroupNotExistsTestValue);
129+
CPPUNIT_ASSERT(groupNotExistsTestValue == readGroupNotExistsTestValue);
130+
123131
sum = 0;
124132
neg_sum = 0;
125133
c = 'A';

0 commit comments

Comments
 (0)