5 #ifndef CCFCOMMON_DB_HPP_
6 #define CCFCOMMON_DB_HPP_
10 #include <mal/Cii.hpp>
11 #include <mal/utility/LoadMal.hpp>
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>
21 #include <utils/bat/dbInterface.hpp>
40 template<
typename TYPE>
41 void Get(
const std::string& key, TYPE& value) {
42 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
43 auto complete_key = core::utils::string::Lower(m_prefix + key);
44 if (m_db.find(complete_key) == m_db.end()) {
45 CCFTHROW(fmt::format(
"Key given not defined in the OLDB: {}", key));
47 core::utils::conversion::Convert(m_db[complete_key], value);
50 template<
typename TYPE>
51 void Set(
const std::string& key,
53 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
54 LOG4CPLUS_DEBUG(
Logger(), fmt::format(
"Writing key: {}={} in internal DB",
55 m_prefix + key, value));
56 auto complete_key = core::utils::string::Lower(m_prefix + key);
57 m_db[complete_key] = boost::lexical_cast<std::string>(value);
60 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
67 std::map<std::string, std::string> m_db;
92 std::string
GenDbPath(
const std::vector<std::string>& keys);
95 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
118 template <
class TYPE>
119 TYPE
Get(
const std::string& key) {
120 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
122 if (key.find(m_prefix) != std::string::npos) {
123 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}", key));
125 BEGIN_CRIT_SEC(
"ccf::common::Db") {
127 m_cii_db.get()->Get(key, tmp_value);
129 m_internal_db.get()->Get(key, tmp_value);
136 template <
class TYPE>
137 TYPE
Get2(
const std::vector<std::string>& keys) {
138 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
143 std::string
GetAsStr(
const std::string& key);
146 template <
class TYPE>
147 void Set(
const std::string& key,
const TYPE value) {
148 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
149 if (key.find(m_prefix) != std::string::npos) {
150 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}", key));
152 BEGIN_CRIT_SEC(
"ccf::common::Db") {
154 m_cii_db.get()->Set(key, value);
156 m_internal_db.get()->Set(key, value);
162 template <
class TYPE>
163 void Set2(
const std::vector<std::string>& keys,
165 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
178 static bool s_simulation;
180 std::string m_prefix;
181 std::unique_ptr<utils::bat::DbInterface> m_cii_db;
182 std::unique_ptr<InternalDb> m_internal_db;
185 std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
187 std::string m_current_status;
188 std::string m_sm_state;
189 std::string m_sm_status_state;
190 std::string m_sm_status_substate;
192 std::string m_server_id;
#define CCFTHROW(msg)
Guard for TRACE logs. Ensures log text is only generated when the given log level is enabled.
Definition: base.hpp:489
Interface to the OLDB and Pub/Sub.
Definition: db.hpp:74
Db & operator=(const Db &)=delete
Disable copy constructor.
void Set(const std::string &key, const TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:147
virtual ~Db()
Definition: db.cpp:81
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition: db.cpp:136
std::string GetSmStatusSubstate()
Definition: db.cpp:95
std::string GetSmStatusState()
Definition: db.cpp:90
std::string GetSmState()
Definition: db.cpp:85
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:125
bool IsConnected()
Check if connected to the associated database(s).
Definition: db.cpp:185
Db()
Constructor. The prefix indicates location in database of this application.
Definition: db.cpp:39
TYPE Get(const std::string &key)
Get value for the given key.
Definition: db.hpp:119
std::string GenDbPath(const std::vector< std::string > &keys)
Generate proper DB name.
Definition: db.cpp:100
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition: db.cpp:21
void Set2(const std::vector< std::string > &keys, const TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:163
std::string GetAsStr(const std::string &key)
Get the attribute as string, in case the type is not know.
Definition: db.cpp:216
static Db * s_instance
Singleton instance.
Definition: db.hpp:78
TYPE Get2(const std::vector< std::string > &keys)
Get value for the given key.
Definition: db.hpp:137
void TestConnection()
Test if the connection to the OLDB is working properly.
Definition: db.cpp:199
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition: db.cpp:194
DB implementing the utils::bat::DbInterface; used for internal/test purposes.
Definition: db.hpp:29
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Definition: db.cpp:284
void Get(const std::string &key, TYPE &value)
Definition: db.hpp:41
InternalDb(const std::string &prefix)
Definition: db.cpp:279
std::string GetPrefix()
Definition: db.hpp:38
InternalDb(const InternalDb &)=delete
InternalDb & operator=(const InternalDb &)=delete
Disable copy constructor.
bool IsConnected()
Definition: db.hpp:36
void Set(const std::string &key, const TYPE &value)
Definition: db.hpp:51
~InternalDb()
Definition: db.hpp:34
Definition: appBase.cpp:8
Definition: appBase.cpp:8
log4cplus::Logger & Logger()
Definition: base.cpp:9
ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition: db.hpp:199