GridFileManager.h
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package MemoryX::Core
17* @author Alexey Kozlov ( kozlov at kit dot edu)
18* @date Sep 27, 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#pragma once
24
25#include <filesystem>
26#include <memory>
27
29
31#include <MemoryX/interface/components/CommonStorageInterface.h>
32#include <MemoryX/interface/core/EntityBase.h>
33#include <MemoryX/interface/core/GridFileInterface.h>
34
35namespace memoryx
36{
37 /**
38 * GridFileManager provides utility functions for working with files in Mongo GridFS and
39 * links to them stored in entity attributes (s. memoryx::MongoDBRef)
40 */
42 {
43 public:
44 /**
45 * Constructs new GridFileManager.
46 *
47 * GridFileManager needs a MongoDB connection, so CommonStorage proxy needs to be specified.
48 * Cache path for files is retrived from the Application properties.
49 *
50 * @param databasePrx CommonStorage proxy. It can be obtained either directly or via getCommonStorage()
51 * method of WorkingMemory and PriorKnowledge.
52 */
53 GridFileManager(const CommonStorageInterfacePrx& databasePrx);
54
55 /**
56 * Constructs new GridFileManager.
57 *
58 * GridFileManager needs a MongoDB connection, so CommonStorage proxy needs to be specified. Use this constructor, if you
59 * want to provide an own cache path.
60 *
61 * @param databasePrx CommonStorage proxy. It can be obtained either directly or via getCommonStorage()
62 * method of WorkingMemory and PriorKnowledge.
63 * @param cachePath Path where to store cache files. Path may contain environmenta variables, which get replaced by their value.
64 */
65 GridFileManager(const CommonStorageInterfacePrx& databasePrx, const std::string& cachePath);
66
67 ~GridFileManager() override;
68
69 /**
70 * Retrieves a local path where files will be cached.
71 */
72 std::string getFileCachePath() const;
73
74 /**
75 * Caches the file locally and opens a filestream for it.
76 *
77 * @param filePrx file proxy
78 * @param fs file stream to open.
79 *
80 * @return true on succcess, false otherwise
81 */
82 bool getFileStream(GridFileInterfacePrx& filePrx, std::ifstream& fs);
83
84 /**
85 * Caches the file locally and opens a filestream for it.
86 *
87 * @param fileAttr entity attribute containing a file reference
88 * @param fs file stream to open.
89 *
90 * @return true on succcess, false otherwise
91 */
92 bool getFileStream(const EntityAttributeBasePtr& fileAttr, std::ifstream& fs);
93
94 /**
95 * Caches the file locally and returns the filename.
96 *
97 * @param filePrx file proxy
98 * @param cacheFileName name of local file
99 * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
100 * or it could be given a special name to simplify modification check (false). Even though the latter option
101 * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
102 * .iv file)
103 *
104 * @return true on succcess, false otherwise
105 */
106 bool ensureFileInCache(GridFileInterfacePrx& filePrx,
107 std::string& cacheFileName,
108 bool preserveOriginalName = false);
109
110 /**
111 * Caches the file locally and returns the filename.
112 *
113 * @param fileAttr entity attribute containing a file reference
114 * @param cacheFileName name of local file
115 * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
116 * or it could be given a special name to simplify modification check (false). Even though the latter option
117 * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
118 * .iv file)
119 *
120 * @return true on succcess, false otherwise
121 */
122 bool ensureFileInCache(const EntityAttributeBasePtr& fileAttr,
123 std::string& cacheFileName,
124 bool preserveOriginalName = false);
125
126 /**
127 * Caches multiple files locally.
128 *
129 * @param fileAttr entity attribute containing file references
130 * @param cacheFileNames names of caches files
131 * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
132 * or it could be given a special name to simplify modification check (false). Even though the latter option
133 * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
134 * .iv file)
135 *
136 * @return true on succcess, false otherwise
137 */
138 bool ensureFilesInCache(const EntityAttributeBasePtr& fileAttr,
139 std::vector<std::string>& cacheFileNames,
140 bool preserveOriginalNames = false);
141
142 /**
143 * Caches multiple files locally. Version without returning the cache file names
144 *
145 * @param fileAttr entity attribute containing file references
146 * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
147 * or it could be given a special name to simplify modification check (false). Even though the latter option
148 * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
149 * .iv file)
150 *
151 * @return true on succcess, false otherwise
152 */
153 bool ensureFilesInCache(const EntityAttributeBasePtr& fileAttr,
154 bool preserveOriginalNames = false);
155
156 /**
157 * Stores a file in GridFS and puts a reference to it into entity attribute. Overwrites any
158 * previously stored attribute.
159 *
160 * @param filesDBName Mongo database name to store file into
161 * @param localFileName name of a file to store
162 * @param fileAttr entity attribute to store file reference into
163 * @param gridFSName name of the file in GridFS. Default: localFileName WITHOUT path
164 *
165 * @return MongoDB id of the file object as returned by GridFS
166 */
167 std::string storeFileToAttr(const std::string& filesDBName,
168 const std::string& localFileName,
169 EntityAttributeBasePtr& fileAttr,
170 const std::string& gridFSName = "");
171
172 /**
173 * Stores a file in GridFS and puts a reference to it into entity attribute. Allows
174 * to add multiple files to an attribdatabasePrxute.
175 *
176 * @param filesDBName Mongo database name to store file into
177 * @param localFileName name of a file to store (gets turned into an absolute path)
178 * @param fileAttr entity attribute to store file reference into
179 * @param gridFSName name of the file in GridFS. Default: localFileName WITHOUT path
180 *
181 * @return MongoDB id of the file object as returned by GridFS
182 */
183 std::string addFileToAttr(const std::string& filesDBName,
184 const std::string& localFileName,
185 EntityAttributeBasePtr& fileAttr,
186 const std::string& gridFSName = "");
187
188 /**
189 * Stores a complete directory tree in GridFS and puts a reference to it into entity attribute
190 * Overwrites any previously stored attribute.
191 *
192 * @param filesDBName Mongo database name to store file into
193 * @param localDirectoryName name of a directory to store
194 * @param fileAttr entity attribute to store file reference into
195 *
196 * @return true on succcess, false otherwise
197 */
198 bool storeDirectoryToAttr(const std::string& filesDBName,
199 const std::string& localDirectoryName,
200 EntityAttributeBasePtr& fileAttr,
201 std::string excludeFilter = ".svn");
202
203 /**
204 * Stores a set of files with in GridFS and puts a reference to it into entity attribute.
205 * The directory structure is preserved while localBaseDirectoryName and all preceding directories are removed from the filenames.
206 * Overwrites any previously stored attribute.
207 *
208 * @param filesDBName Mongo database name to store file into
209 * @param localBaseDirectoryName the base directory. All file names will be made realtive to this directory
210 * @param localFiles The set of files. Absolute filenames.
211 * @param fileAttr entity attribute to store file reference into
212 *
213 * @return true on succcess, false otherwise
214 */
215 bool storeFilesToAttr(const std::string& filesDBName,
216 const std::string& localBaseDirectoryName,
217 const std::vector<std::string>& localFiles,
218 EntityAttributeBasePtr& fileAttr);
219
220 bool removeAttrFile(const EntityAttributeBasePtr& fileAttr, unsigned int fileIndex);
221 /**
222 * Removes all GridFS files referenced by entity attribute
223 *
224 * @param fileAttr entity attribute containing file reference(s)
225 *
226 * @return true on succcess, false otherwise
227 */
228 bool removeAttrFiles(const EntityAttributeBasePtr& fileAttr);
229
230
231 /**
232 * Removes GridFS directory referenced by entity attribute
233 *
234 * @param fileAttr entity attribute containing file reference(s)
235 *
236 * @return true on succcess, false otherwise
237 */
238 bool removeAttrDirectory(const EntityAttributeBasePtr& fileAttr);
239
240 CommonStorageInterfacePrx
241 getDBPrx() const
242 {
243 return databasePrx;
244 }
245
246 protected:
247 void init(std::string cachePath);
248
249 private:
250 std::filesystem::path fileCachePath;
251 CommonStorageInterfacePrx databasePrx;
252
253 GridFileInterfacePrx getFileProxyFromAttr(const AttributeBasePtr& attr) const;
254 GridFileList getFileProxiesFromAttr(const AttributeBasePtr& attr) const;
255
256 MongoDBRefPtr extractMongoDBRef(const AttributeBasePtr& attr) const;
257 MongoDBRefPtr extractMongoDBRef(const armarx::VariantBasePtr& value) const;
258
259 std::string makeRelativePath(const std::filesystem::path& directory,
260 const std::filesystem::path& basePath);
261 };
262
263 using GridFileManagerPtr = std::shared_ptr<GridFileManager>;
264
265} // namespace memoryx
Base Class for all Logging classes.
Definition Logging.h:240
bool storeFilesToAttr(const std::string &filesDBName, const std::string &localBaseDirectoryName, const std::vector< std::string > &localFiles, EntityAttributeBasePtr &fileAttr)
Stores a set of files with in GridFS and puts a reference to it into entity attribute.
void init(std::string cachePath)
bool getFileStream(GridFileInterfacePrx &filePrx, std::ifstream &fs)
Caches the file locally and opens a filestream for it.
bool ensureFileInCache(GridFileInterfacePrx &filePrx, std::string &cacheFileName, bool preserveOriginalName=false)
Caches the file locally and returns the filename.
bool removeAttrFiles(const EntityAttributeBasePtr &fileAttr)
Removes all GridFS files referenced by entity attribute.
GridFileManager(const CommonStorageInterfacePrx &databasePrx)
Constructs new GridFileManager.
bool removeAttrFile(const EntityAttributeBasePtr &fileAttr, unsigned int fileIndex)
std::string getFileCachePath() const
Retrieves a local path where files will be cached.
std::string addFileToAttr(const std::string &filesDBName, const std::string &localFileName, EntityAttributeBasePtr &fileAttr, const std::string &gridFSName="")
Stores a file in GridFS and puts a reference to it into entity attribute.
bool ensureFilesInCache(const EntityAttributeBasePtr &fileAttr, std::vector< std::string > &cacheFileNames, bool preserveOriginalNames=false)
Caches multiple files locally.
bool storeDirectoryToAttr(const std::string &filesDBName, const std::string &localDirectoryName, EntityAttributeBasePtr &fileAttr, std::string excludeFilter=".svn")
Stores a complete directory tree in GridFS and puts a reference to it into entity attribute Overwrite...
CommonStorageInterfacePrx getDBPrx() const
std::string storeFileToAttr(const std::string &filesDBName, const std::string &localFileName, EntityAttributeBasePtr &fileAttr, const std::string &gridFSName="")
Stores a file in GridFS and puts a reference to it into entity attribute.
bool removeAttrDirectory(const EntityAttributeBasePtr &fileAttr)
Removes GridFS directory referenced by entity attribute.
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
VirtualRobot headers.
IceInternal::Handle< MongoDBRef > MongoDBRefPtr
Definition MongoDBRef.h:114
std::shared_ptr< GridFileManager > GridFileManagerPtr