CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/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 namespace cond {
00012 
00013 boost::shared_ptr<cond::IOVSequence> loadIOV( cond::DbSession& dbSess, 
00014                                               const std::string& iovToken ){
00015   boost::shared_ptr<cond::IOVSequence> iov = dbSess.getTypedObject<cond::IOVSequence>( iovToken );
00016   // loading the lazy-loading Queryable vector...
00017   iov->loadAll();
00018   //**** temporary for the schema transition
00019   if( dbSess.isOldSchema() ){
00020     PoolTokenParser parser(  dbSess.storage() ); 
00021     iov->swapTokens( parser );
00022   }
00023   //****
00024   return iov;
00025 }
00026 
00027 std::string insertIOV( cond::DbSession& dbSess, 
00028                        const boost::shared_ptr<IOVSequence>& data,
00029                        bool swapOIds=false ){
00030   dbSess.createDatabase();
00031   // ***** TEMPORARY FOR TRANSITION PHASE
00032   if( swapOIds && dbSess.isOldSchema() ){
00033     PoolTokenWriter writer( dbSess.storage() );
00034     data->swapOIds( writer );
00035   }
00036   // *****
00037   return dbSess.storeObject( data.get(), cond::IOVNames::container());
00038 }
00039 
00040 void updateIOV( cond::DbSession& dbSess, 
00041                 const boost::shared_ptr<IOVSequence>& data,
00042                 const std::string& token ){
00043   // ***** TEMPORARY FOR TRANSITION PHASE
00044   if( dbSess.isOldSchema() ){
00045     PoolTokenWriter writer( dbSess.storage() );
00046     data->swapOIds( writer );
00047   }
00048   // *****
00049   dbSess.updateObject( data.get(), token );
00050 }
00051 
00052 }
00053 
00054 cond::IOVService::IOVService(cond::DbSession& dbSess):
00055   m_dbSess(dbSess) {}
00056 
00057 cond::IOVService::~IOVService(){}
00058 
00059 cond::IOVEditor* 
00060 cond::IOVService::newIOVEditor( const std::string& token ){
00061   return new cond::IOVEditor( m_dbSess,token);
00062 }
00063 
00064 cond::IOVSequence const & cond::IOVService::iovSeq(const std::string& iovToken) {
00065   if (m_token!=iovToken) {
00066     m_iov = loadIOV( m_dbSess, iovToken );
00067     m_token=iovToken;
00068   }
00069   return *m_iov;
00070 }
00071 
00072 std::string 
00073 cond::IOVService::payloadToken( const std::string& iovToken,
00074                                 cond::Time_t currenttime ){
00075   cond::IOVSequence const & iov=iovSeq(iovToken);
00076   cond::IOVSequence::const_iterator iEnd=iov.find(currenttime);
00077   if( iEnd==iov.iovs().end() ){
00078     return "";
00079   }else{
00080     return iEnd->token();
00081   }
00082 }
00083 
00084 cond::TimeType cond::IOVService::timeType( const std::string& iovToken ) {
00085   cond::IOVSequence const & iov=iovSeq(iovToken);
00086   return iov.timeType();
00087 }
00088 
00089 bool cond::IOVService::isValid( const std::string& iovToken,
00090                                 cond::Time_t currenttime ){  
00091   cond::IOVSequence const & iov=iovSeq(iovToken);
00092   return (  currenttime >= iov.firstSince() && 
00093             currenttime <= iov.lastTill() );
00094 }
00095 
00096 std::pair<cond::Time_t, cond::Time_t> 
00097 cond::IOVService::validity( const std::string& iovToken, cond::Time_t currenttime ){
00098   
00099   cond::IOVSequence const & iov=iovSeq(iovToken);
00100   
00101   cond::Time_t since=iov.firstSince();
00102   cond::Time_t till=iov.lastTill();
00103   IOVSequence::const_iterator iter=iov.find(currenttime);
00104   if (iter!=iov.iovs().end())  {
00105     since=iter->sinceTime();
00106     iter++;
00107     if (iter!=iov.iovs().end()) 
00108       till = iter->sinceTime()-1;
00109   }
00110   else {
00111     since=iov.lastTill();
00112   }
00113   return std::pair<cond::Time_t, cond::Time_t>(since,till);
00114 }
00115 
00116 std::set<std::string>  
00117 cond::IOVService::payloadClasses( const std::string& iovToken ){
00118   cond::IOVSequence const & iov=iovSeq(iovToken);
00119   return iov.payloadClasses();
00120 }
00121 
00122 int cond::IOVService::iovSize( const std::string& iovtoken ){
00123   cond::IOVSequence const & iov=iovSeq(iovtoken);
00124   return iov.iovs().size();
00125 }
00126 
00127 void 
00128 cond::IOVService::deleteAll(bool withPayload){
00129   ora::Database& db = m_dbSess.storage();
00130   ora::Container cont = db.containerHandle( cond::IOVNames::container() );
00131   ora::ContainerIterator it = cont.iterator();
00132   while ( it.next() ) {
00133     if(withPayload){
00134       std::string tokenStr;
00135       IOVSequence::const_iterator payloadIt;
00136       boost::shared_ptr<cond::IOVSequence> iov = it.get<cond::IOVSequence>();
00137       IOVSequence::const_iterator payloadItBegin=iov->iovs().begin();
00138       IOVSequence::const_iterator payloadItEnd=iov->iovs().end();
00139       for(payloadIt=payloadItBegin;payloadIt!=payloadItEnd;++payloadIt){
00140         tokenStr=payloadIt->token();
00141         m_dbSess.deleteObject( tokenStr );
00142       }
00143     }
00144     cont.erase( it.itemId() );
00145   }
00146   cont.flush();
00147 }
00148 
00149 std::string
00150 cond::IOVService::exportIOVWithPayload( cond::DbSession& destDB,
00151                                         const std::string& iovToken){
00152   cond::IOVSequence const & iov=iovSeq(iovToken);
00153   
00154   boost::shared_ptr<cond::IOVSequence> newiov(new cond::IOVSequence(iov.timeType(), iov.lastTill(),iov.metadata()));
00155   
00156   for( IOVSequence::const_iterator it=iov.iovs().begin();
00157        it!=iov.iovs().end(); ++it){
00158     std::string newPToken = destDB.importObject( m_dbSess, it->token());
00159     ora::OId poid;
00160     poid.fromString( newPToken );
00161     ora::Container cont = destDB.storage().containerHandle( poid.containerId() );
00162     newiov->add(it->sinceTime(),newPToken, cont.className());
00163   }
00164   return insertIOV( destDB, newiov, true );
00165 }
00166 
00167 std::string 
00168 cond::IOVService::exportIOVRangeWithPayload( cond::DbSession& destDB,
00169                                              const std::string& iovToken,
00170                                              const std::string& destToken,
00171                                              cond::Time_t since,
00172                                              cond::Time_t till,
00173                                              bool outOfOrder ){
00174   cond::IOVSequence const & iov=iovSeq(iovToken);
00175   since = std::max(since, iov.firstSince());
00176   IOVSequence::const_iterator ifirstTill=iov.find(since);
00177   IOVSequence::const_iterator isecondTill=iov.find(till);
00178   if( isecondTill!=iov.iovs().end() ) isecondTill++;
00179   
00180   if (ifirstTill==isecondTill) 
00181     throw cond::Exception("IOVServiceImpl::exportIOVRangeWithPayload Error: empty input range");
00182   
00183   // since > ifirstTill->sinceTime() used to overwrite the actual time
00184   //since = ifirstTill->sinceTime();
00185   
00186   boost::shared_ptr<cond::IOVSequence> newiovref;
00187   //FIXME more option and eventually ability to resume (roll back is difficult)
00188   std::string dToken = destToken;
00189   if (dToken.empty()) {
00190     // create a new one
00191    newiovref.reset( new cond::IOVSequence(iov.timeType(), iov.lastTill(),iov.metadata()));
00192     dToken = insertIOV( destDB, newiovref );
00193   } else {
00194     newiovref = loadIOV( destDB, destToken);
00195     if (newiovref->iovs().empty()) ; // do not waist time
00196     else if (outOfOrder) {
00197       for( IOVSequence::const_iterator it=ifirstTill;
00198            it!=isecondTill; ++it)
00199         if (newiovref->exist(it->sinceTime()))
00200           throw cond::Exception("IOVServiceImpl::exportIOVRangeWithPayload Error: since time already exists");
00201     } else if (since <= newiovref->iovs().back().sinceTime())
00202       throw cond::Exception("IOVServiceImpl::exportIOVRangeWithPayload Error: since time out of range, below last since");
00203    updateIOV( destDB, newiovref, destToken );
00204   }
00205 
00206   cond::Time_t lsince = since;
00207   for(  IOVSequence::const_iterator it=ifirstTill;
00208         it!=isecondTill; ++it, lsince=it->sinceTime()){
00209     // first since overwritten by global since...
00210     
00211     std::string newPtoken = destDB.importObject( m_dbSess,it->token());
00212     ora::OId poid;
00213     poid.fromString( newPtoken );
00214     ora::Container cont = destDB.storage().containerHandle( poid.containerId() );
00215     newiovref->add(lsince, newPtoken,cont.className());
00216   }
00217   newiovref->stamp(cond::userInfo(),false);
00218   updateIOV( destDB, newiovref, dToken );
00219   return dToken;
00220 }
00221