CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CondCore/IOVService/src/PayloadProxy.cc

Go to the documentation of this file.
00001 #include "CondCore/IOVService/interface/PayloadProxy.h"
00002 
00003 #include "CondCore/ORA/interface/Exception.h"
00004 #include "CondCore/DBCommon/interface/Exception.h"
00005 #include "CondCore/DBCommon/interface/DbTransaction.h"
00006 
00007 namespace {
00008 
00009   void fillIt(cond::BasePayloadProxy::ObjId & obj, std::string const & token, cond::Time_t since) {
00010     obj.since = since;
00011     size_t i1 = token.rfind('=');
00012     size_t i2 = token.rfind('-');
00013     obj.oid1 = ::strtol(token.substr(i1+1,8).c_str(),0,16);
00014     obj.oid2 = ::strtol(token.substr(i2+1,8).c_str(),0,16);
00015   }
00016 
00017 }
00018 
00019 namespace cond {
00020 
00021   BasePayloadProxy::Stats BasePayloadProxy::gstats = {0,0,0};
00022 
00023 
00024   BasePayloadProxy::BasePayloadProxy(cond::DbSession& session,
00025                                      const std::string & token,
00026                                      bool errorPolicy) :
00027     m_doThrow(errorPolicy), m_iov(session,token,true,false) {
00028     ++gstats.nProxy;
00029     BasePayloadProxy::Stats s = {0,0,0,ObjIds()};
00030     stats = s;
00031   }
00032 
00033 
00034   BasePayloadProxy::~BasePayloadProxy(){}
00035 
00036   cond::ValidityInterval BasePayloadProxy::loadFor(cond::Time_t time) {
00037     m_element = *m_iov.find(time);
00038     make();
00039     return cond::ValidityInterval(m_element.since(),m_element.till());
00040   }
00041 
00042   cond::ValidityInterval BasePayloadProxy::loadFor(size_t n) {
00043     m_element.set(m_iov.iov(),n);
00044     make();
00045     return cond::ValidityInterval(m_element.since(),m_element.till());
00046   }
00047 
00048 
00049   void  BasePayloadProxy::make() {
00050     ++gstats.nMake;
00051     ++stats.nMake;
00052     bool ok = false;
00053     if ( isValid()) {
00054       // check if (afterall) the payload is still the same...
00055       if (m_element.token()==token()) return;
00056       cond::DbTransaction& trans = m_element.db().transaction();
00057       trans.start(true);
00058       try {
00059         ok = load( m_element.db(),m_element.token());
00060         if (ok) m_token = m_element.token();
00061       } catch( const ora::Exception& e) {
00062         if (m_doThrow) throw cond::Exception(std::string("Condition Payload loader: ")+ e.what());
00063         ok = false;
00064       }
00065       trans.commit();
00066     }
00067 
00068     if (!ok) {
00069       m_element.set(cond::invalidTime,cond::invalidTime,"");
00070       m_token.clear();
00071       if (m_doThrow)
00072         throw cond::Exception("Condition Payload loader: invalid data");
00073     }
00074     if (ok) { 
00075       ++gstats.nLoad; ++stats.nLoad;
00076       stats.ids.push_back(ObjId());
00077       fillIt(stats.ids.back(),m_token, m_element.since());
00078     }
00079   }
00080 
00081 
00082   cond::ValidityInterval BasePayloadProxy::setIntervalFor(cond::Time_t time) {
00083     //FIXME: shall handle truncation...
00084     if ( (!(time<m_element.till())) || time<m_element.since() )
00085       m_element = *m_iov.find(time);
00086     return cond::ValidityInterval(m_element.since(),m_element.till());
00087   }
00088     
00089   bool BasePayloadProxy::isValid() const {
00090     return m_element.since()!=cond::invalidTime && m_element.till()!=cond::invalidTime
00091       &&  !m_element.token().empty();
00092   }
00093 
00094 
00095   bool  BasePayloadProxy::refresh() {
00096     bool anew = m_iov.refresh();
00097     if (anew)  m_element = IOVElementProxy();
00098     return anew;
00099   }
00100 
00101 
00102 
00103 
00104 
00105 }