MemoryHandler.cpp
Go to the documentation of this file.
1 #include "MemoryHandler.h"
2 
3 #include <filesystem>
4 #include <iostream>
5 #include <string>
6 
7 #include <SimoxUtility/algorithm/string/string_tools.h>
8 
10 
16 
18  std::map<std::string, armem::client::Writer>* memoryWriters)
19 {
20  this->memoryWriters = memoryWriters;
21 }
22 
23 void
25 {
26  ARMARX_INFO << "Loading in separate class";
27 
28  std::filesystem::path path(directory);
29 
30  std::map<std::filesystem::path, wm::Memory> memoryData;
31 
32  if (not std::filesystem::is_directory(path))
33  {
34  ARMARX_WARNING << "You are trying to load data from a path which does not exist";
35  return;
36  }
37 
38  ARMARX_INFO << "Starting to load from disk";
39 
40  // Find out whether this is a single memory or a collection of memories by searching
41  // for a data.aron.* or metadata.aron.* file at depth 5 (if 6 then it is collection of memories)
42  bool isSingleMemory = false;
43  for (auto i = std::filesystem::recursive_directory_iterator(path);
44  i != std::filesystem::recursive_directory_iterator();
45  ++i)
46  {
47  if (i.depth() >
49  {
50  // In case we do not find any memory data in here
52  << "You are trying to import a directory which does not contain memory data";
53  return;
54  }
55 
56  auto& dir = *i;
57 
58  // if one matches it is enough to check
59  if (std::filesystem::is_regular_file(dir.path()) &&
60  simox::alg::starts_with(dir.path().filename(), "data.aron"))
61  {
62  isSingleMemory =
63  (i.depth() ==
65  break;
66  }
67  }
68 
69  int numLoaded = 0;
70  auto loadMemory = [&](const std::filesystem::path& p)
71  {
72  if (std::filesystem::is_directory(p))
73  {
74  std::string defaultIdent = "DefaultDisk";
75  std::string exportFolderName = p.parent_path().filename();
76  ARMARX_INFO << "Name of the folder trying to import: " << exportFolderName;
77  std::string exportName = exportFolderName;
78  std::shared_ptr<armem::server::ltm::persistence::DiskPersistence> diskPersistence =
79  std::make_shared<armem::server::ltm::persistence::DiskPersistence>(
80  defaultIdent, exportName, p.parent_path().parent_path());
81 
82  armem::server::ltm::Memory ltm(exportName, p.filename());
83  if (ltm.getPersistenceStrategy())
84  {
85  ltm.getPersistenceStrategy()->addStrategy(diskPersistence);
86  }
87  else
88  {
89  std::shared_ptr<armem::server::ltm::persistence::RedundantPersistenceStrategy>
90  redundantPersistence = std::make_shared<
92 
93  ltm.setPersistenceStrategy(redundantPersistence);
94  ltm.getPersistenceStrategy()->addStrategy(diskPersistence);
95  }
96 
98  ltm.loadAllAndResolve(); // load list of references and load data
99  memoryData[p] = std::move(memory);
100 
101  numLoaded++;
102  }
103  };
104 
105  if (isSingleMemory)
106  {
107  loadMemory(path);
108  }
109  else
110  {
111  // we have to load multiple memories (each subfolder)
112  for (const auto& dir : std::filesystem::directory_iterator(path))
113  {
114  loadMemory(dir.path());
115  }
116  }
117 
118  for (auto& [path, memory] : memoryData)
119  {
120  std::string name = memory.id().memoryName;
121 
122  if (memoryWriters && memoryWriters->count(name) > 0)
123  {
124  // we need to merge the current memory with the one from the disk.
125  // Please note that this may ignores coresegments, if not already existent
126  auto commit = armem::toCommit(memory);
127  memoryWriters->at(name).commit(commit);
128  }
129  else
130  {
131  ARMARX_INFO << "No memory with name '" << name
132  << "' available for commit. Create new virtual memory.";
133 
134  // Please note: Here we assume that a memory server with the same name does not exist.
135  // I think this assumption is ok, since nobody should use filepaths as memory name.
136  // Nonetheless, we did not restrict the user to do so...
137  std::string virtualMemoryName = name; // + " (at " + path.string() + ")";
138  memory.id().memoryName = virtualMemoryName;
139  memoryData[virtualMemoryName] = std::move(memory);
140  }
141  }
142 }
armarx::armem::server::ltm::persistence::RedundantPersistenceStrategy
Basically the option to use multiple ltm sinks as source or target.
Definition: RedundantPersistenceStrategy.h:22
MemoryPersistenceStrategy.h
MemoryHandler.h
armarx::armem::server::ltm::Memory::setPersistenceStrategy
void setPersistenceStrategy(std::shared_ptr< persistence::RedundantPersistenceStrategy > persistenceStrategy)
Definition: Memory.h:59
ltm.h
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:47
armarx::memory
Brief description of class memory.
Definition: memory.h:38
armarx::armem::server::ltm::Memory::getPersistenceStrategy
std::shared_ptr< persistence::RedundantPersistenceStrategy > getPersistenceStrategy() const
Definition: Memory.h:66
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:20
memory_definitions.h
armarx::make_shared
auto make_shared(Args &&... args)
Definition: ManagedIceObject.h:92
armarx::armem::gui::MemoryHandler::loadFromDisk
void loadFromDisk(const std::string &directory)
Definition: MemoryHandler.cpp:24
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::DEPTH_TO_DATA_FILES
static const int DEPTH_TO_DATA_FILES
Definition: MemoryPersistenceStrategy.h:189
armarx::armem::server::ltm::detail::MemoryBase::loadAllAndResolve
armem::wm::Memory loadAllAndResolve()
return the full ltm as a wm::Memory and resolves the references the ltm may be huge,...
Definition: MemoryBase.h:213
armarx::armem::gui::MemoryHandler::MemoryHandler
MemoryHandler(std::map< std::string, armem::client::Writer > *memoryWriters)
Definition: MemoryHandler.cpp:17
Memory.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
DiskPersistence.h
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::toCommit
Commit toCommit(const ContainerT &container)
Definition: operations.h:23