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,
93 NameList
getDBNames(const ::Ice::Current& = Ice::emptyCurrent)
override;
95 const ::Ice::Current& = Ice::emptyCurrent)
override;
96 bool isConnected(const ::Ice::Current&
c = Ice::emptyCurrent)
override;
98 bool reconnect(const ::std::string& hostAndPort,
99 const ::std::string& userName,
100 const ::std::string& password,
101 const ::Ice::Current& = Ice::emptyCurrent)
override;
102 bool authDB(const ::std::string& dbName,
103 const ::std::string& userName,
104 const ::std::string& password,
105 const ::Ice::Current& = Ice::emptyCurrent)
override;
108 const ::Ice::Current& = Ice::emptyCurrent)
override;
110 const ::Ice::Current& = Ice::emptyCurrent)
override;
112 CollectionInterfacePrx
114 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
116 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
118 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
120 std::string
storeFile(const ::std::string& dbName,
121 const ::std::string& fileName,
122 const ::std::string& gridFSName =
"",
123 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
125 const ::std::string& bufferToStore,
126 const ::std::string& gridFSName =
"",
127 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
129 const memoryx::Blob& bufferToStore,
130 const ::std::string& gridFSName =
"",
131 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
134 const ::std::string& fileId,
135 ::std::string& buffer,
136 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
138 const ::std::string& fileId,
139 memoryx::Blob& buffer,
140 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
142 const ::std::string& gridFSName,
143 ::std::string& buffer,
144 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
146 const ::std::string& gridFSName,
147 memoryx::Blob& buffer,
148 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
151 const ::std::string& fileId,
152 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
155 const ::std::string& gridFSName,
156 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
158 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
161 const ::std::string& fileId,
162 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
164 const ::std::string& gridFSName,
165 const ::Ice::Current&
c = Ice::emptyCurrent)
override;
168 Ice::Int
count(
const std::string& ns);
170 DBStorableData
findByMongoId(
const std::string& ns,
const std::string&
id);
172 const std::string& fieldName,
173 const ::std::string& fieldValue);
175 const std::string& fieldName,
176 const NameList& fieldValueList);
178 const std::string& fieldName,
179 const ::std::string& fieldValue);
181 findByQuery(
const std::string& ns,
const std::string& query,
const std::string& where =
"");
182 DBStorableData
findOneByQuery(
const std::string& ns,
const std::string& query);
183 DBStorableDataList
findAll(
const std::string& ns);
185 const ::std::string& fieldName);
186 EntityIdList
findAllIds(
const std::string& ns);
189 std::string
insert(
const std::string& ns,
const DBStorableData& obj,
bool upsert =
false);
190 std::vector<std::string>
insertList(
const std::string& ns,
191 const DBStorableDataList& objectList);
192 bool update(
const std::string& ns,
193 const DBStorableData& obj,
194 const std::string& keyField,
195 bool upsert =
false);
197 updateByQuery(
const std::string& ns,
const std::string& query,
const mongo::BSONObj& obj);
201 const std::string& fieldName,
202 const std::string& fieldValue);
203 bool removeByQuery(
const std::string& ns,
const std::string& query);
206 bool ensureIndex(
const std::string& ns,
const std::string& fieldName,
bool unique);
208 void removeFileByQuery(
const std::string& dbName,
const mongo::BSONObj& fileQuery);
211 const Ice::Current&
c = Ice::emptyCurrent)
override;
213 const Ice::Current&
c = Ice::emptyCurrent)
override;
216 bool removeByMongoQuery(
const std::string& ns,
const mongo::Query& query);
218 findByMongoQuery(
const std::string& ns,
const mongo::Query& query,
bool justOne =
false);
219 NameList findFieldByMongoQuery(
const std::string& ns,
220 const mongo::Query& query,
221 const std::string& fieldName);
222 mongo::GridFile getFileByQuery(
const std::string& dbName,
const mongo::BSONObj& query);
223 GridFileInterfacePrx createFileProxy(mongo::GridFile gridFile,
const Ice::Current&
c);
224 std::string getDocumentId(
const mongo::BSONObj& doc);
225 std::string getDocumentField(
const mongo::BSONObj& doc,
const std::string& fieldName);
226 GridFSPtr getGridFS(
const std::string& dbName);
227 bool readTextFile(mongo::GridFile& gridFile, std::string& buffer);
228 bool readBinaryFile(mongo::GridFile& gridFile, memoryx::Blob& buffer);
230 std::string createPasswordDigest(
const std::string& username,
const std::string& password);
231 std::string extractDBNameFromNS(
const std::string& ns);
232 bool authenticateNS(
const std::string& ns);
233 bool authenticateDB(
const std::string& dbName);
234 bool forceAuthenticate(
const std::string& dbName,
235 const std::string& userName,
236 const std::string& password);
244 void checkConnection();
247 mutable std::mutex serverSettingsMutex;
248 std::string hostAndPort;
250 std::string userName;
251 std::string pwdDigest;
252 std::deque<std::shared_ptr<mongo::DBClientConnection>> pool;
254 struct ConnectionWrapper
257 std::shared_ptr<mongo::DBClientConnection> connPtr);
258 ConnectionWrapper(ConnectionWrapper&&) =
default;
259 ~ConnectionWrapper();
261 mongo::DBClientConnection& conn();
264 std::shared_ptr<mongo::DBClientConnection> connPtr;
265 const std::string hostAndPort;
269 ConnectionWrapper getConnection();
271 std::shared_ptr<mongo::DBClientConnection> conn;
272 std::set<std::string> authDBs;
274 std::map<Ice::Identity, DatabaseInterfacePtr> openedDatabases;
275 mutable std::mutex openedDatabasesMutex;
276 std::map<Ice::Identity, CollectionInterfacePtr> openedCollections;
277 mutable std::mutex openedCollectionsMutex;
278 std::map<Ice::Identity, GridFileWrapperPtr> openedFiles;
279 mutable std::mutex openedFilesMutex;
280 std::map<std::string, GridFSPtr> openedGridFS;
281 mutable std::mutex openedGridFSMutex;
283 mutable std::shared_ptr<std::mutex> accessGridFSFilesMutex;
291 bool keepOldFileIfEqual(
const mongo::GridFile& oldFile,
292 const mongo::GridFile newFile,
293 const mongo::BSONObj& newFileDoc,
294 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
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