CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/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/Time.h"
00005 #include "CondCore/MetaDataService/interface/MetaData.h"
00006 #include "CondCore/DBCommon/interface/Logger.h"
00007 #include "CondCore/DBCommon/interface/LogDBEntry.h"
00008 #include "CondCore/DBCommon/interface/TagInfo.h"
00009 #include "Reflex/Type.h"
00010 #include <string>
00011 #include <map>
00012 
00013 // many many clients do not include explicitely!
00014 #ifndef COND_EXCEPTION_H
00015 #include "CondCore/DBCommon/interface/Exception.h"
00016 // #warning please include  "CondCore/DBCommon/interface/Exception.h" explicitely
00017 // #define COND_EXP_WARNING
00018 #endif
00019 
00020 //
00021 // Package:     DBOutputService
00022 // Class  :     PoolDBOutputService
00023 // 
00027 //
00028 // Author:      Zhen Xie
00029 // Fixes and other changes: Giacomo Govi
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   
00044   namespace service {
00045 
00053     struct GetToken {
00054       virtual std::string operator()(cond::DbSession&) const =0;
00055     };
00056     
00057     struct GetTrivialToken : public GetToken {
00058       
00059       GetTrivialToken(std::string token) :
00060         m_token(token){}
00061       virtual ~GetTrivialToken(){}
00062       virtual std::string operator()(cond::DbSession&) const {
00063         return m_token;
00064       }
00065       std::string m_token;
00066     };
00067     
00068     template<typename T>
00069     struct GetTokenFromPointer : public GetToken {
00070       
00071       static
00072       std::string classNameForPointer( T* pointer ){
00073         if(!pointer) return classNameForTypeId( typeid(T) );
00074         return classNameForTypeId( typeid(*pointer) );
00075       }
00076 
00077       explicit GetTokenFromPointer(T * p) :
00078         m_p(p){}
00079       
00080       virtual std::string operator()(cond::DbSession& pooldb) const {
00081         std::string className = classNameForPointer( m_p );
00082         boost::shared_ptr<T> sptr( m_p );
00083         return pooldb.storeObject(m_p,className);
00084       }
00085       T* m_p;
00086     };
00087 
00088     
00089     class PoolDBOutputService{
00090     public:
00091       PoolDBOutputService( const edm::ParameterSet & iConfig, 
00092                            edm::ActivityRegistry & iAR );
00093       //use these to control connections
00094       //void  postBeginJob();
00095       void  postEndJob();
00096       //
00097       //use these to control transaction interval
00098       //
00099       void preEventProcessing( const edm::EventID & evtID, 
00100                                const edm::Timestamp & iTime );
00101       void preModule(const edm::ModuleDescription& desc);
00102       void postModule(const edm::ModuleDescription& desc);
00103       void preBeginLumi(const edm::LuminosityBlockID&, 
00104                         const edm::Timestamp& );
00105       //
00106       // return the database session in use
00107       //
00108       cond::DbSession session() const;
00109       //
00110       std::string tag( const std::string& recordName );
00111       bool isNewTagRequest( const std::string& recordName );
00112       const cond::Logger& queryLog() const;
00113       
00114       // 
00115       template<typename T>
00116       void writeOne( T * payload, Time_t time, const std::string& recordName, bool withlogging=false ) {
00117         if (isNewTagRequest(recordName) ){
00118           createNewIOV<T>(payload, time, endOfTime(), recordName, withlogging);
00119         }else{
00120           appendSinceTime<T>(payload, time, recordName, withlogging);
00121         }       
00122       }
00123 
00124       // close the IOVSequence setting lastTill
00125       void closeIOV(Time_t lastTill, const std::string& recordName, 
00126                     bool withlogging=false);
00127 
00128       // 
00129       template<typename T>
00130       void createNewIOV( T* firstPayloadObj,
00131                          cond::Time_t firstSinceTime,
00132                          cond::Time_t firstTillTime,
00133                          const std::string& recordName,
00134                          bool withlogging=false){
00135         createNewIOV( GetTokenFromPointer<T>(firstPayloadObj),
00136                       firstSinceTime,
00137                       firstTillTime,
00138                       recordName,
00139                       withlogging);     
00140       }
00141             
00142       void createNewIOV( const std::string& firstPayloadToken,
00143                          cond::Time_t firstSinceTime,
00144                          cond::Time_t firstTillTime,
00145                          const std::string& recordName,
00146                          bool withlogging=false) {
00147         createNewIOV( GetTrivialToken(firstPayloadToken),
00148                       firstSinceTime,
00149                       firstTillTime,
00150                       recordName,
00151                       withlogging);
00152       }
00153 
00154       
00155       // 
00156       template<typename T> void appendSinceTime( T* payloadObj,
00157                                                  cond::Time_t sinceTime,
00158                                                  const std::string& recordName,
00159                                                  bool withlogging=false){
00160         add( GetTokenFromPointer<T>(payloadObj),
00161              sinceTime,
00162              recordName,
00163              withlogging);
00164       }
00165       
00166       // Append the payload and its valid sinceTime into the database
00167       // Note: user looses the ownership of the pointer to the payloadObj
00168       // Note: the iov index appended to MUST pre-existing and the existing 
00169       // conditions data are retrieved from the DB
00170       // 
00171       void appendSinceTime( const std::string& payloadToken,
00172                             cond::Time_t sinceTime,
00173                             const std::string& recordName,
00174                             bool withlogging=false) {
00175         add(GetTrivialToken(payloadToken),
00176             sinceTime,
00177             recordName,
00178             withlogging);
00179       }
00180      
00181       // set last till so that the iov sequence is "closed"
00182       // void closeSequence(cond::Time_t tillTime,
00183       //                 const std::string& recordName,
00184       //                 bool withlogging=false);
00185 
00186 
00187       //
00188       // Service time utility method 
00189       // return the infinity value according to the given timetype
00190       //
00191       cond::Time_t endOfTime() const;
00192       //
00193       // Service time utility method 
00194       // return beginning of time value according to the given timetype
00195       //
00196       cond::Time_t beginOfTime() const;
00197       //
00198       // Service time utility method 
00199       // return the time value of the current edm::Event according to the 
00200       // given timetype
00201       //
00202       cond::Time_t currentTime() const;
00203 
00204       // optional. User can inject additional information into the log associated with a given record
00205       void setLogHeaderForRecord(const std::string& recordName,
00206                                  const std::string& provenance,
00207                                  const std::string& usertext);
00208       // 
00209       // Retrieve tag information of the data
00210       // 
00211       void tagInfo(const std::string& recordName,
00212                    cond::TagInfo& result );
00213       
00214       virtual ~PoolDBOutputService();  
00215       
00216     private:
00217 
00218       struct Record{
00219         Record(): m_tag(),
00220                   m_isNewTag(false),
00221                   m_idName(),
00222                   m_iovtoken(),
00223                   m_timetype(cond::runnumber),
00224                   m_closeIOV(false),
00225                   m_freeInsert(false)
00226         {}
00227 
00228         std::string timetypestr() const { return cond::timeTypeSpecs[m_timetype].name;}
00229         std::string m_tag;
00230         bool m_isNewTag;
00231         std::string m_idName;
00232         std::string m_iovtoken;
00233         cond::TimeType m_timetype;
00234         bool m_closeIOV;
00235         bool m_freeInsert;
00236     };      
00237 
00238 
00239 
00240       void fillRecord( edm::ParameterSet & pset);
00241       
00242       void createNewIOV( GetToken const & token, 
00243                          cond::Time_t firstSinceTime, 
00244                          cond::Time_t firstTillTime,
00245                          const std::string& recordName,
00246                          bool withlogging=false);
00247       
00248       void add( GetToken const & token,  
00249                 cond::Time_t time,
00250                 const std::string& recordName,
00251                 bool withlogging=false);      
00252       
00253       void connect();    
00254       void disconnect();
00255       void initDB( bool forReading=true );
00256       unsigned int appendIOV(cond::DbSession&,
00257                              Record& record,
00258                              const std::string& payloadToken,
00259                              cond::Time_t sinceTime);
00260       
00262       unsigned int 
00263       insertIOV(cond::DbSession& pooldb,
00264                 Record& record,
00265                 const std::string& payloadToken,
00266                 cond::Time_t tillTime);
00267 
00268       Record & lookUpRecord(const std::string& recordName);
00269       cond::UserLogInfo& lookUpUserLogInfo(const std::string& recordName);
00270       
00271     private:
00272       cond::TimeType m_timetype; 
00273       std::string m_timetypestr;
00274       cond::Time_t m_currentTime;
00275 
00276       std::string m_connectionString;
00277       cond::DbSession m_session;
00278       std::string m_logConnectionString;
00279       std::auto_ptr<cond::Logger> m_logdb;
00280       bool m_dbstarted;
00281 
00282       std::map<std::string, Record> m_callbacks;
00283       std::vector< std::pair<std::string,std::string> > m_newtags;
00284       bool m_closeIOV;
00285       bool m_freeInsert;
00286       std::map<std::string, cond::UserLogInfo> m_logheaders;
00287 
00288     };//PoolDBOutputService
00289   }//ns service
00290 }//ns cond
00291 #endif