Skip to content

Commit 06dee0c

Browse files
Move NexusDescriptor to Framework/Nexus
This is in preparation for using it inside of Nexus::File
1 parent e18e4b6 commit 06dee0c

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

Framework/Nexus/inc/MantidNexus/NexusDescriptor.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class MANTID_NEXUS_DLL NexusDescriptor {
9090
/// Query if a given type exists somewhere in the file
9191
bool classTypeExists(const std::string &classType) const;
9292

93+
void addEntry(const std::string &entryName, const std::string &groupClass);
94+
9395
private:
9496
/**
9597
* Sets m_allEntries, called in HDF5 constructor.
@@ -98,9 +100,9 @@ class MANTID_NEXUS_DLL NexusDescriptor {
98100
std::map<std::string, std::set<std::string>> initAllEntries();
99101

100102
/** NeXus HDF5 file name */
101-
std::string m_filename;
103+
const std::string m_filename;
102104
/// Extension
103-
std::string m_extension;
105+
const std::string m_extension;
104106
/// First entry name/type
105107
std::pair<std::string, std::string> m_firstEntryNameType;
106108
/// Root attributes
@@ -114,7 +116,7 @@ class MANTID_NEXUS_DLL NexusDescriptor {
114116
* (e.g. /entry/log)
115117
* </pre>
116118
*/
117-
const std::map<std::string, std::set<std::string>> m_allEntries;
119+
std::map<std::string, std::set<std::string>> m_allEntries;
118120
};
119121

120122
} // namespace Nexus

Framework/Nexus/src/NexusDescriptor.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ void getGroup(H5::Group groupID, std::map<std::string, std::set<std::string>> &a
6868
} // namespace
6969

7070
NexusDescriptor::NexusDescriptor(std::string filename)
71-
: m_filename(std::move(filename)), m_extension(), m_firstEntryNameType(), m_allEntries(initAllEntries()) {}
71+
: m_filename(std::move(filename)), m_extension(std::filesystem::path(m_filename).extension().string()),
72+
m_firstEntryNameType(), m_allEntries(initAllEntries()) {}
7273

7374
// PUBLIC
7475
const std::string &NexusDescriptor::filename() const noexcept { return m_filename; }
@@ -78,10 +79,31 @@ bool NexusDescriptor::hasRootAttr(const std::string &name) const { return (m_roo
7879
const std::map<std::string, std::set<std::string>> &NexusDescriptor::getAllEntries() const noexcept {
7980
return m_allEntries;
8081
}
82+
void NexusDescriptor::addEntry(const std::string &entryName, const std::string &groupClass) {
83+
// simple checks
84+
if (entryName.empty())
85+
throw std::runtime_error("Cannot add empty path");
86+
if (groupClass.empty())
87+
throw std::runtime_error("Cannot add empty class");
88+
if (entryName.find("/") != 0)
89+
throw std::runtime_error("Paths must be absolute: " + entryName);
90+
91+
// do not add path twice
92+
if (this->isEntry(entryName))
93+
throw std::runtime_error("Cannot add an entry twice: " + entryName);
94+
95+
// verify the parent exists
96+
const auto lastPos = entryName.rfind("/");
97+
const auto parentPath = entryName.substr(0, lastPos);
98+
if (!this->isEntry(parentPath))
99+
throw std::runtime_error("Parent path " + parentPath + " does not exist");
100+
101+
// add the path
102+
m_allEntries[groupClass].insert(entryName);
103+
}
81104

82105
// PRIVATE
83106
std::map<std::string, std::set<std::string>> NexusDescriptor::initAllEntries() {
84-
m_extension = std::filesystem::path(m_filename).extension().string();
85107
H5::FileAccPropList access_plist;
86108
access_plist.setFcloseDegree(H5F_CLOSE_STRONG);
87109

Framework/Nexus/test/NexusDescriptorTest.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,28 @@ class NexusDescriptorTest : public CxxTest::TestSuite {
9393
TS_ASSERT(nexusHDF5Descriptor.hasRootAttr("file_name"));
9494
TS_ASSERT(!nexusHDF5Descriptor.hasRootAttr("not_attr"));
9595
}
96+
97+
void test_AddEntry() {
98+
// create a descriptor with the correct values
99+
const std::string filename = getFullPath("EQSANS_89157.nxs.h5");
100+
Mantid::Nexus::NexusDescriptor nexusHDF5Descriptor(filename);
101+
102+
// verify that existing groups are there
103+
TS_ASSERT_EQUALS(nexusHDF5Descriptor.isEntry("/entry/DASlogs", "NXcollection"), true);
104+
TS_ASSERT_EQUALS(nexusHDF5Descriptor.isEntry("/entry/DASlogs/LambdaRequest", "NXlog"), true);
105+
TS_ASSERT_EQUALS(nexusHDF5Descriptor.isEntry("/entry/DASlogs/OmikronRequest", "NXlog"), false);
106+
107+
// can't add a value with relative path
108+
TS_ASSERT_THROWS(nexusHDF5Descriptor.addEntry("entry/DASlogs/OmikronRequest", "NXlog"), const std::runtime_error &);
109+
TS_ASSERT_EQUALS(nexusHDF5Descriptor.isEntry("/entry/DASlogs/OmikronRequest", "NXlog"), false);
110+
111+
// make sure you can't add a group with invalid parent
112+
TS_ASSERT_THROWS(nexusHDF5Descriptor.addEntry("/entry/DASlogginator/OmikronRequest", "NXlog"),
113+
const std::runtime_error &);
114+
TS_ASSERT_EQUALS(nexusHDF5Descriptor.isEntry("/entry/DASlogginator/OmikronRequest", "NXlog"), false);
115+
116+
// add a field correctly
117+
TS_ASSERT_THROWS_NOTHING(nexusHDF5Descriptor.addEntry("/entry/DASlogs/OmikronRequest", "NXlog"));
118+
TS_ASSERT_EQUALS(nexusHDF5Descriptor.isEntry("/entry/DASlogs/OmikronRequest", "NXlog"), true);
119+
}
96120
};

0 commit comments

Comments
 (0)