ifw-ccf 4.0.0
Loading...
Searching...
No Matches
db.hpp
Go to the documentation of this file.
1
5#ifndef CCFCOMMON_DB_HPP_
6#define CCFCOMMON_DB_HPP_
7
8#include <string>
9
10#include <mal/Cii.hpp>
11#include <mal/utility/LoadMal.hpp>
12
13#include <rad/topicPub.hpp>
14#include <rad/mal/publisher.hpp>
15#include <rad/dbAdapterRedis.hpp>
16#include <rad/dbAdapter.hpp>
17#include <rad/smAdapter.hpp>
18
19#include <Stdif.hpp>
20
21#include <utils/bat/dbInterface.hpp>
22
23#include <ccf/common/base.hpp>
24#include <ccf/common/config.hpp>
25#include <ccf/common/setup.hpp>
26
27namespace ccf::common {
28
30 ccf::DataType ParToCcfDataType(const std::string& par);
31
33 class InternalDb {
34 public:
35
36 InternalDb(const std::string& prefix);
37
39
40 inline bool IsConnected() { return m_connected; }
41
42 inline std::string GetPrefix() { return m_prefix; }
43
44 template<typename TYPE>
45 void Get(const std::string& db_key, TYPE& value) {
46 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
47 auto complete_key = core::utils::string::Lower(m_prefix + db_key);
48 if (m_db.find(complete_key) == m_db.end()) {
49 CCFTHROW(fmt::format("Key given not defined in the OLDB: {}", db_key));
50 }
51 core::utils::conversion::Convert(m_db[complete_key], value);
52 }
53
54 template<typename TYPE>
55 void Set(const std::string& db_key,
56 const TYPE& value) {
57 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
58 LOG4CPLUS_DEBUG(Logger(), fmt::format("Writing key: {}={} in internal DB",
59 m_prefix + db_key, value));
60 auto complete_key = core::utils::string::Lower(m_prefix + db_key);
61 m_db[complete_key] = boost::lexical_cast<std::string>(value);
62 }
63
64 std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
65
66 InternalDb(const InternalDb&) = delete;
67 InternalDb& operator=(const InternalDb&) = delete;
68
69 protected:
70 private:
71 std::map<std::string, std::string> m_db;
72 std::string m_prefix;
73 bool m_connected;
74 };
75
76
78 class Db {
79 public:
80
82 static Db* s_instance;
83
85 static Db& Instance();
86
88 static void Simulate();
89
90 static bool IsSimulated();
91
93 Db();
94
95 virtual ~Db();
96
98 std::string GenDbPath(const std::vector<std::string>& db_key_els);
99
101 std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
102
104 bool IsConnected();
105
107 std::string GetSmState();
108
110 std::string GetSmStatusState();
111
113 std::string GetSmStatusSubstate();
114
115 // TODO: Check if possible to use the one from util::bat::DbInterface.
117 void UpdateSmStatus(std::list<scxml4cpp::State*>& status);
118
120 void UpdateSmStatus(const std::string& status);
121
123 void PublishRecStatus(const recif::RecStatus& rec_status);
124
126 template <class TYPE>
127 TYPE Get(const std::string& db_key) {
128 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
129 TYPE tmp_value;
130 if (db_key.find(m_prefix) != std::string::npos) {
131 CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", db_key));
132 }
133 BEGIN_CRIT_SEC("ccf::common::Db") {
134 if (!s_simulation) {
135 m_cii_db.get()->Get(db_key, tmp_value);
136 } else {
137 m_internal_db.get()->Get(db_key, tmp_value);
138 }
139 } END_CRIT_SEC();
140 return tmp_value;
141 }
142
144 template <class TYPE>
145 TYPE Get2(const std::vector<std::string>& db_key_els) {
146 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
147 return Get<TYPE>(GenDbPath(db_key_els));
148 }
149
151 std::string GetAsStr(const std::string& db_key);
152
154 template <class TYPE>
155 void Set(const std::string& db_key, const TYPE value) {
156 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
157 if (db_key.find(m_prefix) != std::string::npos) {
158 CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", db_key));
159 }
160 BEGIN_CRIT_SEC("ccf::common::Db") {
161 if (!s_simulation) {
162 m_cii_db.get()->Set(db_key, value);
163 } else {
164 m_internal_db.get()->Set(db_key, value);
165 }
166 } END_CRIT_SEC();
167 }
168
170 template <class TYPE>
171 void Set2(const std::vector<std::string>& db_key_els,
172 const TYPE value) {
173 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
174 Set(GenDbPath(db_key_els), value);
175 }
176
178 void SetFromStr(const std::string& db_key,
179 const std::string& par_name,
180 const std::string& value);
181
183 void SetFromStr2(const std::vector<std::string>& db_key_els,
184 const std::string& par_name,
185 const std::string& value);
186
188 void TestConnection();
189
192
194 utils::bat::DbInterface& GetBatDb();
195
196 Db(const Db&) = delete;
197 Db& operator=(const Db&) = delete;
198
199 protected:
200
201 private:
202 static bool s_simulation;
203
204 std::string m_prefix;
205 std::unique_ptr<utils::bat::DbInterface> m_cii_db;
206 std::unique_ptr<InternalDb> m_internal_db;
207
208 bool m_with_pubsub;
209 std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
210 std::unique_ptr<rad::cii::Publisher<recif::RecStatus>> m_rec_status_publisher;
211
212 std::string m_current_status; // Current state + substate.
213 std::string m_sm_state;
214 std::string m_sm_status_state;
215 std::string m_sm_status_substate;
216
217 std::string m_server_id;
218 };
219
220} // namespace ccf::common
221
222
223namespace ccf {
227 }
228}
229
230#endif // CCFCOMMON_DB_HPP_
#define CCFTHROW(msg)
Definition: base.hpp:416
Interface to the OLDB and Pub/Sub.
Definition: db.hpp:78
TYPE Get2(const std::vector< std::string > &db_key_els)
Get value for the given key.
Definition: db.hpp:145
virtual ~Db()
Definition: db.cpp:104
Db & operator=(const Db &)=delete
Disable copy constructor.
utils::bat::DbInterface & GetBatDb()
Get reference to BAT DB handle.
Definition: db.cpp:374
std::string GenDbPath(const std::vector< std::string > &db_key_els)
Generate proper DB name.
Definition: db.cpp:123
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition: db.cpp:159
std::string GetSmStatusSubstate()
Definition: db.cpp:118
std::string GetSmStatusState()
Definition: db.cpp:113
static bool IsSimulated()
Definition: db.cpp:229
void PublishRecStatus(const recif::RecStatus &rec_status)
Update the current Recording Status in Pub/Sub.
Definition: db.cpp:208
std::string GetSmState()
Definition: db.cpp:108
std::string GetAsStr(const std::string &db_key)
Get the attribute as string, in case the type is not know.
Definition: db.cpp:311
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Scan the namespace of the associated DB, applying the key and pattern.
Definition: db.cpp:148
bool IsConnected()
Check if connected to the associated database(s).
Definition: db.cpp:215
Db(const Db &)=delete
void Set2(const std::vector< std::string > &db_key_els, const TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:171
void Set(const std::string &db_key, const TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:155
Db()
Constructor. The prefix indicates location in database of this application.
Definition: db.cpp:57
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition: db.cpp:39
TYPE Get(const std::string &db_key)
Get value for the given key.
Definition: db.hpp:127
void SetFromStr2(const std::vector< std::string > &db_key_els, const std::string &par_name, const std::string &value)
Write the parameter, which values is provided as a string, to the OLDB.
Definition: db.cpp:295
static Db * s_instance
Singleton instance.
Definition: db.hpp:82
void UpdateStagingSetupInDb()
Update the current Staging Setup buffered in the DB.
Definition: db.cpp:302
void TestConnection()
Test if the connection to the OLDB is working properly.
Definition: db.cpp:234
void SetFromStr(const std::string &db_key, const std::string &par_name, const std::string &value)
Write the parameter, which values is provided as a string, to the OLDB.
Definition: db.cpp:250
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition: db.cpp:224
DB implementing the utils::bat::DbInterface; used for internal/test purposes.
Definition: db.hpp:33
void Get(const std::string &db_key, TYPE &value)
Definition: db.hpp:45
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Definition: db.cpp:384
void Set(const std::string &db_key, const TYPE &value)
Definition: db.hpp:55
std::string GetPrefix()
Definition: db.hpp:42
InternalDb(const InternalDb &)=delete
bool IsConnected()
Definition: db.hpp:40
InternalDb & operator=(const InternalDb &)=delete
Disable copy constructor.
~InternalDb()
Definition: db.hpp:38
Definition: appBase.cpp:8
ccf::DataType ParToCcfDataType(const std::string &par)
Attempt to determine the native type of the parameter contained in the parameter object.
Definition: appBase.cpp:8
log4cplus::Logger & Logger()
Definition: base.cpp:11
DataType
Definition: dataType.hpp:46
ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition: db.hpp:225