From 7076071f7eb72930bdf78168e3070ff7dd3528ce Mon Sep 17 00:00:00 2001 From: Steven Murray <steven.murray@cern.ch> Date: Wed, 3 Aug 2016 16:36:39 +0200 Subject: [PATCH] Implemented RdbmsCatalogue::modifyTapeLastReadLog() --- catalogue/CatalogueTest.cpp | 86 ++++++++++++++++++++++++++++++++++++ catalogue/RdbmsCatalogue.cpp | 30 ++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/catalogue/CatalogueTest.cpp b/catalogue/CatalogueTest.cpp index c71a5731d9..07a017ef66 100644 --- a/catalogue/CatalogueTest.cpp +++ b/catalogue/CatalogueTest.cpp @@ -2674,6 +2674,92 @@ TEST_P(cta_catalogue_CatalogueTest, modifyTapeLastWrittenLog) { } } +TEST_P(cta_catalogue_CatalogueTest, modifyTapeLastReadLog) { + using namespace cta; + + ASSERT_TRUE(m_catalogue->getTapes().empty()); + + const std::string vid = "vid"; + const std::string logicalLibraryName = "logical_library_name"; + const std::string tapePoolName = "tape_pool_name"; + const std::string encryptionKey = "encryption_key"; + const uint64_t capacityInBytes = (uint64_t)10 * 1000 * 1000 * 1000 * 1000; + const bool disabledValue = true; + const bool fullValue = false; + const std::string comment = "Create tape"; + + m_catalogue->createLogicalLibrary(m_cliSI, logicalLibraryName, "Create logical library"); + + m_catalogue->createTapePool(m_cliSI, tapePoolName, 2, true, "Create tape pool"); + + m_catalogue->createTape(m_cliSI, vid, logicalLibraryName, tapePoolName, encryptionKey, capacityInBytes, disabledValue, + fullValue, comment); + + { + const std::list<common::dataStructures::Tape> tapes = m_catalogue->getTapes(); + + ASSERT_EQ(1, tapes.size()); + + const common::dataStructures::Tape tape = tapes.front(); + ASSERT_EQ(vid, tape.vid); + ASSERT_EQ(logicalLibraryName, tape.logicalLibraryName); + ASSERT_EQ(tapePoolName, tape.tapePoolName); + ASSERT_EQ(encryptionKey, tape.encryptionKey); + ASSERT_EQ(capacityInBytes, tape.capacityInBytes); + ASSERT_TRUE(disabledValue == tape.disabled); + ASSERT_TRUE(fullValue == tape.full); + ASSERT_EQ(comment, tape.comment); + ASSERT_FALSE(tape.labelLog); + ASSERT_FALSE(tape.lastReadLog); + ASSERT_FALSE(tape.lastWriteLog); + + const common::dataStructures::EntryLog creationLog = tape.creationLog; + ASSERT_EQ(m_cliSI.username, creationLog.username); + ASSERT_EQ(m_cliSI.host, creationLog.host); + + const common::dataStructures::EntryLog lastModificationLog = tape.lastModificationLog; + ASSERT_EQ(creationLog, lastModificationLog); + } + + const std::string modifiedDrive = "modified_drive"; + m_catalogue->modifyTapeLastReadLog(m_cliSI, vid, modifiedDrive); + + { + const std::list<common::dataStructures::Tape> tapes = m_catalogue->getTapes(); + + ASSERT_EQ(1, tapes.size()); + + const common::dataStructures::Tape tape = tapes.front(); + ASSERT_EQ(vid, tape.vid); + ASSERT_EQ(logicalLibraryName, tape.logicalLibraryName); + ASSERT_EQ(tapePoolName, tape.tapePoolName); + ASSERT_EQ(encryptionKey, tape.encryptionKey); + ASSERT_EQ(capacityInBytes, tape.capacityInBytes); + ASSERT_TRUE(disabledValue == tape.disabled); + ASSERT_TRUE(fullValue == tape.full); + ASSERT_EQ(comment, tape.comment); + ASSERT_FALSE(tape.labelLog); + ASSERT_TRUE((bool)tape.lastReadLog); + ASSERT_EQ(modifiedDrive, tape.lastReadLog.value().drive); + ASSERT_FALSE(tape.lastWriteLog); + + const common::dataStructures::EntryLog creationLog = tape.creationLog; + ASSERT_EQ(m_cliSI.username, creationLog.username); + ASSERT_EQ(m_cliSI.host, creationLog.host); + } +} + +TEST_P(cta_catalogue_CatalogueTest, modifyTapeLastReadLog_nonExistentTape) { + using namespace cta; + + ASSERT_TRUE(m_catalogue->getTapes().empty()); + + const std::string vid = "vid"; + const std::string drive = "drive"; + + ASSERT_THROW(m_catalogue->modifyTapeLastReadLog(m_cliSI, vid, drive), exception::UserError); +} + TEST_P(cta_catalogue_CatalogueTest, modifyTapeLastWrittenLog_nonExistentTape) { using namespace cta; diff --git a/catalogue/RdbmsCatalogue.cpp b/catalogue/RdbmsCatalogue.cpp index b615ac53c1..59caf53f91 100644 --- a/catalogue/RdbmsCatalogue.cpp +++ b/catalogue/RdbmsCatalogue.cpp @@ -1985,7 +1985,35 @@ void RdbmsCatalogue::modifyTapeLastWrittenLog(const common::dataStructures::Secu //------------------------------------------------------------------------------ void RdbmsCatalogue::modifyTapeLastReadLog(const common::dataStructures::SecurityIdentity &cliIdentity, const std::string &vid, const std::string &drive) { - throw exception::Exception(std::string(__FUNCTION__) + " not implemented"); + try { + const time_t now = time(nullptr); + const char *const sql = + "UPDATE TAPE SET " + "LAST_READ_DRIVE = :LAST_READ_DRIVE," + "LAST_READ_TIME = :LAST_READ_TIME," + "LAST_UPDATE_USER_NAME = :LAST_UPDATE_USER_NAME," + "LAST_UPDATE_HOST_NAME = :LAST_UPDATE_HOST_NAME," + "LAST_UPDATE_TIME = :LAST_UPDATE_TIME " + "WHERE " + "VID = :VID"; + auto conn = m_connPool.getConn(); + auto stmt = conn->createStmt(sql, rdbms::Stmt::AutocommitMode::ON); + stmt->bindString(":LAST_READ_DRIVE", drive); + stmt->bindUint64(":LAST_READ_TIME", now); + stmt->bindString(":LAST_UPDATE_USER_NAME", cliIdentity.username); + stmt->bindString(":LAST_UPDATE_HOST_NAME", cliIdentity.host); + stmt->bindUint64(":LAST_UPDATE_TIME", now); + stmt->bindString(":VID", vid); + stmt->executeNonQuery(); + + if(0 == stmt->getNbAffectedRows()) { + throw exception::UserError(std::string("Cannot modify tape ") + vid + " because it does not exist"); + } + } catch(exception::UserError &) { + throw; + } catch (exception::Exception &ex) { + throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str()); + } } //------------------------------------------------------------------------------ -- GitLab