From 498e9dac6b34bbd28b9d563ef82d50f97122db3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 9 Mar 2026 12:24:50 +0100 Subject: [PATCH] Erase Iteration upon close (write mode) --- include/openPMD/backend/Container.hpp | 1 + include/openPMD/snapshots/StatefulIterator.hpp | 5 +++++ src/snapshots/ContainerImpls.cpp | 6 ++++++ src/snapshots/StatefulIterator.cpp | 17 ++++++++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/openPMD/backend/Container.hpp b/include/openPMD/backend/Container.hpp index 963fd34802..c1cce8f7af 100644 --- a/include/openPMD/backend/Container.hpp +++ b/include/openPMD/backend/Container.hpp @@ -114,6 +114,7 @@ class Container : virtual public Attributable template friend class internal::EraseStaleEntries; friend class StatefulIterator; + friend class StatefulSnapshotsContainer; protected: using ContainerData = internal::ContainerData; diff --git a/include/openPMD/snapshots/StatefulIterator.hpp b/include/openPMD/snapshots/StatefulIterator.hpp index 26ebc4075e..36d777aa99 100644 --- a/include/openPMD/snapshots/StatefulIterator.hpp +++ b/include/openPMD/snapshots/StatefulIterator.hpp @@ -395,6 +395,11 @@ class StatefulIterator -> std::optional; auto peekCurrentlyOpenIteration() -> std::optional; + // Only checks the index state from the Iterator, peekCurrentlyOpenIteration + // actually retrieves the Iteration and checks if it is open + [[nodiscard]] auto currentIterationIndex() const + -> std::optional; + auto reparse_possibly_deleted_iteration(iteration_index_t) -> void; }; } // namespace openPMD diff --git a/src/snapshots/ContainerImpls.cpp b/src/snapshots/ContainerImpls.cpp index dd6a2430a9..a7f90f67fb 100644 --- a/src/snapshots/ContainerImpls.cpp +++ b/src/snapshots/ContainerImpls.cpp @@ -266,6 +266,12 @@ auto StatefulSnapshotsContainer::operator[](key_type const &key) } } + if (auto currentIteration = base_iterator->currentIterationIndex(); + currentIteration.has_value() && *currentIteration != key) + { + s.series.iterations.container().erase(*currentIteration); + } + // create new auto &res = s.series.iterations[key]; Iteration::BeginStepStatus status = [&]() { diff --git a/src/snapshots/StatefulIterator.cpp b/src/snapshots/StatefulIterator.cpp index 2a7f994873..cac1e490df 100644 --- a/src/snapshots/StatefulIterator.cpp +++ b/src/snapshots/StatefulIterator.cpp @@ -360,7 +360,7 @@ auto StatefulIterator::peekCurrentlyOpenIteration() const return std::nullopt; } auto &s = optional.value(); - auto const &maybeCurrentIteration = s.currentIteration(); + auto const &maybeCurrentIteration = currentIterationIndex(); if (!maybeCurrentIteration.has_value()) { return std::nullopt; @@ -391,6 +391,21 @@ auto StatefulIterator::peekCurrentlyOpenIteration() } } +auto StatefulIterator::currentIterationIndex() const + -> std::optional +{ + if (!m_data) + { + return std::nullopt; + } + auto &optional = *m_data; + if (!optional.has_value()) + { + return std::nullopt; + } + auto &s = optional.value(); + return s.currentIteration(); +} auto StatefulIterator::reparse_possibly_deleted_iteration(iteration_index_t idx) -> void {