Question

I'm using HDF5 file to do a sort of "serialization" of C++ class data. Is there a way to transverse an HDF5 file visiting each dataset in the same order they were created?

I chose HDF5 because I need to access the data from outside my program as well as to be able to resume my program state.

Thanks!

Was it helpful?

Solution

For me it is enough to be able to traverse the datasets in a group (and possibly its subgroups) in the same order they were created. To achieve this, (like embert said above) the groups within the file must be created using the H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED flags set. That is, call:

hid_t group_creation_plist;
group_creation_plist = H5Pcreate(H5P_GROUP_CREATE);
status = H5Pset_link_creation_order(group_creation_plist, 
                                 H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED);

And then create groups using:

hid_t group_id;
group_id = H5Gcreate(id_file, 
                     "/mydata", 
                     H5P_DEFAULT, 
                     group_creation_plist, 
                     H5P_DEFAULT);

Add all your data to your new group. To traverse the group in sorted creation order you must use the H5Literate function, making sure to set the index_type argument to H5_INDEX_CRT_ORDER.

    status = H5Literate (group_id, 
                         H5_INDEX_CRT_ORDER, // Note this argument
                         H5_ITER_INC, 
                         NULL, 
                         op_func,
                     (void *) &od);

It is important to start the iteration from the group, and not from root. I have not been able to make the root group maintain the order of creation of links. The solution to this is just to make a group at the root with the appropriate flags.

Using H5Literate is a bit tricky, specially if one wants to do recursive traversal of subgroups. A good example is found here and in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top