Skip to content

Commit d6df848

Browse files
committed
Erase Iteration upon close (write mode)
1 parent 2c6a5ef commit d6df848

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

include/openPMD/backend/Container.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class Container : virtual public Attributable
114114
template <typename>
115115
friend class internal::EraseStaleEntries;
116116
friend class StatefulIterator;
117+
friend class StatefulSnapshotsContainer;
117118

118119
protected:
119120
using ContainerData = internal::ContainerData<T, T_key, T_container>;

include/openPMD/snapshots/StatefulIterator.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ class StatefulIterator
395395
-> std::optional<value_type const *>;
396396
auto peekCurrentlyOpenIteration() -> std::optional<value_type *>;
397397

398+
// Only checks the index state from the Iterator, peekCurrentlyOpenIteration
399+
// actually retrieves the Iteration and checks if it is open
400+
[[nodiscard]] auto currentIterationIndex() const
401+
-> std::optional<iteration_index_t>;
402+
398403
auto reparse_possibly_deleted_iteration(iteration_index_t) -> void;
399404
};
400405
} // namespace openPMD

src/snapshots/ContainerImpls.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ auto StatefulSnapshotsContainer::operator[](key_type const &key)
266266
}
267267
}
268268

269+
if (auto currentIteration = base_iterator->currentIterationIndex();
270+
currentIteration.has_value() && *currentIteration != key)
271+
{
272+
s.series.iterations.container().erase(*currentIteration);
273+
}
274+
269275
// create new
270276
auto &res = s.series.iterations[key];
271277
Iteration::BeginStepStatus status = [&]() {

src/snapshots/StatefulIterator.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ auto StatefulIterator::peekCurrentlyOpenIteration() const
360360
return std::nullopt;
361361
}
362362
auto &s = optional.value();
363-
auto const &maybeCurrentIteration = s.currentIteration();
363+
auto const &maybeCurrentIteration = currentIterationIndex();
364364
if (!maybeCurrentIteration.has_value())
365365
{
366366
return std::nullopt;
@@ -391,6 +391,21 @@ auto StatefulIterator::peekCurrentlyOpenIteration()
391391
}
392392
}
393393

394+
auto StatefulIterator::currentIterationIndex() const
395+
-> std::optional<iteration_index_t>
396+
{
397+
if (!m_data)
398+
{
399+
return std::nullopt;
400+
}
401+
auto &optional = *m_data;
402+
if (!optional.has_value())
403+
{
404+
return std::nullopt;
405+
}
406+
auto &s = optional.value();
407+
return s.currentIteration();
408+
}
394409
auto StatefulIterator::reparse_possibly_deleted_iteration(iteration_index_t idx)
395410
-> void
396411
{

0 commit comments

Comments
 (0)