Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit d95cb55

Browse files
author
Nicolas Cornu
authored
Add a Property for LinkCreationOrder (#683)
1 parent a6e5236 commit d95cb55

File tree

5 files changed

+128
-3
lines changed

5 files changed

+128
-3
lines changed

include/highfive/H5PropertyList.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,42 @@ class MpioNoCollectiveCause {
563563
};
564564
#endif
565565

566+
struct CreationOrder {
567+
enum _CreationOrder {
568+
Tracked = H5P_CRT_ORDER_TRACKED,
569+
Indexed = H5P_CRT_ORDER_INDEXED,
570+
};
571+
};
572+
573+
///
574+
/// \brief Track and index creation order time
575+
///
576+
/// Let user retrieve objects by creation order time instead of name.
577+
///
578+
class LinkCreationOrder {
579+
public:
580+
///
581+
/// \brief Create the property
582+
/// \param flags Should be a composition of HighFive::CreationOrder.
583+
///
584+
explicit LinkCreationOrder(unsigned flags)
585+
: _flags(flags) {}
586+
587+
explicit LinkCreationOrder(const FileCreateProps& fcpl);
588+
explicit LinkCreationOrder(const GroupCreateProps& gcpl);
589+
590+
unsigned getFlags() const;
591+
592+
protected:
593+
void fromPropertyList(hid_t hid);
594+
595+
private:
596+
friend FileCreateProps;
597+
friend GroupCreateProps;
598+
void apply(hid_t hid) const;
599+
unsigned _flags;
600+
};
601+
566602
} // namespace HighFive
567603

568604
#include "bits/H5PropertyList_misc.hpp"

include/highfive/bits/H5Node_traits.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
namespace HighFive {
1818

19+
enum class IndexType : std::underlying_type<H5_index_t>::type {
20+
NAME = H5_INDEX_NAME,
21+
CRT_ORDER = H5_INDEX_CRT_ORDER,
22+
};
23+
1924
///
2025
/// \brief NodeTraits: Base class for Group and File
2126
///
@@ -146,8 +151,11 @@ class NodeTraits {
146151

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

152160
///
153161
/// \brief check a dataset or group exists in the current node / group

include/highfive/bits/H5Node_traits_misc.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ inline bool NodeTraits<Derivate>::rename(const std::string& src_path,
219219
}
220220

221221
template <typename Derivate>
222-
inline std::vector<std::string> NodeTraits<Derivate>::listObjectNames() const {
222+
inline std::vector<std::string> NodeTraits<Derivate>::listObjectNames(IndexType idx_type) const {
223223
std::vector<std::string> names;
224224
details::HighFiveIterateData iterateData(names);
225225

226226
size_t num_objs = getNumberObjects();
227227
names.reserve(num_objs);
228228

229229
if (H5Literate(static_cast<const Derivate*>(this)->getId(),
230-
H5_INDEX_NAME,
230+
static_cast<H5_index_t>(idx_type),
231231
H5_ITER_INC,
232232
NULL,
233233
&details::internal_high_five_iterate<H5L_info_t>,

include/highfive/bits/H5PropertyList_misc.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,28 @@ inline std::pair<uint32_t, uint32_t> MpioNoCollectiveCause::getCause() const {
519519
}
520520
#endif
521521

522+
inline LinkCreationOrder::LinkCreationOrder(const FileCreateProps& fcpl) {
523+
fromPropertyList(fcpl.getId());
524+
}
525+
526+
inline LinkCreationOrder::LinkCreationOrder(const GroupCreateProps& gcpl) {
527+
fromPropertyList(gcpl.getId());
528+
}
529+
530+
inline unsigned LinkCreationOrder::getFlags() const {
531+
return _flags;
532+
}
533+
534+
inline void LinkCreationOrder::LinkCreationOrder::apply(const hid_t hid) const {
535+
if (H5Pset_link_creation_order(hid, _flags) < 0) {
536+
HDF5ErrMapper::ToException<PropertyException>("Error setting LinkCreationOrder.");
537+
}
538+
}
539+
540+
inline void LinkCreationOrder::fromPropertyList(hid_t hid) {
541+
if (H5Pget_link_creation_order(hid, &_flags) < 0) {
542+
HDF5ErrMapper::ToException<PropertyException>(
543+
"Error getting property for link creation order");
544+
}
545+
}
522546
} // namespace HighFive

tests/unit/tests_high_five_base.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,63 @@ TEST_CASE("HighFivePropertyObjects") {
18671867
CHECK(plist_g2.isValid());
18681868
}
18691869

1870+
TEST_CASE("HighFiveLinkCreationOrderProperty") {
1871+
{ // For file
1872+
const std::string FILE_NAME("h5_keep_creation_order_file.h5");
1873+
FileCreateProps keepCreationOrder{};
1874+
keepCreationOrder.add(LinkCreationOrder(CreationOrder::Tracked | CreationOrder::Indexed));
1875+
1876+
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate, keepCreationOrder);
1877+
file.createGroup("1");
1878+
file.createGroup("2");
1879+
file.createGroup("10");
1880+
1881+
CHECK(file.listObjectNames(IndexType::CRT_ORDER) ==
1882+
std::vector<std::string>{"1", "2", "10"});
1883+
CHECK(file.listObjectNames(IndexType::NAME) == std::vector<std::string>{"1", "10", "2"});
1884+
1885+
auto fcpl = file.getCreatePropertyList();
1886+
LinkCreationOrder linkCreationOrder(fcpl);
1887+
CHECK((linkCreationOrder.getFlags() & CreationOrder::Tracked) != 0);
1888+
CHECK((linkCreationOrder.getFlags() & CreationOrder::Indexed) != 0);
1889+
}
1890+
{ // For groups
1891+
const std::string FILE_NAME("h5_keep_creation_order_group.h5");
1892+
GroupCreateProps keepCreationOrder{};
1893+
keepCreationOrder.add(LinkCreationOrder(CreationOrder::Tracked | CreationOrder::Indexed));
1894+
1895+
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);
1896+
auto group = file.createGroup("group_crt", keepCreationOrder);
1897+
group.createGroup("1");
1898+
group.createGroup("2");
1899+
group.createGroup("10");
1900+
1901+
CHECK(group.listObjectNames(IndexType::CRT_ORDER) ==
1902+
std::vector<std::string>{"1", "2", "10"});
1903+
CHECK(group.listObjectNames(IndexType::NAME) == std::vector<std::string>{"1", "10", "2"});
1904+
1905+
auto group2 = file.createGroup("group_name");
1906+
group2.createGroup("1");
1907+
group2.createGroup("2");
1908+
group2.createGroup("10");
1909+
1910+
CHECK(group2.listObjectNames() == std::vector<std::string>{"1", "10", "2"});
1911+
1912+
{
1913+
auto gcpl = group.getCreatePropertyList();
1914+
LinkCreationOrder linkCreationOrder(gcpl);
1915+
CHECK((linkCreationOrder.getFlags() & CreationOrder::Tracked) != 0);
1916+
CHECK((linkCreationOrder.getFlags() & CreationOrder::Indexed) != 0);
1917+
}
1918+
{
1919+
auto gcpl = group2.getCreatePropertyList();
1920+
LinkCreationOrder linkCreationOrder(gcpl);
1921+
CHECK((linkCreationOrder.getFlags() & CreationOrder::Tracked) == 0);
1922+
CHECK((linkCreationOrder.getFlags() & CreationOrder::Indexed) == 0);
1923+
}
1924+
}
1925+
}
1926+
18701927
struct CSL1 {
18711928
int m1;
18721929
int m2;

0 commit comments

Comments
 (0)