CMS 3D CMS Logo

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