CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondCore/DBOutputService/interface/PoolDBOutputService.h

Go to the documentation of this file.
00001 #ifndef CondCore_PoolDBOutputService_h
00002 #define CondCore_PoolDBOutputService_h
00003 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00004 #include "CondCore/DBCommon/interface/DbConnection.h"
00005 #include "CondCore/DBCommon/interface/Time.h"
00006 #include "CondCore/MetaDataService/interface/MetaData.h"
00007 #include "CondCore/DBCommon/interface/Logger.h"
00008 #include "CondCore/DBCommon/interface/LogDBEntry.h"
00009 #include "CondCore/DBCommon/interface/TagInfo.h"
00010 #include "Reflex/Type.h"
00011 #include <string>
00012 #include <map>
00013 
00014 // many many clients do not include explicitely!
00015 #ifndef COND_EXCEPTION_H
00016 #include "CondCore/DBCommon/interface/Exception.h"
00017 // #warning please include  "CondCore/DBCommon/interface/Exception.h" explicitely
00018 // #define COND_EXP_WARNING
00019 #endif
00020 
00021 //
00022 // Package:     DBOutputService
00023 // Class  :     PoolDBOutputService
00024 // 
00028 //
00029 // Author:      Zhen Xie
00030 //
00031 namespace edm{
00032   class Event;
00033   class EventSetup;
00034   class ParameterSet;
00035 }
00036 namespace cond{
00037   
00038   inline std::string classNameForTypeId( const std::type_info& typeInfo ){
00039     Reflex::Type reflexType = Reflex::Type::ByTypeInfo( typeInfo );
00040     //FIXME: should become Reflex::SCOPED?
00041     return reflexType.Name();
00042   }
00043   //FIXME
00044   class Summary;
00045   
00046   namespace service {
00047 
00058     struct GetToken {
00059       virtual std::string operator()(cond::DbSession&) const =0;
00060     };
00061     
00062     struct GetTrivialToken : public GetToken {
00063       
00064       GetTrivialToken(std::string token) :
00065         m_token(token){}
00066       virtual ~GetTrivialToken(){}
00067       virtual std::string operator()(cond::DbSession&) const {
00068         return m_token;
00069       }
00070       std::string m_token;
00071     };
00072     
00073     template<typename T>
00074     struct GetTokenFromPointer : public GetToken {
00075       
00076       static
00077       std::string classNameForPointer( T* pointer ){
00078         if(!pointer) return classNameForTypeId( typeid(T) );
00079         return classNameForTypeId( typeid(*pointer) );
00080       }
00081 
00082       GetTokenFromPointer(T * p, Summary * s=0) :
00083         m_p(p), m_s(s){}
00084       
00085       virtual std::string operator()(cond::DbSession& pooldb) const {
00086         std::string className = classNameForPointer( m_p );
00087         boost::shared_ptr<T> sptr( m_p );
00088         return pooldb.storeObject(m_p,className);
00089       }
00090       T* m_p;
00091       cond::Summary * m_s;
00092     };
00093 
00094     
00095     class PoolDBOutputService{
00096     public:
00097       PoolDBOutputService( const edm::ParameterSet & iConfig, 
00098                            edm::ActivityRegistry & iAR );
00099       //use these to control connections
00100       //void  postBeginJob();
00101       void  postEndJob();
00102       //
00103       //use these to control transaction interval
00104       //
00105       void preEventProcessing( const edm::EventID & evtID, 
00106                                const edm::Timestamp & iTime );
00107       void preModule(const edm::ModuleDescription& desc);
00108       void postModule(const edm::ModuleDescription& desc);
00109       void preBeginLumi(const edm::LuminosityBlockID&, 
00110                         const edm::Timestamp& );
00111       //
00112       // return the database session in use
00113       //
00114       cond::DbSession session() const;
00115       //
00116       std::string tag( const std::string& recordName );
00117       bool isNewTagRequest( const std::string& recordName );
00118       const cond::Logger& queryLog() const;
00119       
00120       // BW-compatible signature
00121       template<typename T>
00122       void writeOne( T * payload, Time_t time, const std::string& recordName, bool withlogging=false ) {
00123         this->writeOne<T>(payload, 0, time, recordName, withlogging);
00124       }
00125 
00126       /* write one (either create or append)
00127        * The ONE and ONLY interface supported in future!
00128        */
00129       template<typename T>
00130       void writeOne( T * payload, Summary * summary, Time_t time, const std::string& recordName, bool withlogging=false) {
00131         if (isNewTagRequest(recordName) ){
00132           createNewIOV<T>(payload, summary,
00133                           time, endOfTime(), recordName, withlogging);
00134         }else{
00135           appendSinceTime<T>(payload, summary, time, recordName, withlogging);
00136         }       
00137       }
00138       
00139       // close the IOVSequence setting lastTill
00140       void closeIOV(Time_t lastTill, const std::string& recordName, 
00141                     bool withlogging=false);
00142 
00143       // BW-compatible signature
00144       template<typename T>
00145       void createNewIOV( T* firstPayloadObj,
00146                          cond::Time_t firstSinceTime,
00147                          cond::Time_t firstTillTime,
00148                          const std::string& recordName,
00149                          bool withlogging=false){
00150         this->createNewIOV(firstPayloadObj, 0, firstSinceTime, firstTillTime, recordName, withlogging);
00151       }
00152       
00153       //
00154       // insert the payload and its valid since time into the database
00155       // Note: user looses the ownership of the pointer to the payloadObj
00156       // The payload object will be stored as well
00157       // 
00158       template<typename T> 
00159       void createNewIOV( T* firstPayloadObj,  
00160                          Summary * summary,
00161                          cond::Time_t firstSinceTime,
00162                          cond::Time_t firstTillTime,
00163                          const std::string& recordName,
00164                          bool withlogging=false ){
00165         createNewIOV( GetTokenFromPointer<T>(firstPayloadObj, summary),
00166                       firstSinceTime,
00167                       firstTillTime,
00168                       recordName,
00169                       withlogging);     
00170       }
00171       
00172       void createNewIOV( const std::string& firstPayloadToken,
00173                          cond::Time_t firstSinceTime,
00174                          cond::Time_t firstTillTime,
00175                          const std::string& recordName,
00176                          bool withlogging=false) {
00177         createNewIOV( GetTrivialToken(firstPayloadToken),
00178                       firstSinceTime,
00179                       firstTillTime,
00180                       recordName,
00181                       withlogging);
00182       }
00183 
00184       
00185       // BW-compatible signature
00186       template<typename T> void appendSinceTime( T* payloadObj,
00187                                                  cond::Time_t sinceTime,
00188                                                  const std::string& recordName,
00189                                                  bool withlogging=false){
00190         this->appendSinceTime<T>(payloadObj, 0, sinceTime, recordName, withlogging);
00191       }
00192       
00193       template<typename T>
00194       void appendSinceTime( T* payloadObj, Summary * summary,
00195                             cond::Time_t sinceTime,
00196                               const std::string& recordName,
00197                             bool withlogging=false){
00198         add( GetTokenFromPointer<T>(payloadObj,summary),
00199              sinceTime,
00200              recordName,
00201              withlogging);
00202       }
00203       
00204       // Append the payload and its valid sinceTime into the database
00205       // Note: user looses the ownership of the pointer to the payloadObj
00206       // Note: the iov index appended to MUST pre-existing and the existing 
00207       // conditions data are retrieved from the DB
00208       // 
00209       void appendSinceTime( const std::string& payloadToken,
00210                             cond::Time_t sinceTime,
00211                             const std::string& recordName,
00212                             bool withlogging=false) {
00213         add(GetTrivialToken(payloadToken),
00214             sinceTime,
00215             recordName,
00216             withlogging);
00217       }
00218      
00219       // set last till so that the iov sequence is "closed"
00220       // void closeSequence(cond::Time_t tillTime,
00221       //                 const std::string& recordName,
00222       //                 bool withlogging=false);
00223 
00224 
00225       //
00226       // Service time utility method 
00227       // return the infinity value according to the given timetype
00228       //
00229       cond::Time_t endOfTime() const;
00230       //
00231       // Service time utility method 
00232       // return beginning of time value according to the given timetype
00233       //
00234       cond::Time_t beginOfTime() const;
00235       //
00236       // Service time utility method 
00237       // return the time value of the current edm::Event according to the 
00238       // given timetype
00239       //
00240       cond::Time_t currentTime() const;
00241 
00242       // optional. User can inject additional information into the log associated with a given record
00243       void setLogHeaderForRecord(const std::string& recordName,
00244                                  const std::string& provenance,
00245                                  const std::string& usertext);
00246       // 
00247       // Retrieve tag information of the data
00248       // 
00249       void tagInfo(const std::string& recordName,
00250                    cond::TagInfo& result );
00251       
00252       virtual ~PoolDBOutputService();  
00253       
00254     private:
00255 
00256       struct Record{
00257         Record(): m_tag(),m_isNewTag(false),
00258                   m_idName(),
00259                   m_iovtoken(),
00260                   m_closeIOV(false),
00261                   m_freeInsert(false)
00262         {}
00263 
00264         std::string timetypestr() const { return cond::timeTypeSpecs[m_timetype].name;}
00265         std::string m_tag;
00266         bool m_isNewTag;
00267         std::string m_idName;
00268         std::string m_iovtoken;
00269         cond::TimeType m_timetype;
00270         bool m_closeIOV;
00271         bool m_freeInsert;
00272     };      
00273 
00274 
00275 
00276       void fillRecord( edm::ParameterSet & pset);
00277       
00278       void createNewIOV( GetToken const & token, 
00279                          cond::Time_t firstSinceTime, 
00280                          cond::Time_t firstTillTime,
00281                          const std::string& recordName,
00282                          bool withlogging=false);
00283       
00284       void add( GetToken const & token,  
00285                 cond::Time_t time,
00286                 const std::string& recordName,
00287                 bool withlogging=false);      
00288       
00289       void connect();    
00290       void disconnect();
00291       void initDB( bool forReading=true );
00292       unsigned int appendIOV(cond::DbSession&,
00293                              Record& record,
00294                              const std::string& payloadToken,
00295                              cond::Time_t sinceTime);
00296       
00298       unsigned int 
00299       insertIOV(cond::DbSession& pooldb,
00300                 Record& record,
00301                 const std::string& payloadToken,
00302                 cond::Time_t tillTime);
00303 
00304       Record & lookUpRecord(const std::string& recordName);
00305       cond::UserLogInfo& lookUpUserLogInfo(const std::string& recordName);
00306       
00307     private:
00308       cond::TimeType m_timetype; 
00309       std::string m_timetypestr;
00310       cond::Time_t m_currentTime;
00311       cond::DbConnection m_connection;
00312       cond::DbSession m_session;
00313       cond::DbSession m_logSession;
00314       std::map<std::string, Record> m_callbacks;
00315       std::vector< std::pair<std::string,std::string> > m_newtags;
00316       bool m_dbstarted;
00317       cond::Logger* m_logdb;
00318       bool m_logdbOn;
00319 
00320       bool m_closeIOV;
00321       
00322       bool m_freeInsert;
00323       
00324       std::map<std::string, cond::UserLogInfo> m_logheaders;
00325 
00326     };//PoolDBOutputService
00327   }//ns service
00328 }//ns cond
00329 #endif