CoreSegment.cpp
Go to the documentation of this file.
1 #include "CoreSegment.h"
2 
5 
7 
9 {
12 
13  const std::string& exportName,
14  const armem::MemoryID& id /* UNESCAPED */,
15  const std::shared_ptr<Processors>& filters) :
16  CoreSegmentBase(exportName, id, filters),
17  DiskMemoryItemMixin(p, exportName, id),
18  MongoDBStorageMixin(s, exportName, id)
19  {
20  //start();
21  }
22 
23  bool
24  CoreSegment::forEachProviderSegment(std::function<void(ProviderSegment&)> func) const
25  {
26  std::lock_guard l(ltm_mutex);
27 
28  if (fullPathExists())
29  {
30  for (const auto& subdirName : getAllDirectories())
31  {
32  std::string segmentName = util::fs::detail::unescapeName(subdirName);
33  segmentName = util::fs::detail::extractLastDirectoryFromPath(segmentName);
35  getSettings(),
36  getExportName(),
37  id().withProviderSegmentName(segmentName),
38  processors);
39  func(c);
40  }
41  }
42  return true;
43  }
44 
45  bool
46  CoreSegment::hasProviderSegment(const std::string& name) const
47  {
48  std::lock_guard l(ltm_mutex);
49 
50  if (fullPathExists())
51  {
53  getSettings(),
54  getExportName(),
55  id().withProviderSegmentName(name),
56  processors);
57 
58  return c.fullPathExists();
59  }
60 
61  return false;
62  }
63 
64  std::shared_ptr<ProviderSegment>
65  CoreSegment::findProviderSegment(const std::string& providerSegmentName) const
66  {
67  std::lock_guard l(ltm_mutex);
68 
69  if (!hasProviderSegment(providerSegmentName))
70  {
71  return nullptr;
72  }
73 
74  return std::make_shared<ProviderSegment>(getMemoryBasePath(),
75  getSettings(),
76  getExportName(),
77  id().withProviderSegmentName(providerSegmentName),
78  processors);
79  }
80 
81  void
83  {
84  std::lock_guard l(ltm_mutex);
85 
86  e.id() = id().getCoreSegmentID().cleanID();
87 
88  //ARMARX_INFO << VAROUT(id());
89 
90  auto& conv = processors->defaultTypeConverter;
92  {
93  // load and set type
94  auto& conv = processors->defaultTypeConverter;
95 
96  auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
97  auto aron = conv.convert(filecontent, "");
98  e.aronType() = aron;
99  }
100 
102  [&e](auto& x)
103  {
105  x.loadAllReferences(s);
107  });
108  }
109 
110  void
112  {
113  std::lock_guard l(ltm_mutex);
114 
115  e.id() = id().getCoreSegmentID().cleanID();
116 
117  auto& conv = processors->defaultTypeConverter;
119  {
120  // load and set type
121  auto& conv = processors->defaultTypeConverter;
122 
123  auto filecontent = readDataFromFile(DiskMemoryItemMixin::TYPE_FILENAME + conv.suffix);
124  auto aron = conv.convert(filecontent, "");
125  e.aronType() = aron;
126  }
127 
129  [&e, &n](auto& x)
130  {
132  x.loadLatestNReferences(n, s);
134  });
135  }
136 
137  void
139  {
140  std::lock_guard l(ltm_mutex);
141 
142  if (/*(connected() && collectionExists()) ||*/ fullPathExists())
143  {
144  c.forEachProviderSegment(
145  [&](auto& e)
146  {
148  getSettings(),
149  getExportName(),
150  id().withProviderSegmentName(e.id().providerSegmentName),
151  processors);
152  c.resolve(e);
153  });
154  }
155  }
156 
157  void
159  {
160  std::lock_guard l(ltm_mutex);
161 
162  if (id().coreSegmentName.empty())
163  {
165  << "During storage of segment '" << c.id().str()
166  << "' I noticed that the corresponding LTM has no id set. "
167  << "I set the id of the LTM to the same name, however this should not happen!";
168  id().coreSegmentName = c.id().coreSegmentName;
169  };
170 
171  /*if (!connected())
172  {
173  ARMARX_WARNING << "LTM CORE SEGMENT NOT CONNECTED ALTHOUGH ENABLED " << id().str();
174  return;
175  }*/
176 
177  // add foreign key to memory collection
178  if (c.hasAronType())
179  {
180  auto& conv = processors->defaultTypeConverter;
181 
182  auto [vec, modeSuffix] = conv.convert(c.aronType());
183  ARMARX_CHECK_EMPTY(modeSuffix);
184 
185  //std::string dataStr{vec.begin(), vec.end()};
186  /*auto dataJson = nlohmann::json::parse(dataStr);
187 
188  writeForeignKeyToPreviousDocument(dataJson);*/
189 
190  ensureFullPathExists(true);
191  std::string filename = (TYPE_FILENAME + conv.suffix);
193  }
194  else
195  {
196  ARMARX_INFO << "CoreSegment does not have aron type, so aron type information "
197  "cannot be exported";
198  /*writeForeignKeyToPreviousDocument();*/
199  }
200 
201  c.forEachProviderSegment(
202  [&](const auto& prov)
203  {
205  getSettings(),
206  getExportName(),
207  id().withProviderSegmentName(prov.id().providerSegmentName),
208  processors);
209 
210  c.store(prov);
211  });
212  }
213 } // namespace armarx::armem::server::ltm
armarx::armem::server::ltm::ProviderSegment
Definition: ProviderSegment.h:13
armarx::armem::server::ltm::detail::MemoryItem::processors
std::shared_ptr< Processors > processors
Definition: MemoryItem.h:54
armarx::armem::wm::ProviderSegment
Client-side working memory provider segment.
Definition: memory_definitions.h:105
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:71
armarx::armem::server::ltm::detail::CoreSegmentBase< ProviderSegment >::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: CoreSegmentBase.h:101
armarx::armem::server::ltm::CoreSegment::_store
void _store(const armem::wm::CoreSegment &) override
Definition: CoreSegment.cpp:158
ARMARX_CHECK_EMPTY
#define ARMARX_CHECK_EMPTY(c)
Definition: ExpressionException.h:218
armarx::armem::base::detail::AronTyped::aronType
aron::type::ObjectPtr & aronType()
Definition: AronTyped.cpp:18
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
CoreSegment.h
armarx::armem::MemoryID::coreSegmentName
std::string coreSegmentName
Definition: MemoryID.h:51
armarx::armem::server::ltm::util::fs::detail::unescapeName
std::string unescapeName(const std::string &escapedName)
Definition: filesystem.cpp:31
memory_definitions.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::server::ltm::CoreSegment::hasProviderSegment
bool hasProviderSegment(const std::string &name) const override
check if provider segment exists
Definition: CoreSegment.cpp:46
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fileExists
bool fileExists(const std::string &filename) const
Definition: DiskStorageMixin.cpp:117
armarx::armem::wm::CoreSegment
Client-side working memory core segment.
Definition: memory_definitions.h:119
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::string &filename) const
Definition: DiskStorageMixin.cpp:153
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::TYPE_FILENAME
static const constexpr char * TYPE_FILENAME
Definition: DiskStorageMixin.h:66
armarx::armem::server::ltm::CoreSegment::_resolve
void _resolve(armem::wm::CoreSegment &) override
Definition: CoreSegment.cpp:138
armarx::armem::base::CoreSegmentBase::addProviderSegment
ProviderSegmentT & addProviderSegment(const std::string &name, aron::type::ObjectPtr providerSegmentType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Add an empty provider segment with the given name, optional provider segment type and prediction engi...
Definition: CoreSegmentBase.h:344
armarx::armem::server::ltm::util::fs::detail::extractLastDirectoryFromPath
std::string extractLastDirectoryFromPath(const std::string &path)
Definition: filesystem.cpp:44
filename
std::string filename
Definition: VisualizationRobot.cpp:83
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getSettings
MongoDBSettings getSettings() const
Definition: MongoDBStorageMixin.cpp:436
armarx::armem::server::ltm::detail::MemoryItem::getExportName
virtual std::string getExportName() const
Definition: MemoryItem.h:26
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::ltm::CoreSegment::_loadLatestNReferences
void _loadLatestNReferences(int n, armem::wm::CoreSegment &c) override
Definition: CoreSegment.cpp:111
armarx::armem::server::ltm::CoreSegment::forEachProviderSegment
bool forEachProviderSegment(std::function< void(ProviderSegment &)> func) const override
iterate over all provider segments of this ltm
Definition: CoreSegment.cpp:24
armarx::armem::server::ltm
Definition: forward_declarations.h:20
armarx::armem::MemoryID::getCoreSegmentID
MemoryID getCoreSegmentID() const
Definition: MemoryID.cpp:289
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_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:110
armarx::armem::server::ltm::CoreSegment::CoreSegment
CoreSegment(const detail::mixin::Path &, const detail::mixin::MongoDBSettings &, const std::string &, const MemoryID &, const std::shared_ptr< Processors > &)
Definition: CoreSegment.cpp:10
armarx::armem::server::ltm::CoreSegment::_loadAllReferences
void _loadAllReferences(armem::wm::CoreSegment &) override
Definition: CoreSegment.cpp:82
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
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::mixin::DiskMemoryItemMixin::writeDataToFile
void writeDataToFile(const std::string &filename, const std::vector< unsigned char > &data) const
Definition: DiskStorageMixin.cpp:145
armarx::armem::server::ltm::disk::constantes::TYPE_FILENAME
const std::string TYPE_FILENAME
Definition: util.h:12
armarx::armem::server::ltm::CoreSegment::findProviderSegment
std::shared_ptr< ProviderSegment > findProviderSegment(const std::string &) const override
find provider segment
Definition: CoreSegment.cpp:65
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFullPathExists
void ensureFullPathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:130
armarx::armem::server::ltm::detail::MemoryItem::name
std::string name() const
Definition: MemoryItem.cpp:43