Go to the documentation of this file.
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>
36 void Config(
const std::string& endpoint,
const timeval& timeout);
39 int Delete(
const std::vector<std::string>& keys);
42 bool Exists(
const std::string& key);
45 std::string
Get(
const std::string& key);
48 std::vector<std::string>
MultiGet(
const std::vector<std::string>& keys);
51 std::string
Get(
const std::string& key,
const std::string& field);
54 std::vector<std::string>
MultiGet(
const std::string& key,
55 const std::vector<std::string>& fields);
58 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
61 void Set(
const std::string& key,
const std::string& value);
64 void MultiSet(
const std::vector<std::string>& kvs);
67 void Set(
const std::string& key,
const std::string& field,
const std::string& value);
70 void MultiSet(
const std::string& key,
const std::vector<std::string>& fvs);
86 std::map<std::string, std::string> m_db;
105 Db(
const std::string& prefix);
116 std::string
GenDbPath(
const std::vector<std::string>& keys,
117 const std::string& base_path =
"");
120 std::map<std::string, std::string>
Scan(
const std::string& key,
const std::string& pattern);
141 std::string
Get(
const std::string& key);
144 std::string
Get2(
const std::vector<std::string>& keys,
145 const std::string& base_path =
"");
148 std::vector<std::string>
MultiGet(
const std::vector<std::string>& keys);
151 bool Exists(
const std::string& key);
154 int Delete(
const std::vector<std::string>& keys);
157 void Set(
const std::string& key,
const std::string& value);
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);
168 void Set2(
const std::vector<std::string>& keys,
169 const std::string& value,
170 const std::string& base_path =
"");
173 template <
class TYPE>
174 void Set2(
const std::vector<std::string>& keys,
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);
183 const std::string&
GetAddress(
const std::string& key);
189 rad::DbAdapter&
GetDb();
194 static bool s_simulation;
196 std::string m_prefix;
197 rad::DbAdapterRedis m_redis_db;
199 std::map<std::string, std::string> m_key_address_map;
202 std::unique_ptr<rad::cii::Publisher<stdif::Status>> m_status_publisher;
204 std::string m_current_status;
205 std::string m_sm_state;
206 std::string m_sm_status_state;
207 std::string m_sm_status_substate;
209 std::string m_server_id;
221 #endif // CCFCOMMON_DB_HPP_
std::vector< std::string > MultiGet(const std::vector< std::string > &keys)
Get a set of keys.
Definition: db.cpp:271
void Connect()
Connect to the DB.
Definition: db.cpp:351
bool IsConnected()
Returns true if connection is establihed.
Definition: db.cpp:361
std::string GetSmStatusSubstate()
Definition: db.cpp:81
std::vector< std::string > MultiGet(const std::vector< std::string > &keys)
Get multiple keys from the DB.
Definition: db.cpp:120
void Set(const std::string &key, const TYPE value)
Set the referenced key to the given value.
Definition: db.hpp:161
static Db * s_instance
Singleton instance.
Definition: db.hpp:96
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
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
std::string Get(const std::string &key)
Get the value of the given key.
Definition: db.cpp:262
std::string Get(const std::string &key)
Get value for the given key.
Definition: db.cpp:86
Interface to the OLDB and Pub/Sub.
Definition: db.hpp:92
void Connect()
Connect to the associated database(s).
Definition: db.cpp:198
void Disconnect()
Disconnect from the DB.
Definition: db.cpp:356
InternalDb()
Definition: db.hpp:31
bool Exists(const std::string &key)
Check if the given key exists.
Definition: db.cpp:231
Db(const std::string &prefix)
Constructor. The prefix indicates location in database of this application.
Definition: db.cpp:25
void MultiSet(const std::vector< std::string > &kvs)
Set a set of keys and values.
Definition: db.cpp:327
~InternalDb()
Definition: db.hpp:33
std::string GetSmState()
Definition: db.cpp:71
void UpdateSmStatus(std::list< scxml4cpp::State * > &status)
Update the state/substate/status in the OLDB/PubSub.
Definition: db.cpp:129
Definition: appBase.cpp:8
Db & operator=(const Db &)=delete
Disable copy constructor.
virtual ~Db()
Definition: db.cpp:67
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
InternalDb(const InternalDb &)=delete
void Config(const std::string &endpoint, const timeval &timeout)
Configure the endpoint and timeout to apply for this interface.
Definition: db.cpp:245
static void Simulate()
Put the object in simulation, using an internal map as DB.
Definition: db.cpp:226
Definition: appBase.cpp:8
static Db & Instance()
Return reference to Singleton instance of the application class.
Definition: db.cpp:17
InternalDb & operator=(const InternalDb &)=delete
Disable copy constructor.
std::string Get2(const std::vector< std::string > &keys, const std::string &base_path="")
Get value for the given key.
Definition: db.cpp:91
rad::DbAdapter & GetDb()
Disable assignment operator.
Definition: db.cpp:236
DB implementing (partially) the rad::DbAdapter interface; used for internal/test purposes.
Definition: db.hpp:28
void Set(const std::string &key, const std::string &value)
Set the referenced key to the given value.
Definition: db.cpp:177
std::string GetSmStatusState()
Definition: db.cpp:76
bool Exists(const std::string &key)
Check if the given key exists.
Definition: db.cpp:258
std::string GenDbPath(const std::vector< std::string > &keys, const std::string &base_path="")
Generate proper DB name.
Definition: db.cpp:99
void Set(const std::string &key, const std::string &value)
Set the given key to the given value.
Definition: db.cpp:318
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
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
void Disconnect()
Disconnect from the assosicated database(s).
Definition: db.cpp:193
int Delete(const std::vector< std::string > &keys)
Delete the keys specified.
Definition: db.cpp:221
int Delete(const std::vector< std::string > &keys)
Delete the keys listed.
Definition: db.cpp:249
ccf::common::Db & GetDb()
Return the reference to the CCF DB Singleton instance.
Definition: db.hpp:216
bool IsConnected()
Check if connected to the associated database(s).
Definition: db.cpp:206
log4cplus::Logger & Logger()
Definition: base.cpp:9