MongoDBStorageMixin.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <fstream>
5 #include <map>
6 #include <memory>
7 #include <mutex>
8 #include <sstream>
9 #include <string>
10 
11 #include "util/mongodb.h"
12 
14 {
16  {
17  public:
18  std::string host = "";
19  unsigned int port = 0;
20  std::string user = "";
21  std::string password = "";
22  int minPoolSize = 5;
23  int maxPoolSize = 100;
24 
25  public:
27  {
29  };
30 
31  /// Fills missing fields from armarx config file
33 
34  bool isSet() const;
35 
36  std::string baseUri() const;
37 
38  std::string key() const;
39 
40  std::string uri() const;
41 
42  std::string toString() const;
43  };
44 
46  {
47  public:
48  MongoDBStorageMixin() = default;
49  MongoDBStorageMixin(const MongoDBSettings& settings,
50  const std::string& exportName,
51  const armem::MemoryID& id);
52 
54 
55  std::string getDocumentName() const;
56  std::string getCollectionName() const;
57  std::string getPreviousCollectionName() const;
58  std::string getDatabaseName() const;
59 
60  std::optional<mongocxx::database> databaseExists() const;
61  std::optional<mongocxx::collection> collectionExists() const;
62  std::optional<mongocxx::collection> previousCollectionExists() const;
63  std::optional<nlohmann::json> documentExists() const;
64  std::optional<nlohmann::json> documentExists(const std::string& id) const;
65 
66  mongocxx::database ensureDatabaseExists(bool createIfNotExistent = false);
67  mongocxx::collection ensureCollectionExists(bool createIfNotExistent = false);
68  mongocxx::collection ensurePreviousCollectionExists(bool createIfNotExistent = false);
69  nlohmann::json ensureDocumentExists(bool createIfNotExistent = false);
70  nlohmann::json ensureDocumentExists(const std::string& id,
71  bool createIfNotExistent = false);
72 
74  void writeForeignKeyToPreviousDocument(const nlohmann::json& type);
75 
76  void writeDataToDocument(const nlohmann::json& data);
77  nlohmann::json readDataFromDocument() const;
78 
79  void writeDataToDocument(const std::string& id, const nlohmann::json& data);
80  nlohmann::json readDataFromDocument(const std::string& id) const;
81 
82  std::vector<nlohmann::json> getAllDocuments() const;
83 
84  protected:
85  void connect() const;
86  bool connected() const;
87 
88  /// setter
89  void setMixinMemoryID(const armem::MemoryID&);
90  void setMixinExportName(const std::string& n);
91  void setHost(const std::string&);
92  void setPort(const unsigned int);
93  void setUser(const std::string&);
94  void setPassword(const std::string&);
95 
96  /// start
97  void start();
98  void stop();
99 
100  /// configuration
101  void configureMixin(const nlohmann::json& json);
102 
103  private:
104  mongocxx::pool* getPool(bool tryToConnect = true) const;
105  mongocxx::pool* ensurePool() const;
106 
107  protected:
108  static const constexpr char* ID = "_id";
109  static const constexpr char* FOREIGN_KEY = "_foreign_key";
110  static const constexpr char* TYPE = "_type";
111  static const constexpr char* DATA = "_data";
112  static const constexpr char* METADATA = "_metadata";
113 
114  private:
115  MongoDBSettings settings;
116  std::string exportName;
117  armem::MemoryID _id;
118 
119  static std::unique_ptr<mongocxx::instance> MongoCXXInstance;
120  static std::atomic_bool MongoCXXInitialized;
121  static std::recursive_mutex MongoCXXConnectionPoolMutex;
122  static std::map<std::string, std::unique_ptr<mongocxx::pool>> MongoCXXConnectionPool;
123  };
124 
125 } // namespace armarx::armem::server::ltm::detail::mixin
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::password
std::string password
Definition: MongoDBStorageMixin.h:21
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::stop
void stop()
Definition: MongoDBStorageMixin.cpp:212
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::uri
std::string uri() const
Definition: MongoDBStorageMixin.cpp:100
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::collectionExists
std::optional< mongocxx::collection > collectionExists() const
Definition: MongoDBStorageMixin.cpp:269
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::start
void start()
start
Definition: MongoDBStorageMixin.cpp:205
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::configureMixin
void configureMixin(const nlohmann::json &json)
configuration
Definition: MongoDBStorageMixin.cpp:456
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::databaseExists
std::optional< mongocxx::database > databaseExists() const
Definition: MongoDBStorageMixin.cpp:262
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::maxPoolSize
int maxPoolSize
Definition: MongoDBStorageMixin.h:23
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getAllDocuments
std::vector< nlohmann::json > getAllDocuments() const
Definition: MongoDBStorageMixin.cpp:425
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::host
std::string host
Definition: MongoDBStorageMixin.h:18
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setPassword
void setPassword(const std::string &)
Definition: MongoDBStorageMixin.cpp:139
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::minPoolSize
int minPoolSize
Definition: MongoDBStorageMixin.h:22
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::previousCollectionExists
std::optional< mongocxx::collection > previousCollectionExists() const
Definition: MongoDBStorageMixin.cpp:281
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::isSet
bool isSet() const
Definition: MongoDBStorageMixin.cpp:67
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setMixinMemoryID
void setMixinMemoryID(const armem::MemoryID &)
setter
Definition: MongoDBStorageMixin.cpp:442
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::ensurePreviousCollectionExists
mongocxx::collection ensurePreviousCollectionExists(bool createIfNotExistent=false)
Definition: MongoDBStorageMixin.cpp:327
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::key
std::string key() const
Definition: MongoDBStorageMixin.cpp:93
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::METADATA
static const constexpr char * METADATA
Definition: MongoDBStorageMixin.h:112
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::writeForeignKeyToPreviousDocument
void writeForeignKeyToPreviousDocument()
Definition: MongoDBStorageMixin.cpp:364
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::connect
void connect() const
Definition: MongoDBStorageMixin.cpp:145
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getSettings
MongoDBSettings getSettings() const
Definition: MongoDBStorageMixin.cpp:436
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::port
unsigned int port
Definition: MongoDBStorageMixin.h:19
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getCollectionName
std::string getCollectionName() const
Definition: MongoDBStorageMixin.cpp:243
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setUser
void setUser(const std::string &)
Definition: MongoDBStorageMixin.cpp:133
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin
Definition: MongoDBStorageMixin.h:45
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setMixinExportName
void setMixinExportName(const std::string &n)
Definition: MongoDBStorageMixin.cpp:450
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::ensureCollectionExists
mongocxx::collection ensureCollectionExists(bool createIfNotExistent=false)
Definition: MongoDBStorageMixin.cpp:320
mongodb.h
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::ensureDocumentExists
nlohmann::json ensureDocumentExists(bool createIfNotExistent=false)
Definition: MongoDBStorageMixin.cpp:335
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::FOREIGN_KEY
static const constexpr char * FOREIGN_KEY
Definition: MongoDBStorageMixin.h:109
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::baseUri
std::string baseUri() const
Definition: MongoDBStorageMixin.cpp:74
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::initializeFromArmarXConfig
void initializeFromArmarXConfig()
Fills missing fields from armarx config file.
Definition: MongoDBStorageMixin.cpp:20
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::TYPE
static const constexpr char * TYPE
Definition: MongoDBStorageMixin.h:110
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setHost
void setHost(const std::string &)
Definition: MongoDBStorageMixin.cpp:121
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::connected
bool connected() const
Definition: MongoDBStorageMixin.cpp:217
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getDatabaseName
std::string getDatabaseName() const
Definition: MongoDBStorageMixin.cpp:256
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::ensureDatabaseExists
mongocxx::database ensureDatabaseExists(bool createIfNotExistent=false)
Definition: MongoDBStorageMixin.cpp:313
armarx::armem::server::ltm::detail::mixin
Definition: BufferedMemoryMixin.cpp:3
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::DATA
static const constexpr char * DATA
Definition: MongoDBStorageMixin.h:111
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::MongoDBStorageMixin
MongoDBStorageMixin()=default
armarx::armem::server::ltm::detail::mixin::MongoDBSettings
Definition: MongoDBStorageMixin.h:15
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::MongoDBSettings
MongoDBSettings()
Definition: MongoDBStorageMixin.h:26
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::setPort
void setPort(const unsigned int)
Definition: MongoDBStorageMixin.cpp:127
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::documentExists
std::optional< nlohmann::json > documentExists() const
Definition: MongoDBStorageMixin.cpp:293
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::readDataFromDocument
nlohmann::json readDataFromDocument() const
Definition: MongoDBStorageMixin.cpp:419
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::toString
std::string toString() const
Definition: MongoDBStorageMixin.cpp:108
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::ID
static const constexpr char * ID
Definition: MongoDBStorageMixin.h:108
armarx::armem::server::ltm::detail::mixin::MongoDBSettings::user
std::string user
Definition: MongoDBStorageMixin.h:20
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::writeDataToDocument
void writeDataToDocument(const nlohmann::json &data)
Definition: MongoDBStorageMixin.cpp:413
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getDocumentName
std::string getDocumentName() const
Definition: MongoDBStorageMixin.cpp:237
armarx::armem::server::ltm::detail::mixin::MongoDBStorageMixin::getPreviousCollectionName
std::string getPreviousCollectionName() const
Definition: MongoDBStorageMixin.cpp:249