EntitySnapshot.cpp
Go to the documentation of this file.
1 // Header
2 #include "EntitySnapshot.h"
3 
4 // STD / STL
5 #include <fstream>
6 #include <iostream>
7 
8 // ArmarX
11 
13 
15 {
18 
19  const std::string& exportName,
20  const armem::MemoryID& id /* UNESCAPED */,
21  const std::shared_ptr<Processors>& filters) :
22  EntitySnapshotBase(exportName, id, filters),
23  DiskMemoryItemMixin(p, exportName, id),
24  MongoDBStorageMixin(s, exportName, id)
25  {
26  //start();
27  }
28 
29  bool
30  EntitySnapshot::forEachInstance(std::function<void(EntityInstance&)> func) const
31  {
32  std::lock_guard l(ltm_mutex);
33 
34  /*if (connected() && collectionExists())
35  {
36  if (auto d = documentExists(); d)
37  {
38  nlohmann::json doc = *d;
39  std::vector<nlohmann::json> instances = doc[DATA];
40  for (unsigned int i = 0; i < instances.size(); ++i)
41  {
42  EntityInstance c(getMemoryBasePath(),
43  getSettings(),
44  getExportName(),
45  id().withInstanceIndex(i),
46  processors);
47  func(c);
48  }
49  }
50  } else */
51 
52  if (fullPathExists())
53  {
54  for (const auto& i : getAllDirectories())
55  {
57  getSettings(),
58  getExportName(),
59  id().withInstanceIndex(std::stoi(i.filename())).cleanID(),
60  processors);
61  func(c);
62  }
63  }
64  return true;
65  }
66 
67  bool
69  {
70  std::lock_guard l(ltm_mutex);
71 
72  /*if (connected() && collectionExists())
73  {
74  if (auto d = documentExists(); d)
75  {
76  nlohmann::json doc = *d;
77  std::vector<nlohmann::json> instances = doc[DATA];
78  return (size_t)index < instances.size();
79  }
80  }*/
81 
82  if (fullPathExists())
83  {
85  getSettings(),
86  getExportName(),
87  id().withInstanceIndex(index),
88  processors);
89  return c.fullPathExists();
90  }
91 
92  return false;
93  }
94 
95  std::shared_ptr<EntityInstance>
97  {
98  std::lock_guard l(ltm_mutex);
99 
100  if (!hasInstance(index))
101  {
102  return nullptr;
103  }
104  return std::make_shared<EntityInstance>(getMemoryBasePath(),
105  getSettings(),
106  getExportName(),
107  id().withInstanceIndex(index),
108  processors);
109  }
110 
111  void
113  {
114  std::lock_guard l(ltm_mutex);
115 
116  e.id() = id().getEntitySnapshotID().cleanID();
117 
119  x.loadAllReferences(e);
120  });
121  }
122 
123  void
125  {
126  std::lock_guard l(ltm_mutex);
127 
128  if (/*(connected() && collectionExists() && documentExists()) ||*/ fullPathExists())
129  {
130  p.forEachInstance(
131  [&](auto& e)
132  {
134  getSettings(),
135  getExportName(),
136  id().withInstanceIndex(e.id().instanceIndex),
137  processors);
138  c.resolve(e);
139  });
140  }
141  }
142 
143  void
145  {
146 
147  std::lock_guard l(ltm_mutex);
148 
149  if (id().timestamp.isInvalid())
150  {
152  << "During storage of segment '" << p.id().str()
153  << "' I noticed that the corresponding LTM has no id set. "
154  << "I set the id of the LTM to the same name, however this should not happen!";
155  id().timestamp = p.id().timestamp;
156  }
157 
158  /*if (!connected())
159  {
160  ARMARX_WARNING << "LTM ENTITY SNAPSHOT NOT CONNECTED ALTHOUGH ENABLED " << id().str();
161  return;
162  }*/
163 
164  //nlohmann::json data;
165  //data[DATA] = std::vector<nlohmann::json>();
166 
167  p.forEachInstance(
168  [&](const auto& e)
169  {
171  getSettings(),
172  getExportName(),
173  id().withInstanceIndex(e.id().instanceIndex),
174  processors);
175  c.store(e);
176 
177  //data[DATA].push_back(c.store(e));
178  statistics.recordedInstances++;
179  });
180 
181  //writeDataToDocument(data);
182  }
183 } // namespace armarx::armem::server::ltm
armarx::armem::MemoryID::timestamp
Time timestamp
Definition: MemoryID.h:54
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:64
armarx::armem::server::ltm::EntitySnapshot::hasInstance
bool hasInstance(const int) const override
check if Instance segment exists
Definition: EntitySnapshot.cpp:68
armarx::armem::server::ltm::detail::MemoryItem::processors
std::shared_ptr< Processors > processors
Definition: MemoryItem.h:54
armarx::armem::server::ltm::EntitySnapshot::forEachInstance
bool forEachInstance(std::function< void(EntityInstance &)> func) const override
iterate over all Instance segments of this ltm
Definition: EntitySnapshot.cpp:30
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:71
armarx::armem::base::EntitySnapshotBase::forEachInstance
bool forEachInstance(InstanceFunctionT &&func)
Definition: EntitySnapshotBase.h:178
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem::server::ltm::EntitySnapshot::EntitySnapshot
EntitySnapshot(const detail::mixin::Path &, const detail::mixin::MongoDBSettings &, const std::string &, const MemoryID &id, const std::shared_ptr< Processors > &p)
Definition: EntitySnapshot.cpp:16
armarx::armem::server::ltm::EntitySnapshot::findInstance
std::shared_ptr< EntityInstance > findInstance(const int) const override
find Instance segment
Definition: EntitySnapshot.cpp:96
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
Dict.h
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getAllDirectories
std::vector< Path > getAllDirectories() const
Definition: DiskStorageMixin.cpp:160
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::MemoryID::getEntitySnapshotID
MemoryID getEntitySnapshotID() const
Definition: MemoryID.cpp:313
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getSettings
MongoDBSettings getSettings() const
Definition: MongoDBStorageMixin.cpp:436
armarx::armem::server::ltm::detail::EntitySnapshotBase< EntityInstance >::statistics
Statistics statistics
Definition: EntitySnapshotBase.h:86
armarx::armem::server::ltm::detail::MemoryItem::getExportName
virtual std::string getExportName() const
Definition: MemoryItem.h:26
armarx::armem::wm::EntitySnapshot
Client-side working memory entity snapshot.
Definition: memory_definitions.h:80
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::ltm::detail::EntitySnapshotBase< EntityInstance >::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: EntitySnapshotBase.h:84
EntitySnapshot.h
armarx::armem::server::ltm
Definition: forward_declarations.h:20
armarx::armem::server::ltm::EntityInstance
Definition: EntityInstance.h:13
armarx::armem::server::ltm::detail::MemoryItem::id
MemoryID id() const
Definition: MemoryItem.cpp:37
armarx::armem::MemoryID::cleanID
MemoryID cleanID() const
Definition: MemoryID.cpp:132
TimeUtil.h
armarx::armem::server::ltm::EntitySnapshot::_store
void _store(const armem::wm::EntitySnapshot &) override
Definition: EntitySnapshot.cpp:144
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:110
armarx::armem::server::ltm::detail::mixin::MongoDBSettings
Definition: MongoDBStorageMixin.h:15
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::armem::server::ltm::EntitySnapshot::_loadAllReferences
void _loadAllReferences(armem::wm::EntitySnapshot &) const override
Definition: EntitySnapshot.cpp:112
armarx::armem::server::ltm::EntitySnapshot::_resolve
void _resolve(armem::wm::EntitySnapshot &) const override
Definition: EntitySnapshot.cpp:124
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::server::ltm::detail::EntityInstanceBase::loadAllReferences
void loadAllReferences(armem::wm::EntitySnapshot &e) const
return the full sub-ltm as a wm::EntitySnapshot with only references the ltm may be huge,...
Definition: EntityInstanceBase.h:30