CMS 3D CMS Logo

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