Commit b2886872 authored by Daniele Kruse's avatar Daniele Kruse
Browse files

WIP Sqlite implementation of backend

parent 72dfbc0d
......@@ -58,7 +58,10 @@ set (MIDDLE_TIER_UNIT_TESTS_LIB_SRC_FILES
MockMiddleTierUserTest.cpp
MockTapeTableTest.cpp
MockTapePoolTableTest.cpp
SqliteMiddleTierAdminTest.cpp
UtilsTest.cpp)
add_library (ctamiddletierunittests SHARED
${MIDDLE_TIER_UNIT_TESTS_LIB_SRC_FILES})
target_link_libraries (ctamiddletierunittests ${SQLITE3_LIBRARY_RELEASE})
......@@ -45,12 +45,13 @@ void cta::SqliteDatabase::createMigrationRouteTable() {
char *zErrMsg = 0;
int rc = sqlite3_exec(m_dbHandle,
"CREATE TABLE MIGRATIONROUTE("
"STORAGECLASS_NAME TEXT PRIMARY KEY,"
"COPYNB INTEGER PRIMARY KEY,"
"STORAGECLASS_NAME TEXT,"
"COPYNB INTEGER,"
"TAPEPOOL_NAME TEXT,"
"UID INTEGER,"
"GID INTEGER,"
"COMMENT TEXT,"
"PRIMARY KEY (STORAGECLASS_NAME, COPYNB),"
"FOREIGN KEY(STORAGECLASS_NAME) REFERENCES STORAGECLASS(NAME),"
"FOREIGN KEY(TAPEPOOL_NAME) REFERENCES TAPEPOOL(NAME)"
");",
......@@ -108,6 +109,60 @@ void cta::SqliteDatabase::createTapePoolTable() {
}
}
//------------------------------------------------------------------------------
// createDirectoryTable
//------------------------------------------------------------------------------
void cta::SqliteDatabase::createDirectoryTable() {
char *zErrMsg = 0;
int rc = sqlite3_exec(m_dbHandle,
"CREATE TABLE DIRECTORY("
"PATH TEXT PRIMARY KEY,"
"STORAGECLASS_NAME TEXT,"
"UID INTEGER,"
"GID INTEGER,"
"MODE INTEGER,"
"FOREIGN KEY (STORAGECLASS_NAME) REFERENCES STORAGECLASS(NAME)"
");",
0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
message << "SQLite error: " << zErrMsg;
sqlite3_free(zErrMsg);
throw(Exception(message.str()));
}
rc = sqlite3_exec(m_dbHandle, "INSERT INTO TAPEPOOL VALUES('/','',0,0,0);", 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
message << "SQLite error: " << zErrMsg;
sqlite3_free(zErrMsg);
throw(Exception(message.str()));
}
}
//------------------------------------------------------------------------------
// createFileTable
//------------------------------------------------------------------------------
void cta::SqliteDatabase::createFileTable() {
char *zErrMsg = 0;
int rc = sqlite3_exec(m_dbHandle,
"CREATE TABLE FILE("
"PATH TEXT,"
"NAME TEXT,"
"UID INTEGER,"
"GID INTEGER,"
"MODE INTEGER,"
"PRIMARY KEY (PATH, NAME),"
"FOREIGN KEY (PATH) REFERENCES DIRECTORY(PATH)"
");",
0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
message << "SQLite error: " << zErrMsg;
sqlite3_free(zErrMsg);
throw(Exception(message.str()));
}
}
//------------------------------------------------------------------------------
// createSchema
//------------------------------------------------------------------------------
......@@ -115,6 +170,76 @@ void cta::SqliteDatabase::createSchema() {
createStorageClassTable();
createTapePoolTable();
createMigrationRouteTable();
createDirectoryTable();
createFileTable();
}
//------------------------------------------------------------------------------
// sanitizePathname
//------------------------------------------------------------------------------
std::string cta::SqliteDatabase::sanitizePathname(const std::string &pathname){
return pathname.substr(0, pathname.find_last_not_of('/')+1);
}
//------------------------------------------------------------------------------
// getPath
//------------------------------------------------------------------------------
std::string cta::SqliteDatabase::getPath(const std::string &pathname){
return pathname.substr(0, pathname.find_last_of('/'));
}
//------------------------------------------------------------------------------
// getName
//------------------------------------------------------------------------------
std::string cta::SqliteDatabase::getName(const std::string &pathname){
size_t last = pathname.find_last_of('/');
return pathname.substr(last+1, pathname.length()-last);
}
//------------------------------------------------------------------------------
// insertFile
//------------------------------------------------------------------------------
void cta::SqliteDatabase::insertFile(const SecurityIdentity &requester, const std::string &pathname, const uint16_t mode) {
std::string s_pathname = sanitizePathname(pathname);
std::string path = getPath(s_pathname);
std::string name = getName(s_pathname);
char *zErrMsg = 0;
std::ostringstream query;
query << "INSERT INTO FILE VALUES('" << path << "','" << name << "',"<< requester.user.getUid() << "," << requester.user.getGid() << "," << (int)mode << ");";
int rc = sqlite3_exec(m_dbHandle, query.str().c_str(), 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
message << "SQLite error: " << zErrMsg;
sqlite3_free(zErrMsg);
throw(Exception(message.str()));
}
}
//------------------------------------------------------------------------------
// getStorageClass
//------------------------------------------------------------------------------
std::string cta::SqliteDatabase::getStorageClass(const std::string &path){
return "";
}
//------------------------------------------------------------------------------
// insertDirectory
//------------------------------------------------------------------------------
void cta::SqliteDatabase::insertDirectory(const SecurityIdentity &requester, const std::string &pathname, const uint16_t mode) {
std::string s_pathname = sanitizePathname(pathname);
std::string path = getPath(s_pathname);
std::string name = getName(s_pathname);
std::string storageClass = getStorageClass(path);
char *zErrMsg = 0;
std::ostringstream query;
query << "INSERT INTO DIRECTORY VALUES('" << s_pathname << "','" << storageClass << "'," << requester.user.getUid() << "," << requester.user.getGid() << "," << (int)mode << ");";
int rc = sqlite3_exec(m_dbHandle, query.str().c_str(), 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
message << "SQLite error: " << zErrMsg;
sqlite3_free(zErrMsg);
throw(Exception(message.str()));
}
}
//------------------------------------------------------------------------------
......@@ -123,7 +248,7 @@ void cta::SqliteDatabase::createSchema() {
void cta::SqliteDatabase::insertTapePool(const SecurityIdentity &requester, const std::string &name, const uint16_t nbDrives, const uint32_t nbPartialTapes, const std::string &comment) {
char *zErrMsg = 0;
std::ostringstream query;
query << "INSERT INTO TAPEPOOL VALUES('" << name << "'," << nbDrives << "," << nbPartialTapes << "," << requester.user.getUid() << "," << requester.user.getGid() << ",'" << comment << "');";
query << "INSERT INTO TAPEPOOL VALUES('" << name << "'," << (int)nbDrives << "," << (int)nbPartialTapes << "," << requester.user.getUid() << "," << requester.user.getGid() << ",'" << comment << "');";
int rc = sqlite3_exec(m_dbHandle, query.str().c_str(), 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
......@@ -139,7 +264,7 @@ void cta::SqliteDatabase::insertTapePool(const SecurityIdentity &requester, cons
void cta::SqliteDatabase::insertStorageClass(const SecurityIdentity &requester, const std::string &name, const uint8_t nbCopies, const std::string &comment) {
char *zErrMsg = 0;
std::ostringstream query;
query << "INSERT INTO STORAGECLASS VALUES('" << name << "'," << nbCopies << "," << requester.user.getUid() << "," << requester.user.getGid() << ",'" << comment << "');";
query << "INSERT INTO STORAGECLASS VALUES('" << name << "'," << (int)nbCopies << "," << requester.user.getUid() << "," << requester.user.getGid() << ",'" << comment << "');";
int rc = sqlite3_exec(m_dbHandle, query.str().c_str(), 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
......@@ -155,7 +280,7 @@ void cta::SqliteDatabase::insertStorageClass(const SecurityIdentity &requester,
void cta::SqliteDatabase::insertMigrationRoute(const SecurityIdentity &requester, const std::string &storageClassName, const uint8_t copyNb, const std::string &tapePoolName, const std::string &comment) {
char *zErrMsg = 0;
std::ostringstream query;
query << "INSERT INTO MIGRATIONROUTE VALUES('" << storageClassName << "'," << copyNb << ",'" << tapePoolName << "'," << requester.user.getUid() << "," << requester.user.getGid() << ",'" << comment << "');";
query << "INSERT INTO MIGRATIONROUTE VALUES('" << storageClassName << "'," << (int)copyNb << ",'" << tapePoolName << "'," << requester.user.getUid() << "," << requester.user.getGid() << ",'" << comment << "');";
int rc = sqlite3_exec(m_dbHandle, query.str().c_str(), 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
......@@ -203,7 +328,7 @@ void cta::SqliteDatabase::deleteStorageClass(const SecurityIdentity &requester,
void cta::SqliteDatabase::deleteMigrationRoute(const SecurityIdentity &requester, const std::string &storageClassName, const uint8_t copyNb) {
char *zErrMsg = 0;
std::ostringstream query;
query << "DELETE FROM MIGRATIONROUTE WHERE NAME='" << storageClassName << "' AND COPYNB=" << copyNb << ";";
query << "DELETE FROM MIGRATIONROUTE WHERE NAME='" << storageClassName << "' AND COPYNB=" << (int)copyNb << ";";
int rc = sqlite3_exec(m_dbHandle, query.str().c_str(), 0, 0, &zErrMsg);
if(rc!=SQLITE_OK){
std::ostringstream message;
......@@ -220,7 +345,7 @@ std::list<cta::TapePool> cta::SqliteDatabase::selectAllTapePools(const SecurityI
char *zErrMsg = 0;
std::ostringstream query;
std::list<cta::TapePool> pools;
query << "SELECT * FROM TAPEPOOL;";
query << "SELECT * FROM TAPEPOOL ORDER BY NAME;";
sqlite3_stmt *statement;
int rc = sqlite3_prepare(m_dbHandle, query.str().c_str(), -1, &statement, 0 );
if(rc!=SQLITE_OK){
......@@ -249,7 +374,7 @@ std::list<cta::StorageClass> cta::SqliteDatabase::selectAllStorageClasses(const
char *zErrMsg = 0;
std::ostringstream query;
std::list<cta::StorageClass> classes;
query << "SELECT * FROM STORAGECLASS;";
query << "SELECT * FROM STORAGECLASS ORDER BY NAME;";
sqlite3_stmt *statement;
int rc = sqlite3_prepare(m_dbHandle, query.str().c_str(), -1, &statement, 0 );
if(rc!=SQLITE_OK){
......@@ -277,7 +402,7 @@ std::list<cta::MigrationRoute> cta::SqliteDatabase::selectAllMigrationRoutes(co
char *zErrMsg = 0;
std::ostringstream query;
std::list<cta::MigrationRoute> routes;
query << "SELECT * FROM MIGRATIONROUTE;";
query << "SELECT * FROM MIGRATIONROUTE ORDER BY STORAGECLASS_NAME, COPYNB;";
sqlite3_stmt *statement;
int rc = sqlite3_prepare(m_dbHandle, query.str().c_str(), -1, &statement, 0 );
if(rc!=SQLITE_OK){
......
......@@ -29,6 +29,28 @@ public:
* Destructor.
*/
~SqliteDatabase() throw();
void insertTapePool(const SecurityIdentity &requester, const std::string &name, const uint16_t nbDrives, const uint32_t nbPartialTapes, const std::string &comment);
void insertStorageClass(const SecurityIdentity &requester, const std::string &name, const uint8_t nbCopies, const std::string &comment);
void insertMigrationRoute(const SecurityIdentity &requester, const std::string &storageClassName, const uint8_t copyNb, const std::string &tapePoolName, const std::string &comment);
void insertFile(const SecurityIdentity &requester, const std::string &pathname, const uint16_t mode);
void insertDirectory(const SecurityIdentity &requester, const std::string &pathname, const uint16_t mode);
void deleteTapePool(const SecurityIdentity &requester, const std::string &name);
void deleteStorageClass(const SecurityIdentity &requester, const std::string &name);
void deleteMigrationRoute(const SecurityIdentity &requester, const std::string &storageClassName, const uint8_t copyNb);
std::list<cta::TapePool> selectAllTapePools(const SecurityIdentity &requester);
std::list<cta::StorageClass> selectAllStorageClasses(const SecurityIdentity &requester);
std::list<cta::MigrationRoute> selectAllMigrationRoutes(const SecurityIdentity &requester);
private:
......@@ -42,26 +64,20 @@ private:
void createStorageClassTable();
void createTapePoolTable();
void createSchema();
void insertTapePool(const SecurityIdentity &requester, const std::string &name, const uint16_t nbDrives, const uint32_t nbPartialTapes, const std::string &comment);
void createDirectoryTable();
void insertStorageClass(const SecurityIdentity &requester, const std::string &name, const uint8_t nbCopies, const std::string &comment);
void createFileTable();
void createSchema();
void insertMigrationRoute(const SecurityIdentity &requester, const std::string &storageClassName, const uint8_t copyNb, const std::string &tapePoolName, const std::string &comment);
std::string sanitizePathname(const std::string &pathname);
void deleteTapePool(const SecurityIdentity &requester, const std::string &name);
std::string getPath(const std::string &pathname);
void deleteStorageClass(const SecurityIdentity &requester, const std::string &name);
void deleteMigrationRoute(const SecurityIdentity &requester, const std::string &storageClassName, const uint8_t copyNb);
std::string getName(const std::string &pathname);
std::list<cta::TapePool> selectAllTapePools(const SecurityIdentity &requester);
std::list<cta::StorageClass> selectAllStorageClasses(const SecurityIdentity &requester);
std::list<cta::MigrationRoute> selectAllMigrationRoutes(const SecurityIdentity &requester);
std::string getStorageClass(const std::string &path);
}; // struct SqliteDatabase
......
......@@ -78,8 +78,7 @@ std::list<cta::AdminHost> cta::SqliteMiddleTierAdmin::getAdminHosts(
void cta::SqliteMiddleTierAdmin::createStorageClass(
const SecurityIdentity &requester, const std::string &name,
const uint8_t nbCopies, const std::string &comment) {
m_db.storageClasses.createStorageClass(name, nbCopies, requester.user,
comment);
m_sqlite_db.insertStorageClass(requester, name, nbCopies, comment);
}
//------------------------------------------------------------------------------
......@@ -87,20 +86,7 @@ void cta::SqliteMiddleTierAdmin::createStorageClass(
//------------------------------------------------------------------------------
void cta::SqliteMiddleTierAdmin::deleteStorageClass(const SecurityIdentity &requester,
const std::string &name) {
checkStorageClassIsNotInAMigrationRoute(name);
m_db.storageClasses.deleteStorageClass(name);
}
//------------------------------------------------------------------------------
// checkStorageClassIsNotInAMigrationRoute
//------------------------------------------------------------------------------
void cta::SqliteMiddleTierAdmin::checkStorageClassIsNotInAMigrationRoute(
const std::string &name) const {
if(m_db.migrationRoutes.storageClassIsInAMigrationRoute(name)) {
std::ostringstream message;
message << "The " << name << " storage class is in use";
throw Exception(message.str());
}
m_sqlite_db.deleteStorageClass(requester, name);
}
//------------------------------------------------------------------------------
......@@ -108,7 +94,7 @@ void cta::SqliteMiddleTierAdmin::checkStorageClassIsNotInAMigrationRoute(
//------------------------------------------------------------------------------
std::list<cta::StorageClass> cta::SqliteMiddleTierAdmin::getStorageClasses(
const SecurityIdentity &requester) const {
return m_db.storageClasses.getStorageClasses();
return m_sqlite_db.selectAllStorageClasses(requester);
}
//------------------------------------------------------------------------------
......@@ -120,8 +106,7 @@ void cta::SqliteMiddleTierAdmin::createTapePool(
const uint16_t nbDrives,
const uint32_t nbPartialTapes,
const std::string &comment) {
m_db.tapePools.createTapePool(requester, name, nbDrives, nbPartialTapes,
comment);
m_sqlite_db.insertTapePool(requester, name, nbDrives, nbPartialTapes, comment);
}
//------------------------------------------------------------------------------
......@@ -129,20 +114,7 @@ void cta::SqliteMiddleTierAdmin::createTapePool(
//------------------------------------------------------------------------------
void cta::SqliteMiddleTierAdmin::deleteTapePool(const SecurityIdentity &requester,
const std::string &name) {
checkTapePoolIsNotInUse(name);
m_db.tapePools.deleteTapePool(requester, name);
}
//------------------------------------------------------------------------------
// checkTapePoolIsNotInUse
//------------------------------------------------------------------------------
void cta::SqliteMiddleTierAdmin::checkTapePoolIsNotInUse(const std::string &name)
const {
if(m_db.migrationRoutes.tapePoolIsInAMigrationRoute(name)) {
std::ostringstream message;
message << "The " << name << " tape pool is in use";
throw Exception(message.str());
}
m_sqlite_db.deleteTapePool(requester, name);
}
//------------------------------------------------------------------------------
......@@ -150,7 +122,7 @@ void cta::SqliteMiddleTierAdmin::checkTapePoolIsNotInUse(const std::string &name
//------------------------------------------------------------------------------
std::list<cta::TapePool> cta::SqliteMiddleTierAdmin::getTapePools(
const SecurityIdentity &requester) const {
return m_db.tapePools.getTapePools(requester);
return m_sqlite_db.selectAllTapePools(requester);
}
//------------------------------------------------------------------------------
......@@ -162,12 +134,7 @@ void cta::SqliteMiddleTierAdmin::createMigrationRoute(
const uint8_t copyNb,
const std::string &tapePoolName,
const std::string &comment) {
return m_db.migrationRoutes.createMigrationRoute(
storageClassName,
copyNb,
tapePoolName,
requester.user,
comment);
return m_sqlite_db.insertMigrationRoute(requester, storageClassName, copyNb, tapePoolName, comment);
}
//------------------------------------------------------------------------------
......@@ -177,7 +144,7 @@ void cta::SqliteMiddleTierAdmin::deleteMigrationRoute(
const SecurityIdentity &requester,
const std::string &storageClassName,
const uint8_t copyNb) {
return m_db.migrationRoutes.deleteMigrationRoute(storageClassName, copyNb);
return m_sqlite_db.deleteMigrationRoute(requester, storageClassName, copyNb);
}
//------------------------------------------------------------------------------
......@@ -185,7 +152,7 @@ void cta::SqliteMiddleTierAdmin::deleteMigrationRoute(
//------------------------------------------------------------------------------
std::list<cta::MigrationRoute> cta::SqliteMiddleTierAdmin::getMigrationRoutes(
const SecurityIdentity &requester) const {
return m_db.migrationRoutes.getMigrationRoutes();
return m_sqlite_db.selectAllMigrationRoutes(requester);
}
//------------------------------------------------------------------------------
......
......@@ -304,14 +304,6 @@ protected:
*/
void checkTapePoolIsNotInUse(const std::string &name) const;
/**
* Throws an exception if the specified storage class is used in a migration
* route.
*
* @param name The name of the storage class.
*/
void checkStorageClassIsNotInAMigrationRoute(const std::string &name) const;
/**
* Returns true if the specified absolute path is that of an existing
* directory within the archive namepsace.
......
This diff is collapsed.
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