Skip to content

Commit

Permalink
add additional tests and refactor io functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Aug 7, 2024
1 parent 3a9aa90 commit 51d4c52
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
32 changes: 15 additions & 17 deletions src/hdf5/HDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ Status HDF5IO::open(bool newfile)

Status HDF5IO::close()
{
file = nullptr;
opened = false;
if (this->file != nullptr && opened) {
this->file->close();
this->file = nullptr;
this->opened = false;
}

return Status::Success;
}
Expand Down Expand Up @@ -403,10 +406,8 @@ Status HDF5IO::stopRecording()
// if SWMR mode is disabled, stopping the recording will leave the file open
if (!disableSWMRMode) {
close(); // SWMR mode cannot be disabled so close the file
}
else
{
H5Fflush(this->file->getId(), H5F_SCOPE_GLOBAL); // flush all data to disk
} else {
H5Fflush(this->file->getId(), H5F_SCOPE_GLOBAL); // flush all data to disk
}
return Status::Success;
}
Expand All @@ -416,21 +417,18 @@ bool HDF5IO::canModifyObjects()
if (!opened)
return false;

// Get the file access intent
// Check if we are in SWMR mode
bool inSWMRMode = false;
unsigned int intent;
herr_t status = H5Fget_intent(this->file->getId(), &intent);
if (status < 0) {
return false; // We could not access the file so modifying objects is not
// going to work
bool statusOK = (status >= 0);
if (statusOK) {
inSWMRMode = (intent & (H5F_ACC_SWMR_READ | H5F_ACC_SWMR_WRITE));
}

// Check if SWMR mode is enabled
if (intent & (H5F_ACC_SWMR_READ | H5F_ACC_SWMR_WRITE)) {
return false; // File is in SWMR mode
} else {
return true; // File is not in SWMR mode
}
return true;
// if the file is opened and we are not in swmr mode then we can modify
// objects
return statusOK && !inSWMRMode;
}

std::unique_ptr<AQNWB::BaseRecordingData> HDF5IO::getDataSet(
Expand Down
3 changes: 2 additions & 1 deletion src/hdf5/HDF5IO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ class HDF5IO : public BaseIO

private:
std::unique_ptr<H5::H5File> file;
bool disableSWMRMode; // when set do not use SWMR mode when opening the HDF5 file
bool disableSWMRMode; // when set do not use SWMR mode when opening the HDF5
// file
};

/**
Expand Down
25 changes: 24 additions & 1 deletion tests/testHDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,29 @@ TEST_CASE("SWMRmode", "[hdf5io]")
// restarted
status = hdf5io->stopRecording();
REQUIRE(hdf5io->isOpen() == true);
REQUIRE(hdf5io->startRecording() == Status::Success);

// restart recording and write to a dataset
Status statusRestart = hdf5io->startRecording();
REQUIRE(statusRestart == Status::Success);

std::string dataPathPostRestart = "/dataPostRestart/data";
hdf5io->createGroup("/dataPostRestart");
std::unique_ptr<BaseRecordingData> datasetPostRestart =
hdf5io->createArrayDataSet(BaseDataType::I32,
SizeArray {0},
SizeArray {1},
dataPathPostRestart);

for (int b = 0; b <= numBlocks; b++) {
// write data block and flush to file
std::vector<SizeType> dataShape = {numSamples};
datasetPostRestart->writeDataBlock(
dataShape, BaseDataType::I32, &testData[0]);
H5Dflush(static_cast<HDF5::HDF5RecordingData*>(datasetPostRestart.get())
->getDataSet()
->getId());
}

hdf5io->close();
}
}

0 comments on commit 51d4c52

Please sign in to comment.