Commit b19a36e3 authored by Jelte Jansen's avatar Jelte Jansen
Browse files

[1062] added getDBName to DatabaseConnection

and add database name to logging output
parent 12b34733
......@@ -164,7 +164,8 @@ DatabaseClient::Finder::find(const isc::dns::Name& name,
isc::dns::RRsetPtr result_rrset;
ZoneFinder::Result result_status = SUCCESS;
RRsigStore sig_store;
logger.debug(DBG_TRACE_DETAILED, DATASRC_DATABASE_FIND_RECORDS).arg(name).arg(type);
logger.debug(DBG_TRACE_DETAILED, DATASRC_DATABASE_FIND_RECORDS)
.arg(connection_->getDBName()).arg(name).arg(type);
try {
connection_->searchForRecords(zone_id_, name.toText());
......@@ -233,17 +234,20 @@ DatabaseClient::Finder::find(const isc::dns::Name& name,
}
}
} catch (const DataSourceError& dse) {
logger.error(DATASRC_DATABASE_FIND_ERROR).arg(dse.what());
logger.error(DATASRC_DATABASE_FIND_ERROR)
.arg(connection_->getDBName()).arg(dse.what());
// call cleanup and rethrow
connection_->resetSearch();
throw;
} catch (const isc::Exception& isce) {
logger.error(DATASRC_DATABASE_FIND_UNCAUGHT_ISC_ERROR).arg(isce.what());
logger.error(DATASRC_DATABASE_FIND_UNCAUGHT_ISC_ERROR)
.arg(connection_->getDBName()).arg(isce.what());
// cleanup, change it to a DataSourceError and rethrow
connection_->resetSearch();
isc_throw(DataSourceError, isce.what());
} catch (const std::exception& ex) {
logger.error(DATASRC_DATABASE_FIND_UNCAUGHT_ERROR).arg(ex.what());
logger.error(DATASRC_DATABASE_FIND_UNCAUGHT_ERROR)
.arg(connection_->getDBName()).arg(ex.what());
connection_->resetSearch();
throw;
}
......
......@@ -147,6 +147,17 @@ public:
/// The number of fields the columns array passed to getNextRecord should have
static const size_t RecordColumnCount = 4;
/**
* \brief Returns a string identifying this dabase backend
*
* Any implementation is free to choose the exact string content,
* but it is advisable to make it a name that is distinguishable
* from the others.
*
* \return the name of the dabase
*/
virtual const std::string& getDBName() const = 0;
};
/**
......@@ -273,6 +284,7 @@ public:
* returned, though.
*/
virtual FindResult findZone(const isc::dns::Name& name) const;
private:
/// \brief Our connection.
const boost::shared_ptr<DatabaseConnection> connection_;
......
......@@ -63,23 +63,23 @@ The maximum allowed number of items of the hotspot cache is set to the given
number. If there are too many, some of them will be dropped. The size of 0
means no limit.
% DATASRC_DATABASE_FIND_ERROR error retrieving data from database datasource: %1
% DATASRC_DATABASE_FIND_ERROR error retrieving data from datasource %1: %2
The was an internal error while reading data from a datasource. This can either
mean the specific data source implementation is not behaving correctly, or the
data it provides is invalid. The current search is aborted.
The error message contains specific information about the error.
% DATASRC_DATABASE_FIND_RECORDS looking for record %1/%2
% DATASRC_DATABASE_FIND_RECORDS looking in datasource %1 for record %2/%3
Debug information. The database data source is looking up records with the given
name and type in the database.
% DATASRC_DATABASE_FIND_UNCAUGHT_ERROR uncaught general error retrieving data from database datasource: %1
% DATASRC_DATABASE_FIND_UNCAUGHT_ERROR uncaught general error retrieving data from datasource %1: %2
There was an uncaught general exception while reading data from a datasource.
This most likely points to a logic error in the code, and can be considered a
bug. The current search is aborted. Specific information about the exception is
printed in this error message.
% DATASRC_DATABASE_FIND_UNCAUGHT_ISC_ERROR uncaught error retrieving data from database datasource: %1
% DATASRC_DATABASE_FIND_UNCAUGHT_ISC_ERROR uncaught error retrieving data from datasource %1: %2
There was an uncaught ISC exception while reading data from a datasource. This
most likely points to a logic error in the code, and can be considered a bug.
The current search is aborted. Specific information about the exception is
......
......@@ -17,6 +17,7 @@
#include <datasrc/sqlite3_connection.h>
#include <datasrc/logger.h>
#include <datasrc/data_source.h>
#include <util/filename.h>
namespace isc {
namespace datasrc {
......@@ -48,7 +49,9 @@ struct SQLite3Parameters {
SQLite3Connection::SQLite3Connection(const std::string& filename,
const isc::dns::RRClass& rrclass) :
dbparameters_(new SQLite3Parameters),
class_(rrclass.toText())
class_(rrclass.toText()),
database_name_("sqlite3_" +
isc::util::Filename(filename).nameAndExtension())
{
LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_NEWCONN);
......
......@@ -135,6 +135,8 @@ public:
*/
virtual void resetSearch();
virtual const std::string& getDBName() const { return database_name_; }
private:
/// \brief Private database data
SQLite3Parameters* dbparameters_;
......@@ -144,6 +146,7 @@ private:
void open(const std::string& filename);
/// \brief Closes the database
void close();
const std::string database_name_;
};
}
......
......@@ -37,7 +37,11 @@ namespace {
*/
class MockConnection : public DatabaseConnection {
public:
MockConnection() : search_running_(false) { fillData(); }
MockConnection() : search_running_(false),
database_name_("mock_database")
{
fillData();
}
virtual std::pair<bool, int> getZone(const Name& name) const {
if (name == Name("example.org")) {
......@@ -108,6 +112,9 @@ public:
return (search_running_);
}
virtual const std::string& getDBName() const {
return database_name_;
}
private:
std::map<std::string, std::vector< std::vector<std::string> > > records;
// used as internal index for getNextRecord()
......@@ -125,6 +132,8 @@ private:
// hardcode some exceptions into getNextRecord
std::string searched_name_;
const std::string database_name_;
// Adds one record to the current name in the database
// The actual data will not be added to 'records' until
// addCurName() is called
......@@ -271,6 +280,8 @@ public:
// Will be deleted by client_, just keep the current value for comparison.
MockConnection* current_connection_;
shared_ptr<DatabaseClient> client_;
const std::string database_name_;
/**
* Check the zone finder is a valid one and references the zone ID and
* connection available here.
......
......@@ -30,7 +30,9 @@ namespace {
// Some test data
std::string SQLITE_DBFILE_EXAMPLE = TEST_DATA_DIR "/test.sqlite3";
std::string SQLITE_DBFILE_EXAMPLE2 = TEST_DATA_DIR "/example2.com.sqlite3";
std::string SQLITE_DBNAME_EXAMPLE2 = "sqlite3_example2.com.sqlite3";
std::string SQLITE_DBFILE_EXAMPLE_ROOT = TEST_DATA_DIR "/test-root.sqlite3";
std::string SQLITE_DBNAME_EXAMPLE_ROOT = "sqlite3_test-root.sqlite3";
std::string SQLITE_DBFILE_BROKENDB = TEST_DATA_DIR "/brokendb.sqlite3";
std::string SQLITE_DBFILE_MEMORY = ":memory:";
......@@ -101,6 +103,16 @@ TEST_F(SQLite3Conn, noClass) {
EXPECT_FALSE(conn->getZone(Name("example.com")).first);
}
TEST(SQLite3Open, getDBNameExample2) {
SQLite3Connection conn(SQLITE_DBFILE_EXAMPLE2, RRClass::IN());
EXPECT_EQ(SQLITE_DBNAME_EXAMPLE2, conn.getDBName());
}
TEST(SQLite3Open, getDBNameExampleROOT) {
SQLite3Connection conn(SQLITE_DBFILE_EXAMPLE_ROOT, RRClass::IN());
EXPECT_EQ(SQLITE_DBNAME_EXAMPLE_ROOT, conn.getDBName());
}
// Simple function to cound the number of records for
// any name
void
......
......@@ -103,6 +103,11 @@ public:
return (extension_);
}
/// \return Name + extension of Given File Name
std::string nameAndExtension() const {
return (name_ + extension_);
}
/// \brief Expand Name with Default
///
/// A default file specified is supplied and used to fill in any missing
......
......@@ -51,42 +51,49 @@ TEST_F(FilenameTest, Components) {
EXPECT_EQ("/alpha/beta/", fname.directory());
EXPECT_EQ("gamma", fname.name());
EXPECT_EQ(".delta", fname.extension());
EXPECT_EQ("gamma.delta", fname.nameAndExtension());
// Directory only
fname.setName("/gamma/delta/");
EXPECT_EQ("/gamma/delta/", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("", fname.nameAndExtension());
// Filename only
fname.setName("epsilon");
EXPECT_EQ("", fname.directory());
EXPECT_EQ("epsilon", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("epsilon", fname.nameAndExtension());
// Extension only
fname.setName(".zeta");
EXPECT_EQ("", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ(".zeta", fname.extension());
EXPECT_EQ(".zeta", fname.nameAndExtension());
// Missing directory
fname.setName("eta.theta");
EXPECT_EQ("", fname.directory());
EXPECT_EQ("eta", fname.name());
EXPECT_EQ(".theta", fname.extension());
EXPECT_EQ("eta.theta", fname.nameAndExtension());
// Missing filename
fname.setName("/iota/.kappa");
EXPECT_EQ("/iota/", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ(".kappa", fname.extension());
EXPECT_EQ(".kappa", fname.nameAndExtension());
// Missing extension
fname.setName("lambda/mu/nu");
EXPECT_EQ("lambda/mu/", fname.directory());
EXPECT_EQ("nu", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("nu", fname.nameAndExtension());
// Check that the decomposition can occur in the presence of leading and
// trailing spaces
......@@ -94,18 +101,21 @@ TEST_F(FilenameTest, Components) {
EXPECT_EQ("lambda/mu/", fname.directory());
EXPECT_EQ("nu", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("nu", fname.nameAndExtension());
// Empty string
fname.setName("");
EXPECT_EQ("", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("", fname.nameAndExtension());
// ... and just spaces
fname.setName(" ");
EXPECT_EQ("", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("", fname.nameAndExtension());
// Check corner cases - where separators are present, but strings are
// absent.
......@@ -113,16 +123,19 @@ TEST_F(FilenameTest, Components) {
EXPECT_EQ("/", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ("", fname.extension());
EXPECT_EQ("", fname.nameAndExtension());
fname.setName(".");
EXPECT_EQ("", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ(".", fname.extension());
EXPECT_EQ(".", fname.nameAndExtension());
fname.setName("/.");
EXPECT_EQ("/", fname.directory());
EXPECT_EQ("", fname.name());
EXPECT_EQ(".", fname.extension());
EXPECT_EQ(".", fname.nameAndExtension());
// Note that the space is a valid filename here; only leading and trailing
// spaces should be trimmed.
......@@ -130,11 +143,13 @@ TEST_F(FilenameTest, Components) {
EXPECT_EQ("/", fname.directory());
EXPECT_EQ(" ", fname.name());
EXPECT_EQ(".", fname.extension());
EXPECT_EQ(".", fname.nameAndExtension());
fname.setName(" / . ");
EXPECT_EQ("/", fname.directory());
EXPECT_EQ(" ", fname.name());
EXPECT_EQ(".", fname.extension());
EXPECT_EQ(".", fname.nameAndExtension());
}
// Check that the expansion with a default works.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment