icinga2/lib/db_ido/dbconnection.hpp

125 lines
3.8 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef DBCONNECTION_H
#define DBCONNECTION_H
2014-05-25 16:23:35 +02:00
#include "db_ido/i2-db_ido.hpp"
2018-01-18 13:50:38 +01:00
#include "db_ido/dbconnection-ti.hpp"
2014-05-25 16:23:35 +02:00
#include "db_ido/dbobject.hpp"
#include "db_ido/dbquery.hpp"
#include "base/timer.hpp"
#include "base/ringbuffer.hpp"
#include <boost/thread/once.hpp>
#include <boost/thread/mutex.hpp>
#define IDO_CURRENT_SCHEMA_VERSION "1.14.3"
#define IDO_COMPAT_SCHEMA_VERSION "1.14.3"
namespace icinga
{
/**
* A database connection.
*
2013-10-14 20:12:42 +02:00
* @ingroup db_ido
*/
2017-12-31 07:22:16 +01:00
class DbConnection : public ObjectImpl<DbConnection>
{
public:
2014-11-03 00:44:04 +01:00
DECLARE_OBJECT(DbConnection);
static void InitializeDbTimer();
2013-08-01 11:14:33 +02:00
void SetConfigHash(const DbObject::Ptr& dbobj, const String& hash);
void SetConfigHash(const DbType::Ptr& type, const DbReference& objid, const String& hash);
String GetConfigHash(const DbObject::Ptr& dbobj) const;
String GetConfigHash(const DbType::Ptr& type, const DbReference& objid) const;
2013-08-02 08:56:36 +02:00
void SetObjectID(const DbObject::Ptr& dbobj, const DbReference& dbref);
DbReference GetObjectID(const DbObject::Ptr& dbobj) const;
void SetInsertID(const DbObject::Ptr& dbobj, const DbReference& dbref);
2014-01-31 08:28:00 +01:00
void SetInsertID(const DbType::Ptr& type, const DbReference& objid, const DbReference& dbref);
2013-08-02 08:56:36 +02:00
DbReference GetInsertID(const DbObject::Ptr& dbobj) const;
2014-01-31 08:28:00 +01:00
DbReference GetInsertID(const DbType::Ptr& type, const DbReference& objid) const;
void SetObjectActive(const DbObject::Ptr& dbobj, bool active);
bool GetObjectActive(const DbObject::Ptr& dbobj) const;
void ClearIDCache();
void SetConfigUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
bool GetConfigUpdate(const DbObject::Ptr& dbobj) const;
void SetStatusUpdate(const DbObject::Ptr& dbobj, bool hasupdate);
bool GetStatusUpdate(const DbObject::Ptr& dbobj) const;
int GetQueryCount(RingBuffer::SizeType span);
virtual int GetPendingQueryCount() const = 0;
void ValidateFailoverTimeout(const Lazy<double>& lvalue, const ValidationUtils& utils) final;
void ValidateCategories(const Lazy<Array::Ptr>& lvalue, const ValidationUtils& utils) final;
protected:
void OnConfigLoaded() override;
void Start(bool runtimeCreated) override;
void Stop(bool runtimeRemoved) override;
void Resume() override;
void Pause() override;
virtual void ExecuteQuery(const DbQuery& query) = 0;
virtual void ExecuteMultipleQueries(const std::vector<DbQuery>&) = 0;
virtual void ActivateObject(const DbObject::Ptr& dbobj) = 0;
virtual void DeactivateObject(const DbObject::Ptr& dbobj) = 0;
virtual void CleanUpExecuteQuery(const String& table, const String& time_column, double max_age);
2014-01-31 08:28:00 +01:00
virtual void FillIDCache(const DbType::Ptr& type) = 0;
virtual void NewTransaction() = 0;
void UpdateObject(const ConfigObject::Ptr& object);
void UpdateAllObjects();
void PrepareDatabase();
void IncreaseQueryCount();
bool IsIDCacheValid() const;
void SetIDCacheValid(bool valid);
void EnableActiveChangedHandler();
static void UpdateProgramStatus();
static int GetSessionToken();
private:
bool m_IDCacheValid{false};
std::map<std::pair<DbType::Ptr, DbReference>, String> m_ConfigHashes;
2013-08-02 08:56:36 +02:00
std::map<DbObject::Ptr, DbReference> m_ObjectIDs;
2014-01-31 08:28:00 +01:00
std::map<std::pair<DbType::Ptr, DbReference>, DbReference> m_InsertIDs;
std::set<DbObject::Ptr> m_ActiveObjects;
std::set<DbObject::Ptr> m_ConfigUpdates;
std::set<DbObject::Ptr> m_StatusUpdates;
2013-10-26 09:41:45 +02:00
Timer::Ptr m_CleanUpTimer;
void CleanUpHandler();
static Timer::Ptr m_ProgramStatusTimer;
static boost::once_flag m_OnceFlag;
static void InsertRuntimeVariable(const String& key, const Value& value);
mutable boost::mutex m_StatsMutex;
RingBuffer m_QueryStats{15 * 60};
bool m_ActiveChangedHandler{false};
};
struct database_error : virtual std::exception, virtual boost::exception { };
struct errinfo_database_query_;
typedef boost::error_info<struct errinfo_database_query_, std::string> errinfo_database_query;
}
#endif /* DBCONNECTION_H */