Commit 61dbb4bc authored by Michael Davis's avatar Michael Davis
Browse files

[catalogue] Separates create temporary table code from GetArchiveFilesItor

parent dc25b0bc
......@@ -56,6 +56,34 @@ MysqlCatalogue::MysqlCatalogue(
MysqlCatalogue::~MysqlCatalogue() {
}
//------------------------------------------------------------------------------
// createAndPopulateTempTableFxid
//------------------------------------------------------------------------------
std::string MysqlCatalogue::createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const {
const std::string tempTableName = "ORA$PTT_DISK_FXIDS";
try {
if(tapeFileSearchCriteria.diskFileIds) {
// ON COMMIT PRESERVE DEFINITION preserves the table until the end of the session
std::string sql = "CREATE PRIVATE TEMPORARY TABLE " + tempTableName +
"(DISK_FILE_ID VARCHAR2(100)) ON COMMIT PRESERVE DEFINITION";
conn.executeNonQuery(sql);
sql = "INSERT INTO " + tempTableName + " VALUES(:DISK_FILE_ID)";
auto stmt = conn.createStmt(sql);
for(auto &diskFileId : tapeFileSearchCriteria.diskFileIds.value()) {
stmt.bindString(":DISK_FILE_ID", diskFileId);
stmt.executeNonQuery();
}
}
return tempTableName;
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
throw;
}
}
//------------------------------------------------------------------------------
// getNextArchiveFileId
//------------------------------------------------------------------------------
......
......@@ -57,6 +57,15 @@ public:
protected:
/**
* Creates a temporary table from the list of disk file IDs provided in the search criteria.
*
* @param conn The database connection.
* @param tapeFileSearchCriteria Search criteria containing a list of disk file IDs (fxid).
* @return Name of the temporary table
*/
std::string createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const override;
/**
* Returns a unique archive ID that can be used by a new archive file within
* the catalogue.
......
......@@ -151,6 +151,34 @@ OracleCatalogue::OracleCatalogue(
OracleCatalogue::~OracleCatalogue() {
}
//------------------------------------------------------------------------------
// createAndPopulateTempTableFxid
//------------------------------------------------------------------------------
std::string OracleCatalogue::createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const {
const std::string tempTableName = "ORA$PTT_DISK_FXIDS";
try {
if(tapeFileSearchCriteria.diskFileIds) {
// ON COMMIT PRESERVE DEFINITION preserves the table until the end of the session
std::string sql = "CREATE PRIVATE TEMPORARY TABLE " + tempTableName +
"(DISK_FILE_ID VARCHAR2(100)) ON COMMIT PRESERVE DEFINITION";
conn.executeNonQuery(sql);
sql = "INSERT INTO " + tempTableName + " VALUES(:DISK_FILE_ID)";
auto stmt = conn.createStmt(sql);
for(auto &diskFileId : tapeFileSearchCriteria.diskFileIds.value()) {
stmt.bindString(":DISK_FILE_ID", diskFileId);
stmt.executeNonQuery();
}
}
return tempTableName;
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
throw;
}
}
//------------------------------------------------------------------------------
// getNextArchiveFileId
//------------------------------------------------------------------------------
......
......@@ -57,6 +57,15 @@ public:
*/
~OracleCatalogue() override;
/**
* Creates a temporary table from the list of disk file IDs provided in the search criteria.
*
* @param conn The database connection.
* @param tapeFileSearchCriteria Search criteria containing a list of disk file IDs (fxid).
* @return Name of the temporary table
*/
std::string createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const override;
/**
* Returns a unique archive ID that can be used by a new archive file within
* the catalogue.
......
......@@ -149,6 +149,34 @@ PostgresCatalogue::PostgresCatalogue(
PostgresCatalogue::~PostgresCatalogue() {
}
//------------------------------------------------------------------------------
// createAndPopulateTempTableFxid
//------------------------------------------------------------------------------
std::string PostgresCatalogue::createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const {
const std::string tempTableName = "ORA$PTT_DISK_FXIDS";
try {
if(tapeFileSearchCriteria.diskFileIds) {
// ON COMMIT PRESERVE DEFINITION preserves the table until the end of the session
std::string sql = "CREATE PRIVATE TEMPORARY TABLE " + tempTableName +
"(DISK_FILE_ID VARCHAR2(100)) ON COMMIT PRESERVE DEFINITION";
conn.executeNonQuery(sql);
sql = "INSERT INTO " + tempTableName + " VALUES(:DISK_FILE_ID)";
auto stmt = conn.createStmt(sql);
for(auto &diskFileId : tapeFileSearchCriteria.diskFileIds.value()) {
stmt.bindString(":DISK_FILE_ID", diskFileId);
stmt.executeNonQuery();
}
}
return tempTableName;
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
throw;
}
}
//------------------------------------------------------------------------------
// getNextArchiveFileId
//------------------------------------------------------------------------------
......
......@@ -84,6 +84,15 @@ public:
*/
void filesWrittenToTape(const std::set<TapeItemWrittenPointer> &events) override;
/**
* Creates a temporary table from the list of disk file IDs provided in the search criteria.
*
* @param conn The database connection.
* @param tapeFileSearchCriteria Search criteria containing a list of disk file IDs (fxid).
* @return Name of the temporary table
*/
std::string createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const override;
/**
* Returns a unique archive ID that can be used by a new archive file within
* the catalogue.
......
......@@ -6541,7 +6541,11 @@ ArchiveFileItor RdbmsCatalogue::getArchiveFilesItor(const TapeFileSearchCriteria
checkTapeFileSearchCriteria(searchCriteria);
try {
auto impl = new RdbmsCatalogueGetArchiveFilesItor(m_log, m_archiveFileListingConnPool, searchCriteria);
// Create a connection to populate the temporary table (specialised by database type)
std::unique_ptr<rdbms::Conn> conn_p(new rdbms::Conn(m_archiveFileListingConnPool.getConn()));
auto tempDiskFxidsTableName = createAndPopulateTempTableFxid(*conn_p, searchCriteria);
// Pass ownership of the connection to the Iterator object
auto impl = new RdbmsCatalogueGetArchiveFilesItor(m_log, std::move(*(conn_p.release())), searchCriteria, tempDiskFxidsTableName);
return ArchiveFileItor(impl);
} catch(exception::UserError &) {
throw;
......
......@@ -1519,6 +1519,15 @@ protected:
const std::string &requesterName,
const std::string &requesterGroupName) const;
/**
* Creates a temporary table from the list of disk file IDs provided in the search criteria.
*
* @param conn The database connection.
* @param tapeFileSearchCriteria Search criteria containing a list of disk file IDs (fxid).
* @return Name of the temporary table
*/
virtual std::string createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const = 0;
/**
* Returns a unique archive ID that can be used by a new archive file within
* the catalogue.
......
......@@ -78,18 +78,17 @@ namespace {
//------------------------------------------------------------------------------
RdbmsCatalogueGetArchiveFilesItor::RdbmsCatalogueGetArchiveFilesItor(
log::Logger &log,
rdbms::ConnPool &connPool,
const TapeFileSearchCriteria &searchCriteria):
rdbms::Conn &&conn,
const TapeFileSearchCriteria &searchCriteria,
const std::string &tempDiskFxidsTableName) :
m_log(log),
m_connPool(connPool),
m_searchCriteria(searchCriteria),
m_rsetIsEmpty(true),
m_hasMoreHasBeenCalled(false),
m_conn(std::move(conn)),
m_archiveFileBuilder(log)
{
try {
m_conn = connPool.getConn();
std::string sql =
"SELECT "
"ARCHIVE_FILE.ARCHIVE_FILE_ID AS ARCHIVE_FILE_ID,"
......@@ -153,23 +152,8 @@ RdbmsCatalogueGetArchiveFilesItor::RdbmsCatalogueGetArchiveFilesItor(
}
if(searchCriteria.diskFileIds) {
if(addedAWhereConstraint) sql += " AND ";
sql += "ARCHIVE_FILE.DISK_FILE_ID IN (SELECT DISK_FILE_ID FROM ORA$PTT_INSERT_DISK_FIDS)";
sql += "ARCHIVE_FILE.DISK_FILE_ID IN (SELECT DISK_FILE_ID FROM " + tempDiskFxidsTableName + ")";
addedAWhereConstraint = true;
// Create and populate temporary table from fxid list
// ON COMMIT PRESERVE DEFINITION preserves the table until the end of the session
std::string sql_temp_table = "CREATE PRIVATE TEMPORARY TABLE"
" ORA$PTT_INSERT_DISK_FIDS(DISK_FILE_ID VARCHAR2(100))"
" ON COMMIT PRESERVE DEFINITION";
m_stmt = m_conn.createStmt(sql_temp_table);
m_stmt.executeNonQuery();
std::string sql_insert_fid = "INSERT INTO ORA$PTT_INSERT_DISK_FIDS VALUES(:DISK_FILE_ID)";
for(auto &diskFileId : searchCriteria.diskFileIds.value()) {
m_stmt = m_conn.createStmt(sql_insert_fid);
m_stmt.bindString(":DISK_FILE_ID", diskFileId);
m_stmt.executeNonQuery();
}
}
if(hideSuperseded) {
if(addedAWhereConstraint) sql += " AND ";
......@@ -177,8 +161,7 @@ RdbmsCatalogueGetArchiveFilesItor::RdbmsCatalogueGetArchiveFilesItor(
addedAWhereConstraint = true;
}
// Order by FSEQ if we are listing the contents of a tape, else order by
// archive file ID
// Order by FSEQ if we are listing the contents of a tape, else order by archive file ID
if(searchCriteria.vid) {
sql += " ORDER BY FSEQ";
} else {
......@@ -196,12 +179,16 @@ RdbmsCatalogueGetArchiveFilesItor::RdbmsCatalogueGetArchiveFilesItor(
m_stmt.bindString(":VID", searchCriteria.vid.value());
}
m_rset = m_stmt.executeQuery();
{
log::LogContext lc(m_log);
lc.log(log::INFO, "RdbmsCatalogueGetArchiveFilesItor - immediately after m_stmt.executeQuery()");
}
// Clean up temporary table
if(searchCriteria.diskFileIds) {
m_conn.executeNonQuery("DROP TABLE " + tempDiskFxidsTableName);
}
m_rsetIsEmpty = !m_rset.next();
} catch(exception::UserError &) {
throw;
......
......@@ -40,14 +40,15 @@ public:
* Constructor.
*
* @param log Object representing the API to the CTA logging system.
* @param connPool The database connection pool.
* @param conn The database connection.
* @param searchCriteria The search criteria to be used when listing archive
* files.
*/
RdbmsCatalogueGetArchiveFilesItor(
log::Logger &log,
rdbms::ConnPool &connPool,
const TapeFileSearchCriteria &searchCriteria);
rdbms::Conn &&conn,
const TapeFileSearchCriteria &searchCriteria,
const std::string &tempDiskFxidsTableName);
/**
* Destructor.
......@@ -71,11 +72,6 @@ private:
*/
log::Logger &m_log;
/**
* The database connection pool.
*/
rdbms::ConnPool &m_connPool;
/**
* The search criteria to be used when listing archive files.
*/
......
......@@ -193,6 +193,33 @@ void SqliteCatalogue::DO_NOT_USE_deleteArchiveFile_DO_NOT_USE(const std::string
}
}
//------------------------------------------------------------------------------
// createAndPopulateTempTableFxid
//------------------------------------------------------------------------------
std::string SqliteCatalogue::createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const {
const std::string tempTableName = "TEMP.DISK_FXIDS";
try {
if(tapeFileSearchCriteria.diskFileIds) {
// ON COMMIT PRESERVE DEFINITION preserves the table until the end of the session
std::string sql = "CREATE TEMPORARY TABLE " + tempTableName + "(DISK_FILE_ID TEXT)";
conn.executeNonQuery(sql);
sql = "INSERT INTO " + tempTableName + " VALUES(:DISK_FILE_ID)";
auto stmt = conn.createStmt(sql);
for(auto &diskFileId : tapeFileSearchCriteria.diskFileIds.value()) {
stmt.bindString(":DISK_FILE_ID", diskFileId);
stmt.executeNonQuery();
}
}
return tempTableName;
} catch(exception::Exception &ex) {
ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
throw;
}
}
//------------------------------------------------------------------------------
// getNextArchiveFileId
//------------------------------------------------------------------------------
......
......@@ -83,6 +83,15 @@ public:
protected:
/**
* Creates a temporary table from the list of disk file IDs provided in the search criteria.
*
* @param conn The database connection.
* @param tapeFileSearchCriteria Search criteria containing a list of disk file IDs (fxid).
* @return Name of the temporary table
*/
std::string createAndPopulateTempTableFxid(rdbms::Conn &conn, const TapeFileSearchCriteria &tapeFileSearchCriteria) const override;
/**
* Returns a unique archive ID that can be used by a new archive file within
* the catalogue.
......
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