Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondCore/CondDB/interface/RunInfoProxy.h"
0002 #include "SessionImpl.h"
0003 
0004 namespace cond {
0005 
0006   namespace persistency {
0007 
0008     // implementation details...
0009     // only hosting data in this case
0010     class RunInfoProxyData {
0011     public:
0012       RunInfoProxyData() : runList() {}
0013 
0014       // the data loaded
0015       RunInfoProxy::RunInfoData runList;
0016     };
0017 
0018     RunInfoProxy::Iterator::Iterator() : m_current() {}
0019 
0020     RunInfoProxy::Iterator::Iterator(RunInfoData::const_iterator current) : m_current(current) {}
0021 
0022     RunInfoProxy::Iterator::Iterator(const Iterator& rhs) : m_current(rhs.m_current) {}
0023 
0024     RunInfoProxy::Iterator& RunInfoProxy::Iterator::operator=(const Iterator& rhs) {
0025       if (this != &rhs) {
0026         m_current = rhs.m_current;
0027       }
0028       return *this;
0029     }
0030 
0031     cond::RunInfo_t RunInfoProxy::Iterator::operator*() { return cond::RunInfo_t(*m_current); }
0032 
0033     RunInfoProxy::Iterator& RunInfoProxy::Iterator::operator++() {
0034       m_current++;
0035       return *this;
0036     }
0037 
0038     RunInfoProxy::Iterator RunInfoProxy::Iterator::operator++(int) {
0039       Iterator tmp(*this);
0040       operator++();
0041       return tmp;
0042     }
0043 
0044     bool RunInfoProxy::Iterator::operator==(const Iterator& rhs) const {
0045       if (m_current != rhs.m_current)
0046         return false;
0047       return true;
0048     }
0049 
0050     bool RunInfoProxy::Iterator::operator!=(const Iterator& rhs) const { return !operator==(rhs); }
0051 
0052     RunInfoProxy::RunInfoProxy() : m_data(), m_session() {}
0053 
0054     RunInfoProxy::RunInfoProxy(const std::shared_ptr<SessionImpl>& session)
0055         : m_data(new RunInfoProxyData), m_session(session) {}
0056 
0057     RunInfoProxy::RunInfoProxy(const RunInfoProxy& rhs) : m_data(rhs.m_data), m_session(rhs.m_session) {}
0058 
0059     RunInfoProxy& RunInfoProxy::operator=(const RunInfoProxy& rhs) {
0060       m_data = rhs.m_data;
0061       m_session = rhs.m_session;
0062       return *this;
0063     }
0064 
0065     //
0066     void RunInfoProxy::load(Time_t low, Time_t up) {
0067       if (!m_data.get())
0068         return;
0069 
0070       // clear
0071       reset();
0072 
0073       checkTransaction("RunInfoProxy::load(Time_t,Time_t)");
0074 
0075       std::string dummy;
0076       if (!m_session->runInfoSchema().runInfoTable().getInclusiveRunRange(low, up, m_data->runList)) {
0077         throwException("No runs have been found in the range (" + std::to_string(low) + "," + std::to_string(up) + ")",
0078                        "RunInfoProxy::load(Time_t,Time_t)");
0079       }
0080     }
0081 
0082     void RunInfoProxy::load(const boost::posix_time::ptime& low, const boost::posix_time::ptime& up) {
0083       if (!m_data.get())
0084         return;
0085 
0086       // clear
0087       reset();
0088 
0089       checkTransaction("RunInfoProxy::load(const boost::posix_time::ptime&,const boost::posix_time::ptime&)");
0090 
0091       std::string dummy;
0092       if (!m_session->runInfoSchema().runInfoTable().getInclusiveTimeRange(low, up, m_data->runList)) {
0093         throwException("No runs have been found in the interval (" + boost::posix_time::to_simple_string(low) + "," +
0094                            boost::posix_time::to_simple_string(up) + ")",
0095                        "RunInfoProxy::load(boost::posix_time::ptime&,const boost::posix_time::ptime&)");
0096       }
0097     }
0098 
0099     void RunInfoProxy::reset() {
0100       if (m_data.get()) {
0101         m_data->runList.clear();
0102       }
0103     }
0104 
0105     void RunInfoProxy::checkTransaction(const std::string& ctx) {
0106       if (!m_session.get())
0107         throwException("The session is not active.", ctx);
0108       if (!m_session->isTransactionActive(false))
0109         throwException("The transaction is not active.", ctx);
0110     }
0111 
0112     RunInfoProxy::Iterator RunInfoProxy::begin() const {
0113       if (m_data.get()) {
0114         return Iterator(m_data->runList.begin());
0115       }
0116       return Iterator();
0117     }
0118 
0119     RunInfoProxy::Iterator RunInfoProxy::end() const {
0120       if (m_data.get()) {
0121         return Iterator(m_data->runList.end());
0122       }
0123       return Iterator();
0124     }
0125 
0126     // comparison functor for iov tuples: Time_t
0127     struct IOVRunComp {
0128       bool operator()(const std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime>& x,
0129                       const cond::Time_t& y) {
0130         return (y > std::get<0>(x));
0131       }
0132     };
0133 
0134     // comparison functor for iov tuples: boost::posix_time::ptime
0135     struct IOVTimeComp {
0136       bool operator()(const std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime>& x,
0137                       const boost::posix_time::ptime& y) {
0138         return (y > std::get<2>(x));
0139       }
0140     };
0141 
0142     RunInfoProxy::Iterator RunInfoProxy::find(Time_t target) const {
0143       if (m_data.get()) {
0144         auto p = std::lower_bound(m_data->runList.begin(), m_data->runList.end(), target, IOVRunComp());
0145         return Iterator(p);
0146       }
0147       return Iterator();
0148     }
0149 
0150     RunInfoProxy::Iterator RunInfoProxy::find(const boost::posix_time::ptime& target) const {
0151       if (m_data.get()) {
0152         auto p = std::lower_bound(m_data->runList.begin(), m_data->runList.end(), target, IOVTimeComp());
0153         return Iterator(p);
0154       }
0155       return Iterator();
0156     }
0157 
0158     //
0159     cond::RunInfo_t RunInfoProxy::get(Time_t target) const {
0160       Iterator it = find(target);
0161       if (it == Iterator())
0162         throwException("No data has been found.", "RunInfoProxy::get(Time_t)");
0163       if (it == end())
0164         throwException("The target run has not been found in the selected run range.", "RunInfoProxy::get(Time_t)");
0165       return *it;
0166     }
0167 
0168     //
0169     cond::RunInfo_t RunInfoProxy::get(const boost::posix_time::ptime& target) const {
0170       Iterator it = find(target);
0171       if (it == Iterator())
0172         throwException("No data has been found.", "RunInfoProxy::get(const boost::posix_time::ptime&)");
0173       if (it == end())
0174         throwException("The target time has not been found in the selected time range.",
0175                        "RunInfoProxy::get(const boost::posix_time::ptime&)");
0176       return *it;
0177     }
0178 
0179     int RunInfoProxy::size() const { return m_data.get() ? m_data->runList.size() : 0; }
0180 
0181   }  // namespace persistency
0182 }  // namespace cond