aqnwb 0.1.0
Loading...
Searching...
No Matches
HDF5IO.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <memory>
5#include <string>
6
7#include <H5Opublic.h>
8
9#include "Types.hpp"
10#include "io/BaseIO.hpp"
11#include "io/ReadIO.hpp"
12
13namespace H5
14{
15class DataSet;
16class Attribute;
17class H5File;
18class DataType;
19class Exception;
20class CommonFG;
21class PredType;
22class DataSpace;
23} // namespace H5
24
30{
31
32class HDF5RecordingData; // forward declaration
33
38class HDF5IO : public BaseIO
39{
40public:
50 HDF5IO(const std::string& fileName, const bool disableSWMRMode = false);
51
55 ~HDF5IO();
56
61 Status open() override;
62
68 Status open(FileMode mode) override;
69
74 Status close() override;
75
80 Status flush() override;
81
90 StorageObjectType getStorageObjectType(std::string path) const override;
91
106 const std::string& dataPath,
107 const std::vector<SizeType>& start = {},
108 const std::vector<SizeType>& count = {},
109 const std::vector<SizeType>& stride = {},
110 const std::vector<SizeType>& block = {}) override;
111
126 const std::string& dataPath) const override;
127
138 const void* data,
139 const std::string& path,
140 const std::string& name,
141 const SizeType& size = 1) override;
142
150 Status createAttribute(const std::string& data,
151 const std::string& path,
152 const std::string& name) override;
153
161 Status createAttribute(const std::vector<std::string>& data,
162 const std::string& path,
163 const std::string& name) override;
164
173 Status createAttribute(const std::vector<const char*>& data,
174 const std::string& path,
175 const std::string& name,
176 const SizeType& maxSize) override;
177
185 Status createReferenceAttribute(const std::string& referencePath,
186 const std::string& path,
187 const std::string& name) override;
188
194 Status createGroup(const std::string& path) override;
195
203 Status createLink(const std::string& path,
204 const std::string& reference) override;
205
212 Status createStringDataSet(const std::string& path,
213 const std::string& value) override;
214
221 Status createStringDataSet(const std::string& path,
222 const std::vector<std::string>& values) override;
223
232 const std::string& path,
233 const std::vector<std::string>& references) override;
234
239 Status startRecording() override;
240
245 Status stopRecording() override;
246
253 bool canModifyObjects() override;
254
264 std::unique_ptr<IO::BaseRecordingData> createArrayDataSet(
265 const IO::BaseDataType& type,
266 const SizeArray& size,
267 const SizeArray& chunking,
268 const std::string& path) override;
269
275 std::unique_ptr<IO::BaseRecordingData> getDataSet(
276 const std::string& path) override;
277
284 bool objectExists(const std::string& path) const override;
285
294 bool attributeExists(const std::string& path) const override;
295
313 virtual std::vector<std::pair<std::string, StorageObjectType>>
314 getStorageObjects(const std::string& path,
315 const StorageObjectType& objectType =
316 StorageObjectType::Undefined) const override;
317
324 H5O_type_t getH5ObjectType(const std::string& path) const;
325
336 static H5::DataType getNativeType(IO::BaseDataType type);
337
346 static IO::BaseDataType getBaseDataType(const H5::DataType& nativeType);
347
357 static H5::DataType getH5Type(IO::BaseDataType type);
358
359protected:
365 Status createGroupIfDoesNotExist(const std::string& path) override;
366
367private:
385 template<typename T, typename HDF5TYPE>
386 std::vector<T> readDataHelper(const HDF5TYPE& dataSource,
387 size_t numElements,
388 const H5::DataSpace& memspace,
389 const H5::DataSpace& dataspace) const;
390
408 template<typename T, typename HDF5TYPE>
409 std::vector<T> readDataHelper(const HDF5TYPE& dataSource,
410 size_t numElements) const;
411
424 template<typename HDF5TYPE>
425 std::vector<std::string> readStringDataHelper(
426 const HDF5TYPE& dataSource,
427 size_t numElements,
428 const H5::DataSpace& memspace,
429 const H5::DataSpace& dataspace) const;
430
446 template<typename HDF5TYPE>
447 std::vector<std::string> readStringDataHelper(const HDF5TYPE& dataSource,
448 size_t numElements) const;
449
450 std::unique_ptr<H5::Attribute> getAttribute(const std::string& path) const;
451
455 std::unique_ptr<H5::H5File> m_file;
456
462};
463
464} // namespace AQNWB::IO::HDF5
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:19
AQNWB::Types::Status Status
Definition BaseIO.hpp:20
AQNWB::Types::SizeArray SizeArray
Definition BaseIO.hpp:21
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
Represents a base data type.
Definition BaseIO.hpp:41
BaseIO(const std::string &filename)
Constructor for the BaseIO class.
Definition BaseIO.cpp:35
Generic structure to hold type-erased data and shape.
Definition ReadIO.hpp:29
Status stopRecording() override
Stops the recording process.
Definition HDF5IO.cpp:946
bool m_disableSWMRMode
When set true, then do not switch to SWMR mode when starting the recording.
Definition HDF5IO.hpp:461
Status createGroup(const std::string &path) override
Creates a new group in the file.
Definition HDF5IO.cpp:807
static IO::BaseDataType getBaseDataType(const H5::DataType &nativeType)
Returns the BaseDataType for a given HDF5 native data type.
Definition HDF5IO.cpp:1198
Status createLink(const std::string &path, const std::string &reference) override
Creates a soft link to another location in the file.
Definition HDF5IO.cpp:839
Status createReferenceDataSet(const std::string &path, const std::vector< std::string > &references) override
Creates a dataset that holds an array of references to groups within the file.
Definition HDF5IO.cpp:854
bool attributeExists(const std::string &path) const override
Checks whether an Attribute exists at the location in the file.
Definition HDF5IO.cpp:986
virtual std::vector< std::pair< std::string, StorageObjectType > > getStorageObjects(const std::string &path, const StorageObjectType &objectType=StorageObjectType::Undefined) const override
Gets the list of storage objects (groups, datasets, attributes) inside a group.
Definition HDF5IO.cpp:993
StorageObjectType getStorageObjectType(std::string path) const override
Get the storage type (Group, Dataset, Attribute) of the object at path.
Definition HDF5IO.cpp:138
AQNWB::IO::DataBlockGeneric readAttribute(const std::string &dataPath) const override
Reads a attribute and determines the data type.
Definition HDF5IO.cpp:304
std::unique_ptr< IO::BaseRecordingData > createArrayDataSet(const IO::BaseDataType &type, const SizeArray &size, const SizeArray &chunking, const std::string &path) override
Creates an extendable dataset with a given base data type, size, chunking, and path.
Definition HDF5IO.cpp:1073
~HDF5IO()
Destructor for the HDF5IO class.
Definition HDF5IO.cpp:25
Status createAttribute(const IO::BaseDataType &type, const void *data, const std::string &path, const std::string &name, const SizeType &size=1) override
Creates an attribute at a given location in the file.
Definition HDF5IO.cpp:621
AQNWB::IO::DataBlockGeneric readDataset(const std::string &dataPath, const std::vector< SizeType > &start={}, const std::vector< SizeType > &count={}, const std::vector< SizeType > &stride={}, const std::vector< SizeType > &block={}) override
Reads a dataset or attribute and determines the data type.
Definition HDF5IO.cpp:436
Status startRecording() override
Start SWMR write to start recording process.
Definition HDF5IO.cpp:934
std::unique_ptr< H5::Attribute > getAttribute(const std::string &path) const
Definition HDF5IO.cpp:102
std::vector< std::string > readStringDataHelper(const HDF5TYPE &dataSource, size_t numElements, const H5::DataSpace &memspace, const H5::DataSpace &dataspace) const
Reads a variable-length string from an HDF5 dataset or attribute.
Definition HDF5IO.cpp:198
Status createReferenceAttribute(const std::string &referencePath, const std::string &path, const std::string &name) override
Sets an object reference attribute for a given location in the file.
Definition HDF5IO.cpp:759
Status close() override
Closes the file.
Definition HDF5IO.cpp:77
bool canModifyObjects() override
Checks whether the file is in a mode where objects can be added or deleted. Note, this does not apply...
Definition HDF5IO.cpp:957
std::unique_ptr< IO::BaseRecordingData > getDataSet(const std::string &path) override
Returns a pointer to a dataset at a given path.
Definition HDF5IO.cpp:1050
HDF5IO(const std::string &fileName, const bool disableSWMRMode=false)
Constructor for the HDF5IO class that takes a file name as input.
Definition HDF5IO.cpp:19
H5O_type_t getH5ObjectType(const std::string &path) const
Returns the HDF5 type of object at a given path.
Definition HDF5IO.cpp:1122
Status createStringDataSet(const std::string &path, const std::string &value) override
Creates a non-modifiable dataset with a string value.
Definition HDF5IO.cpp:897
static H5::DataType getH5Type(IO::BaseDataType type)
Returns the HDF5 data type for a given base data type.
Definition HDF5IO.cpp:1241
bool objectExists(const std::string &path) const override
Checks whether a Dataset, Group, or Link already exists at the location in the file.
Definition HDF5IO.cpp:976
std::vector< T > readDataHelper(const HDF5TYPE &dataSource, size_t numElements, const H5::DataSpace &memspace, const H5::DataSpace &dataspace) const
Reads data from an HDF5 dataset or attribute into a vector of the appropriate type.
Definition HDF5IO.cpp:166
static H5::DataType getNativeType(IO::BaseDataType type)
Returns the HDF5 native data type for a given base data type.
Definition HDF5IO.cpp:1147
Status open() override
Opens an existing file or creates a new file for writing.
Definition HDF5IO.cpp:30
Status flush() override
Flush data to disk.
Definition HDF5IO.cpp:96
Status createGroupIfDoesNotExist(const std::string &path) override
Creates a new group if it does not exist.
Definition HDF5IO.cpp:825
std::unique_ptr< H5::H5File > m_file
Unique pointer to the HDF5 file for reading.
Definition HDF5IO.hpp:455
Represents an HDF5 Dataset that can be extended indefinitely in blocks.
Definition HDF5RecordingData.hpp:21
Definition HDF5IO.hpp:30
FileMode
The access mode for the file.
Definition BaseIO.hpp:111
Definition HDF5IO.hpp:14