ifw-ccf 6.0.0
 
Loading...
Searching...
No Matches
db.hpp
Go to the documentation of this file.
1
6
7#ifndef CCFCOMMON_DB_HPP_
8#define CCFCOMMON_DB_HPP_
9
10#include <string>
11
12#include <mal/Cii.hpp>
13#include <mal/utility/LoadMal.hpp>
14
15#include <rad/topicPub.hpp>
16#include <rad/mal/publisher.hpp>
17#include <rad/dbAdapterRedis.hpp>
18#include <rad/dbAdapter.hpp>
19#include <rad/smAdapter.hpp>
20
21#include <Stdif.hpp>
22
23#include <ifw/core/utils/bat/dbInterface.hpp>
24
28
29
30namespace ifw::ccf::common {
31
33 ifw::fnd::datatype::DataType ParToIfwDataType(const std::string& par);
34
36 class InternalDb {
37 public:
38
39 InternalDb(const std::string& prefix);
40
42
43 inline bool IsConnected() { return m_connected; }
44
45 inline std::string GetPrefix() { return m_prefix; }
46
47 template<typename TYPE>
48 void Get(const std::string& db_key, TYPE& value) {
49 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
50 auto complete_key = ifw::core::utils::string::Lower(m_prefix + db_key);
51 if (m_db.find(complete_key) == m_db.end()) {
52 CCFTHROW(fmt::format("Key given not defined in the OLDB: {}", db_key));
53 }
54 ifw::core::utils::conversion::Convert(m_db[complete_key], value);
55 }
56
57 template<typename TYPE>
58 void Set(const std::string& db_key,
59 const TYPE& value) {
60 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
61 LOG4CPLUS_DEBUG(Logger(), fmt::format("Writing key: {}={} in internal DB",
62 m_prefix + db_key, value));
63 auto complete_key = ifw::core::utils::string::Lower(m_prefix + db_key);
64 m_db[complete_key] = boost::lexical_cast<std::string>(value);
65 }
66
67 std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
68
69 InternalDb(const InternalDb&) = delete;
70 InternalDb& operator=(const InternalDb&) = delete;
71
72 protected:
73 private:
74 std::map<std::string, std::string> m_db;
75 std::string m_prefix;
76 bool m_connected;
77 };
78
79
81 class Db {
82 public:
83
85 static Db* s_instance;
86
88 static Db& Instance();
89
91 static void Simulate();
92
93 static bool IsSimulated();
94
96 Db();
97
98 virtual ~Db();
99
101 std::string GenDbPath(const std::vector<std::string>& db_key_els);
102
104 std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
105
107 bool IsConnected();
108
110 std::string GetSmState();
111
113 std::string GetSmStatusState();
114
116 std::string GetSmStatusSubstate();
117
118 // TODO: Check if possible to use the one from util::bat::DbInterface.
120 void UpdateSmStatus(std::list<scxml4cpp::State*>& status);
121
123 void UpdateSmStatus(const std::string& status);
124
126 void PublishRecStatus(const recif::RecStatus& rec_status);
127
129 template <class TYPE>
130 TYPE Get(const std::string& db_key) {
131 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
132 TYPE tmp_value;
133 if (db_key.find(m_prefix) != std::string::npos) {
134 CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", db_key));
135 }
136 BEGIN_CRIT_SEC("ifw::ccf::common::Db") {
137 if (!s_simulation) {
138 m_cii_db.get()->Get(db_key, tmp_value);
139 } else {
140 m_internal_db.get()->Get(db_key, tmp_value);
141 }
142 } END_CRIT_SEC();
143 return tmp_value;
144 }
145
147 template <class TYPE>
148 TYPE Get2(const std::vector<std::string>& db_key_els) {
149 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
150 return Get<TYPE>(GenDbPath(db_key_els));
151 }
152
154 std::string GetAsStr(const std::string& db_key);
155
157 template <class TYPE>
158 void Set(const std::string& db_key,
159 const TYPE value,
160 const bool force_update = false) {
161 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
162 if (db_key.find(m_prefix) != std::string::npos) {
163 CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", db_key));
164 }
165
166 auto it = m_last_update_map.find(db_key);
167 if (it == m_last_update_map.end()) {
168 m_last_update_map[db_key] = 0;
169 }
170 it = m_last_update_map.find(db_key);
171
172 double time_now = ifw::core::utils::time::Time();
173 if (!force_update && (time_now - it->second) < m_max_update_period) {
174 return;
175 }
176
177 BEGIN_CRIT_SEC("ifw::ccf::common::Db") {
178 if (!s_simulation) {
179 m_cii_db.get()->Set(db_key, value);
180 } else {
181 m_internal_db.get()->Set(db_key, value);
182 }
183 } END_CRIT_SEC();
184
185 m_last_update_map[db_key] = time_now;
186 }
187
189 template <class TYPE>
190 void Set2(const std::vector<std::string>& db_key_els,
191 const TYPE value,
192 const bool force_update = false) {
193 LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
194 Set(GenDbPath(db_key_els), value, force_update);
195 }
196
198 void SetFromStr(const std::string& db_key,
199 const std::string& par_name,
200 const std::string& value,
201 const bool force_update = false);
202
204 void SetFromStr2(const std::vector<std::string>& db_key_els,
205 const std::string& par_name,
206 const std::string& value,
207 const bool force_update = false);
208
210 void SetFromStr(const std::string& db_key,
211 const std::string& value,
212 const ifw::fnd::datatype::DataType data_type,
213 const bool force_update = false);
214
216 void SetFromStr2(const std::vector<std::string>& db_key_els,
217 const std::string& value,
218 const ifw::fnd::datatype::DataType data_type,
219 const bool force_update = false);
220
222 void TestConnection();
223
226
228 ifw::core::utils::bat::DbInterface& GetBatDb();
229
230 Db(const Db&) = delete;
231 Db& operator=(const Db&) = delete;
232
233 protected:
234
235 private:
236 static bool s_simulation;
237
238 std::string m_prefix;
239 std::unique_ptr<ifw::core::utils::bat::DbInterface> m_cii_db;
240 std::unique_ptr<InternalDb> m_internal_db;
241
242 double m_max_update_period{0.5};
243 std::map<std::string, double> m_last_update_map;
244
245 bool m_with_pubsub;
246 std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
247 std::unique_ptr<rad::cii::Publisher<recif::RecStatus>> m_rec_status_publisher;
248
249 std::string m_current_status; // Current state + substate.
250 std::string m_sm_state;
251 std::string m_sm_status_state;
252 std::string m_sm_status_substate;
253
254 std::string m_server_id;
255 };
256
257} // namespace ifw::ccf::common
258
259
260namespace ifw::ccf {
265}
266
267#endif // CCFCOMMON_DB_HPP_
#define CCFTHROW(msg)
Definition base.hpp:399
Interface to the OLDB and Pub/Sub.
Definition db.hpp:81
Db(const Db &)=delete
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition db.cpp:230
void PublishRecStatus(const recif::RecStatus &rec_status)
Update the current Recording Status in Pub/Sub.
Definition db.cpp:214
std::string GetAsStr(const std::string &db_key)
Get the attribute as string, in case the type is not know.
Definition db.cpp:383
void Set2(const std::vector< std::string > &db_key_els, const TYPE value, const bool force_update=false)
Set the referenced key to the given value.
Definition db.hpp:190
virtual ~Db()
Definition db.cpp:109
void SetFromStr2(const std::vector< std::string > &db_key_els, const std::string &par_name, const std::string &value, const bool force_update=false)
Write the parameter, which values is provided as a string, to the OLDB.
Definition db.cpp:364
Db & operator=(const Db &)=delete
Disable copy constructor.
static Db * s_instance
Singleton instance.
Definition db.hpp:85
void TestConnection()
Test if the connection to the OLDB is working properly.
Definition db.cpp:240
void SetFromStr(const std::string &db_key, const std::string &par_name, const std::string &value, const bool force_update=false)
Write the parameter, which values is provided as a string, to the OLDB.
Definition db.cpp:318
static bool IsSimulated()
Definition db.cpp:235
void UpdateStagingSetupInDb()
Update the current Staging Setup buffered in the DB.
Definition db.cpp:372
std::string GenDbPath(const std::vector< std::string > &db_key_els)
Generate proper DB name.
Definition db.cpp:128
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition db.cpp:164
std::string GetSmStatusSubstate()
Definition db.cpp:123
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:153
std::string GetSmState()
Definition db.cpp:113
ifw::core::utils::bat::DbInterface & GetBatDb()
Get reference to BAT DB handle.
Definition db.cpp:447
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition db.cpp:41
bool IsConnected()
Check if connected to the associated database(s).
Definition db.cpp:221
TYPE Get2(const std::vector< std::string > &db_key_els)
Get value for the given key.
Definition db.hpp:148
TYPE Get(const std::string &db_key)
Get value for the given key.
Definition db.hpp:130
Db()
Constructor. The prefix indicates location in database of this application.
Definition db.cpp:60
void Set(const std::string &db_key, const TYPE value, const bool force_update=false)
Set the referenced key to the given value.
Definition db.hpp:158
std::string GetSmStatusState()
Definition db.cpp:118
InternalDb & operator=(const InternalDb &)=delete
Disable copy constructor.
InternalDb(const InternalDb &)=delete
void Set(const std::string &db_key, const TYPE &value)
Definition db.hpp:58
bool IsConnected()
Definition db.hpp:43
~InternalDb()
Definition db.hpp:41
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Definition db.cpp:457
void Get(const std::string &db_key, TYPE &value)
Definition db.hpp:48
std::string GetPrefix()
Definition db.hpp:45
InternalDb(const std::string &prefix)
Definition db.cpp:452
Definition appBase.cpp:10
ifw::fnd::datatype::DataType ParToIfwDataType(const std::string &par)
Attempt to determine the native type of the parameter contained in the parameter object.
Definition appBase.cpp:10
log4cplus::Logger & Logger()
Definition base.cpp:24
ifw::ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition db.hpp:262