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:
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:
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
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
void initializeFromArmarXConfig()
Fills missing fields from armarx config file.
void configureMixin(const nlohmann::json &json)
configuration
mongocxx::collection ensureCollectionExists(bool createIfNotExistent=false)
nlohmann::json ensureDocumentExists(bool createIfNotExistent=false)
mongocxx::database ensureDatabaseExists(bool createIfNotExistent=false)
std::optional< mongocxx::collection > collectionExists() const
mongocxx::collection ensurePreviousCollectionExists(bool createIfNotExistent=false)
std::optional< mongocxx::collection > previousCollectionExists() const