ifw-ccf  2.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>
22 #include <ccf/common/config.hpp>
23 
24 namespace ccf::common {
25 
28 class InternalDb : public rad::DbAdapter {
29  public:
30 
31  InternalDb() {m_connected = false;};
32 
34 
36  void Config(const std::string& endpoint, const timeval& timeout);
37 
39  int Delete(const std::vector<std::string>& keys);
40 
42  bool Exists(const std::string& key);
43 
45  std::string Get(const std::string& key);
46 
48  std::vector<std::string> MultiGet(const std::vector<std::string>& keys);
49 
51  std::string Get(const std::string& key, const std::string& field);
52 
54  std::vector<std::string> MultiGet(const std::string& key,
55  const std::vector<std::string>& fields);
56 
58  std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
59 
61  void Set(const std::string& key, const std::string& value);
62 
64  void MultiSet(const std::vector<std::string>& kvs);
65 
67  void Set(const std::string& key, const std::string& field, const std::string& value);
68 
70  void MultiSet(const std::string& key, const std::vector<std::string>& fvs);
71 
73  void Connect();
74 
76  void Disconnect();
77 
79  bool IsConnected();
80 
81  InternalDb(const InternalDb&) = delete;
82  InternalDb& operator=(const InternalDb&) = delete;
83 
84  protected:
85  private:
86  std::map<std::string, std::string> m_db;
87  bool m_connected;
88 };
89 
90 
92 class Db {
93  public:
94 
96  static Db* s_instance;
97 
99  static Db& Instance();
100 
102  static void Simulate();
103 
105  Db(const std::string& prefix);
106 
107  virtual ~Db();
108 
110  void Disconnect();
111 
113  void Connect();
114 
116  std::string GenDbPath(const std::vector<std::string>& keys,
117  const std::string& base_path = "");
118 
120  std::map<std::string, std::string> Scan(const std::string& key, const std::string& pattern);
121 
123  bool IsConnected();
124 
126  std::string GetSmState();
127 
129  std::string GetSmStatusState();
130 
132  std::string GetSmStatusSubstate();
133 
135  void UpdateSmStatus(std::list<scxml4cpp::State*>& status);
136 
138  void UpdateSmStatus(const std::string& status);
139 
141  std::string Get(const std::string& key);
142 
144  std::string Get2(const std::vector<std::string>& keys,
145  const std::string& base_path = "");
146 
148  std::vector<std::string> MultiGet(const std::vector<std::string>& keys);
149 
151  bool Exists(const std::string& key);
152 
154  int Delete(const std::vector<std::string>& keys);
155 
157  void Set(const std::string& key, const std::string& value);
158 
160  template <class TYPE>
161  void Set(const std::string& key, const TYPE value) {
162  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
163  std::string str_value = std::to_string(value);
164  Set(key, str_value);
165  }
166 
168  void Set2(const std::vector<std::string>& keys,
169  const std::string& value,
170  const std::string& base_path = "");
171 
173  template <class TYPE>
174  void Set2(const std::vector<std::string>& keys,
175  const TYPE value,
176  const std::string& base_path = "") {
177  LOG4CPLUS_TRACE_METHOD(Logger(), __PRETTY_FUNCTION__);
178  std::string str_value = std::to_string(value);
179  Set2(keys, str_value, base_path);
180  }
181 
183  const std::string& GetAddress(const std::string& key);
184 
185  Db(const Db&) = delete;
186  Db& operator=(const Db&) = delete;
187 
189  rad::DbAdapter& GetDb();
190 
191  protected:
192 
193  private:
194  static bool s_simulation;
195 
196  std::string m_prefix;
197  rad::DbAdapterRedis m_redis_db;
198  InternalDb m_internal_db;
199  std::map<std::string, std::string> m_key_address_map;
200 
201  bool m_with_pubsub;
202  std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
203 
204  std::string m_current_status; // Current state + substate.
205  std::string m_sm_state;
206  std::string m_sm_status_state;
207  std::string m_sm_status_substate;
208 
209  std::string m_server_id;
210 };
211 
212 } // namespace ccf::common
213 
214 namespace ccf {
216  inline ccf::common::Db& GetDb() {
217  return ccf::common::Db::Instance();
218  }
219 }
220 
221 #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:351
ccf::common::InternalDb::IsConnected
bool IsConnected()
Returns true if connection is establihed.
Definition: db.cpp:361
ccf::common::Db::GetSmStatusSubstate
std::string GetSmStatusSubstate()
Definition: db.cpp:81
ccf::common::Db::MultiGet
std::vector< std::string > MultiGet(const std::vector< std::string > &keys)
Get multiple keys from the DB.
Definition: db.cpp:120
ccf::common::Db::Set
void Set(const std::string &key, const TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:161
ccf::common::Db::s_instance
static Db * s_instance
Singleton instance.
Definition: db.hpp:96
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:115
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:86
ccf::common::Db
Interface to the OLDB and Pub/Sub.
Definition: db.hpp:92
config.hpp
ccf::common::Db::Connect
void Connect()
Connect to the associated database(s).
Definition: db.cpp:198
ccf::common::InternalDb::Disconnect
void Disconnect()
Disconnect from the DB.
Definition: db.cpp:356
ccf::common::InternalDb::InternalDb
InternalDb()
Definition: db.hpp:31
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:327
ccf::common::InternalDb::~InternalDb
~InternalDb()
Definition: db.hpp:33
ccf::common::Db::GetSmState
std::string GetSmState()
Definition: db.cpp:71
ccf::common::Db::UpdateSmStatus
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition: db.cpp:129
ccf
Definition: appBase.cpp:8
ccf::common::Db::operator=
Db & operator=(const Db &)=delete
Disable copy constructor.
ccf::common::Db::~Db
virtual ~Db()
Definition: db.cpp:67
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::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
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::Get2
std::string Get2(const std::vector< std::string > &keys, const std::string &base_path="")
Get value for the given key.
Definition: db.cpp:91
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:28
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:177
ccf::common::Db::GetSmStatusState
std::string GetSmStatusState()
Definition: db.cpp:76
ccf::common::InternalDb::Exists
bool Exists(const std::string &key)
Check if the given key exists.
Definition: db.cpp:258
ccf::common::Db::GenDbPath
std::string GenDbPath(const std::vector< std::string > &keys, const std::string &base_path="")
Generate proper DB name.
Definition: db.cpp:99
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:318
ccf::common::Db::Set2
void Set2(const std::vector< std::string > &keys, const TYPE value, const std::string &base_path="")
Set the referenced key to the given value.
Definition: db.hpp:174
ccf::common::Db::Set2
void Set2(const std::vector< std::string > &keys, const std::string &value, const std::string &base_path="")
Set the referenced key to the given value.
Definition: db.cpp:184
ccf::common::Db::Disconnect
void Disconnect()
Disconnect from the assosicated database(s).
Definition: db.cpp:193
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::GetDb
ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition: db.hpp:216
ccf::common::Db::IsConnected
bool IsConnected()
Check if connected to the associated database(s).
Definition: db.cpp:206
ccf::Logger
log4cplus::Logger & Logger()
Definition: base.cpp:9