diff --git a/lib/src/H5Writer.cpp b/lib/src/H5Writer.cpp index c25eac1..f625262 100644 --- a/lib/src/H5Writer.cpp +++ b/lib/src/H5Writer.cpp @@ -243,6 +243,19 @@ bool H5Writer::is_file_open() const return (file.getId() != -1); } +inline size_t H5Writer::get_relative_data_index(const size_t data_index) +{ + // No file roll over. + if (frames_per_file == 0) { + return data_index; + } + + size_t destination_file_index = data_index / frames_per_file; + size_t relative_data_index = data_index - (destination_file_index * frames_per_file); + + return relative_data_index; +} + inline bool H5Writer::is_data_for_current_file(const size_t data_index) { if (frames_per_file) { @@ -260,15 +273,11 @@ inline bool H5Writer::is_data_for_current_file(const size_t data_index) hsize_t H5Writer::prepare_storage_for_data(const string& dataset_name, const size_t data_index, const std::vector& data_shape, const string& data_type, const string& endianness) { - hsize_t relative_data_index = data_index; - // Check if we have to create a new file. if (!is_data_for_current_file(data_index)) { + // Calculate to which file (1 based) the data_index belongs. hsize_t frame_chunk = (data_index / frames_per_file) + 1; create_file(frame_chunk); - - // Make the data_index relative to this chunk (file). - relative_data_index = data_index - ((frame_chunk - 1) * frames_per_file); } // Open the file if needed. @@ -283,6 +292,8 @@ hsize_t H5Writer::prepare_storage_for_data(const string& dataset_name, const siz hsize_t current_dataset_size = datasets_current_size.at(dataset_name); + hsize_t relative_data_index = get_relative_data_index(data_index); + // Expand the dataset if needed. if (relative_data_index > current_dataset_size) { auto dataset = datasets.at(dataset_name); diff --git a/lib/src/H5Writer.hpp b/lib/src/H5Writer.hpp index 6c76275..15477a2 100644 --- a/lib/src/H5Writer.hpp +++ b/lib/src/H5Writer.hpp @@ -30,6 +30,8 @@ class H5Writer void create_dataset(const std::string& dataset_name, const std::vector& data_shape, const std::string& data_type, const std::string& endianness); + + size_t get_relative_data_index(const size_t data_index); public: H5Writer(const std::string& filename, hsize_t frames_per_file=0, hsize_t initial_dataset_size=1000, hsize_t dataset_increase_step=1000); @@ -41,6 +43,7 @@ class H5Writer const size_t data_bytes_size, const std::string& data_type, const std::string& endianness); virtual H5::H5File& get_h5_file(); virtual bool is_data_for_current_file(const size_t data_index); + }; class DummyH5Writer : public H5Writer