5#ifndef CCFCOMMON_DB_HPP_
6#define CCFCOMMON_DB_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>
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));
51 core::utils::conversion::Convert(m_db[complete_key], value);
54 template<
typename TYPE>
55 void Set(
const std::string& db_key,
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);
64 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
71 std::map<std::string, std::string> m_db;
98 std::string
GenDbPath(
const std::vector<std::string>& db_key_els);
101 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
126 template <
class TYPE>
127 TYPE
Get(
const std::string& db_key) {
128 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
130 if (db_key.find(m_prefix) != std::string::npos) {
131 CCFTHROW(fmt::format(
"OLDB keys shall be given without prefix: {}", db_key));
133 BEGIN_CRIT_SEC(
"ccf::common::Db") {
135 m_cii_db.get()->Get(db_key, tmp_value);
137 m_internal_db.get()->Get(db_key, tmp_value);
144 template <
class TYPE>
145 TYPE
Get2(
const std::vector<std::string>& db_key_els) {
146 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
151 std::string
GetAsStr(
const std::string& db_key);
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));
160 BEGIN_CRIT_SEC(
"ccf::common::Db") {
162 m_cii_db.get()->Set(db_key, value);
164 m_internal_db.get()->Set(db_key, value);
170 template <
class TYPE>
171 void Set2(
const std::vector<std::string>& db_key_els,
173 LOG4CPLUS_TRACE_METHOD(
Logger(), __PRETTY_FUNCTION__);
179 const std::string& par_name,
180 const std::string& value);
183 void SetFromStr2(
const std::vector<std::string>& db_key_els,
184 const std::string& par_name,
185 const std::string& value);
194 utils::bat::DbInterface&
GetBatDb();
202 static bool s_simulation;
204 std::string m_prefix;
205 std::unique_ptr<utils::bat::DbInterface> m_cii_db;
206 std::unique_ptr<InternalDb> m_internal_db;
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;
212 std::string m_current_status;
213 std::string m_sm_state;
214 std::string m_sm_status_state;
215 std::string m_sm_status_substate;
217 std::string m_server_id;
#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
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