Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondCore/CondDB/interface/GTEditor.h"
0002 #include "SessionImpl.h"
0003 //
0004 
0005 namespace cond {
0006 
0007   namespace persistency {
0008 
0009     // GT data...
0010     class GTEditorData {
0011     public:
0012       explicit GTEditorData() : name(""), description(""), release(""), snapshotTime(), tagListBuffer() {}
0013       // GT data
0014       std::string name;
0015       cond::Time_t validity = cond::time::MAX_VAL;
0016       std::string description;
0017       std::string release;
0018       boost::posix_time::ptime snapshotTime;
0019       bool change = false;
0020       bool exists = false;
0021       // buffer for the GT tag map
0022       std::vector<std::tuple<std::string, std::string, std::string> > tagListBuffer;
0023     };
0024 
0025     GTEditor::GTEditor() : m_data(), m_session() {}
0026 
0027     GTEditor::GTEditor(const std::shared_ptr<SessionImpl>& session) : m_data(new GTEditorData), m_session(session) {}
0028 
0029     GTEditor::GTEditor(const std::shared_ptr<SessionImpl>& session, const std::string& gtName)
0030         : m_data(new GTEditorData), m_session(session) {
0031       m_data->name = gtName;
0032       m_data->change = true;
0033     }
0034 
0035     GTEditor::GTEditor(const GTEditor& rhs) : m_data(rhs.m_data), m_session(rhs.m_session) {}
0036 
0037     GTEditor& GTEditor::operator=(const GTEditor& rhs) {
0038       m_data = rhs.m_data;
0039       m_session = rhs.m_session;
0040       return *this;
0041     }
0042 
0043     void GTEditor::load(const std::string& gtName) {
0044       checkTransaction("GTEditor::load");
0045 
0046       // loads the current header data in memory
0047       if (!m_session->gtSchema().gtTable().select(
0048               gtName, m_data->validity, m_data->description, m_data->release, m_data->snapshotTime)) {
0049         throwException("Global Tag \"" + gtName + "\" has not been found in the database.", "GTEditor::load");
0050       }
0051       m_data->name = gtName;
0052       m_data->exists = true;
0053       m_data->change = false;
0054     }
0055 
0056     std::string GTEditor::name() const { return m_data.get() ? m_data->name : ""; }
0057 
0058     cond::Time_t GTEditor::validity() const { return m_data.get() ? m_data->validity : cond::time::MIN_VAL; }
0059 
0060     void GTEditor::setValidity(cond::Time_t validity) {
0061       if (m_data.get()) {
0062         m_data->validity = validity;
0063         m_data->change = true;
0064       }
0065     }
0066 
0067     std::string GTEditor::description() const { return m_data.get() ? m_data->description : ""; }
0068 
0069     void GTEditor::setDescription(const std::string& description) {
0070       if (m_data.get()) {
0071         m_data->description = description;
0072         m_data->change = true;
0073       }
0074     }
0075 
0076     std::string GTEditor::release() const { return m_data.get() ? m_data->release : ""; }
0077 
0078     void GTEditor::setRelease(const std::string& release) {
0079       if (m_data.get()) {
0080         m_data->release = release;
0081         m_data->change = true;
0082       }
0083     }
0084 
0085     boost::posix_time::ptime GTEditor::snapshotTime() const {
0086       return m_data.get() ? m_data->snapshotTime : boost::posix_time::ptime();
0087     }
0088 
0089     void GTEditor::setSnapshotTime(const boost::posix_time::ptime& snapshotTime) {
0090       if (m_data.get()) {
0091         m_data->snapshotTime = snapshotTime;
0092         m_data->change = true;
0093       }
0094     }
0095 
0096     void GTEditor::insert(const std::string& recordName, const std::string& tagName, bool checkType) {
0097       insert(recordName, "-", tagName, checkType);
0098     }
0099 
0100     void GTEditor::insert(const std::string& recordName,
0101                           const std::string& recordLabel,
0102                           const std::string& tagName,
0103                           bool) {
0104       if (m_data.get()) {
0105         // here the type check could be added
0106 
0107         std::string rlabel = recordLabel;
0108         if (rlabel.empty()) {
0109           rlabel = "-";
0110         }
0111         m_data->tagListBuffer.push_back(std::tie(recordName, rlabel, tagName));
0112       }
0113     }
0114 
0115     bool GTEditor::flush(const boost::posix_time::ptime& operationTime) {
0116       bool ret = false;
0117       checkTransaction("GTEditor::flush");
0118       if (m_data->change) {
0119         if (m_data->description.empty())
0120           throwException("A non-empty Description string is mandatory.", "GTEditor::flush");
0121         if (m_data->release.empty())
0122           throwException("A non-empty Release string is mandatory.", "GTEditor::flush");
0123         if (!m_data->exists) {
0124           m_session->gtSchema().gtTable().insert(
0125               m_data->name, m_data->validity, m_data->description, m_data->release, m_data->snapshotTime, operationTime);
0126           ret = true;
0127           m_data->exists = true;
0128         } else {
0129           m_session->gtSchema().gtTable().update(
0130               m_data->name, m_data->validity, m_data->description, m_data->release, m_data->snapshotTime, operationTime);
0131           ret = true;
0132         }
0133         m_data->change = false;
0134       }
0135       if (!m_data->tagListBuffer.empty()) {
0136         // insert the new iovs
0137         m_session->gtSchema().gtMapTable().insert(m_data->name, m_data->tagListBuffer);
0138         m_data->tagListBuffer.clear();
0139         ret = true;
0140       }
0141       return ret;
0142     }
0143 
0144     bool GTEditor::flush() { return flush(boost::posix_time::microsec_clock::universal_time()); }
0145 
0146     void GTEditor::checkTransaction(const std::string& ctx) {
0147       if (!m_session.get())
0148         throwException("The session is not active.", ctx);
0149       if (!m_session->isTransactionActive(false))
0150         throwException("The transaction is not active.", ctx);
0151     }
0152 
0153   }  // namespace persistency
0154 }  // namespace cond