Skip to content

Commit

Permalink
Add a Property for LinkCreationOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed Feb 4, 2023
1 parent 4e5e971 commit 2202c1f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
26 changes: 26 additions & 0 deletions include/highfive/H5PropertyList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,32 @@ class UseCollectiveIO {
};
#endif

enum CreationOrder {
Tracked = H5P_CRT_ORDER_TRACKED,
Indexed = H5P_CRT_ORDER_INDEXED,
};

///
/// \brief Track and index creation order time
///
/// Let user retrieve objects by creation order time instead of name.
///
class LinkCreationOrder {
public:
///
/// \brief Create the property
/// \param flags Should be a composition of HighFive::CreationOrder.
///
explicit LinkCreationOrder(unsigned flags)
: _flags(flags) {}

private:
friend FileCreateProps;
friend GroupCreateProps;
void apply(hid_t hid) const;
unsigned _flags;
};

} // namespace HighFive

#include "bits/H5PropertyList_misc.hpp"
10 changes: 9 additions & 1 deletion include/highfive/bits/H5Node_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

namespace HighFive {

enum class IndexType: std::underlying_type<H5_index_t>::type {
NAME = H5_INDEX_NAME,
CRT_ORDER = H5_INDEX_CRT_ORDER,
};

///
/// \brief NodeTraits: Base class for Group and File
///
Expand Down Expand Up @@ -146,8 +151,11 @@ class NodeTraits {

///
/// \brief list all leaf objects name of the node / group
/// \param idx_type tell if the list should be ordered by Name or CreationOrderTime.
/// CreationOrderTime can be use only if the file/group has been created with
/// the HighFive::LinkCreationTime property.
/// \return number of leaf objects
std::vector<std::string> listObjectNames() const;
std::vector<std::string> listObjectNames(IndexType idx_type = IndexType::NAME) const;

///
/// \brief check a dataset or group exists in the current node / group
Expand Down
4 changes: 2 additions & 2 deletions include/highfive/bits/H5Node_traits_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ inline bool NodeTraits<Derivate>::rename(const std::string& src_path,
}

template <typename Derivate>
inline std::vector<std::string> NodeTraits<Derivate>::listObjectNames() const {
inline std::vector<std::string> NodeTraits<Derivate>::listObjectNames(IndexType idx_type) const {
std::vector<std::string> names;
details::HighFiveIterateData iterateData(names);

size_t num_objs = getNumberObjects();
names.reserve(num_objs);

if (H5Literate(static_cast<const Derivate*>(this)->getId(),
H5_INDEX_NAME,
static_cast<H5_index_t>(idx_type),
H5_ITER_INC,
NULL,
&details::internal_high_five_iterate<H5L_info_t>,
Expand Down
5 changes: 5 additions & 0 deletions include/highfive/bits/H5PropertyList_misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,9 @@ inline void UseCollectiveIO::apply(const hid_t hid) const {
}
#endif

inline void LinkCreationOrder::apply(const hid_t hid) const {
if (H5Pset_link_creation_order(hid, _flags) < 0) {
HDF5ErrMapper::ToException<PropertyException>("Error setting LinkCreationOrder.");
}
}
} // namespace HighFive
37 changes: 37 additions & 0 deletions tests/unit/tests_high_five_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,43 @@ TEST_CASE("HighFivePropertyObjects") {
CHECK(plist_g2.isValid());
}

TEST_CASE("HighFiveLinkCreationOrderProperty") {
{ // For file
const std::string FILE_NAME("h5_keep_creation_order_file.h5");
FileCreateProps keepCreationOrder{};
keepCreationOrder.add(LinkCreationOrder(Tracked | Indexed));

File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate, keepCreationOrder);
file.createGroup("1");
file.createGroup("2");
file.createGroup("10");

CHECK(file.listObjectNames(IndexType::CRT_ORDER) == std::vector<std::string>{"1", "2", "10"});
CHECK(file.listObjectNames(IndexType::NAME) == std::vector<std::string>{"1", "10", "2"});
}
{ // For groups
const std::string FILE_NAME("h5_keep_creation_order_group.h5");
GroupCreateProps keepCreationOrder{};
keepCreationOrder.add(LinkCreationOrder(Tracked | Indexed));

File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);
auto group = file.createGroup("group_crt", keepCreationOrder);
group.createGroup("1");
group.createGroup("2");
group.createGroup("10");

CHECK(group.listObjectNames(IndexType::CRT_ORDER) == std::vector<std::string>{"1", "2", "10"});
CHECK(group.listObjectNames(IndexType::NAME) == std::vector<std::string>{"1", "10", "2"});

auto group2 = file.createGroup("group_name");
group2.createGroup("1");
group2.createGroup("2");
group2.createGroup("10");

CHECK(group2.listObjectNames() == std::vector<std::string>{"1", "10", "2"});
}
}

struct CSL1 {
int m1;
int m2;
Expand Down

0 comments on commit 2202c1f

Please sign in to comment.