DiskPersistence.cpp
Go to the documentation of this file.
2 
4 
6 {
7 
8  std::vector<std::string>
10  {
11  std::vector<std::string> containers;
12 
13  if (!enabled_)
14  {
15  return containers;
16  }
17 
18  // If it has a nice form
19  if (!id.hasEntityName() || id.hasTimestamp())
20  {
21  std::vector<std::filesystem::path> dirs = getAllDirectories(id);
22 
23 
24  for (auto& path : dirs)
25  {
26  std::string container = path.filename().string();
27 
28  if (!container.empty())
29  {
30  containers.emplace_back(container);
31  }
32  }
33  }
34  // If it has not a nice form
35  else
36  {
37  std::vector<std::filesystem::path> dayDirs = getAllDirectories(id);
38 
39  for (std::filesystem::path& dayDir : dayDirs)
40  {
41  if (!util::fs::detail::isDateString(dayDir.filename()))
42  {
43  ARMARX_WARNING << "Found a non-date folder inside an entity '" << id.str()
44  << "' with name '" << dayDir.filename() << "'. "
45  << "Ignoring this folder, however this is a bad situation.";
46  continue;
47  }
48 
49  std::vector<std::filesystem::path> secondDirs = util::fs::getAllDirectories(dayDir);
50 
51  for (std::filesystem::path& secondDir : secondDirs)
52  {
53  if (!util::fs::detail::isNumberString(secondDir.filename()))
54  {
55  ARMARX_WARNING << "Found a non-timestamp folder inside an entity '"
56  << id.str() << "' hours folder with name '"
57  << secondDir.filename() << "'. "
58  << "Ignoring this folder, however this is a bad situation.";
59  continue;
60  }
61 
62  std::vector<std::filesystem::path> timestampDirs =
63  util::fs::getAllDirectories(secondDir);
64 
65  for (std::filesystem::path& timestampDir : timestampDirs)
66  {
67  if (!util::fs::detail::isNumberString(timestampDir.filename()))
68  {
70  << "Found a non-timestamp folder inside an entity '" << id.str()
71  << "' seconds folder with name '" << timestampDir.filename()
72  << "'. "
73  << "Ignoring this folder, however this is a bad situation.";
74  continue;
75  }
76 
77  std::string container = timestampDir.filename().string();
78 
79  if (!container.empty())
80  {
81  containers.emplace_back(container);
82  }
83  }
84  }
85  }
86  }
87 
88  return containers;
89  }
90 
91  std::vector<std::string>
93  {
94  if (!enabled_)
95  {
96  return std::vector<std::string>();
97  }
98 
99  std::vector<std::filesystem::path> files = getAllFiles(id);
100  std::vector<std::string> filesStr;
101 
102  for (auto& path : files)
103  {
104  std::string item = path.filename().string();
105 
106  if (!item.empty())
107  {
108  filesStr.emplace_back(item);
109  }
110  }
111 
112  return filesStr;
113  }
114 
115  bool
117  {
118  if (!enabled_)
119  {
120  return false;
121  }
122 
123  //from id get the directory for this memory item and append the key to it, then check if the directory exists
124  auto path_to_id = getFullPath(id);
125  auto correct_container_path = path_to_id / key;
126  bool contains_container = util::fs::directoryExists(correct_container_path);
127 
128  return contains_container;
129  }
130 
131  bool
133  {
134  if (!enabled_)
135  {
136  return false;
137  }
138 
139  return fileExists(id, key);
140  }
141 
142  void
144  std::string key,
145  std::vector<unsigned char>& data)
146  {
147  if (!enabled_)
148  {
149  return;
150  }
151 
152  ensureFullPathExists(id, true);
153  ensureFileExists(id, key, true);
154 
155  if (enoughDiskSpaceLeft())
156  {
157  writeDataToFile(id, key, data);
158  }
159  else
160  {
161  ARMARX_DEBUG << "Not enough DiskSpace available for DiskPersistance-Strategy";
162  }
163  }
164 
165  std::vector<unsigned char>
167  {
168  if (!enabled_)
169  {
170  return std::vector<unsigned char>();
171  }
172 
173  if (fileExists(id, key))
174  {
175  return readDataFromFile(id, key);
176  }
177 
178  return std::vector<unsigned char>();
179  }
180 
181  std::filesystem::path
182  DiskPersistence::getMemoryParentPath()
183  {
184  std::string p = memoryParentPath_.string();
185 
187 
188  return p;
189  }
190 
191  std::filesystem::path
192  DiskPersistence::getFullPath(const armarx::armem::MemoryID& id)
193  {
194  auto p = getMemoryParentPath() / getExportName();
195 
196  auto cleanID =
197  id.cleanID(); //somehow, the iDs are jumbled when loading the LTM from disk, this solves it for now
198 
199  auto fullPath = util::fs::toPath(p, cleanID);
200 
201  return fullPath;
202  }
203 
204  bool
205  DiskPersistence::fullPathExists(const armarx::armem::MemoryID& id)
206  {
207  auto p = getFullPath(id);
208  return util::fs::directoryExists(p);
209  }
210 
211  bool
212  DiskPersistence::fileExists(const armarx::armem::MemoryID& id, const std::string& filename)
213  {
214  auto p = getFullPath(id) / filename;
215  return util::fs::fileExists(p);
216  }
217 
218  void
219  DiskPersistence::ensureFullPathExists(const armarx::armem::MemoryID& id,
220  bool createIfNotExistent)
221  {
222  auto p = getFullPath(id);
223  util::fs::ensureDirectoryExists(p, createIfNotExistent);
224  }
225 
226  void
227  DiskPersistence::ensureFileExists(const armarx::armem::MemoryID& id,
228  const std::string& filename,
229  bool createIfNotExistent)
230  {
231  auto p = getFullPath(id) / filename;
232  util::fs::ensureFileExists(p, createIfNotExistent);
233  }
234 
235  void
236  DiskPersistence::writeDataToFile(const armarx::armem::MemoryID& id,
237  const std::string& filename,
238  const std::vector<unsigned char>& data)
239  {
240  auto p = getFullPath(id) / filename;
242  }
243 
244  std::vector<unsigned char>
245  DiskPersistence::readDataFromFile(const armarx::armem::MemoryID& id,
246  const std::string& filename)
247  {
248  auto p = getFullPath(id) / filename;
249  return util::fs::readDataFromFile(p);
250  }
251 
252  std::vector<std::filesystem::path>
253  DiskPersistence::getAllFiles(const armarx::armem::MemoryID& id)
254  {
255  if (fullPathExists(id))
256  {
257  auto p = getFullPath(id);
258  return util::fs::getAllFiles(p);
259  }
260 
261  return std::vector<std::filesystem::path>();
262  }
263 
264  std::vector<std::filesystem::path>
265  DiskPersistence::getAllDirectories(const armarx::armem::MemoryID& id)
266  {
267  if (fullPathExists(id))
268  {
269  auto p = getFullPath(id);
270  return util::fs::getAllDirectories(p);
271  }
272 
273  return std::vector<std::filesystem::path>();
274  }
275 
276  bool
277  DiskPersistence::enoughDiskSpaceLeft()
278  {
279  std::string path_to_disk = this->getMemoryParentPath();
280  bool debug_info_output_enabled = false;
281 
282  if (std::filesystem::exists(path_to_disk))
283  {
284  try
285  {
286  auto space_info = std::filesystem::space(path_to_disk);
287  int const conversion_factor = 1024;
288 
289  auto available_space = space_info.available /
290  (conversion_factor * conversion_factor * conversion_factor);
291 
292  if (debug_info_output_enabled)
293  {
294  ARMARX_DEBUG << "Capacity: "
295  << space_info.capacity /
296  (conversion_factor * conversion_factor * conversion_factor)
297  << " GB\n";
298  ARMARX_DEBUG << "Free space: "
299  << space_info.free /
300  (conversion_factor * conversion_factor * conversion_factor)
301  << " GB\n";
302  ARMARX_DEBUG << "Available space: "
303  << space_info.available /
304  (conversion_factor * conversion_factor * conversion_factor)
305  << " GB\n";
306 
307  ARMARX_DEBUG << "Min disk space: " << this->minDiskSpace << " GB\n";
308  }
309  return static_cast<bool>(available_space >= this->minDiskSpace);
310  }
311  catch (const std::filesystem::filesystem_error& e)
312  {
313  ARMARX_WARNING << "Error: " << e.what() << '\n';
314  return false;
315  }
316  catch (...)
317  {
318  ARMARX_DEBUG << "Error while trying to get info on available disk space";
319  return false;
320  }
321  }
322  else
323  {
324  ARMARX_DEBUG << "Cannot find path to disk and thus cannot check if enough space is "
325  "still available";
326  return true;
327  }
328  }
329 } // 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:166
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:143
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::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:132
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:92
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:116