2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-03 15:35:17 +00:00

[4489] Removed RestrictedConstPtr class.

This commit is contained in:
Marcin Siodelski
2016-07-28 21:08:43 +02:00
parent fe0424b032
commit bfb36f3d0c
5 changed files with 57 additions and 56 deletions

View File

@@ -11,6 +11,7 @@
#include <dhcp/option_definition.h> #include <dhcp/option_definition.h>
#include <dhcp/option_space.h> #include <dhcp/option_space.h>
#include <dhcpsrv/cfg_option.h> #include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/dhcpsrv_log.h> #include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/mysql_host_data_source.h> #include <dhcpsrv/mysql_host_data_source.h>
#include <dhcpsrv/db_exceptions.h> #include <dhcpsrv/db_exceptions.h>
@@ -1760,6 +1761,15 @@ public:
StatementIndex stindex, StatementIndex stindex,
boost::shared_ptr<MySqlHostExchange> exchange) const; boost::shared_ptr<MySqlHostExchange> exchange) const;
/// @brief Throws exception if database is read only.
///
/// This method should be called by the methods which write to the
/// database. If the backend is operating in read-only mode this
/// method will throw exception.
///
/// @throw DbReadOnly if backend is operating in read only mode.
void checkReadOnly() const;
/// @brief Pointer to the object representing an exchange which /// @brief Pointer to the object representing an exchange which
/// can be used to retrieve hosts and DHCPv4 options. /// can be used to retrieve hosts and DHCPv4 options.
boost::shared_ptr<MySqlHostWithOptionsExchange> host_exchange_; boost::shared_ptr<MySqlHostWithOptionsExchange> host_exchange_;
@@ -2202,21 +2212,29 @@ getHost(const SubnetID& subnet_id,
return (result); return (result);
} }
void
MySqlHostDataSourceImpl::checkReadOnly() const {
if (is_readonly_) {
isc_throw(ReadOnlyDb, "MySQL host database backend is configured to"
" operate in read only mode");
}
}
MySqlHostDataSource:: MySqlHostDataSource::
MySqlHostDataSource(const MySqlConnection::ParameterMap& parameters) MySqlHostDataSource(const MySqlConnection::ParameterMap& parameters)
: impl_(new MySqlHostDataSourceImpl(parameters), : impl_(new MySqlHostDataSourceImpl(parameters)) {
"MySQL host database backend is configured to"
" operate in read only mode") {
impl_.allowConstOnly(impl_->is_readonly_);
} }
MySqlHostDataSource::~MySqlHostDataSource() { MySqlHostDataSource::~MySqlHostDataSource() {
delete impl_.getPtr(); delete impl_;
} }
void void
MySqlHostDataSource::add(const HostPtr& host) { MySqlHostDataSource::add(const HostPtr& host) {
// If operating in read-only mode, throw exception.
impl_->checkReadOnly();
// Initiate MySQL transaction as we will have to make multiple queries // Initiate MySQL transaction as we will have to make multiple queries
// to insert host information into multiple tables. If that fails on // to insert host information into multiple tables. If that fails on
// any stage, the transaction will be rolled back by the destructor of // any stage, the transaction will be rolled back by the destructor of
@@ -2539,12 +2557,16 @@ std::pair<uint32_t, uint32_t> MySqlHostDataSource::getVersion() const {
void void
MySqlHostDataSource::commit() { MySqlHostDataSource::commit() {
// If operating in read-only mode, throw exception.
impl_->checkReadOnly();
impl_->conn_.commit(); impl_->conn_.commit();
} }
void void
MySqlHostDataSource::rollback() { MySqlHostDataSource::rollback() {
// If operating in read-only mode, throw exception.
impl_->checkReadOnly();
impl_->conn_.rollback(); impl_->conn_.rollback();
} }

View File

@@ -10,7 +10,6 @@
#include <dhcpsrv/base_host_data_source.h> #include <dhcpsrv/base_host_data_source.h>
#include <dhcpsrv/db_exceptions.h> #include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/mysql_connection.h> #include <dhcpsrv/mysql_connection.h>
#include <util/pointer_util.h>
namespace isc { namespace isc {
namespace dhcp { namespace dhcp {
@@ -257,7 +256,7 @@ public:
private: private:
/// @brief Pointer to the implementation of the @ref MySqlHostDataSource. /// @brief Pointer to the implementation of the @ref MySqlHostDataSource.
util::RestrictedConstPtr<MySqlHostDataSourceImpl, ReadOnlyDb> impl_; MySqlHostDataSourceImpl* impl_;
}; };
} }

View File

@@ -10,6 +10,7 @@
#include <dhcp/option.h> #include <dhcp/option.h>
#include <dhcp/option_definition.h> #include <dhcp/option_definition.h>
#include <dhcp/option_space.h> #include <dhcp/option_space.h>
#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/cfg_option.h> #include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/dhcpsrv_log.h> #include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/pgsql_host_data_source.h> #include <dhcpsrv/pgsql_host_data_source.h>
@@ -1230,6 +1231,14 @@ public:
StatementIndex stindex, StatementIndex stindex,
boost::shared_ptr<PgSqlHostExchange> exchange) const; boost::shared_ptr<PgSqlHostExchange> exchange) const;
/// @brief Throws exception if database is read only.
///
/// This method should be called by the methods which write to the
/// database. If the backend is operating in read-only mode this
/// method will throw exception.
///
/// @throw DbReadOnly if backend is operating in read only mode.
void checkReadOnly() const;
/// @brief Returns PostgreSQL schema version of the open database /// @brief Returns PostgreSQL schema version of the open database
/// ///
@@ -1661,23 +1670,32 @@ std::pair<uint32_t, uint32_t> PgSqlHostDataSourceImpl::getVersion() const {
return (std::make_pair<uint32_t, uint32_t>(version, minor)); return (std::make_pair<uint32_t, uint32_t>(version, minor));
} }
void
PgSqlHostDataSourceImpl::checkReadOnly() const {
if (is_readonly_) {
isc_throw(ReadOnlyDb, "PostgreSQL host database backend is configured"
" to operate in read only mode");
}
}
/*********** PgSqlHostDataSource *********************/ /*********** PgSqlHostDataSource *********************/
PgSqlHostDataSource:: PgSqlHostDataSource::
PgSqlHostDataSource(const PgSqlConnection::ParameterMap& parameters) PgSqlHostDataSource(const PgSqlConnection::ParameterMap& parameters)
: impl_(new PgSqlHostDataSourceImpl(parameters), : impl_(new PgSqlHostDataSourceImpl(parameters)) {
"PostgreSQL host database backend is configured to"
" operate in read only mode") {
impl_.allowConstOnly(impl_->is_readonly_);
} }
PgSqlHostDataSource::~PgSqlHostDataSource() { PgSqlHostDataSource::~PgSqlHostDataSource() {
delete impl_.getPtr(); delete impl_;
} }
void void
PgSqlHostDataSource::add(const HostPtr& host) { PgSqlHostDataSource::add(const HostPtr& host) {
// If operating in read-only mode, throw exception.
impl_->checkReadOnly();
// Initiate PostgreSQL transaction as we will have to make multiple queries // Initiate PostgreSQL transaction as we will have to make multiple queries
// to insert host information into multiple tables. If that fails on // to insert host information into multiple tables. If that fails on
// any stage, the transaction will be rolled back by the destructor of // any stage, the transaction will be rolled back by the destructor of
@@ -1928,12 +1946,16 @@ std::pair<uint32_t, uint32_t> PgSqlHostDataSource::getVersion() const {
void void
PgSqlHostDataSource::commit() { PgSqlHostDataSource::commit() {
// If operating in read-only mode, throw exception.
impl_->checkReadOnly();
impl_->conn_.commit(); impl_->conn_.commit();
} }
void void
PgSqlHostDataSource::rollback() { PgSqlHostDataSource::rollback() {
// If operating in read-only mode, throw exception.
impl_->checkReadOnly();
impl_->conn_.rollback(); impl_->conn_.rollback();
} }

View File

@@ -8,10 +8,8 @@
#define PGSQL_HOST_DATA_SOURCE_H #define PGSQL_HOST_DATA_SOURCE_H
#include <dhcpsrv/base_host_data_source.h> #include <dhcpsrv/base_host_data_source.h>
#include <dhcpsrv/db_exceptions.h>
#include <dhcpsrv/pgsql_connection.h> #include <dhcpsrv/pgsql_connection.h>
#include <dhcpsrv/pgsql_exchange.h> #include <dhcpsrv/pgsql_exchange.h>
#include <util/pointer_util.h>
namespace isc { namespace isc {
namespace dhcp { namespace dhcp {
@@ -288,7 +286,7 @@ public:
private: private:
/// @brief Pointer to the implementation of the @ref PgSqlHostDataSource. /// @brief Pointer to the implementation of the @ref PgSqlHostDataSource.
util::RestrictedConstPtr<PgSqlHostDataSourceImpl, ReadOnlyDb> impl_; PgSqlHostDataSourceImpl* impl_;
}; };
} }

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -7,49 +7,9 @@
#ifndef POINTER_UTIL_H #ifndef POINTER_UTIL_H
#define POINTER_UTIL_H #define POINTER_UTIL_H
#include <exceptions/exceptions.h>
#include <string>
namespace isc { namespace isc {
namespace util { namespace util {
template <typename T, typename E>
class RestrictedConstPtr {
public:
RestrictedConstPtr(T* ptr, const std::string& error_text)
: ptr_(ptr), const_only_(false), error_text_(error_text) {
}
void allowConstOnly(const bool const_only) {
const_only_ = const_only;
}
T* operator->() const {
return (ptr_);
}
T* operator->() {
if (const_only_) {
isc_throw(E, error_text_);
}
return (ptr_);
}
T* getPtr() const {
return (ptr_);
}
private:
T* ptr_;
bool const_only_;
std::string error_text_;
};
/// @brief This function checks if two pointers are non-null and values /// @brief This function checks if two pointers are non-null and values
/// are equal. /// are equal.
/// ///