Memory.cpp
Go to the documentation of this file.
1 #include "Memory.h"
2 
5 
7 
9 {
10  void
11  Memory::_configure(const nlohmann::json& json)
12  {
13  std::lock_guard l(ltm_mutex);
14 
19  }
20 
21  Memory::Memory() : Memory(std::filesystem::path("/tmp/ltm"), {}, "MemoryExport", "Test")
22  {
23  }
24 
26 
27  const std::string& exportName,
28  const std::string& memoryName /* UNESCAPED */) :
29  Memory(p, {}, exportName, memoryName)
30  {
31  }
32 
35 
36  const std::string& exportName,
37  const std::string& memoryName /* UNESCAPED */) :
38  MemoryBase(exportName, MemoryID(memoryName, "")),
41  DiskMemoryItemMixin(p, exportName, MemoryID(memoryName, "")),
42  MongoDBStorageMixin(s, exportName, MemoryID(memoryName, ""))
43  {
44  ARMARX_INFO << "Creating a new memory at " << p.string() << " and " << exportName;
45  }
46 
47  void
48  Memory::_setExportName(const std::string& n)
49  {
52  }
53 
54  void
56  {
58  //MongoDBStorageMixin::start();
59  }
60 
61  void
63  {
66  ARMARX_IMPORTANT << "Storing of data finished, starting to generate and save statistics...";
68  }
69 
70  void
72  {
73  std::lock_guard l(ltm_mutex);
74 
79  }
80 
81  bool
82  Memory::forEachCoreSegment(std::function<void(CoreSegment&)> func) const
83  {
84  std::lock_guard l(ltm_mutex);
85 
86  // legacy: check fs
87  if (fullPathExists())
88  {
89  for (const auto& subdirName : getAllDirectories())
90  {
91  const std::string& segmentName_one = util::fs::detail::unescapeName(subdirName);
92  auto segmentName = util::fs::detail::extractLastDirectoryFromPath(segmentName_one);
94  getSettings(),
95  getExportName(),
96  id().withCoreSegmentName(segmentName),
97  processors);
98  func(c);
99  }
100  }
101  else
102  {
103  ARMARX_WARNING << "Could not load the core segments of LTM "
104  << id().str()
105  << " as the path "
106  << getFullPath().string()
107  << " does not exist.";
108  }
109 
110  ARMARX_DEBUG << "All CoreSegments handeled";
111 
112  return true;
113  }
114 
115  bool
116  Memory::hasCoreSegment(const std::string& name) const
117  {
118  std::lock_guard l(ltm_mutex);
119 
121  {
122  return true;
123  }
124 
125  // check if collection exists
126  /*if (connected() && collectionExists())
127  {
128  auto c = CoreSegment(getMemoryBasePath(),
129  getSettings(),
130  getExportName(),
131  id().withCoreSegmentName(name),
132  processors);
133 
134  return (bool)c.collectionExists();
135  }*/
136 
137  // legacy: check if segment is stored on hard drive without db
138  if (fullPathExists())
139  {
141  getSettings(),
142  getExportName(),
143  id().withCoreSegmentName(name),
144  processors);
145 
146  return c.fullPathExists();
147  }
148 
149  return false;
150  }
151 
152  std::unique_ptr<CoreSegment>
153  Memory::findCoreSegment(const std::string& coreSegmentName) const
154  {
155  std::lock_guard l(ltm_mutex);
156 
157  if (!hasCoreSegment(coreSegmentName))
158  {
159  return nullptr;
160  }
161 
162  return std::make_unique<CoreSegment>(getMemoryBasePath(),
163  getSettings(),
164  getExportName(),
165  id().withCoreSegmentName(coreSegmentName),
166  processors);
167  }
168 
169  void
171  {
175  }
176 
177  void
179  {
180  std::lock_guard l(ltm_mutex);
181 
182  m.id() = id().getMemoryID().cleanID();
183 
185  [&m](auto& x)
186  {
188  x.loadAllReferences(s);
189  m.addCoreSegment(s);
190  });
191  }
192 
193  void
195  {
196  std::lock_guard l(ltm_mutex);
197  m.id() = id().getMemoryID().cleanID();
198 
200  [&m, &n](auto& x)
201  {
203  x.loadLatestNReferences(n, s);
204  m.addCoreSegment(s);
205  });
206  }
207 
208  void
209  Memory::_loadLatestNReferences(int n, armem::wm::Memory& m, std::list<std::string> coreSegNames)
210  {
211  std::lock_guard l(ltm_mutex);
212  m.id() = id().getMemoryID().cleanID();
213 
215  [&m, &n, &coreSegNames](auto& x)
216  {
217  bool loadCoreSeg = (std::find(coreSegNames.begin(), coreSegNames.end(), x.id().coreSegmentName) != coreSegNames.end());
218  if(loadCoreSeg){
220  x.loadLatestNReferences(n, s);
221  m.addCoreSegment(s);
222  } else {
223  ARMARX_DEBUG << "Skipping loading CoreSegment with name "
224  << x.id().coreSegmentName
225  << " from LTM into WM as it is not in the defined list";
226  }
227 
228  });
229  }
230 
231  void
233  {
234  std::lock_guard l(ltm_mutex); // we cannot load a memory multiple times simultaneously
235 
237 
238  if (/*(connected() && collectionExists()) ||*/ fullPathExists())
239  {
241  [&](auto& e)
242  {
243  //ARMARX_INFO << "resolve for CoreSegment " << e.id().str();
245  getSettings(),
246  getExportName(),
247  id().withCoreSegmentName(e.id().coreSegmentName),
248  processors);
249  c.resolve(e);
250  });
251  } else {
252  ARMARX_WARNING << "You are trying to resolve an LTM from a path that does not exist: "
253  << getFullPath();
254  }
255  }
256 
257  void
259  {
261  }
262 
263  void
265  {
266  std::lock_guard l(ltm_mutex); // we cannot store a memory multiple times simultaneously
267 
268  if (id().memoryName.empty())
269  {
271  << "During storage of memory '" << memory.id().str()
272  << "' I noticed that the corresponding LTM has no id set. "
273  << "I set the id of the LTM to the same name, however this should not happen!";
274  setMemoryID(memory.id());
275  }
276 
277  /*if (!connected())
278  {
279  ARMARX_WARNING << "LTM NOT CONNECTED ALTHOUGH ENABLED " << id().str();
280  return;
281  }*/
282 
283  memory.forEachCoreSegment(
284  [&](const auto& core)
285  {
287  getSettings(),
288  getExportName(),
289  id().withCoreSegmentName(core.id().coreSegmentName),
290  processors);
291 
292  // 2. store data
293  c.store(core);
294 
295 
296  // 3. update statistics
297  statistics.recordedCoreSegments++;
298  });
299 
300  // 4. update cache
301  //CachedBase::addToCache(memory);
302  }
303 
304  void
306  //not used any more
307 
308  }
309 
310  void
312  {
313  auto firstTimeStarted = this->statistics.firstStarted;
314  if (!firstTimeStarted.isValid())
315  {
316  //No statistics to be saved, as recording was never started
317  ARMARX_DEBUG << "No Statistics will be saved because firstStarted is invalid: "
318  << VAROUT(this->statistics.firstStarted);
319  return;
320  }
321 
322  ARMARX_INFO << "Preparing to save statistics for " << this->name();
323  try
324  {
325  auto first_stats = this->getFilterStatistics();
326  if (first_stats.empty())
327  {
328  //empty statistics mean no data was recorded and no statistics should be saved
329  ARMARX_DEBUG << "No Statistics will be saved because no actual data was recorded.";
330  return;
331  }
332  std::map<std::string,
333  std::map<std::string, ltm::processor::SnapshotFilter::FilterStatistics>>
334  information;
335  std::map<std::string, armarx::core::time::DateTime> times;
336 
337  try
338  {
339  times["Started LTM1"] = this->getStatistics().lastStarted;
340  times["Stopped LTM1"] = this->getStatistics().lastStopped;
341  information["LTM"] = first_stats;
342  }
343  catch (...)
344  {
345  ARMARX_INFO << "Something went wrong after getting the statistics";
346  }
347  auto exportPath = this->getMemoryBasePath();
348  auto exportName = this->getExportName();
349  auto recording_started = this->getStatistics().lastStarted;
350  auto recording_stopped = this->getStatistics().lastStopped;
351  test::save_statistics(information,
352  times,
353  recording_started,
354  recording_stopped,
355  exportPath,
356  exportName,
357  this->name());
358  }
359  catch (...)
360  {
361  ARMARX_INFO << "Something went wrong with the statistics saving process";
362  }
363  }
364 } // namespace armarx::armem::server::ltm
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >::getStatistics
Statistics getStatistics() const
Definition: MemoryBase.h:276
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::stop
void stop()
Definition: MongoDBStorageMixin.cpp:212
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >::stop
void stop()
Definition: BufferedMemoryMixin.h:81
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >::setMixinMemoryID
void setMixinMemoryID(const MemoryID &id)
Definition: BufferedMemoryMixin.h:57
armarx::armem::server::ltm::Memory::_loadAllReferences
void _loadAllReferences(armem::wm::Memory &) final
Definition: Memory.cpp:178
armarx::armem::server::ltm::detail::mixin::CachedMemoryMixin< CoreSegment >::setMixinMemoryID
void setMixinMemoryID(const MemoryID &id)
Definition: CachedMemoryMixin.h:35
armarx::armem::server::ltm::Memory::_setExportName
void _setExportName(const std::string &n) final
Definition: Memory.cpp:48
armarx::armem::server::ltm::detail::MemoryItem::processors
std::shared_ptr< Processors > processors
Definition: MemoryItem.h:54
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::armem::server::ltm::Memory::Memory
Memory()
Definition: Memory.cpp:21
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::configureMixin
void configureMixin(const nlohmann::json &json)
configuration
Definition: MongoDBStorageMixin.cpp:456
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:71
armarx::armem::server::ltm::Memory::getAndSaveStatistics
void getAndSaveStatistics()
getAndSaveStatistics generates and saves statistics for a LTM recording
Definition: Memory.cpp:311
armarx::armem::server::ltm::Memory::_directlyStore
void _directlyStore(const armem::wm::Memory &) final
Definition: Memory.cpp:264
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)
Definition: DiskStorageMixin.cpp:173
armarx::armem::server::ltm::Memory::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix) override
default parameters. Implementation should use the configuration to configure
Definition: Memory.cpp:170
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::setMixinMemoryID
void setMixinMemoryID(const MemoryID &n)
Definition: DiskStorageMixin.cpp:54
armarx::armem::server::ltm::Memory::_disable
void _disable() final
Definition: Memory.cpp:62
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >::p
struct armarx::armem::server::ltm::detail::MemoryBase::Properties p
armarx::memory
Brief description of class memory.
Definition: memory.h:39
armarx::armem::server::ltm::Memory::_configure
void _configure(const nlohmann::json &config) final
configuration
Definition: Memory.cpp:11
armarx::armem::server::ltm::util::fs::detail::unescapeName
std::string unescapeName(const std::string &escapedName)
Definition: filesystem.cpp:31
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >::statistics
Statistics statistics
Definition: MemoryBase.h:366
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >
armarx::armem::server::ltm::Memory::_loadOnStartup
void _loadOnStartup() final
Definition: Memory.cpp:305
armarx::armem::base::MemoryBase::forEachCoreSegment
bool forEachCoreSegment(CoreSegmentFunctionT &&func)
Definition: MemoryBase.h:186
armarx::armem::server::ltm::Memory
A memory storing data on the hard drive and in mongodb (needs 'armarx memory start' to start the mong...
Definition: Memory.h:19
memory_definitions.h
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setMixinMemoryID
void setMixinMemoryID(const armem::MemoryID &)
setter
Definition: MongoDBStorageMixin.cpp:442
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getAllDirectories
std::vector< Path > getAllDirectories() const
Definition: DiskStorageMixin.cpp:160
armarx::armem::server::ltm::Memory::_resolve
void _resolve(armem::wm::Memory &) final
Definition: Memory.cpp:232
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >::addToBuffer
void addToBuffer(const armem::wm::Memory &memory)
Definition: BufferedMemoryMixin.h:143
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm::detail::MemoryItem::setMemoryID
void setMemoryID(const MemoryID &)
Definition: MemoryItem.cpp:30
armarx::armem::server::ltm::Memory::_setMemoryID
void _setMemoryID(const MemoryID &id) final
Definition: Memory.cpp:71
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >::start
void start()
Definition: BufferedMemoryMixin.h:66
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: MemoryBase.h:364
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::wm::CoreSegment
Client-side working memory core segment.
Definition: memory_definitions.h:119
armarx::armem::server::ltm::detail::mixin::CachedMemoryMixin< CoreSegment >::configureMixin
void configureMixin(const nlohmann::json &json)
configuration
Definition: CachedMemoryMixin.h:29
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::server::ltm::util::fs::detail::extractLastDirectoryFromPath
std::string extractLastDirectoryFromPath(const std::string &path)
Definition: filesystem.cpp:44
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::setMixinExportName
void setMixinExportName(const std::string &n)
Definition: DiskStorageMixin.cpp:47
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getSettings
MongoDBSettings getSettings() const
Definition: MongoDBStorageMixin.cpp:436
armarx::armem::base::MemoryBase::addCoreSegment
CoreSegmentT & addCoreSegment(const std::string &name, aron::type::ObjectPtr coreSegmentType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Add an empty core segment with the given name, type and prediction engines.
Definition: MemoryBase.h:259
armarx::armem::server::ltm::Memory::forEachCoreSegment
bool forEachCoreSegment(std::function< void(CoreSegment &)> func) const final
iterate over all core segments of this ltm
Definition: Memory.cpp:82
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >::createPropertyDefinitions
virtual void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)
default parameters. Implementation should use the configuration to configure
Definition: MemoryBase.h:232
armarx::armem::server::ltm::detail::MemoryItem::getExportName
virtual std::string getExportName() const
Definition: MemoryItem.h:26
armarx::armem::server::ltm::detail::MemoryBase< CoreSegment >::getFilterStatistics
std::map< std::string, processor::SnapshotFilter::FilterStatistics > getFilterStatistics()
Definition: MemoryBase.h:282
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin
Definition: MongoDBStorageMixin.h:45
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setMixinExportName
void setMixinExportName(const std::string &n)
Definition: MongoDBStorageMixin.cpp:450
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::ltm::detail::mixin::CachedMemoryMixin< CoreSegment >
armarx::armem::server::ltm
Definition: forward_declarations.h:20
Memory.h
armarx::armem::server::ltm::Memory::_enable
void _enable() final
Definition: Memory.cpp:55
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
armarx::armem::MemoryID::getMemoryID
MemoryID getMemoryID() const
Definition: MemoryID.cpp:281
TimeUtil.h
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >::configureMixin
void configureMixin(const nlohmann::json &json)
configuration
Definition: BufferedMemoryMixin.h:124
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::server::ltm::CoreSegment
Definition: CoreSegment.h:13
std
Definition: Application.h:66
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:110
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >
armarx::armem::server::ltm::detail::mixin::CachedMemoryMixin< CoreSegment >::cacheHasCoreSegment
bool cacheHasCoreSegment(const std::string &n) const
Definition: CachedMemoryMixin.h:50
armarx::armem::server::ltm::Memory::_loadLatestNReferences
void _loadLatestNReferences(int n, armem::wm::Memory &m) final
Definition: Memory.cpp:194
armarx::armem::server::ltm::detail::mixin::BufferedMemoryMixin< CoreSegment >::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)
Definition: BufferedMemoryMixin.h:135
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::Memory::findCoreSegment
std::unique_ptr< CoreSegment > findCoreSegment(const std::string &coreSegmentName) const final
find core segment
Definition: Memory.cpp:153
armarx::armem::server::ltm::Memory::_store
void _store(const armem::wm::Memory &) final
Definition: Memory.cpp:258
armarx::armem::server::ltm::Memory::hasCoreSegment
bool hasCoreSegment(const std::string &name) const final
check if core segment exists
Definition: Memory.cpp:116
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::configureMixin
void configureMixin(const nlohmann::json &json)
Definition: DiskStorageMixin.cpp:62
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getFullPath
Path getFullPath() const
Definition: DiskStorageMixin.cpp:87
armarx::armem::server::ltm::detail::MemoryItem::name
std::string name() const
Definition: MemoryItem.cpp:43