ifw-ccf  1.0.0
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 <ccf/common/base.hpp>
23 
24 namespace ccf::common {
25 
27 class InternalDb : public rad::DbAdapter {
28  public:
29 
30  InternalDb() {m_connected = false;};
31 
33 
35  void Config(const std::string& endpoint, const timeval& timeout);
36 
38  int Delete(const std::vector<std::string>& keys);
39 
41  bool Exists(const std::string& key);
42 
44  std::string Get(const std::string& key);
45 
47  std::vector<std::string> MultiGet(const std::vector<std::string>& keys);
48 
50  std::string Get(const std::string& key, const std::string& field);
51 
53  std::vector<std::string> MultiGet(const std::string& key,
54  const std::vector<std::string>& fields);
55 
57  std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
58 
60  void Set(const std::string& key, const std::string& value);
61 
63  void MultiSet(const std::vector<std::string>& kvs);
64 
66  void Set(const std::string& key, const std::string& field, const std::string& value);
67 
69  void MultiSet(const std::string& key, const std::vector<std::string>& fvs);
70 
72  void Connect();
73 
75  void Disconnect();
76 
78  bool IsConnected();
79 
80  InternalDb(const InternalDb&) = delete;
81  InternalDb& operator=(const InternalDb&) = delete;
82 
83  protected:
84  private:
85  std::map<std::string, std::string> m_db;
86  bool m_connected;
87 };
88 
89 
91 class Db {
92  public:
93 
95  static Db* s_instance;
96 
98  static Db& Instance();
99 
101  static void Simulate();
102 
104  Db(const std::string& prefix);
105 
106  virtual ~Db();
107 
109  void Disconnect();
110 
112  void Connect();
113 
115  std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
116 
118  bool IsConnected();
119 
121  std::string GetSmState();
122 
124  std::string GetSmStatusState();
125 
127  std::string GetSmStatusSubstate();
128 
130  void UpdateSmStatus(std::set<scxml4cpp::State*>& status);
131 
133  void UpdateSmStatus(const std::string& status);
134 
136  std::string Get(const std::string& key);
137 
139  std::vector<std::string> MultiGet(const std::vector<std::string>& keys);
140 
142  bool Exists(const std::string& key);
143 
145  int Delete(const std::vector<std::string>& keys);
146 
148  void Set(const std::string& key, const std::string& value);
149 
151  template <class TYPE>
152  void Set(const std::string& key, TYPE value) {
153  CCFTRACE;
154  std::string str_value = std::to_string(value);
155  Set(key, str_value);
156  }
157 
159  const std::string& GetAddress(const std::string& key);
160 
161  Db(const Db&) = delete;
162  Db& operator=(const Db&) = delete;
163 
165  rad::DbAdapter& GetDb();
166 
167  protected:
168 
169  private:
170  static bool s_simulation;
171 
172  std::string m_prefix;
173  rad::DbAdapterRedis m_redis_db;
174  InternalDb m_internal_db;
175  std::map<std::string, std::string> m_key_address_map;
176 
177  bool m_with_pubsub;
178  std::unique_ptr<rad::cii::Publisher<stdif::StatusTopic>> m_status_publisher;
179 
180  std::string m_current_status; // Current state + substate.
181  std::string m_sm_state;
182  std::string m_sm_status_state;
183  std::string m_sm_status_substate;
184 };
185 
187 inline Db& GetDb() {
188  return ccf::common::Db::Instance();
189 }
190 
191 } // namespace ccf::common
192 
193 #endif // CCFCOMMON_DB_HPP_
ccf::common::InternalDb::MultiGet
std::vector< std::string > MultiGet(const std::vector< std::string > &keys)
Get a set of keys.
Definition: db.cpp:271
ccf::common::InternalDb::Connect
void Connect()
Connect to the DB.
Definition: db.cpp:350
ccf::common::InternalDb::IsConnected
bool IsConnected()
Returns true if connection is establihed.
Definition: db.cpp:360
ccf::common::Db::GetSmStatusSubstate
std::string GetSmStatusSubstate()
Definition: db.cpp:78
ccf::common::Db::MultiGet
std::vector< std::string > MultiGet(const std::vector< std::string > &keys)
Get multiple keys from the DB.
Definition: db.cpp:95
ccf::common::Db::s_instance
static Db * s_instance
Singleton instance.
Definition: db.hpp:95
ccf::common::InternalDb::Scan
std::map< std::string, std::string > Scan(const std::string &key, const std::string &pattern)
Scan the DB for the given key applying the given key/field.
Definition: db.cpp:307
ccf::common::Db::Scan
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:90
ccf::common::InternalDb::Get
std::string Get(const std::string &key)
Get the value of the given key.
Definition: db.cpp:262
ccf::common::Db::Get
std::string Get(const std::string &key)
Get value for the given key.
Definition: db.cpp:84
ccf::common::Db
Interface to the OLDB and Pub/Sub.
Definition: db.hpp:91
ccf::common::GetDb
Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition: db.hpp:187
ccf::common::Db::Connect
void Connect()
Connect to the associated database(s).
Definition: db.cpp:195
ccf::common::InternalDb::Disconnect
void Disconnect()
Disconnect from the DB.
Definition: db.cpp:355
ccf::common::InternalDb::InternalDb
InternalDb()
Definition: db.hpp:30
ccf::common::Db::Exists
bool Exists(const std::string &key)
Check if the given key exists.
Definition: db.cpp:231
ccf::common::Db::Db
Db(const std::string &prefix)
Constructor. The prefix indicates location in database of this application.
Definition: db.cpp:25
ccf::common::InternalDb::MultiSet
void MultiSet(const std::vector< std::string > &kvs)
Set a set of keys and values.
Definition: db.cpp:326
ccf::common::InternalDb::~InternalDb
~InternalDb()
Definition: db.hpp:32
ccf::common::Db::GetSmState
std::string GetSmState()
Definition: db.cpp:66
ccf::common::Db::operator=
Db & operator=(const Db &)=delete
Disable copy constructor.
ccf::common::Db::~Db
virtual ~Db()
Definition: db.cpp:62
ccf::common::Db::GetAddress
const std::string & GetAddress(const std::string &key)
Get the complete address of the given key (append the index if missing and cache the name).
Definition: db.cpp:211
ccf::common::Db::Set
void Set(const std::string &key, TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:152
ccf::common::InternalDb::InternalDb
InternalDb(const InternalDb &)=delete
ccf::common::InternalDb::Config
void Config(const std::string &endpoint, const timeval &timeout)
Configure the endpoint and timeout to apply for this interface.
Definition: db.cpp:245
ccf::common::Db::UpdateSmStatus
void UpdateSmStatus(std::set< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition: db.cpp:104
base.hpp
ccf::common::Db::Simulate
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition: db.cpp:226
ccf::common
Definition: appBase.cpp:8
ccf::common::Db::Instance
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition: db.cpp:17
ccf::common::InternalDb::operator=
InternalDb & operator=(const InternalDb &)=delete
Disable copy constructor.
ccf::common::Db::GetDb
rad::DbAdapter & GetDb()
Disable assignment operator.
Definition: db.cpp:236
ccf::common::InternalDb
DB implementing (partially) the rad::DbAdapter interface; used for internal/test purposes.
Definition: db.hpp:27
ccf::common::Db::Set
void Set(const std::string &key, const std::string &value)
Set the referenced key to the given value.
Definition: db.cpp:179
ccf::common::Db::GetSmStatusState
std::string GetSmStatusState()
Definition: db.cpp:72
ccf::common::InternalDb::Exists
bool Exists(const std::string &key)
Check if the given key exists.
Definition: db.cpp:258
ccf::common::InternalDb::Set
void Set(const std::string &key, const std::string &value)
Set the given key to the given value.
Definition: db.cpp:317
CCFTRACE
#define CCFTRACE
TRACE log macro. Includes the location ("CCFLOC") in the log message.
Definition: base.hpp:403
ccf::common::Db::Disconnect
void Disconnect()
Disconnect from the assosicated database(s).
Definition: db.cpp:189
ccf::common::Db::Delete
int Delete(const std::vector< std::string > &keys)
Delete the keys specified.
Definition: db.cpp:221
ccf::common::Db::Db
Db(const Db &)=delete
ccf::common::InternalDb::Delete
int Delete(const std::vector< std::string > &keys)
Delete the keys listed.
Definition: db.cpp:249
ccf::common::Db::IsConnected
bool IsConnected()
Check if connected to the associated database(s).
Definition: db.cpp:205
configBase.hpp