DiskPersistence.cpp
Go to the documentation of this file.
2 
4 
6 {
7 
8  std::vector<std::string>
10  {
11  ARMARX_DEBUG << "DiskPersistence: GetContainerKey, memID=" << id.str();
12 
13  std::vector<std::string> containers;
14 
15  if (!enabled_)
16  {
17  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
18  return containers;
19  }
20 
21  // If it has a nice form
22  if (!id.hasEntityName() || id.hasTimestamp())
23  {
24  std::vector<std::filesystem::path> dirs = getAllDirectories(id);
25 
26 
27  for (auto& path : dirs)
28  {
29  std::string container = path.filename().string();
30 
31  if (!container.empty())
32  {
33  containers.emplace_back(container);
34  }
35  }
36  }
37  // If it has not a nice form
38  else
39  {
40  std::vector<std::filesystem::path> dayDirs = getAllDirectories(id);
41 
42  for (std::filesystem::path& dayDir : dayDirs)
43  {
44  if (!util::fs::detail::isDateString(dayDir.filename()))
45  {
46  ARMARX_WARNING << "Found a non-date folder inside an entity '" << id.str()
47  << "' with name '" << dayDir.filename() << "'. "
48  << "Ignoring this folder, however this is a bad situation.";
49  continue;
50  }
51 
52  std::vector<std::filesystem::path> secondDirs = util::fs::getAllDirectories(dayDir);
53 
54  for (std::filesystem::path& secondDir : secondDirs)
55  {
56  if (!util::fs::detail::isNumberString(secondDir.filename()))
57  {
58  ARMARX_WARNING << "Found a non-timestamp folder inside an entity '"
59  << id.str() << "' hours folder with name '"
60  << secondDir.filename() << "'. "
61  << "Ignoring this folder, however this is a bad situation.";
62  continue;
63  }
64 
65  std::vector<std::filesystem::path> timestampDirs =
66  util::fs::getAllDirectories(secondDir);
67 
68  for (std::filesystem::path& timestampDir : timestampDirs)
69  {
70  if (!util::fs::detail::isNumberString(timestampDir.filename()))
71  {
73  << "Found a non-timestamp folder inside an entity '" << id.str()
74  << "' seconds folder with name '" << timestampDir.filename()
75  << "'. "
76  << "Ignoring this folder, however this is a bad situation.";
77  continue;
78  }
79 
80  std::string container = timestampDir.filename().string();
81 
82  if (!container.empty())
83  {
84  containers.emplace_back(container);
85  }
86  }
87  }
88  }
89  }
90 
91  return containers;
92  }
93 
94  std::vector<std::string>
96  {
97  if (!enabled_)
98  {
99  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
100  return std::vector<std::string>();
101  }
102 
103  std::vector<std::filesystem::path> files = getAllFiles(id);
104  std::vector<std::string> filesStr;
105 
106  for (auto& path : files)
107  {
108  std::string item = path.filename().string();
109 
110  if (!item.empty())
111  {
112  filesStr.emplace_back(item);
113  }
114  }
115 
116  return filesStr;
117  }
118 
119  bool
121  {
122  ARMARX_DEBUG << "Contains container key=" << key << "? mem id=" << id.str();
123 
124  if (!enabled_)
125  {
126  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
127  return false;
128  }
129 
130  //from id get the directory for this memory item and append the key to it, then check if the directory exists
131  auto path_to_id = getFullPath(id);
132  auto correct_container_path = path_to_id / key;
133  bool contains_container = util::fs::directoryExists(correct_container_path);
134 
135  ARMARX_DEBUG << "Contains container?: " << contains_container;
136 
137  return contains_container;
138  }
139 
140  bool
142  {
143  if (!enabled_)
144  {
145  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
146  return false;
147  }
148 
149  return fileExists(id, key);
150  }
151 
152  void
154  std::string key,
155  std::vector<unsigned char>& data)
156  {
157  if (!enabled_)
158  {
159  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
160  return;
161  }
162 
163  ensureFullPathExists(id, true);
164  ensureFileExists(id, key, true);
165 
166  if (enoughDiskSpaceLeft())
167  {
168  writeDataToFile(id, key, data);
169  }
170  else
171  {
172  ARMARX_DEBUG << "Not enough DiskSpace available for DiskPersistance-Strategy";
173  }
174  }
175 
176  std::vector<unsigned char>
178  {
179  if (!enabled_)
180  {
181  ARMARX_DEBUG << "Disk strategy " << identifier_ << " disabled. Aborting call.";
182  return std::vector<unsigned char>();
183  }
184 
185  if (fileExists(id, key))
186  {
187  return readDataFromFile(id, key);
188  }
189 
190  return std::vector<unsigned char>();
191  }
192 
193  std::filesystem::path
194  DiskPersistence::getMemoryParentPath()
195  {
196  std::string p = memoryParentPath_.string();
197 
199 
200  return p;
201  }
202 
203  std::filesystem::path
204  DiskPersistence::getFullPath(const armarx::armem::MemoryID& id)
205  {
206  auto p = getMemoryParentPath() / getExportName();
207 
208  auto cleanID =
209  id.cleanID(); //somehow, the iDs are jumbled when loading the LTM from disk, this solves it for now
210 
211  auto fullPath = util::fs::toPath(p, cleanID);
212 
213  //inform user about change:
214  ARMARX_DEBUG << "Full path is " << fullPath;
215 
216  return fullPath;
217  }
218 
219  bool
220  DiskPersistence::fullPathExists(const armarx::armem::MemoryID& id)
221  {
222  auto p = getFullPath(id);
223  return util::fs::directoryExists(p);
224  }
225 
226  bool
227  DiskPersistence::fileExists(const armarx::armem::MemoryID& id, const std::string& filename)
228  {
229  auto p = getFullPath(id) / filename;
230  return util::fs::fileExists(p);
231  }
232 
233  void
234  DiskPersistence::ensureFullPathExists(const armarx::armem::MemoryID& id,
235  bool createIfNotExistent)
236  {
237  auto p = getFullPath(id);
238  util::fs::ensureDirectoryExists(p, createIfNotExistent);
239  }
240 
241  void
242  DiskPersistence::ensureFileExists(const armarx::armem::MemoryID& id,
243  const std::string& filename,
244  bool createIfNotExistent)
245  {
246  auto p = getFullPath(id) / filename;
247  util::fs::ensureFileExists(p, createIfNotExistent);
248  }
249 
250  void
251  DiskPersistence::writeDataToFile(const armarx::armem::MemoryID& id,
252  const std::string& filename,
253  const std::vector<unsigned char>& data)
254  {
255  auto p = getFullPath(id) / filename;
257  }
258 
259  std::vector<unsigned char>
260  DiskPersistence::readDataFromFile(const armarx::armem::MemoryID& id,
261  const std::string& filename)
262  {
263  auto p = getFullPath(id) / filename;
264  return util::fs::readDataFromFile(p);
265  }
266 
267  std::vector<std::filesystem::path>
268  DiskPersistence::getAllFiles(const armarx::armem::MemoryID& id)
269  {
270  if (fullPathExists(id))
271  {
272  auto p = getFullPath(id);
273  return util::fs::getAllFiles(p);
274  }
275 
276  return std::vector<std::filesystem::path>();
277  }
278 
279  std::vector<std::filesystem::path>
280  DiskPersistence::getAllDirectories(const armarx::armem::MemoryID& id)
281  {
282  if (fullPathExists(id))
283  {
284  auto p = getFullPath(id);
285  return util::fs::getAllDirectories(p);
286  }
287 
288  return std::vector<std::filesystem::path>();
289  }
290 
291  bool
292  DiskPersistence::enoughDiskSpaceLeft()
293  {
294  std::string path_to_disk = this->getMemoryParentPath();
295  ARMARX_DEBUG << "Checking availability of disk space at " << path_to_disk;
296 
297  if (std::filesystem::exists(path_to_disk))
298  {
299  try
300  {
301  auto space_info = std::filesystem::space(path_to_disk);
302  int const conversion_factor = 1024;
303 
304  auto available_space = space_info.available /
305  (conversion_factor * conversion_factor * conversion_factor);
306  ARMARX_DEBUG << "Capacity: "
307  << space_info.capacity /
308  (conversion_factor * conversion_factor * conversion_factor)
309  << " GB\n";
310  ARMARX_DEBUG << "Free space: "
311  << space_info.free /
312  (conversion_factor * conversion_factor * conversion_factor)
313  << " GB\n";
314  ARMARX_DEBUG << "Available space: "
315  << space_info.available /
316  (conversion_factor * conversion_factor * conversion_factor)
317  << " GB\n";
318 
319  ARMARX_DEBUG << "Min disk space: " << this->minDiskSpace << " GB\n";
320 
321  return static_cast<bool>(available_space >= this->minDiskSpace);
322  }
323  catch (const std::filesystem::filesystem_error& e)
324  {
325  ARMARX_WARNING << "Error: " << e.what() << '\n';
326  return false;
327  }
328  catch (...)
329  {
330  ARMARX_DEBUG << "Error while trying to get info on available disk space";
331  return false;
332  }
333  }
334  else
335  {
336  ARMARX_DEBUG << "Cannot find path to disk and thus cannot check if enough space is "
337  "still available";
338  return true;
339  }
340  }
341 } // namespace armarx::armem::server::ltm::persistence
files
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation files(the "Software")
armarx::armem::server::ltm::persistence::DiskPersistence::retrieveItem
std::vector< unsigned char > retrieveItem(const armarx::armem::MemoryID &id, std::string key) override
Reads the data of the file with name 'key' at the current location.
Definition: DiskPersistence.cpp:177
armarx::armem::server::ltm::util::fs::ensureFileExists
void ensureFileExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:162
armarx::armem::server::ltm::util::fs::writeDataToFile
void writeDataToFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem.cpp:181
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::enabled_
bool enabled_
If false, the strategy is not writing or reading anything.
Definition: MemoryPersistenceStrategy.h:213
armarx::armem::server::ltm::persistence
Definition: DiskPersistence.cpp:5
armarx::armem::server::ltm::persistence::DiskPersistence::getContainerKeys
std::vector< std::string > getContainerKeys(const armarx::armem::MemoryID &id) override
Returns all containers for the current id.
Definition: DiskPersistence.cpp:9
armarx::armem::server::ltm::util::fs::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &p)
Definition: filesystem.cpp:195
armarx::armem::server::ltm::util::fs::ensureDirectoryExists
void ensureDirectoryExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:145
armarx::armem::server::ltm::persistence::DiskPersistence::storeItem
void storeItem(const armarx::armem::MemoryID &id, std::string key, std::vector< unsigned char > &data) override
Create a new file with name 'key' and stores the data inside it.
Definition: DiskPersistence.cpp:153
space
std::string space
Definition: OrientedTactileSensorUnit.cpp:371
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::armem::server::ltm::util::fs::getAllDirectories
std::vector< std::filesystem::path > getAllDirectories(const std::filesystem::path &p)
Definition: filesystem.cpp:205
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::getExportName
std::string getExportName()
Definition: MemoryPersistenceStrategy.h:150
armarx::armem::server::ltm::util::fs::detail::isNumberString
bool isNumberString(const std::string &s)
Definition: filesystem.cpp:71
armarx::armem::server::ltm::util::fs::toPath
std::filesystem::path toPath(const std::filesystem::path &base, const armem::MemoryID &id)
Definition: filesystem.cpp:97
armarx::armem::server::ltm::util::fs::directoryExists
bool directoryExists(const std::filesystem::path &p)
Definition: filesystem.cpp:133
armarx::armem::server::ltm::persistence::MemoryPersistenceStrategy::identifier_
std::string identifier_
Name of the strategy.
Definition: MemoryPersistenceStrategy.h:201
armarx::armem::server::ltm::util::fs::detail::isDateString
bool isDateString(const std::string &s)
Definition: filesystem.cpp:84
armarx::armem::server::ltm::persistence::DiskPersistence::containsItem
bool containsItem(const armarx::armem::MemoryID &id, std::string key) override
Checks if current container contains the item defined by its key.
Definition: DiskPersistence.cpp:141
armarx::ArmarXDataPath::ReplaceEnvVars
static bool ReplaceEnvVars(std::string &string)
ReplaceEnvVars replaces environment variables in a string with their values, if the env.
Definition: ArmarXDataPath.cpp:480
filesystem.h
DiskPersistence.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::ltm::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:139
armarx::armem::server::ltm::persistence::DiskPersistence::getItemKeys
std::vector< std::string > getItemKeys(const armarx::armem::MemoryID &id) override
Returns all items for the current id.
Definition: DiskPersistence.cpp:95
armarx::armem::server::ltm::persistence::DiskPersistence::minDiskSpace
int minDiskSpace
Definition: DiskPersistence.h:128
armarx::armem::server::ltm::util::fs::getAllFiles
std::vector< std::filesystem::path > getAllFiles(const std::filesystem::path &p)
Definition: filesystem.cpp:224
armarx::armem::server::ltm::persistence::DiskPersistence::containsContainer
bool containsContainer(const armarx::armem::MemoryID &id, std::string key) override
Checks if the container is available for the current memory id.
Definition: DiskPersistence.cpp:120