CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/CondCore/IOVService/src/IOVService.cc

Go to the documentation of this file.
00001 #include "CondCore/IOVService/interface/IOVService.h"
00002 #include "CondCore/IOVService/interface/IOVEditor.h"
00003 
00004 #include "FWCore/Framework/interface/IOVSyncValue.h"
00005 #include "CondCore/IOVService/interface/IOVNames.h"
00006 #include "CondCore/DBCommon/interface/DbTransaction.h"
00007 #include "CondCore/DBCommon/interface/Exception.h"
00008 
00009 #include "CondCore/DBCommon/interface/IOVInfo.h"
00010 
00011 
00012 
00013 
00014 cond::IOVService::IOVService(cond::DbSession& dbSess):
00015   m_dbSess(dbSess) {}
00016 
00017 cond::IOVService::~IOVService(){}
00018 
00019 cond::IOVEditor* 
00020 cond::IOVService::newIOVEditor( const std::string& token ){
00021   return new cond::IOVEditor( m_dbSess,token);
00022 }
00023 
00024 
00025 
00026 
00027 cond::IOVSequence const & cond::IOVService::iovSeq(const std::string& iovToken) {
00028   if (m_token!=iovToken) {
00029     m_iov = m_dbSess.getTypedObject<cond::IOVSequence>( iovToken );
00030     m_token=iovToken;
00031   }
00032   return *m_iov;
00033 }
00034 
00035 
00036 
00037 std::string 
00038 cond::IOVService::payloadToken( const std::string& iovToken,
00039                                 cond::Time_t currenttime ){
00040   cond::IOVSequence const & iov=iovSeq(iovToken);
00041   cond::IOVSequence::const_iterator iEnd=iov.find(currenttime);
00042   if( iEnd==iov.iovs().end() ){
00043     return "";
00044   }else{
00045     return iEnd->wrapperToken();
00046   }
00047 }
00048 
00049 cond::TimeType cond::IOVService::timeType( const std::string& iovToken ) {
00050   cond::IOVSequence const & iov=iovSeq(iovToken);
00051   return iov.timeType();
00052 }
00053 
00054 bool cond::IOVService::isValid( const std::string& iovToken,
00055                                 cond::Time_t currenttime ){
00056   
00057   cond::IOVSequence const & iov=iovSeq(iovToken);
00058   return (  currenttime >= iov.firstSince() && 
00059             currenttime <= iov.lastTill() );
00060 }
00061 
00062 std::pair<cond::Time_t, cond::Time_t> 
00063 cond::IOVService::validity( const std::string& iovToken, cond::Time_t currenttime ){
00064   
00065   cond::IOVSequence const & iov=iovSeq(iovToken);
00066   
00067   cond::Time_t since=iov.firstSince();
00068   cond::Time_t till=iov.lastTill();
00069   IOVSequence::const_iterator iter=iov.find(currenttime);
00070   if (iter!=iov.iovs().end())  {
00071     since=iter->sinceTime();
00072     iter++;
00073     if (iter!=iov.iovs().end()) 
00074       till = iter->sinceTime()-1;
00075   }
00076   else {
00077     since=iov.lastTill();
00078   }
00079   return std::pair<cond::Time_t, cond::Time_t>(since,till);
00080 }
00081 
00082 std::string 
00083 cond::IOVService::payloadContainerName( const std::string& iovToken ){
00084   cond::IOVSequence const & iov=iovSeq(iovToken);
00085   
00086   // FIXME move to metadata
00087   std::string payloadtokstr=iov.iovs().front().wrapperToken();
00088   std::pair<std::string,int> oidData = parseToken( payloadtokstr );
00089   return oidData.first;
00090 }
00091 
00092 
00093 
00094 
00095 void 
00096 cond::IOVService::deleteAll(bool withPayload){
00097   ora::Database& db = m_dbSess.storage();
00098   ora::Container cont = db.containerHandle( cond::IOVNames::container() );
00099   ora::ContainerIterator it = cont.iterator();
00100   while ( it.next() ) {
00101     if(withPayload){
00102       std::string tokenStr;
00103       IOVSequence::const_iterator payloadIt;
00104       boost::shared_ptr<cond::IOVSequence> iov = it.get<cond::IOVSequence>();
00105       IOVSequence::const_iterator payloadItBegin=iov->iovs().begin();
00106       IOVSequence::const_iterator payloadItEnd=iov->iovs().end();
00107       for(payloadIt=payloadItBegin;payloadIt!=payloadItEnd;++payloadIt){
00108         tokenStr=payloadIt->wrapperToken();
00109         m_dbSess.deleteObject( tokenStr );
00110       }
00111     }
00112     cont.erase( it.itemId() );
00113   }
00114   cont.flush();
00115 }
00116 
00117 std::string
00118 cond::IOVService::exportIOVWithPayload( cond::DbSession& destDB,
00119                                         const std::string& iovToken){
00120   cond::IOVSequence const & iov=iovSeq(iovToken);
00121   
00122   boost::shared_ptr<cond::IOVSequence> newiov(new cond::IOVSequence(iov.timeType(), iov.lastTill(),iov.metadataToken()));
00123   
00124   for( IOVSequence::const_iterator it=iov.iovs().begin();
00125        it!=iov.iovs().end(); ++it){
00126     std::string newPToken = destDB.importObject( m_dbSess, it->wrapperToken());
00127     newiov->add(it->sinceTime(),newPToken);
00128   }
00129   return destDB.storeObject(newiov.get(), cond::IOVNames::container());
00130 }
00131 
00132 #include "CondCore/DBCommon/interface/ClassInfoLoader.h"
00133 /*
00134 void cond::IOVService::loadDicts( const std::string& iovToken) {
00135   // loadlib
00136   boost::shared_ptr<cond::IOVSequence> iov = m_dbSess.getTypedObject<cond::IOVSequence>(iovToken);
00137   // FIXME use iov metadata
00138   std::string ptok = iov->iovs().front().wrapperToken();
00139   m_dbSess.transaction().commit();
00140   cond::reflexTypeByToken(ptok);
00141   m_dbSess.transaction().start(true);
00142 }
00143 */
00144 
00145 std::string 
00146 cond::IOVService::exportIOVRangeWithPayload( cond::DbSession& destDB,
00147                                              const std::string& iovToken,
00148                                              const std::string& destToken,
00149                                              cond::Time_t since,
00150                                              cond::Time_t till,
00151                                              bool outOfOrder
00152                                              ){
00153   
00154   
00156   //loadDicts(iovToken);
00157    cond::IOVSequence const & iov=iovSeq(iovToken);
00158   since = std::max(since, iov.firstSince());
00159   IOVSequence::const_iterator ifirstTill=iov.find(since);
00160   IOVSequence::const_iterator isecondTill=iov.find(till);
00161   if( isecondTill!=iov.iovs().end() ) isecondTill++;
00162   
00163   if (ifirstTill==isecondTill) 
00164     throw cond::Exception("IOVServiceImpl::exportIOVRangeWithPayload Error: empty input range");
00165   
00166   // since > ifirstTill->sinceTime() used to overwrite the actual time
00167   //since = ifirstTill->sinceTime();
00168   
00169   boost::shared_ptr<cond::IOVSequence> newiovref;
00170   //FIXME more option and eventually ability to resume (roll back is difficult)
00171   std::string dToken = destToken;
00172   if (dToken.empty()) {
00173     // create a new one
00174     newiovref.reset( new cond::IOVSequence(iov.timeType(), iov.lastTill(),iov.metadataToken()));
00175     dToken = destDB.storeObject( newiovref.get(),cond::IOVNames::container());
00176   } else {
00177     newiovref = destDB.getTypedObject<cond::IOVSequence>(destToken);
00178     if (newiovref->iovs().empty()) ; // do not waist time
00179     else if (outOfOrder) {
00180       for( IOVSequence::const_iterator it=ifirstTill;
00181            it!=isecondTill; ++it)
00182         if (newiovref->exist(it->sinceTime()))
00183           throw cond::Exception("IOVServiceImpl::exportIOVRangeWithPayload Error: since time already exists");
00184     } else if (since <= newiovref->iovs().back().sinceTime())
00185       throw cond::Exception("IOVServiceImpl::exportIOVRangeWithPayload Error: since time out of range, below last since");
00186     destDB.updateObject( newiovref.get(),destToken );
00187   }
00188 
00189   cond::Time_t lsince = since;
00190   for(  IOVSequence::const_iterator it=ifirstTill;
00191         it!=isecondTill; ++it, lsince=it->sinceTime()){
00192     // first since overwritten by global since...
00193     
00194     std::string newPtoken = destDB.importObject( m_dbSess,it->wrapperToken());
00195     newiovref->add(lsince, newPtoken);
00196   }
00197   newiovref->stamp(cond::userInfo(),false);
00198   destDB.updateObject( newiovref.get(),dToken );
00199   return dToken;
00200 }
00201