Commit 017c90de authored by Cedric CAFFY's avatar Cedric CAFFY
Browse files

cta-catalogue-schema-verify use only 1 connection per database

parent 68d58713
......@@ -21,45 +21,72 @@
namespace cta {
namespace catalogue {
CatalogueMetadataGetter::CatalogueMetadataGetter(cta::rdbms::ConnPool & connPool):m_connPool(connPool){}
CatalogueMetadataGetter::CatalogueMetadataGetter(cta::rdbms::Conn& conn):m_conn(conn){}
std::string CatalogueMetadataGetter::getCatalogueVersion(){
std::string schemaVersion;
const char *const sql =
"SELECT "
"CTA_CATALOGUE.SCHEMA_VERSION_MAJOR AS SCHEMA_VERSION_MAJOR,"
"CTA_CATALOGUE.SCHEMA_VERSION_MINOR AS SCHEMA_VERSION_MINOR "
"FROM "
"CTA_CATALOGUE";
auto stmt = m_conn.createStmt(sql);
auto rset = stmt.executeQuery();
if(rset.next()) {
schemaVersion += std::to_string(rset.columnUint64("SCHEMA_VERSION_MAJOR"));
schemaVersion += ".";
schemaVersion += std::to_string(rset.columnUint64("SCHEMA_VERSION_MINOR"));
return schemaVersion;
} else {
throw exception::Exception("SCHEMA_VERSION_MAJOR,SCHEMA_VERSION_MINOR not found in the CTA_CATALOGUE");
}
}
CatalogueMetadataGetter::~CatalogueMetadataGetter() {}
SQLiteCatalogueMetadataGetter::SQLiteCatalogueMetadataGetter(cta::rdbms::ConnPool & connPool):CatalogueMetadataGetter(connPool){}
SQLiteCatalogueMetadataGetter::SQLiteCatalogueMetadataGetter(cta::rdbms::Conn & conn):CatalogueMetadataGetter(conn){}
SQLiteCatalogueMetadataGetter::~SQLiteCatalogueMetadataGetter(){}
std::list<std::string> SQLiteCatalogueMetadataGetter::getIndexNames() {
rdbms::Conn connection = m_connPool.getConn();
std::list<std::string> indexNames = connection.getIndexNames();
connection.closeUnderlyingStmtsAndConn();
std::list<std::string> indexNames = m_conn.getIndexNames();
indexNames.remove_if([](std::string& indexName){
return ((indexName.find("sqlite_autoindex") != std::string::npos));
});
return indexNames;
}
OracleCatalogueMetadataGetter::OracleCatalogueMetadataGetter(cta::rdbms::ConnPool & connPool):CatalogueMetadataGetter(connPool){}
std::list<std::string> SQLiteCatalogueMetadataGetter::getTableNames(){
return std::list<std::string>();
}
OracleCatalogueMetadataGetter::OracleCatalogueMetadataGetter(cta::rdbms::Conn & conn):CatalogueMetadataGetter(conn){}
OracleCatalogueMetadataGetter::~OracleCatalogueMetadataGetter(){}
std::list<std::string> OracleCatalogueMetadataGetter::getIndexNames() {
rdbms::Conn connection = m_connPool.getConn();
std::list<std::string> indexNames = connection.getIndexNames();
connection.closeUnderlyingStmtsAndConn();
std::list<std::string> indexNames = m_conn.getIndexNames();
indexNames.remove_if([](std::string& indexName){
return ((indexName.find("_UN") != std::string::npos) || (indexName.find("PK") != std::string::npos) || (indexName.find("_LLN") != std::string::npos));
});
return indexNames;
}
std::list<std::string> OracleCatalogueMetadataGetter::getTableNames() {
return std::list<std::string>();
}
CatalogueMetadataGetter * CatalogueMetadataGetterFactory::create(const rdbms::Login::DbType dbType, cta::rdbms::ConnPool & connPool) {
CatalogueMetadataGetter * CatalogueMetadataGetterFactory::create(const rdbms::Login::DbType dbType, cta::rdbms::Conn & conn) {
typedef rdbms::Login::DbType DbType;
switch(dbType){
case DbType::DBTYPE_IN_MEMORY:
case DbType::DBTYPE_SQLITE:
return new SQLiteCatalogueMetadataGetter(connPool);
return new SQLiteCatalogueMetadataGetter(conn);
case DbType::DBTYPE_ORACLE:
return new OracleCatalogueMetadataGetter(connPool);
return new OracleCatalogueMetadataGetter(conn);
default:
return nullptr;
}
......
......@@ -29,30 +29,34 @@ namespace catalogue {
class CatalogueMetadataGetter {
public:
protected:
rdbms::ConnPool& m_connPool;
rdbms::Conn& m_conn;
public:
CatalogueMetadataGetter(cta::rdbms::ConnPool & connPool);
CatalogueMetadataGetter(cta::rdbms::Conn & conn);
virtual ~CatalogueMetadataGetter();
std::string getCatalogueVersion();
virtual std::list<std::string> getIndexNames() = 0;
virtual std::list<std::string> getTableNames() = 0;
};
class SQLiteCatalogueMetadataGetter: public CatalogueMetadataGetter{
public:
SQLiteCatalogueMetadataGetter(cta::rdbms::ConnPool & connPool);
SQLiteCatalogueMetadataGetter(cta::rdbms::Conn & conn);
virtual ~SQLiteCatalogueMetadataGetter();
std::list<std::string> getIndexNames() override;
std::list<std::string> getTableNames() override;
};
class OracleCatalogueMetadataGetter: public CatalogueMetadataGetter{
public:
OracleCatalogueMetadataGetter(cta::rdbms::ConnPool & connPool);
OracleCatalogueMetadataGetter(cta::rdbms::Conn & conn);
virtual ~OracleCatalogueMetadataGetter();
std::list<std::string> getIndexNames() override;
std::list<std::string> getTableNames() override;
};
class CatalogueMetadataGetterFactory {
public:
static CatalogueMetadataGetter* create(const rdbms::Login::DbType dbType, cta::rdbms::ConnPool & connPool);
static CatalogueMetadataGetter* create(const rdbms::Login::DbType dbType, cta::rdbms::Conn & conn);
};
}}
\ No newline at end of file
......@@ -25,13 +25,13 @@
namespace cta {
namespace catalogue {
SQLiteSchemaComparer::SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, const std::string &schemaVersion, rdbms::ConnPool &catalogueConnPool): SchemaComparer(catalogueDbType,schemaVersion,catalogueConnPool) {
SQLiteSchemaComparer::SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, rdbms::Conn &catalogueConn): SchemaComparer(catalogueDbType,catalogueConn) {
log::DummyLogger dl("dummy","dummy");
auto login = rdbms::Login::parseString("in_memory");
m_sqliteConnPool.reset(new rdbms::ConnPool(login,1));
m_catalogueMetadataGetter.reset(CatalogueMetadataGetterFactory::create(catalogueDbType,catalogueConnPool));
m_sqliteSchemaMetadataGetter.reset(new SQLiteCatalogueMetadataGetter(*m_sqliteConnPool));
rdbms::ConnPool connPool(login,1);
rdbms::Conn conn = connPool.getConn();
m_sqliteConn = std::move(conn);
m_sqliteSchemaMetadataGetter.reset(new SQLiteCatalogueMetadataGetter(m_sqliteConn));
}
SQLiteSchemaComparer::~SQLiteSchemaComparer() {
......@@ -41,15 +41,26 @@ SchemaComparerResult SQLiteSchemaComparer::compare(){
SchemaComparerResult res;
insertSchemaInSQLite();
res += compareIndexes();
res += compareTables();
return res;
}
SchemaComparerResult SQLiteSchemaComparer::compareTables(){
std::list<std::string> catalogueTables = m_catalogueMetadataGetter->getTableNames();
std::list<std::string> schemaTables = m_sqliteSchemaMetadataGetter->getTableNames();
std::cout<<"catalogue tables = "<<std::endl;
for(auto& catalogueTable: catalogueTables){
std::cout<<catalogueTable<<std::endl;
}
std::cout<<"schema tables = "<<std::endl;
for(auto& schemaTable: schemaTables){
std::cout<<schemaTable<<std::endl;
}
return SchemaComparerResult();
}
void SQLiteSchemaComparer::insertSchemaInSQLite() {
cta::catalogue::SQLiteSchemaInserter schemaInserter(m_schemaVersion,m_dbType,"/home/cedric/CTA/catalogue/",*m_sqliteConnPool);
cta::catalogue::SQLiteSchemaInserter schemaInserter(m_catalogueSchemaVersion,m_dbType,"/home/cedric/CTA/catalogue/",m_sqliteConn);
schemaInserter.insert();
}
......
......@@ -18,15 +18,13 @@
#pragma once
#include "SchemaComparer.hpp"
#include "SchemaCreatingSqliteCatalogue.hpp"
#include "InMemoryCatalogue.hpp"
namespace cta {
namespace catalogue {
class SQLiteSchemaComparer: public SchemaComparer {
public:
SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, const std::string &schemaVersion,rdbms::ConnPool &catalogueConnPool);
SQLiteSchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType, rdbms::Conn &catalogueConnPool);
SchemaComparerResult compare() override;
virtual ~SQLiteSchemaComparer();
......@@ -34,7 +32,7 @@ private:
void insertSchemaInSQLite();
SchemaComparerResult compareIndexes();
SchemaComparerResult compareTables() override;
std::unique_ptr<rdbms::ConnPool> m_sqliteConnPool;
rdbms::Conn m_sqliteConn;
std::unique_ptr<SQLiteCatalogueMetadataGetter> m_sqliteSchemaMetadataGetter;
};
......
......@@ -27,7 +27,7 @@
namespace cta {
namespace catalogue {
SQLiteSchemaInserter::SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allVersionsSchemaDirectory,rdbms::ConnPool &sqliteConnPool):m_schemaVersion(schemaVersion),m_dbType(catalogueDbType),m_allVersionSchemaDirectory(allVersionsSchemaDirectory), m_sqliteCatalogueConnPool(sqliteConnPool){}
SQLiteSchemaInserter::SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allVersionsSchemaDirectory,rdbms::Conn &sqliteConn):m_schemaVersion(schemaVersion),m_dbType(catalogueDbType),m_allVersionSchemaDirectory(allVersionsSchemaDirectory), m_sqliteCatalogueConn(sqliteConn){}
SQLiteSchemaInserter::~SQLiteSchemaInserter() {}
......@@ -43,9 +43,8 @@ void SQLiteSchemaInserter::insert() {
}
void SQLiteSchemaInserter::executeStatements(const std::list<std::string> & stmts){
rdbms::Conn conn = m_sqliteCatalogueConnPool.getConn();
for(auto& sqliteStatement: stmts){
auto stmt = conn.createStmt(sqliteStatement);
auto stmt = m_sqliteCatalogueConn.createStmt(sqliteStatement);
stmt.executeNonQuery();
}
}
......
......@@ -25,7 +25,7 @@ namespace catalogue {
class SQLiteSchemaInserter {
public:
SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allVersionsSchemaDirectory,rdbms::ConnPool &sqliteConnPool);
SQLiteSchemaInserter(const std::string & schemaVersion, const cta::rdbms::Login::DbType &catalogueDbType, const std::string &allVersionsSchemaDirectory,rdbms::Conn &sqliteConn);
void insert();
virtual ~SQLiteSchemaInserter();
private:
......@@ -34,7 +34,7 @@ namespace catalogue {
std::string m_schemaVersion;
cta::rdbms::Login::DbType m_dbType;
std::string m_allVersionSchemaDirectory;
cta::rdbms::ConnPool & m_sqliteCatalogueConnPool;
cta::rdbms::Conn & m_sqliteCatalogueConn;
std::string readSchemaFromFile();
std::list<std::string> getAllStatementsFromSchema(const std::string &schema);
std::string getDatabaseType();
......
......@@ -26,12 +26,13 @@
namespace cta {
namespace catalogue {
SchemaComparer::SchemaComparer(const cta::rdbms::Login::DbType &dbType, const std::string &schemaVersion,rdbms::ConnPool &connPool): m_dbType(dbType),m_schemaVersion(schemaVersion),m_catalogueConnPool(connPool){
m_catalogueMetadataGetter.reset(CatalogueMetadataGetterFactory::create(dbType,m_catalogueConnPool));
SchemaComparer::SchemaComparer(const cta::rdbms::Login::DbType &dbType, rdbms::Conn &conn): m_dbType(dbType),m_catalogueConn(conn){
m_catalogueMetadataGetter.reset(CatalogueMetadataGetterFactory::create(dbType,m_catalogueConn));
m_catalogueSchemaVersion = m_catalogueMetadataGetter->getCatalogueVersion();
}
std::string SchemaComparer::getCatalogueVersion(){
return m_schemaVersion;
return m_catalogueSchemaVersion;
}
SchemaComparer::~SchemaComparer() {
......
......@@ -33,15 +33,16 @@ namespace catalogue {
class SchemaComparer {
public:
SchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType,const std::string &schemaVersion,cta::rdbms::ConnPool &connPool);
SchemaComparer(const cta::rdbms::Login::DbType &catalogueDbType,cta::rdbms::Conn &conn);
virtual ~SchemaComparer();
virtual SchemaComparerResult compare() = 0;
std::string getCatalogueVersion();
protected:
const cta::rdbms::Login::DbType &m_dbType;
const std::string &m_schemaVersion;
cta::rdbms::ConnPool &m_catalogueConnPool;
std::string m_catalogueSchemaVersion;
cta::rdbms::Conn &m_catalogueConn;
std::unique_ptr<cta::catalogue::CatalogueMetadataGetter> m_catalogueMetadataGetter;
private:
virtual SchemaComparerResult compareTables() = 0;
virtual SchemaComparerResult compareIndexes() = 0;
......
......@@ -63,7 +63,7 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
}
auto login = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
const uint64_t maxNbConns = 10;
const uint64_t maxNbConns = 1;
rdbms::ConnPool connPool(login, maxNbConns);
auto conn = connPool.getConn();
......@@ -74,22 +74,13 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
return 1;
}
log::DummyLogger dummyLog("dummy", "dummy");
const auto catalogueFactory = CatalogueFactoryFactory::create(dummyLog, login, maxNbConns, maxNbConns);
auto catalogue = catalogueFactory->create();
//Get the version of the catalogue
std::map<std::string, uint64_t> schemaVersion = catalogue->getSchemaVersion();
uint64_t schemaVersionMajor = schemaVersion.at("SCHEMA_VERSION_MAJOR");
uint64_t schemaVersionMinor = schemaVersion.at("SCHEMA_VERSION_MINOR");
std::string schemaVersionStr = std::to_string(schemaVersionMajor)+"."+std::to_string(schemaVersionMinor);
std::unique_ptr<cta::catalogue::SchemaComparer> schemaComparer;
schemaComparer.reset(new cta::catalogue::SQLiteSchemaComparer(login.dbType,schemaVersionStr,connPool));
schemaComparer.reset(new cta::catalogue::SQLiteSchemaComparer(login.dbType,conn));
cta::catalogue::SchemaComparerResult res = schemaComparer->compare();
std::cout << "Schema version : " << schemaComparer->getCatalogueVersion() << std::endl;
std::cout << "Status of the checking : " << cta::catalogue::SchemaComparerResult::StatusToString(res.getStatus()) << std::endl;
res.printDiffs();
schemaComparer.release();
/*std::unique_ptr<CatalogueSchema> schema;
switch(login.dbType) {
case rdbms::Login::DBTYPE_IN_MEMORY:
......@@ -122,9 +113,9 @@ int VerifySchemaCmd::exceptionThrowingMain(const int argc, char *const *const ar
}
std::cerr << "Checking schema version..." << std::endl;
//log::DummyLogger dummyLog("dummy", "dummy");
//const auto catalogueFactory = CatalogueFactoryFactory::create(dummyLog, login, maxNbConns, maxNbConns);
//const auto catalogue = catalogueFactory->create();
log::DummyLogger dummyLog("dummy", "dummy");
const auto catalogueFactory = CatalogueFactoryFactory::create(dummyLog, login, maxNbConns, maxNbConns);
const auto catalogue = catalogueFactory->create();
const auto schemaDbVersion = catalogue->getSchemaVersion();
const auto schemaVersion = schema->getSchemaVersion();
const VerifyStatus verifySchemaStatus = verifySchemaVersion(schemaVersion, schemaDbVersion);
......
Supports Markdown
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