ifw-ccf  3.0.0-pre2
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 
26 namespace ccf::common {
27 
29 class InternalDb {
30  public:
31 
32  InternalDb(const std::string& prefix);
33 
35 
36  inline bool IsConnected() { return m_connected; }
37 
38  inline std::string GetPrefix() { return m_prefix; }
39 
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));
46  }
47  core::utils::conversion::Convert(m_db[complete_key], value);
48  }
49 
50  template<typename TYPE>
51  void Set(const std::string& key,
52  const TYPE& value) {
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);
58  }
59 
60  std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
61 
62  InternalDb(const InternalDb&) = delete;
63  InternalDb& operator=(const InternalDb&) = delete;
64 
65  protected:
66  private:
67  std::map<std::string, std::string> m_db;
68  std::string m_prefix;
69  bool m_connected;
70 };
71 
72 
74 class Db {
75  public:
76 
78  static Db* s_instance;
79 
81  static Db& Instance();
82 
84  static void Simulate();
85 
87  Db();
88 
89  virtual ~Db();
90 
92  std::string GenDbPath(const std::vector<std::string>& keys);
93 
95  std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
96 
98  bool IsConnected();
99 
101  std::string GetSmState();
102 
104  std::string GetSmStatusState();
105 
107  std::string GetSmStatusSubstate();
108 
109  // TODO: Check if possible to use the one from util::bat::DbInterface.
111  void UpdateSmStatus(std::list<scxml4cpp::State*>& status);
112 
113  // TODO: Check if possible to use the one from util::bat::DbInterface.
115  void UpdateSmStatus(const std::string& status);
116 
118  template <class TYPE>
119  TYPE Get(const std::string& key) {
120  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
121  TYPE tmp_value;
122  if (key.find(m_prefix) != std::string::npos) {
123  CCFTHROW(fmt::format("OLDB keys shall be given without prefix: {}", key));
124  }
125  BEGIN_CRIT_SEC("ccf::common::Db") {
126  if (!s_simulation) {
127  m_cii_db.get()->Get(key, tmp_value);
128  } else {
129  m_internal_db.get()->Get(key, tmp_value);
130  }
131  } END_CRIT_SEC();
132  return tmp_value;
133  }
134 
136  template <class TYPE>
137  TYPE Get2(const std::vector<std::string>& keys) {
138  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
139  return Get<TYPE>(GenDbPath(keys));
140  }
141 
143  std::string GetAsStr(const std::string& key);
144 
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));
151  }
152  BEGIN_CRIT_SEC("ccf::common::Db") {
153  if (!s_simulation) {
154  m_cii_db.get()->Set(key, value);
155  } else {
156  m_internal_db.get()->Set(key, value);
157  }
158  } END_CRIT_SEC();
159  }
160 
162  template <class TYPE>
163  void Set2(const std::vector<std::string>& keys,
164  const TYPE value) {
165  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
166  Set(GenDbPath(keys), value);
167  }
168 
170  void TestConnection();
171 
172  Db(const Db&) = delete;
173  Db& operator=(const Db&) = delete;
174 
175  protected:
176 
177  private:
178  static bool s_simulation;
179 
180  std::string m_prefix;
181  std::unique_ptr<utils::bat::DbInterface> m_cii_db;
182  std::unique_ptr<InternalDb> m_internal_db;
183 
184  bool m_with_pubsub;
185  std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
186 
187  std::string m_current_status; // Current state + substate.
188  std::string m_sm_state;
189  std::string m_sm_status_state;
190  std::string m_sm_status_substate;
191 
192  std::string m_server_id;
193 };
194 
195 } // namespace ccf::common
196 
197 namespace ccf {
199  inline ccf::common::Db& GetDb() {
200  return ccf::common::Db::Instance();
201  }
202 }
203 
204 #endif // CCFCOMMON_DB_HPP_
#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(const Db &)=delete
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