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 
28 #include <MemoryX/interface/core/EntityBase.h>
29 #include <MemoryX/interface/core/GridFileInterface.h>
30 #include <MemoryX/interface/components/CommonStorageInterface.h>
31 
34 
35 
36 namespace memoryx
37 {
38  /**
39  * GridFileManager provides utility functions for working with files in Mongo GridFS and
40  * links to them stored in entity attributes (s. memoryx::MongoDBRef)
41  */
43  public armarx::Logging
44  {
45  public:
46 
47  /**
48  * Constructs new GridFileManager.
49  *
50  * GridFileManager needs a MongoDB connection, so CommonStorage proxy needs to be specified.
51  * Cache path for files is retrived from the Application properties.
52  *
53  * @param databasePrx CommonStorage proxy. It can be obtained either directly or via getCommonStorage()
54  * method of WorkingMemory and PriorKnowledge.
55  */
56  GridFileManager(const CommonStorageInterfacePrx& databasePrx);
57 
58  /**
59  * Constructs new GridFileManager.
60  *
61  * GridFileManager needs a MongoDB connection, so CommonStorage proxy needs to be specified. Use this constructor, if you
62  * want to provide an own cache path.
63  *
64  * @param databasePrx CommonStorage proxy. It can be obtained either directly or via getCommonStorage()
65  * method of WorkingMemory and PriorKnowledge.
66  * @param cachePath Path where to store cache files. Path may contain environmenta variables, which get replaced by their value.
67  */
68  GridFileManager(const CommonStorageInterfacePrx& databasePrx, const std::string& cachePath);
69 
70  ~GridFileManager() override;
71 
72  /**
73  * Retrieves a local path where files will be cached.
74  */
75  std::string getFileCachePath() const;
76 
77  /**
78  * Caches the file locally and opens a filestream for it.
79  *
80  * @param filePrx file proxy
81  * @param fs file stream to open.
82  *
83  * @return true on succcess, false otherwise
84  */
85  bool getFileStream(GridFileInterfacePrx& filePrx, std::ifstream& fs);
86 
87  /**
88  * Caches the file locally and opens a filestream for it.
89  *
90  * @param fileAttr entity attribute containing a file reference
91  * @param fs file stream to open.
92  *
93  * @return true on succcess, false otherwise
94  */
95  bool getFileStream(const EntityAttributeBasePtr& fileAttr, std::ifstream& fs);
96 
97  /**
98  * Caches the file locally and returns the filename.
99  *
100  * @param filePrx file proxy
101  * @param cacheFileName name of local file
102  * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
103  * or it could be given a special name to simplify modification check (false). Even though the latter option
104  * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
105  * .iv file)
106  *
107  * @return true on succcess, false otherwise
108  */
109  bool ensureFileInCache(GridFileInterfacePrx& filePrx, std::string& cacheFileName,
110  bool preserveOriginalName = false);
111 
112  /**
113  * Caches the file locally and returns the filename.
114  *
115  * @param fileAttr entity attribute containing a file reference
116  * @param cacheFileName name of local file
117  * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
118  * or it could be given a special name to simplify modification check (false). Even though the latter option
119  * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
120  * .iv file)
121  *
122  * @return true on succcess, false otherwise
123  */
124  bool ensureFileInCache(const EntityAttributeBasePtr& fileAttr, std::string& cacheFileName,
125  bool preserveOriginalName = false);
126 
127  /**
128  * Caches multiple files locally.
129  *
130  * @param fileAttr entity attribute containing file references
131  * @param cacheFileNames names of caches files
132  * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
133  * or it could be given a special name to simplify modification check (false). Even though the latter option
134  * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
135  * .iv file)
136  *
137  * @return true on succcess, false otherwise
138  */
139  bool ensureFilesInCache(const EntityAttributeBasePtr& fileAttr, std::vector<std::string>& cacheFileNames, bool preserveOriginalNames = false);
140 
141  /**
142  * Caches multiple files locally. Version without returning the cache file names
143  *
144  * @param fileAttr entity attribute containing file references
145  * @param preserveOriginalName whether file should be saved under its original GridFS name (true) or
146  * or it could be given a special name to simplify modification check (false). Even though the latter option
147  * is preferred, sometimes original names must be preserved because of links between files (e.g. textures in
148  * .iv file)
149  *
150  * @return true on succcess, false otherwise
151  */
152  bool ensureFilesInCache(const EntityAttributeBasePtr& fileAttr, bool preserveOriginalNames = false);
153 
154  /**
155  * Stores a file in GridFS and puts a reference to it into entity attribute. Overwrites any
156  * previously stored attribute.
157  *
158  * @param filesDBName Mongo database name to store file into
159  * @param localFileName name of a file to store
160  * @param fileAttr entity attribute to store file reference into
161  * @param gridFSName name of the file in GridFS. Default: localFileName WITHOUT path
162  *
163  * @return MongoDB id of the file object as returned by GridFS
164  */
165  std::string storeFileToAttr(const std::string& filesDBName, const std::string& localFileName,
166  EntityAttributeBasePtr& fileAttr, const std::string& gridFSName = "");
167 
168  /**
169  * Stores a file in GridFS and puts a reference to it into entity attribute. Allows
170  * to add multiple files to an attribdatabasePrxute.
171  *
172  * @param filesDBName Mongo database name to store file into
173  * @param localFileName name of a file to store (gets turned into an absolute path)
174  * @param fileAttr entity attribute to store file reference into
175  * @param gridFSName name of the file in GridFS. Default: localFileName WITHOUT path
176  *
177  * @return MongoDB id of the file object as returned by GridFS
178  */
179  std::string addFileToAttr(const std::string& filesDBName, const std::string& localFileName,
180  EntityAttributeBasePtr& fileAttr, const std::string& gridFSName = "");
181 
182  /**
183  * Stores a complete directory tree in GridFS and puts a reference to it into entity attribute
184  * Overwrites any previously stored attribute.
185  *
186  * @param filesDBName Mongo database name to store file into
187  * @param localDirectoryName name of a directory to store
188  * @param fileAttr entity attribute to store file reference into
189  *
190  * @return true on succcess, false otherwise
191  */
192  bool storeDirectoryToAttr(const std::string& filesDBName, const std::string& localDirectoryName,
193  EntityAttributeBasePtr& fileAttr, std::string excludeFilter = ".svn");
194 
195  /**
196  * Stores a set of files with in GridFS and puts a reference to it into entity attribute.
197  * The directory structure is preserved while localBaseDirectoryName and all preceding directories are removed from the filenames.
198  * Overwrites any previously stored attribute.
199  *
200  * @param filesDBName Mongo database name to store file into
201  * @param localBaseDirectoryName the base directory. All file names will be made realtive to this directory
202  * @param localFiles The set of files. Absolute filenames.
203  * @param fileAttr entity attribute to store file reference into
204  *
205  * @return true on succcess, false otherwise
206  */
207  bool storeFilesToAttr(const std::string& filesDBName,
208  const std::string& localBaseDirectoryName,
209  const std::vector<std::string>& localFiles,
210  EntityAttributeBasePtr& fileAttr);
211 
212  bool removeAttrFile(const EntityAttributeBasePtr& fileAttr, unsigned int fileIndex);
213  /**
214  * Removes all GridFS files referenced by entity attribute
215  *
216  * @param fileAttr entity attribute containing file reference(s)
217  *
218  * @return true on succcess, false otherwise
219  */
220  bool removeAttrFiles(const EntityAttributeBasePtr& fileAttr);
221 
222 
223  /**
224  * Removes GridFS directory referenced by entity attribute
225  *
226  * @param fileAttr entity attribute containing file reference(s)
227  *
228  * @return true on succcess, false otherwise
229  */
230  bool removeAttrDirectory(const EntityAttributeBasePtr& fileAttr);
231 
232  CommonStorageInterfacePrx getDBPrx() const
233  {
234  return databasePrx;
235  }
236 
237  protected:
238  void init(std::string cachePath);
239  private:
240  std::filesystem::path fileCachePath;
241  CommonStorageInterfacePrx databasePrx;
242 
243  GridFileInterfacePrx getFileProxyFromAttr(const AttributeBasePtr& attr) const;
244  GridFileList getFileProxiesFromAttr(const AttributeBasePtr& attr) const;
245 
246  MongoDBRefPtr extractMongoDBRef(const AttributeBasePtr& attr) const;
247  MongoDBRefPtr extractMongoDBRef(const armarx::VariantBasePtr& value) const;
248 
249  std::string makeRelativePath(const std::filesystem::path& directory, const std::filesystem::path& basePath);
250  };
251 
252  using GridFileManagerPtr = std::shared_ptr<GridFileManager>;
253 
254 }
255 
memoryx::GridFileManager::GridFileManager
GridFileManager(const CommonStorageInterfacePrx &databasePrx)
Constructs new GridFileManager.
Definition: GridFileManager.cpp:40
memoryx::GridFileManager::getFileStream
bool getFileStream(GridFileInterfacePrx &filePrx, std::ifstream &fs)
Caches the file locally and opens a filestream for it.
Definition: GridFileManager.cpp:225
MongoDBRef.h
memoryx::GridFileManager::storeFilesToAttr
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.
Definition: GridFileManager.cpp:326
memoryx::GridFileManager::getDBPrx
CommonStorageInterfacePrx getDBPrx() const
Definition: GridFileManager.h:232
memoryx::GridFileManager::removeAttrFiles
bool removeAttrFiles(const EntityAttributeBasePtr &fileAttr)
Removes all GridFS files referenced by entity attribute.
Definition: GridFileManager.cpp:364
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::GridFileManager::ensureFileInCache
bool ensureFileInCache(GridFileInterfacePrx &filePrx, std::string &cacheFileName, bool preserveOriginalName=false)
Caches the file locally and returns the filename.
Definition: GridFileManager.cpp:99
memoryx::GridFileManager::removeAttrFile
bool removeAttrFile(const EntityAttributeBasePtr &fileAttr, unsigned int fileIndex)
Definition: GridFileManager.cpp:350
memoryx::GridFileManager::removeAttrDirectory
bool removeAttrDirectory(const EntityAttributeBasePtr &fileAttr)
Removes GridFS directory referenced by entity attribute.
IceInternal::Handle
Definition: forward_declarations.h:8
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
memoryx::GridFileManager::ensureFilesInCache
bool ensureFilesInCache(const EntityAttributeBasePtr &fileAttr, std::vector< std::string > &cacheFileNames, bool preserveOriginalNames=false)
Caches multiple files locally.
Definition: GridFileManager.cpp:191
memoryx::GridFileManager::storeFileToAttr
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.
Definition: GridFileManager.cpp:249
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:32
memoryx::GridFileManager::getFileCachePath
std::string getFileCachePath() const
Retrieves a local path where files will be cached.
Definition: GridFileManager.cpp:94
memoryx::GridFileManager::init
void init(std::string cachePath)
Definition: GridFileManager.cpp:67
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
memoryx::GridFileManager::~GridFileManager
~GridFileManager() override
Definition: GridFileManager.cpp:63
Logging.h
memoryx::GridFileManager::storeDirectoryToAttr
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...
Definition: GridFileManager.cpp:290
memoryx::GridFileManager::addFileToAttr
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.
Definition: GridFileManager.cpp:256