Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:28

0001 #include "CondCore/CondDB/interface/RunInfoEditor.h"
0002 #include "CondCore/CondDB/interface/Utils.h"
0003 #include "SessionImpl.h"
0004 //
0005 
0006 namespace cond {
0007 
0008   namespace persistency {
0009 
0010     // implementation details. holds only data.
0011     class RunInfoEditorData {
0012     public:
0013       explicit RunInfoEditorData() : runBuffer(), updateBuffer() {}
0014       // update buffers
0015       std::vector<std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime> > runBuffer;
0016       std::vector<std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime> > updateBuffer;
0017     };
0018 
0019     RunInfoEditor::RunInfoEditor() : m_data(), m_session() {}
0020 
0021     RunInfoEditor::RunInfoEditor(const std::shared_ptr<SessionImpl>& session)
0022         : m_data(new RunInfoEditorData), m_session(session) {}
0023 
0024     RunInfoEditor::RunInfoEditor(const RunInfoEditor& rhs) : m_data(rhs.m_data), m_session(rhs.m_session) {}
0025 
0026     RunInfoEditor& RunInfoEditor::operator=(const RunInfoEditor& rhs) {
0027       m_data = rhs.m_data;
0028       m_session = rhs.m_session;
0029       return *this;
0030     }
0031 
0032     void RunInfoEditor::init() {
0033       if (m_data.get()) {
0034         checkTransaction("RunInfoEditor::init");
0035         if (!m_session->runInfoSchema().exists())
0036           m_session->runInfoSchema().create();
0037       }
0038     }
0039 
0040     cond::Time_t RunInfoEditor::getLastInserted() {
0041       if (m_data.get()) {
0042         checkTransaction("RunInfoEditor::getLastInserted");
0043         boost::posix_time::ptime start, end;
0044         return m_session->runInfoSchema().runInfoTable().getLastInserted(start, end);
0045       }
0046       return cond::time::MIN_VAL;
0047     }
0048 
0049     void RunInfoEditor::insert(cond::Time_t runNumber,
0050                                const boost::posix_time::ptime& start,
0051                                const boost::posix_time::ptime& end) {
0052       if (m_data.get())
0053         m_data->runBuffer.push_back(std::tie(runNumber, start, end));
0054     }
0055 
0056     void RunInfoEditor::insertNew(cond::Time_t runNumber,
0057                                   const boost::posix_time::ptime& start,
0058                                   const boost::posix_time::ptime& end) {
0059       if (m_data.get())
0060         m_data->updateBuffer.push_back(std::tie(runNumber, start, end));
0061     }
0062 
0063     size_t RunInfoEditor::flush() {
0064       size_t ret = 0;
0065       if (m_data.get()) {
0066         checkTransaction("RunInfoEditor::flush");
0067         m_session->runInfoSchema().runInfoTable().insert(m_data->runBuffer);
0068         ret += m_data->runBuffer.size();
0069         for (auto update : m_data->updateBuffer) {
0070           cond::Time_t newRun = std::get<0>(update);
0071           boost::posix_time::ptime& newRunStart = std::get<1>(update);
0072           boost::posix_time::ptime& newRunEnd = std::get<2>(update);
0073           boost::posix_time::ptime existingRunStart;
0074           boost::posix_time::ptime existingRunEnd;
0075           if (m_session->runInfoSchema().runInfoTable().select(newRun, existingRunStart, existingRunEnd)) {
0076             if (newRunStart != existingRunStart) {
0077               std::stringstream msg;
0078               msg << "Attempt to update start time of existing run " << newRun;
0079               throwException(msg.str(), "RunInfoEditor::flush");
0080             }
0081             if (existingRunEnd == newRunEnd) {
0082               // nothing to do
0083               continue;
0084             } else {
0085               if (existingRunEnd != existingRunStart) {
0086                 std::stringstream msg;
0087                 msg << "Attempt to update end time of existing run " << newRun;
0088                 throwException(msg.str(), "RunInfoEditor::flush");
0089               }
0090             }
0091             m_session->runInfoSchema().runInfoTable().updateEnd(newRun, newRunEnd);
0092           } else {
0093             m_session->runInfoSchema().runInfoTable().insertOne(newRun, newRunStart, newRunEnd);
0094           }
0095           ret++;
0096         }
0097         m_data->runBuffer.clear();
0098         m_data->updateBuffer.clear();
0099       }
0100       return ret;
0101     }
0102 
0103     void RunInfoEditor::checkTransaction(const std::string& ctx) {
0104       if (!m_session.get())
0105         throwException("The session is not active.", ctx);
0106       if (!m_session->isTransactionActive(false))
0107         throwException("The transaction is not active.", ctx);
0108     }
0109 
0110   }  // namespace persistency
0111 }  // namespace cond