CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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/ORA/interface/OId.h"
00006 #include "CondCore/MetaDataService/interface/MetaData.h"
00007 
00008 namespace {
00009 
00010   void fillIt(cond::BasePayloadProxy::ObjId & obj, std::string const & token, cond::Time_t since) {
00011     obj.since = since;
00012     ora::OId oid;
00013     oid.fromString( token );
00014     obj.oid1 = oid.containerId();
00015     obj.oid2 = oid.itemId();
00016   }
00017 
00018 }
00019 
00020 namespace cond {
00021 
00022   BasePayloadProxy::Stats BasePayloadProxy::gstats = {0,0,0,0,0,0,0};
00023 
00024 
00025   BasePayloadProxy::BasePayloadProxy(cond::DbSession& session,
00026                                      bool errorPolicy) :
00027     m_doThrow(errorPolicy), m_iov(session),m_session(session) {
00028     ++gstats.nProxy;
00029     BasePayloadProxy::Stats s = {0,0,0,0,0,0,0,ObjIds()};
00030     stats = s;
00031   }
00032 
00033   BasePayloadProxy::BasePayloadProxy(cond::DbSession& session,
00034                                      const std::string & iovToken,
00035                                      bool errorPolicy) :
00036     m_doThrow(errorPolicy), m_iov(session),m_session(session) {
00037     m_session.transaction().start(true);
00038     m_iov.load( iovToken );
00039     m_session.transaction().commit();
00040     ++gstats.nProxy;
00041     BasePayloadProxy::Stats s = {0,0,0,0,0,0,0,ObjIds()};
00042     stats = s;
00043   }
00044 
00045 
00046   BasePayloadProxy::~BasePayloadProxy(){}
00047 
00048   void BasePayloadProxy::loadIov( const std::string iovToken ){
00049     m_session.transaction().start(true);
00050     m_iov.load( iovToken );
00051     m_session.transaction().commit();
00052     invalidateCache();
00053   }
00054    
00055   void BasePayloadProxy::loadTag( const std::string tag ){
00056     m_session.transaction().start(true);
00057     cond::MetaData metadata(m_session);
00058     std::string iovToken = metadata.getToken(tag);
00059     m_iov.load( iovToken );
00060     m_session.transaction().commit();
00061     invalidateCache();
00062   }
00063 
00064   cond::ValidityInterval BasePayloadProxy::loadFor(cond::Time_t time) {
00065     m_element = *m_iov.find(time);
00066     make();
00067     return cond::ValidityInterval(m_element.since(),m_element.till());
00068   }
00069 
00070   cond::ValidityInterval BasePayloadProxy::loadFor(size_t n) {
00071     m_element.set(m_iov.iov(),n);
00072     make();
00073     return cond::ValidityInterval(m_element.since(),m_element.till());
00074   }
00075 
00076 
00077   void  BasePayloadProxy::make() {
00078     ++gstats.nMake;
00079     ++stats.nMake;
00080     bool ok = false;
00081     if ( isValid()) {
00082       // check if (afterall) the payload is still the same...
00083       if (m_element.token()==token()) return;
00084       try {
00085         m_session.transaction().start(true);
00086         ok = load( m_session ,m_element.token());
00087         m_session.transaction().commit();
00088         if (ok) m_token = m_element.token();
00089       } catch( const ora::Exception& e) {
00090         if (m_doThrow) throw cond::Exception(std::string("Condition Payload loader: ")+ e.what());
00091         ok = false;
00092       }
00093     }
00094 
00095     if (!ok) {
00096       m_element.set(cond::invalidTime,cond::invalidTime,"");
00097       m_token.clear();
00098       if (m_doThrow){
00099         throw cond::Exception("Condition Payload loader: invalid data");
00100       }
00101     }
00102     if (ok) { 
00103       ++gstats.nLoad; ++stats.nLoad;
00104       stats.ids.push_back(ObjId());
00105       fillIt(stats.ids.back(),m_token, m_element.since());
00106     }
00107   }
00108 
00109 
00110   cond::ValidityInterval BasePayloadProxy::setIntervalFor(cond::Time_t time) {
00111     //FIXME: shall handle truncation...
00112     if ( (!(time<m_element.till())) || time<m_element.since() ){
00113       m_element = *m_iov.find(time);
00114     }
00115     return cond::ValidityInterval(m_element.since(),m_element.till());
00116   }
00117     
00118   bool BasePayloadProxy::isValid() const {
00119     return m_element.since()!=cond::invalidTime && m_element.till()!=cond::invalidTime
00120       &&  !m_element.token().empty();
00121   }
00122 
00123 
00124   bool  BasePayloadProxy::refresh() {
00125     ++gstats.nRefresh; ++stats.nRefresh;
00126     m_session.transaction().start(true);
00127     bool anew = m_iov.refresh();
00128     m_session.transaction().commit();
00129     if (anew)  {
00130       m_element = IOVElementProxy();
00131       ++gstats.nArefresh; ++stats.nArefresh;
00132     }
00133     return anew;
00134   }
00135 
00136   bool BasePayloadProxy::refresh( cond::DbSession& newSession ) {
00137     ++gstats.nReconnect; ++stats.nReconnect;
00138     m_session = newSession;
00139     m_session.transaction().start(true);
00140     bool anew = m_iov.refresh( m_session );
00141     m_session.transaction().commit();
00142     if (anew) {
00143       m_element = IOVElementProxy();
00144       ++gstats.nAreconnect; ++stats.nAreconnect;
00145     }
00146     return anew;
00147   }
00148 
00149 }