CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondCore/IOVService/src/IOVProxy.cc

Go to the documentation of this file.
00001 #include "CondCore/IOVService/interface/IOVProxy.h"
00002 
00003 #include "CondFormats/Common/interface/Time.h"
00004 #include "CondFormats/Common/interface/TimeConversions.h"
00005 #include "CondCore/DBCommon/interface/DbTransaction.h"
00006 #include "CondCore/DBCommon/interface/DbScopedTransaction.h"
00007 
00008 #include "CondFormats/Common/interface/IOVSequence.h"
00009 
00010 namespace cond {
00011   namespace impl {
00012 
00013     struct IOVImpl {
00014       explicit IOVImpl(cond::DbSession& dbs) : dbSession(dbs), m_token(""){
00015       }
00016 
00017       IOVImpl(cond::DbSession& dbs,
00018               const std::string & token) : dbSession(dbs), m_token(token){
00019         refresh();
00020       }
00021       
00022       void refresh() {
00023         iov = dbSession.getTypedObject<cond::IOVSequence>( m_token );
00024         // loading the lazy-loading Queryable vector...
00025         iov->loadAll();
00026         //**** temporary for the schema transition
00027         if( dbSession.isOldSchema() ){
00028           PoolTokenParser parser(  dbSession.storage() ); 
00029           iov->swapTokens( parser );
00030         }
00031         //****
00032       }
00033       
00034       ~IOVImpl(){  
00035       }
00036       
00037       cond::DbSession dbSession;
00038       boost::shared_ptr<cond::IOVSequence> iov;
00039       std::string m_token;
00040     };
00041   }
00042   
00043   void IOVElementProxy::set(IOVSequence const & v, int ii) {
00044     size_t i =ii;
00045     if (i>=v.iovs().size()) {
00046       set(cond::invalidTime, cond::invalidTime,"");
00047       return;
00048     }
00049     m_token = v.iovs()[i].token();
00050     m_since =  v.iovs()[i].sinceTime();
00051     if(i+1==v.iovs().size()) {
00052       m_till = v.lastTill();
00053       return;
00054     }
00055     cond::UnpackedTime unpackedTime;
00056     cond::Time_t totalNanoseconds;
00057     cond::Time_t totalSecondsInNanoseconds;
00058     switch (v.timeType()) {
00059     case timestamp:
00060       //unpacking
00061       unpackedTime = cond::time::unpack(v.iovs()[i+1].sinceTime());
00062       //number of seconds in nanoseconds (avoid multiply and divide by 1e09)
00063       totalSecondsInNanoseconds = ((cond::Time_t)unpackedTime.first)*1000000000;
00064       //total number of nanoseconds
00065       totalNanoseconds = totalSecondsInNanoseconds + ((cond::Time_t)(unpackedTime.second));
00066       //now decrementing of 1 nanosecond
00067       totalNanoseconds--;
00068       //now repacking (just change the value of the previous pair)
00069       unpackedTime.first = (unsigned int) (totalNanoseconds/1000000000);
00070       unpackedTime.second = (unsigned int)(totalNanoseconds - (cond::Time_t)unpackedTime.first*1000000000);
00071       m_till = cond::time::pack(unpackedTime);
00072       break;
00073     default:
00074       m_till = v.iovs()[i+1].sinceTime()-1;
00075     }
00076   }
00077 
00078   IOVProxy::IterHelp::IterHelp(impl::IOVImpl & impl) :
00079     iov(&(*impl.iov)), elem(){}
00080   
00081   IOVProxy::IOVProxy() : m_low(0), m_high(0){}
00082  
00083   IOVProxy::~IOVProxy() {}
00084 
00085   IOVProxy::IOVProxy(cond::DbSession& dbSession)
00086     :m_iov(new impl::IOVImpl(dbSession)), m_low(0), m_high(0){}
00087 
00088   IOVProxy::IOVProxy(cond::DbSession& dbSession,
00089                      const std::string & token)
00090     :m_iov(new impl::IOVImpl(dbSession,token)), m_low(0), m_high(size()){}
00091 
00092   void IOVProxy::load( const std::string & token){
00093     m_iov->m_token = token;
00094     m_iov->refresh();
00095     m_high = size();
00096   }
00097 
00098   bool IOVProxy::refresh() {
00099     int oldsize = size();
00100     m_iov->refresh();
00101    bool anew = oldsize<size();
00102     if (anew) m_high = size();  // FIXME
00103     return anew;
00104   }
00105 
00106   void IOVProxy::resetRange() const {
00107     m_low=0;
00108     m_high=size();
00109   }
00110 
00111 
00112   void IOVProxy::setRange(cond::Time_t since, cond::Time_t  till) const {
00113      m_low = (since<iov().iovs().front().sinceTime()) ? 0 :
00114       iov().find(since)-iov().iovs().begin();
00115     m_high=iov().find(till)-iov().iovs().begin();
00116     m_high=std::min(m_high+1,size());
00117   }
00118 
00119 
00120   //FIXME cannot be done twice....
00121   void IOVProxy::head(int n) const {
00122     m_high = std::min(m_low+n,m_high);
00123   }
00124 
00125   void  IOVProxy::tail(int n) const {
00126     m_low = std::max(m_high-n,m_low);
00127   }
00128 
00129 
00130   IOVProxy::const_iterator IOVProxy::find(cond::Time_t time) const {
00131     int n = iov().find(time)-iov().iovs().begin();
00132     return (n<m_low || m_high<n ) ? 
00133       end() :  
00134       boost::make_transform_iterator(boost::counting_iterator<int>(n),
00135                                      IterHelp(*m_iov));
00136   }
00137 
00138 
00139   int IOVProxy::size() const {
00140     return iov().iovs().size();
00141   }
00142 
00143   IOV const & IOVProxy::iov() const {
00144     return *(*m_iov).iov;
00145   }
00146 
00147   TimeType IOVProxy::timetype() const {
00148     return iov().timeType();     
00149   }
00150 
00151   
00152   std::set<std::string> const& 
00153   IOVProxy::payloadClasses() const{
00154     return iov().payloadClasses();
00155   }
00156   
00157   std::string 
00158   IOVProxy::comment() const{
00159     return iov().comment();
00160   }
00161 
00162   int 
00163   IOVProxy::revision() const{
00164     return iov().revision();
00165   }
00166 
00167   cond::Time_t IOVProxy::timestamp() const {
00168     return iov().timestamp();
00169   }
00170 
00171   cond::DbSession& IOVProxy::db() const {
00172     return m_iov->dbSession;
00173   }
00174 
00175 
00176 
00177 }