Kea 1.5.0
database_connection.h
Go to the documentation of this file.
1// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef DATABASE_CONNECTION_H
8#define DATABASE_CONNECTION_H
9
10#include <cc/data.h>
11#include <boost/noncopyable.hpp>
12#include <boost/function.hpp>
13#include <boost/shared_ptr.hpp>
15#include <map>
16#include <string>
17
18namespace isc {
19namespace db {
20
22class NoDatabaseName : public Exception {
23public:
24 NoDatabaseName(const char* file, size_t line, const char* what) :
25 isc::Exception(file, line, what) {}
26};
27
29class DbOpenError : public Exception {
30public:
31 DbOpenError(const char* file, size_t line, const char* what) :
32 isc::Exception(file, line, what) {}
33};
34
37public:
38 DbOperationError(const char* file, size_t line, const char* what) :
39 isc::Exception(file, line, what) {}
40};
41
45class InvalidType : public Exception {
46public:
47 InvalidType(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) {}
49};
50
55public:
56 DbInvalidTimeout(const char* file, size_t line, const char* what) :
57 isc::Exception(file, line, what) {}
58};
59
64public:
65 DbInvalidReadOnly(const char* file, size_t line, const char* what) :
66 isc::Exception(file, line, what) {}
67};
68
77public:
82 ReconnectCtl(const std::string& backend_type, unsigned int max_retries,
83 unsigned int retry_interval)
84 : backend_type_(backend_type), max_retries_(max_retries),
85 retries_left_(max_retries), retry_interval_(retry_interval) {}
86
88 std::string backendType() const {
89 return (backend_type_);
90 }
91
96 bool checkRetries() {
97 return (retries_left_ ? --retries_left_ : false);
98 }
99
101 unsigned int maxRetries() {
102 return (max_retries_);
103 }
104
106 unsigned int retriesLeft() {
107 return (retries_left_);
108 }
109
111 unsigned int retryInterval() {
112 return (retry_interval_);
113 }
114
115private:
117 const std::string backend_type_;
118
120 unsigned int max_retries_;
121
123 unsigned int retries_left_;
124
126 unsigned int retry_interval_;
127};
128
130typedef boost::shared_ptr<ReconnectCtl> ReconnectCtlPtr;
131
140class DatabaseConnection : public boost::noncopyable {
141public:
142
149 static const time_t MAX_DB_TIME;
150
152 typedef std::map<std::string, std::string> ParameterMap;
153
159 :parameters_(parameters) {
160 }
161
164
168 virtual ReconnectCtlPtr makeReconnectCtl() const;
169
175 std::string getParameter(const std::string& name) const;
176
185 static ParameterMap parse(const std::string& dbaccess);
186
195 static std::string redactedAccessString(const ParameterMap& parameters);
196
203 bool configuredReadOnly() const;
204
206 typedef boost::function<bool (ReconnectCtlPtr db_retry)> DbLostCallback;
207
217 bool invokeDbLostCallback() const;
218
223 static isc::data::ElementPtr toElement(const ParameterMap& params);
224
229 static isc::data::ElementPtr toElementDbAccessString(const std::string& dbaccess);
230
234
235private:
236
242 ParameterMap parameters_;
243
244};
245
246}; // end of isc::db namespace
247}; // end of isc namespace
248
249#endif // DATABASE_CONNECTION_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Common database connection class.
virtual ~DatabaseConnection()
Destructor.
bool configuredReadOnly() const
Convenience method checking if database should be opened with read only access.
boost::function< bool(ReconnectCtlPtr db_retry)> DbLostCallback
Defines a callback prototype for propogating events upward.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
static isc::data::ElementPtr toElement(const ParameterMap &params)
Unparse a parameter map.
static isc::data::ElementPtr toElementDbAccessString(const std::string &dbaccess)
Unparse an access string.
static DbLostCallback db_lost_callback
Optional call back function to invoke if a successfully open connection subsequently fails.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
bool invokeDbLostCallback() const
Invokes the connection's lost connectivity callback.
virtual ReconnectCtlPtr makeReconnectCtl() const
Instantiates a ReconnectCtl based on the connection's reconnect parameters.
DatabaseConnection(const ParameterMap &parameters)
Constructor.
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Invalid 'readonly' value specification.
DbInvalidReadOnly(const char *file, size_t line, const char *what)
DbInvalidTimeout(const char *file, size_t line, const char *what)
Exception thrown on failure to open database.
DbOpenError(const char *file, size_t line, const char *what)
Exception thrown on failure to execute a database function.
DbOperationError(const char *file, size_t line, const char *what)
Invalid type exception.
InvalidType(const char *file, size_t line, const char *what)
Exception thrown if name of database is not specified.
NoDatabaseName(const char *file, size_t line, const char *what)
Warehouses DB reconnect control values.
std::string backendType() const
Returns the type of the caller backend.
unsigned int retriesLeft()
Returns the number for retries remaining.
bool checkRetries()
Decrements the number of retries remaining.
unsigned int maxRetries()
Returns the maximum number for retries allowed.
unsigned int retryInterval()
Returns the amount of time to wait between reconnect attempts.
ReconnectCtl(const std::string &backend_type, unsigned int max_retries, unsigned int retry_interval)
Constructor.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-dhcp-ddns.