6#include <SimoxUtility/algorithm/string.h>
22 const std::shared_ptr<Processors>&
filters,
23 const std::shared_ptr<persistence::MemoryPersistenceStrategy>& persistenceStrategy) :
24 EntityBase(exportName, memoryId,
filters), persistenceStrategy_(persistenceStrategy) {}
37 for (
auto& entitySnapshotKey : persistenceStrategy_->getContainerKeys(
id()))
39 ARMARX_DEBUG <<
"Found entity snapshot=" << entitySnapshotKey;
41 std::shared_ptr<persistence::MemoryPersistenceStrategy> entitySnapshotPersistenceStrategy(persistenceStrategy_);
47 entitySnapshotPersistenceStrategy);
50 func(ltmEntitySnapshot);
65 if (first < 0 or last < 0)
68 unsigned long size = 0;
69 auto countFunc = [&](
EntitySnapshot& ltmEntitySnapshot) { size++; };
79 if (checked >= first && checked <= last)
81 func(ltmEntitySnapshot);
98 Time ts = ltmEntitySnapshot.id().timestamp;
99 if (ts >=
min && ts <=
max)
101 func(ltmEntitySnapshot);
116 Time ts = ltmEntitySnapshot.id().timestamp;
119 func(ltmEntitySnapshot);
133 Time ts = ltmEntitySnapshot.id().timestamp;
136 func(ltmEntitySnapshot);
150 auto keys = persistenceStrategy_->getContainerKeys(
id());
153 std::vector<Time> timestamps;
154 timestamps.reserve(keys.size());
155 for (
const auto& key : keys)
160 timestamps.push_back(ts);
163 std::sort(timestamps.begin(), timestamps.end(), std::greater<Time>());
166 for (
const Time& ts : timestamps)
168 std::shared_ptr<persistence::MemoryPersistenceStrategy> entitySnapshotPersistenceStrategy(persistenceStrategy_);
172 id().withTimestamp(ts),
174 entitySnapshotPersistenceStrategy);
176 if (!func(ltmEntitySnapshot))
193 bool foundSnapshot = persistenceStrategy_->containsContainer(
id(), std::to_string(snapshotTime.
toMicroSecondsSinceEpoch()));
195 return foundSnapshot;
198 std::shared_ptr<EntitySnapshot>
214 std::shared_ptr<EntitySnapshot>
222 Time ts = ltmEntitySnapshot.id().timestamp;
244 persistenceStrategy_);
247 std::shared_ptr<EntitySnapshot>
255 auto ts = e.id().timestamp;
256 if (ts < time && ts > bestMatch)
277 persistenceStrategy_);
280 std::shared_ptr<EntitySnapshot>
288 auto ts = e.id().timestamp;
289 if (ts <= time && ts > bestMatch)
308 persistenceStrategy_);
311 std::shared_ptr<EntitySnapshot>
319 auto ts = e.id().timestamp;
320 if (ts > time && ts < bestMatch)
337 id().withTimestamp(bestMatch),
339 persistenceStrategy_);
342 std::shared_ptr<EntitySnapshot>
350 auto ts = e.id().timestamp;
351 if (ts >= time && ts < bestMatch)
369 persistenceStrategy_);
380 [&wmEntity](
auto& ltmEntitySnapshot)
383 ltmEntitySnapshot.id()
387 ltmEntitySnapshot.loadAllReferences(s);
411 [&wmEntity, &n, ¤t, &count](
auto& ltmEntitySnapshot)
414 ltmEntitySnapshot.id()
417 if (current >= (count - n))
420 ltmEntitySnapshot.loadAllReferences(wmEntitySnapshot);
421 ltmEntitySnapshot.resolve(wmEntitySnapshot);
426 ARMARX_DEBUG <<
"Skipping snapshot with timestamp " << ltmEntitySnapshot.id().timestamp;
441 [&](
auto& wmEntitySnapshot)
446 std::shared_ptr<persistence::MemoryPersistenceStrategy> entitySnapshotPersistenceStrategy(persistenceStrategy_);
450 id().withTimestamp(wmEntitySnapshot.id().timestamp),
452 entitySnapshotPersistenceStrategy);
453 ltmEntitySnapshot.
resolve(wmEntitySnapshot);
462 if (
id().entityName.empty())
465 <<
"During storage of segment '" << wmEntity.
id().
str()
466 <<
"' I noticed that the corresponding LTM has no id set. "
467 <<
"I set the id of the LTM to the same name, however this should not happen!";
483 [&](
const auto& wmEntitySnapshot)
488 std::shared_ptr<persistence::MemoryPersistenceStrategy> entitySnapshotPersistenceStrategy(persistenceStrategy_);
491 id().withTimestamp(wmEntitySnapshot.id().timestamp),
493 entitySnapshotPersistenceStrategy);
498 ARMARX_DEBUG <<
"Ignoring to put an EntitiySnapshot into the LTM because "
499 "the timestamp already existed (we assume snapshots are "
500 "const and do not change outside the ltm).";
509 ltmEntitySnapshot.
store(wmEntitySnapshot);
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
MemoryID withTimestamp(Time time) const
MemoryID getEntityID() const
EntitySnapshotT & addSnapshot(const Time ×tamp)
Add a snapshot at the given time.
bool forEachSnapshot(SnapshotFunctionT &&func)
bool hasSnapshot(const Time &time) const
Indicate whether a snapshot at the given time exists.
bool _implForEachSnapshotBeforeReverse(const Time &time, std::function< bool(EntitySnapshot &)> func) const override
bool _implForEachSnapshotInTimeRange(const Time &min, const Time &max, std::function< void(EntitySnapshot &)> func) const override
bool _implHasSnapshot(const Time &snapshotTime) const override
bool _implForEachSnapshotInIndexRange(long first, long last, std::function< void(EntitySnapshot &)> func) const override
bool _implForEachSnapshot(std::function< void(EntitySnapshot &)> func) const override
bool _implForEachSnapshotBefore(const Time &time, std::function< void(EntitySnapshot &)> func) const override
std::shared_ptr< EntitySnapshot > _implFindLatestSnapshot() const override
bool _implForEachSnapshotBeforeOrAt(const Time &time, std::function< void(EntitySnapshot &)> func) const override
void _store(const armem::wm::Entity &wmEntity, bool simulatedVersion) override
std::shared_ptr< EntitySnapshot > _implFindFirstSnapshotAfterOrAt(const Time &time) const override
void _resolve(armem::wm::Entity &wmEntity) override
std::shared_ptr< EntitySnapshot > _implFindFirstSnapshotAfter(const Time &time) const override
void _loadLatestNReferences(int n, armem::wm::Entity &wmEntity) override
Entity(const std::string &exportName, const MemoryID &memoryId, const std::shared_ptr< Processors > &filters, const std::shared_ptr< persistence::MemoryPersistenceStrategy > &persistenceStrategy)
void _loadAllReferences(armem::wm::Entity &wmEntity) override
std::shared_ptr< EntitySnapshot > _implFindLatestSnapshotBefore(const Time &time) const override
std::shared_ptr< EntitySnapshot > _implFindSnapshot(const Time &snapshotTime) const override
std::shared_ptr< EntitySnapshot > _implFindLatestSnapshotBeforeOrAt(const Time &time) const override
void resolve(armem::wm::EntitySnapshot &e) const
convert the references of the input into a wm::Memory
void store(const armem::wm::EntitySnapshot &e)
encode the content of a wm::Memory and store
virtual std::string getExportName() const
std::shared_ptr< Processors > processors
Client-side working memory entity snapshot.
Client-side working memory entity.
static DateTime Invalid()
std::int64_t toMicroSecondsSinceEpoch() const
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
size_t negativeIndexSemantics(long index, size_t size)
armarx::core::time::DateTime Time
Time timeFromStringMicroSeconds(const std::string µSeconds)
Get a Time from the microseconds as text.
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)