33#include <MemoryX/interface/components/CommonStorageInterface.h>
40 class DBClientConnection;
48 class GridFileWrapper;
58 "MongoHost",
"localhost",
"MongoDB hostname and optionally port number")
59 .setCaseInsensitive(
true);
62 "MongoAuth",
false,
"Whether authentication should be used for MongoDB");
76 virtual public CommonStorageInterface,
94 NameList
getDBNames(const ::Ice::Current& = Ice::emptyCurrent)
override;
96 const ::Ice::Current& = Ice::emptyCurrent)
override;
97 bool isConnected(const ::Ice::Current&
c = Ice::emptyCurrent)
override;
99 bool reconnect(const ::std::string& hostAndPort,
100 const ::std::string& userName,
101 const ::std::string& password,
102 const ::Ice::Current& = Ice::emptyCurrent)
override;
103 bool authDB(const ::std::string& dbName,
104 const ::std::string& userName,
105 const ::std::string& password,
106 const ::Ice::Current& = Ice::emptyCurrent)
override;
109 const ::Ice::Current& = Ice::emptyCurrent)
override;
111 const ::Ice::Current& = Ice::emptyCurrent)
override;
113 CollectionInterfacePrx
115 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
117 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
119 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
121 std::string
storeFile(const ::std::string& dbName,
122 const ::std::string& fileName,
123 const ::std::string& gridFSName =
"",
124 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
126 const ::std::string& bufferToStore,
127 const ::std::string& gridFSName =
"",
128 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
130 const memoryx::Blob& bufferToStore,
131 const ::std::string& gridFSName =
"",
132 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
135 const ::std::string& fileId,
136 ::std::string& buffer,
137 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
139 const ::std::string& fileId,
140 memoryx::Blob& buffer,
141 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
143 const ::std::string& gridFSName,
144 ::std::string& buffer,
145 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
147 const ::std::string& gridFSName,
148 memoryx::Blob& buffer,
149 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
152 const ::std::string& fileId,
153 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
156 const ::std::string& gridFSName,
157 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
159 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
162 const ::std::string& fileId,
163 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
165 const ::std::string& gridFSName,
166 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
169 Ice::Int
count(
const std::string& ns);
171 DBStorableData
findByMongoId(
const std::string& ns,
const std::string&
id);
173 const std::string& fieldName,
174 const ::std::string& fieldValue);
176 const std::string& fieldName,
177 const NameList& fieldValueList);
179 const std::string& fieldName,
180 const ::std::string& fieldValue);
182 findByQuery(
const std::string& ns,
const std::string& query,
const std::string& where =
"");
183 DBStorableData
findOneByQuery(
const std::string& ns,
const std::string& query);
184 DBStorableDataList
findAll(
const std::string& ns);
186 const ::std::string& fieldName);
187 EntityIdList
findAllIds(
const std::string& ns);
190 std::string
insert(
const std::string& ns,
const DBStorableData& obj,
bool upsert =
false);
191 std::vector<std::string>
insertList(
const std::string& ns,
192 const DBStorableDataList& objectList);
193 bool update(
const std::string& ns,
194 const DBStorableData& obj,
195 const std::string& keyField,
196 bool upsert =
false);
198 updateByQuery(
const std::string& ns,
const std::string& query,
const mongo::BSONObj& obj);
202 const std::string& fieldName,
203 const std::string& fieldValue);
204 bool removeByQuery(
const std::string& ns,
const std::string& query);
207 bool ensureIndex(
const std::string& ns,
const std::string& fieldName,
bool unique);
209 void removeFileByQuery(
const std::string& dbName,
const mongo::BSONObj& fileQuery);
212 const Ice::Current&
c = Ice::emptyCurrent)
override;
214 const Ice::Current&
c = Ice::emptyCurrent)
override;
217 bool removeByMongoQuery(
const std::string& ns,
const mongo::Query& query);
219 findByMongoQuery(
const std::string& ns,
const mongo::Query& query,
bool justOne =
false);
220 NameList findFieldByMongoQuery(
const std::string& ns,
221 const mongo::Query& query,
222 const std::string& fieldName);
223 mongo::GridFile getFileByQuery(
const std::string& dbName,
const mongo::BSONObj& query);
224 GridFileInterfacePrx createFileProxy(mongo::GridFile gridFile,
const Ice::Current&
c);
225 std::string getDocumentId(
const mongo::BSONObj& doc);
226 std::string getDocumentField(
const mongo::BSONObj& doc,
const std::string& fieldName);
227 GridFSPtr getGridFS(
const std::string& dbName);
228 bool readTextFile(mongo::GridFile& gridFile, std::string& buffer);
229 bool readBinaryFile(mongo::GridFile& gridFile, memoryx::Blob& buffer);
231 std::string createPasswordDigest(
const std::string& username,
const std::string& password);
232 std::string extractDBNameFromNS(
const std::string& ns);
233 bool authenticateNS(
const std::string& ns);
234 bool authenticateDB(
const std::string& dbName);
235 bool forceAuthenticate(
const std::string& dbName,
236 const std::string& userName,
237 const std::string& password);
245 void checkConnection();
248 mutable std::mutex serverSettingsMutex;
249 std::string hostAndPort;
251 std::string userName;
252 std::string pwdDigest;
253 std::deque<std::shared_ptr<mongo::DBClientConnection>> pool;
255 struct ConnectionWrapper
258 std::shared_ptr<mongo::DBClientConnection> connPtr);
259 ConnectionWrapper(ConnectionWrapper&&) =
default;
260 ~ConnectionWrapper();
262 mongo::DBClientConnection& conn();
265 std::shared_ptr<mongo::DBClientConnection> connPtr;
266 const std::string hostAndPort;
270 ConnectionWrapper getConnection();
272 std::shared_ptr<mongo::DBClientConnection> conn;
273 std::set<std::string> authDBs;
275 std::map<Ice::Identity, DatabaseInterfacePtr> openedDatabases;
276 mutable std::mutex openedDatabasesMutex;
277 std::map<Ice::Identity, CollectionInterfacePtr> openedCollections;
278 mutable std::mutex openedCollectionsMutex;
279 std::map<Ice::Identity, GridFileWrapperPtr> openedFiles;
280 mutable std::mutex openedFilesMutex;
281 std::map<std::string, GridFSPtr> openedGridFS;
282 mutable std::mutex openedGridFSMutex;
284 mutable std::shared_ptr<std::mutex> accessGridFSFilesMutex;
292 bool keepOldFileIfEqual(
const mongo::GridFile& oldFile,
293 const mongo::GridFile newFile,
294 const mongo::BSONObj& newFileDoc,
295 const std::string dbName,
#define ARMARXCOMPONENT_IMPORT_EXPORT
Default component property definition container.
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Baseclass for all ArmarX ManagedIceObjects requiring properties.
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
PropertyDefinition< PropertyType > & defineRequiredProperty(const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
CommonStoragePropertyDefinitions(std::string prefix)
The CommonStorage class provides an interface to MongoDB.
void onInitComponent() override
Pure virtual hook for the subclass.
bool isConnected(const ::Ice::Current &c=Ice::emptyCurrent) override
bool removeByMongoId(const std::string &ns, const std::string &id)
bool getBinaryFileById(const ::std::string &dbName, const ::std::string &fileId, memoryx::Blob &buffer, const ::Ice::Current &c=Ice::emptyCurrent) override
bool removeFileById(const ::std::string &dbName, const ::std::string &fileId, const ::Ice::Current &c=Ice::emptyCurrent) override
bool removeByQuery(const std::string &ns, const std::string &query)
void releaseCollection(const CollectionInterfacePrx &coll, const ::Ice::Current &c=Ice::emptyCurrent) override
GridFileInterfacePrx getFileProxyById(const ::std::string &dbName, const ::std::string &fileId, const ::Ice::Current &c=Ice::emptyCurrent) override
Ice::Int count(const std::string &ns)
bool clearCollection(const std::string &ns)
NameList getDBNames(const ::Ice::Current &=Ice::emptyCurrent) override
std::string storeBinaryFile(const ::std::string &dbName, const memoryx::Blob &bufferToStore, const ::std::string &gridFSName="", const ::Ice::Current &c=Ice::emptyCurrent) override
bool getTextFileById(const ::std::string &dbName, const ::std::string &fileId, ::std::string &buffer, const ::Ice::Current &c=Ice::emptyCurrent) override
DBStorableData findByMongoId(const std::string &ns, const std::string &id)
GridFileInterfacePrx getFileProxyByName(const ::std::string &dbName, const ::std::string &gridFSName, const ::Ice::Current &c=Ice::emptyCurrent) override
NameList getFileIdList(const std::string &dbName, const Ice::Current &c=Ice::emptyCurrent) override
bool removeFileByName(const ::std::string &dbName, const ::std::string &gridFSName, const ::Ice::Current &c=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
std::string insert(const std::string &ns, const DBStorableData &obj, bool upsert=false)
DBStorableDataList findByQuery(const std::string &ns, const std::string &query, const std::string &where="")
void releaseFileProxy(const GridFileInterfacePrx &fileProxy, const ::Ice::Current &c=Ice::emptyCurrent) override
bool removeByFieldValue(const std::string &ns, const std::string &fieldName, const std::string &fieldValue)
DBStorableDataList findAll(const std::string &ns)
NameList findAllFieldValues(const std::string &ns, const std::string &fieldName)
bool update(const std::string &ns, const DBStorableData &obj, const std::string &keyField, bool upsert=false)
std::string storeTextFile(const ::std::string &dbName, const ::std::string &bufferToStore, const ::std::string &gridFSName="", const ::Ice::Current &c=Ice::emptyCurrent) override
std::vector< std::string > insertList(const std::string &ns, const DBStorableDataList &objectList)
DBStorableData findOneByQuery(const std::string &ns, const std::string &query)
DBStorableData findOneByFieldValue(const std::string &ns, const std::string &fieldName, const ::std::string &fieldValue)
DBStorableDataList findByFieldValue(const std::string &ns, const std::string &fieldName, const ::std::string &fieldValue)
void onConnectComponent() override
Pure virtual hook for the subclass.
CollectionInterfacePrx requestCollection(const std::string &collectionNS, const ::Ice::Current &c=Ice::emptyCurrent) override
DBStorableData findAllUniqueByFieldName(const std::string &ns, const ::std::string &fieldName)
bool ensureIndex(const std::string &ns, const std::string &fieldName, bool unique)
DatabaseInterfacePrx requestDatabase(const ::std::string &dbName, const ::Ice::Current &=Ice::emptyCurrent) override
static std::string GetDefaultName()
NameList getCollectionNames(const ::std::string &dbName, const ::Ice::Current &=Ice::emptyCurrent) override
bool updateByQuery(const std::string &ns, const std::string &query, const mongo::BSONObj &obj)
EntityIdList findAllIds(const std::string &ns)
bool reconnect(const ::std::string &hostAndPort, const ::std::string &userName, const ::std::string &password, const ::Ice::Current &=Ice::emptyCurrent) override
void releaseDatabase(const DatabaseInterfacePrx &db, const ::Ice::Current &=Ice::emptyCurrent) override
bool authDB(const ::std::string &dbName, const ::std::string &userName, const ::std::string &password, const ::Ice::Current &=Ice::emptyCurrent) override
std::string storeFile(const ::std::string &dbName, const ::std::string &fileName, const ::std::string &gridFSName="", const ::Ice::Current &c=Ice::emptyCurrent) override
void onExitComponent() override
Hook for subclass.
void dropCollection(const std::string &collectionNS, const ::Ice::Current &c=Ice::emptyCurrent) override
NameList getFileNameList(const std::string &dbName, const Ice::Current &c=Ice::emptyCurrent) override
DBStorableDataList findByFieldValueList(const std::string &ns, const std::string &fieldName, const NameList &fieldValueList)
std::string getMongoHostAndPort(const ::Ice::Current &c=Ice::emptyCurrent) override
std::string getDefaultName() const override
Retrieve default name of component.
bool getBinaryFileByName(const ::std::string &dbName, const ::std::string &gridFSName, memoryx::Blob &buffer, const ::Ice::Current &c=Ice::emptyCurrent) override
void removeFileByQuery(const std::string &dbName, const mongo::BSONObj &fileQuery)
bool getTextFileByName(const ::std::string &dbName, const ::std::string &gridFSName, ::std::string &buffer, const ::Ice::Current &c=Ice::emptyCurrent) override
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< GridFileWrapper > GridFileWrapperPtr
std::shared_ptr< mongo::GridFS > GridFSPtr
IceUtil::Handle< CommonStorage > CommonStoragePtr