diff --git a/reader/JFJochXFELReader.cpp b/reader/JFJochXFELReader.cpp index 3051edf1a..d59e524b4 100644 --- a/reader/JFJochXFELReader.cpp +++ b/reader/JFJochXFELReader.cpp @@ -84,6 +84,7 @@ std::shared_ptr JFJochXFELReader::RawFileAt(size if (raw_files_.size() != files_.size()) { raw_files_.assign(files_.size(), nullptr); user_block_.assign(files_.size(), 0); + raw_order_.clear(); } if (!raw_files_[file]) { const hid_t fcpl = H5Fget_create_plist(FileAt(file).GetID()); @@ -97,6 +98,11 @@ std::shared_ptr JFJochXFELReader::RawFileAt(size throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Cannot open " + files_[file]); raw_files_[file] = raw; user_block_[file] = user_block; + raw_order_.push_back(file); + if (raw_order_.size() > MAX_OPEN_FILES) { + raw_files_[raw_order_.front()].reset(); + raw_order_.erase(raw_order_.begin()); + } } return raw_files_[file]; } @@ -718,6 +724,7 @@ void JFJochXFELReader::Close() { open_files_.clear(); raw_files_.clear(); user_block_.clear(); + raw_order_.clear(); files_.clear(); frames_.clear(); dataset_.reset(); diff --git a/reader/JFJochXFELReader.h b/reader/JFJochXFELReader.h index ec79bdfae..2f9059b56 100644 --- a/reader/JFJochXFELReader.h +++ b/reader/JFJochXFELReader.h @@ -84,9 +84,11 @@ class JFJochXFELReader : public JFJochReader { }; Layout layout_; // Positional-read handles on the files, opened as first needed (under hdf5_mutex), with the size of - // each file's user block, which HDF5 chunk addresses count from. + // each file's user block, which HDF5 chunk addresses count from. Like open_files_, only the most + // recently used MAX_OPEN_FILES are held (raw_order_); a reader still using a released one keeps it. mutable std::vector> raw_files_; mutable std::vector user_block_; + mutable std::vector raw_order_; static Layout ReadLayout(HDF5ReadOnlyFile &file, const Frame &frame); // The files, in the order given, every frame in them, and the run's mean of every value the geometry