DiskStorageMixin.cpp
Go to the documentation of this file.
1 // Header
2 #include "DiskStorageMixin.h"
3 
4 // STD/STL
5 #include <algorithm>
6 #include <fstream>
7 #include <iostream>
8 
9 // Simox
10 #include <SimoxUtility/algorithm/string.h>
11 
12 // ArmarX
17 
19 {
21  const std::string& exportName,
22  const armem::MemoryID& id) :
23  memoryBasePath(memoryParentPath), exportName(exportName), _id(id)
24  {
25  }
26 
27  Path
29  {
30  //set path to include date of creation as prefix:
31  auto current_date = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
32  std::tm* localTime = std::localtime(&current_date);
33 
34  // convert current time into string:
35  std::stringstream ss;
36  ss << std::put_time(localTime, "%Y_%m_%d");
37  std::string dateString = ss.str();
38 
39  // change memory base path to include current date (date of memory creation)
40  std::filesystem::path current_base_path = n;
41  current_base_path.append(dateString);
42 
43  return current_base_path;
44  }
45 
46  void
48  {
49  ARMARX_DEBUG << "Currently setting export name to: " << n;
50  exportName = n;
51  }
52 
53  void
55  {
56  ARMARX_CHECK_NOT_EMPTY(_id.memoryName) << " The full id was: " << _id.str();
57 
58  _id = n;
59  }
60 
61  void
62  DiskMemoryItemMixin::configureMixin(const nlohmann::json& json)
63  {
64  if (json.find("DiskMemory.memoryParentPath") != json.end())
65  {
66  //memoryBasePath = Path(json.at("DiskMemory.memoryParentPath"));
67  }
68  }
69 
70  Path
72  {
73  std::string p = [&](){
74  if(memoryBasePathString.empty()){
75  return memoryBasePath.string();
76  }
77 
78  return memoryBasePathString;
79  }();
80 
82 
83  return p;
84  }
85 
86  Path
88  {
89  auto p = getMemoryBasePath() / exportName;
90  //p = this->addDateToMemoryBasePath(p);
91  //ARMARX_INFO << VAROUT(_id);
92  //ARMARX_INFO << VAROUT(_id.cleanID());
93  auto cleanID = _id.cleanID(); //somehow, the iDs are jumbled when loading the LTM from disk, this solves it for now
94 
95  auto fullPath = util::fs::toPath(p, cleanID);
96 
97  //inform user about change:
98  ARMARX_DEBUG << "Full path is " << fullPath;
99 
100  return fullPath;
101  }
102 
103  bool
105  {
107  }
108 
109  bool
111  {
112  auto p = getFullPath();
113  return util::fs::directoryExists(p);
114  }
115 
116  bool
117  DiskMemoryItemMixin::fileExists(const std::string& filename) const
118  {
119  auto p = getFullPath() / filename;
120  return util::fs::fileExists(p);
121  }
122 
123  void
124  DiskMemoryItemMixin::ensureMemoryBasePathExists(bool createIfNotExistent) const
125  {
126  util::fs::ensureDirectoryExists(getMemoryBasePath(), createIfNotExistent);
127  }
128 
129  void
130  DiskMemoryItemMixin::ensureFullPathExists(bool createIfNotExistent) const
131  {
132  auto p = getFullPath();
133  util::fs::ensureDirectoryExists(p, createIfNotExistent);
134  }
135 
136  void
138  bool createIfNotExistent) const
139  {
140  auto p = getFullPath() / filename;
141  util::fs::ensureFileExists(p, createIfNotExistent);
142  }
143 
144  void
146  const std::vector<unsigned char>& data) const
147  {
148  auto p = getFullPath() / filename;
150  }
151 
152  std::vector<unsigned char>
154  {
155  auto p = getFullPath() / filename;
156  return util::fs::readDataFromFile(p);
157  }
158 
159  std::vector<Path>
161  {
162  auto p = getFullPath();
163  return util::fs::getAllDirectories(p);
164  }
165 
166  std::vector<Path>
168  {
169  auto p = getFullPath();
170  return util::fs::getAllFiles(p);
171  }
172 
174  {
175  defs->optional(memoryBasePathString, prefix + "exportPath");
176  }
177 } // namespace armarx::armem::server::ltm::detail::mixin
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::addDateToMemoryBasePath
Path addDateToMemoryBasePath(const std::filesystem::path &n) const
Definition: DiskStorageMixin.cpp:28
LocalException.h
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureMemoryBasePathExists
void ensureMemoryBasePathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:124
armarx::armem::server::ltm::util::fs::ensureFileExists
void ensureFileExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:159
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getMemoryBasePath
Path getMemoryBasePath() const
Definition: DiskStorageMixin.cpp:71
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFileExists
void ensureFileExists(const std::string &filename, bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:137
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::createPropertyDefinitions
void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)
Definition: DiskStorageMixin.cpp:173
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem::server::ltm::util::fs::writeDataToFile
void writeDataToFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem.cpp:178
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getAllFiles
std::vector< Path > getAllFiles() const
Definition: DiskStorageMixin.cpp:167
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::setMixinMemoryID
void setMixinMemoryID(const MemoryID &n)
Definition: DiskStorageMixin.cpp:54
ARMARX_CHECK_NOT_EMPTY
#define ARMARX_CHECK_NOT_EMPTY(c)
Definition: ExpressionException.h:224
armarx::armem::server::ltm::util::fs::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::filesystem::path &p)
Definition: filesystem.cpp:192
armarx::armem::server::ltm::util::fs::ensureDirectoryExists
void ensureDirectoryExists(const std::filesystem::path &p, bool createIfNotExistent=false)
Definition: filesystem.cpp:142
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::detail::mixin::DiskMemoryItemMixin::fileExists
bool fileExists(const std::string &filename) const
Definition: DiskStorageMixin.cpp:117
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
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::util::fs::getAllDirectories
std::vector< std::filesystem::path > getAllDirectories(const std::filesystem::path &p)
Definition: filesystem.cpp:202
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::setMixinExportName
void setMixinExportName(const std::string &n)
Definition: DiskStorageMixin.cpp:47
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::DiskMemoryItemMixin
DiskMemoryItemMixin()=default
filename
std::string filename
Definition: VisualizationRobot.cpp:83
armarx::armem::server::ltm::util::fs::toPath
std::filesystem::path toPath(const std::filesystem::path &base, const armem::MemoryID &id)
Definition: filesystem.cpp:94
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
armarx::armem::server::ltm::util::fs::directoryExists
bool directoryExists(const std::filesystem::path &p)
Definition: filesystem.cpp:130
armarx::armem::MemoryID::cleanID
MemoryID cleanID() const
Definition: MemoryID.cpp:132
TimeUtil.h
armarx::armem::server::ltm::detail::mixin
Definition: BufferedMemoryMixin.cpp:3
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:110
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:483
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
Logging.h
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::configureMixin
void configureMixin(const nlohmann::json &json)
Definition: DiskStorageMixin.cpp:62
ArmarXDataPath.h
armarx::armem::server::ltm::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:136
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getFullPath
Path getFullPath() const
Definition: DiskStorageMixin.cpp:87
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
DiskStorageMixin.h
armarx::armem::server::ltm::util::fs::getAllFiles
std::vector< std::filesystem::path > getAllFiles(const std::filesystem::path &p)
Definition: filesystem.cpp:221
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFullPathExists
void ensureFullPathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:130
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::memoryBasePathExists
bool memoryBasePathExists() const
Definition: DiskStorageMixin.cpp:104