32 #include <MemoryX/interface/components/CommonStorageInterface.h>
39 class DBClientConnection;
47 class GridFileWrapper;
58 defineOptionalProperty<std::string>(
"MongoHost",
"localhost",
"MongoDB hostname and optionally port number")
59 .setCaseInsensitive(
true);
61 defineOptionalProperty<bool>(
"MongoAuth",
false,
"Whether authentication should be used for MongoDB");
63 defineRequiredProperty<std::string>(
"MongoUser",
"MongoDB user name");
65 defineRequiredProperty<std::string>(
"MongoPassword",
"MongoDB password");
75 virtual public CommonStorageInterface,
80 std::string getDefaultName()
const override;
81 void onInitComponent()
override;
82 void onConnectComponent()
override;
83 void onExitComponent()
override;
91 std::string getMongoHostAndPort(const ::Ice::Current&
c = Ice::emptyCurrent)
override;
92 NameList getDBNames(const ::Ice::Current& = Ice::emptyCurrent)
override;
93 NameList getCollectionNames(const ::std::string& dbName, const ::Ice::Current& = Ice::emptyCurrent)
override;
94 bool isConnected(const ::Ice::Current&
c = Ice::emptyCurrent)
override;
96 bool reconnect(const ::std::string& hostAndPort, const ::std::string& userName, const ::std::string& password, const ::Ice::Current& = Ice::emptyCurrent)
override;
97 bool authDB(const ::std::string& dbName, const ::std::string& userName, const ::std::string& password, const ::Ice::Current& = Ice::emptyCurrent)
override;
99 DatabaseInterfacePrx requestDatabase(const ::std::string& dbName, const ::Ice::Current& = Ice::emptyCurrent)
override;
100 void releaseDatabase(
const DatabaseInterfacePrx& db, const ::Ice::Current& = Ice::emptyCurrent)
override;
102 CollectionInterfacePrx requestCollection(
const std::string& collectionNS, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
103 void releaseCollection(
const CollectionInterfacePrx& coll, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
104 void dropCollection(
const std::string& collectionNS, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
106 std::string storeFile(const ::std::string& dbName, const ::std::string& fileName, const ::std::string& gridFSName =
"", const ::Ice::Current&
c = Ice::emptyCurrent)
override;
107 std::string storeTextFile(const ::std::string& dbName, const ::std::string& bufferToStore, const ::std::string& gridFSName =
"", const ::Ice::Current&
c = Ice::emptyCurrent)
override;
108 std::string storeBinaryFile(const ::std::string& dbName,
const memoryx::Blob& bufferToStore, const ::std::string& gridFSName =
"", const ::Ice::Current&
c = Ice::emptyCurrent)
override;
110 bool getTextFileById(const ::std::string& dbName, const ::std::string& fileId, ::std::string& buffer, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
111 bool getBinaryFileById(const ::std::string& dbName, const ::std::string& fileId, memoryx::Blob& buffer, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
112 bool getTextFileByName(const ::std::string& dbName, const ::std::string& gridFSName, ::std::string& buffer, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
113 bool getBinaryFileByName(const ::std::string& dbName, const ::std::string& gridFSName, memoryx::Blob& buffer, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
115 GridFileInterfacePrx getFileProxyById(const ::std::string& dbName, const ::std::string& fileId, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
116 GridFileInterfacePrx getFileProxyByName(const ::std::string& dbName, const ::std::string& gridFSName, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
117 void releaseFileProxy(
const GridFileInterfacePrx& fileProxy, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
119 bool removeFileById(const ::std::string& dbName, const ::std::string& fileId, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
120 bool removeFileByName(const ::std::string& dbName, const ::std::string& gridFSName, const ::Ice::Current&
c = Ice::emptyCurrent)
override;
123 Ice::Int count(
const std::string& ns);
125 DBStorableData findByMongoId(
const std::string& ns,
const std::string&
id);
126 DBStorableDataList findByFieldValue(
const std::string& ns,
const std::string& fieldName, const ::std::string& fieldValue);
127 DBStorableDataList findByFieldValueList(
const std::string& ns,
const std::string& fieldName,
const NameList& fieldValueList);
128 DBStorableData findOneByFieldValue(
const std::string& ns,
const std::string& fieldName, const ::std::string& fieldValue);
129 DBStorableDataList findByQuery(
const std::string& ns,
const std::string& query,
const std::string& where =
"");
130 DBStorableData findOneByQuery(
const std::string& ns,
const std::string& query);
131 DBStorableDataList findAll(
const std::string& ns);
132 DBStorableData findAllUniqueByFieldName(
const std::string& ns, const ::std::string& fieldName);
133 EntityIdList findAllIds(
const std::string& ns);
134 NameList findAllFieldValues(
const std::string& ns,
const std::string& fieldName);
136 std::string
insert(
const std::string& ns,
const DBStorableData& obj,
bool upsert =
false);
137 std::vector<std::string> insertList(
const std::string& ns,
const DBStorableDataList& objectList);
138 bool update(
const std::string& ns,
const DBStorableData& obj,
const std::string& keyField,
bool upsert =
false);
139 bool updateByQuery(
const std::string& ns,
const std::string& query,
const mongo::BSONObj& obj);
141 bool removeByMongoId(
const std::string& ns,
const std::string&
id);
142 bool removeByFieldValue(
const std::string& ns,
const std::string& fieldName,
const std::string& fieldValue);
143 bool removeByQuery(
const std::string& ns,
const std::string& query);
144 bool clearCollection(
const std::string& ns);
146 bool ensureIndex(
const std::string& ns,
const std::string& fieldName,
bool unique);
148 void removeFileByQuery(
const std::string& dbName,
const mongo::BSONObj& fileQuery);
150 NameList getFileNameList(
const std::string& dbName,
const Ice::Current&
c = Ice::emptyCurrent)
override;
151 NameList getFileIdList(
const std::string& dbName,
const Ice::Current&
c = Ice::emptyCurrent)
override;
153 bool removeByMongoQuery(
const std::string& ns,
const mongo::Query& query);
154 DBStorableDataList findByMongoQuery(
const std::string& ns,
const mongo::Query& query,
bool justOne =
false);
155 NameList findFieldByMongoQuery(
const std::string& ns,
const mongo::Query& query,
const std::string& fieldName);
156 mongo::GridFile getFileByQuery(
const std::string& dbName,
const mongo::BSONObj& query);
157 GridFileInterfacePrx createFileProxy(mongo::GridFile gridFile,
const Ice::Current&
c);
158 std::string getDocumentId(
const mongo::BSONObj& doc);
159 std::string getDocumentField(
const mongo::BSONObj& doc,
const std::string& fieldName);
160 GridFSPtr getGridFS(
const std::string& dbName);
161 bool readTextFile(mongo::GridFile& gridFile, std::string& buffer);
162 bool readBinaryFile(mongo::GridFile& gridFile, memoryx::Blob& buffer);
164 std::string createPasswordDigest(
const std::string& username,
const std::string& password);
165 std::string extractDBNameFromNS(
const std::string& ns);
166 bool authenticateNS(
const std::string& ns);
167 bool authenticateDB(
const std::string& dbName);
168 bool forceAuthenticate(
const std::string& dbName,
const std::string& userName,
const std::string& password);
176 void checkConnection();
179 mutable std::mutex serverSettingsMutex;
180 std::string hostAndPort;
182 std::string userName;
183 std::string pwdDigest;
184 std::deque<std::shared_ptr<mongo::DBClientConnection>> pool;
185 struct ConnectionWrapper
189 std::shared_ptr<mongo::DBClientConnection> connPtr
191 ConnectionWrapper(ConnectionWrapper&&) =
default;
192 ~ConnectionWrapper();
194 mongo::DBClientConnection& conn();
197 std::shared_ptr<mongo::DBClientConnection> connPtr;
198 const std::string hostAndPort;
201 ConnectionWrapper getConnection();
203 std::shared_ptr<mongo::DBClientConnection> conn;
204 std::set<std::string> authDBs;
206 std::map<Ice::Identity, DatabaseInterfacePtr> openedDatabases;
207 mutable std::mutex openedDatabasesMutex;
208 std::map<Ice::Identity, CollectionInterfacePtr> openedCollections;
209 mutable std::mutex openedCollectionsMutex;
210 std::map<Ice::Identity, GridFileWrapperPtr> openedFiles;
211 mutable std::mutex openedFilesMutex;
212 std::map<std::string, GridFSPtr> openedGridFS;
213 mutable std::mutex openedGridFSMutex;
215 mutable std::shared_ptr<std::mutex> accessGridFSFilesMutex;
223 bool keepOldFileIfEqual(
const mongo::GridFile& oldFile,
const mongo::GridFile newFile,
const mongo::BSONObj& newFileDoc,
const std::string dbName, std::string& oldId);