Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondCore/CondDB/interface/Exception.h"
0002 #include "RunInfoSchema.h"
0003 //
0004 #include <openssl/sha.h>
0005 
0006 namespace cond {
0007 
0008   namespace persistency {
0009 
0010     RUN_INFO::Table::Table(coral::ISchema& schema) : m_schema(schema) {}
0011 
0012     bool RUN_INFO::Table::Table::exists() { return existsTable(m_schema, tname); }
0013 
0014     void RUN_INFO::Table::create() {
0015       if (exists()) {
0016         throwException("RUN_INFO table already exists in this schema.", "RUN_INFO::Table::create");
0017       }
0018       TableDescription<RUN_NUMBER, START_TIME, END_TIME> descr(tname);
0019       descr.setPrimaryKey<RUN_NUMBER>();
0020       createTable(m_schema, descr.get());
0021     }
0022 
0023     bool RUN_INFO::Table::select(cond::Time_t runNumber,
0024                                  boost::posix_time::ptime& start,
0025                                  boost::posix_time::ptime& end) {
0026       Query<START_TIME, END_TIME> q(m_schema);
0027       q.addCondition<RUN_NUMBER>(runNumber);
0028       bool ret = false;
0029       for (auto r : q) {
0030         ret = true;
0031         std::tie(start, end) = r;
0032       }
0033       return ret;
0034     }
0035 
0036     cond::Time_t RUN_INFO::Table::getLastInserted(boost::posix_time::ptime& start, boost::posix_time::ptime& end) {
0037       cond::Time_t run = cond::time::MIN_VAL;
0038       Query<MAX_RUN_NUMBER> q0(m_schema);
0039       try {
0040         for (auto r : q0) {
0041           run = std::get<0>(r);
0042         }
0043         // cope with mis-beahviour in the sqlite plugin: no result for MAX() returns NULL
0044       } catch (const coral::AttributeException& e) {
0045         std::string message(e.what());
0046         if (message.find("Attempt to access data of NULL attribute") != 0)
0047           throw;
0048       }
0049       select(run, start, end);
0050       return run;
0051     }
0052 
0053     bool RUN_INFO::Table::getInclusiveRunRange(
0054         cond::Time_t lower,
0055         cond::Time_t upper,
0056         std::vector<std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime> >& runData) {
0057       // first find the lowest existing run >= upper
0058       Query<MIN_RUN_NUMBER> q0(m_schema);
0059       q0.addCondition<RUN_NUMBER>(upper, ">=");
0060       for (auto r : q0)
0061         upper = std::get<0>(r);
0062       // then find the inclusive range
0063       Query<RUN_NUMBER, START_TIME, END_TIME> q1(m_schema);
0064       q1.addCondition<RUN_NUMBER>(lower, ">=").addCondition<RUN_NUMBER>(upper, "<=");
0065       size_t prevSize = runData.size();
0066       for (auto r : q1) {
0067         runData.push_back(r);
0068       }
0069       return runData.size() > prevSize;
0070     }
0071 
0072     bool RUN_INFO::Table::getInclusiveTimeRange(
0073         const boost::posix_time::ptime& lower,
0074         const boost::posix_time::ptime& upper,
0075         std::vector<std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime> >& runData) {
0076       boost::posix_time::ptime up = upper;
0077       // first find the lowest existing run >= upper
0078       Query<START_TIME> q0(m_schema);
0079       q0.addCondition<START_TIME>(upper, ">=");
0080       bool found = q0.retrievedRows();
0081       if (!found)
0082         return false;
0083       Query<MIN_START_TIME> q1(m_schema);
0084       q1.addCondition<START_TIME>(upper, ">=");
0085       for (auto r : q1)
0086         up = std::get<0>(r);
0087       // then find the inclusive range
0088       Query<RUN_NUMBER, START_TIME, END_TIME> q2(m_schema);
0089       q2.addCondition<END_TIME>(lower, ">=").addCondition<START_TIME>(up, "<=");
0090       size_t prevSize = runData.size();
0091       for (auto r : q2) {
0092         runData.push_back(r);
0093       }
0094       return runData.size() > prevSize;
0095     }
0096 
0097     void RUN_INFO::Table::insertOne(cond::Time_t runNumber,
0098                                     const boost::posix_time::ptime& start,
0099                                     const boost::posix_time::ptime& end) {
0100       RowBuffer<RUN_NUMBER, START_TIME, END_TIME> dataToInsert(std::tie(runNumber, start, end));
0101       insertInTable(m_schema, tname, dataToInsert.get());
0102     }
0103 
0104     void RUN_INFO::Table::insert(
0105         const std::vector<std::tuple<cond::Time_t, boost::posix_time::ptime, boost::posix_time::ptime> >& runs) {
0106       BulkInserter<RUN_NUMBER, START_TIME, END_TIME> inserter(m_schema, tname);
0107       for (auto run : runs)
0108         inserter.insert(run);
0109       inserter.flush();
0110     }
0111 
0112     void RUN_INFO::Table::updateEnd(cond::Time_t runNumber, const boost::posix_time::ptime& end) {
0113       UpdateBuffer buffer;
0114       buffer.setColumnData<END_TIME>(std::tie(end));
0115       buffer.addWhereCondition<RUN_NUMBER>(runNumber);
0116       updateTable(m_schema, tname, buffer);
0117     }
0118 
0119     RunInfoSchema::RunInfoSchema(coral::ISchema& schema) : m_runInfoTable(schema) {}
0120 
0121     bool RunInfoSchema::exists() {
0122       if (!m_runInfoTable.exists())
0123         return false;
0124       return true;
0125     }
0126 
0127     bool RunInfoSchema::create() {
0128       bool created = false;
0129       if (!exists()) {
0130         m_runInfoTable.create();
0131         created = true;
0132       }
0133       return created;
0134     }
0135 
0136     IRunInfoTable& RunInfoSchema::runInfoTable() { return m_runInfoTable; }
0137 
0138   }  // namespace persistency
0139 }  // namespace cond