CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CondCore/PopCon/interface/PopConSourceHandler.h

Go to the documentation of this file.
00001 #ifndef  PopConSourceHandler_H
00002 #define  PopConSourceHandler_H
00003 
00004 #include "CondCore/DBCommon/interface/DbSession.h"
00005 #include "CondCore/DBCommon/interface/DbTransaction.h"
00006 
00007 #include "CondCore/DBCommon/interface/Time.h"
00008 #include "CondCore/DBCommon/interface/TagInfo.h"
00009 #include "CondCore/DBCommon/interface/LogDBEntry.h"
00010 
00011 #include <boost/bind.hpp>
00012 #include <algorithm>
00013 #include <vector>
00014 #include <string>
00015 
00016 namespace cond {
00017   class Summary;
00018 }
00019 
00020 #include "CondFormats/Common/interface/GenericSummary.h"
00021 
00022 
00023 
00024 namespace popcon {
00025 
00034   template <class T>
00035   class PopConSourceHandler{
00036   public: 
00037     typedef T value_type;
00038     typedef PopConSourceHandler<T> self;
00039     typedef cond::Time_t Time_t;
00040     typedef cond::Summary Summary;
00041     
00042     struct Triplet {
00043       value_type * payload;
00044       Summary * summary;
00045       Time_t time;
00046     };
00047     
00048     typedef std::vector<Triplet> Container;
00049     
00050     typedef std::vector<std::pair<T*, cond::Time_t> > OldContainer;
00051     
00052     
00053     class Ref {
00054     public:
00055       Ref() : m_dbsession(){}
00056       Ref(cond::DbSession& dbsession, std::string token) : 
00057         m_dbsession(dbsession){
00058         m_d = m_dbsession.getTypedObject<T>(token);
00059       }
00060       ~Ref() {
00061       }
00062       
00063       Ref(const Ref & ref) :
00064         m_dbsession(ref.m_dbsession), m_d(ref.m_d) {
00065       }
00066       
00067       Ref & operator=(const Ref & ref) {
00068         m_dbsession = ref.m_dbsession;
00069         m_d = ref.m_d;
00070         return *this;
00071       }
00072       
00073       T const * ptr() const {
00074         return m_d.get();
00075       }
00076       
00077       T const * operator->() const {
00078         return ptr();
00079       }
00080       // dereference operator
00081       T const & operator*() const {
00082         return *ptr();
00083       }
00084       
00085       
00086     private:
00087       
00088       cond::DbSession m_dbsession;
00089       boost::shared_ptr<T> m_d;
00090     };
00091     
00092     
00093     PopConSourceHandler(){}
00094     
00095     virtual ~PopConSourceHandler(){
00096     }
00097     
00098     
00099     cond::TagInfo const & tagInfo() const { return  *m_tagInfo; }
00100     
00101     // return last paylod of the tag
00102     Ref lastPayload() const {
00103       return Ref(m_session,tagInfo().lastPayloadToken);
00104     }
00105     
00106     // return last successful log entry for the tag in question
00107     cond::LogDBEntry const & logDBEntry() const { return *m_logDBEntry; }
00108     
00109     
00110     void initialize (cond::DbSession dbSession,
00111                      cond::TagInfo const & tagInfo, cond::LogDBEntry const & logDBEntry) { 
00112       m_session = dbSession;
00113       m_tagInfo = &tagInfo;
00114       m_logDBEntry = &logDBEntry;
00115     }
00116     
00117     // this is the only mandatory interface
00118     std::pair<Container const *, std::string const>  operator()(cond::DbSession session,
00119                                                                 cond::TagInfo const & tagInfo, 
00120                                                                 cond::LogDBEntry const & logDBEntry) const {
00121       const_cast<self*>(this)->initialize(session, tagInfo, logDBEntry);
00122       return std::pair<Container const *, std::string const>(&(const_cast<self*>(this)->returnData()), userTextLog());
00123     }
00124     
00125     Container const &  returnData() {
00126       getNewObjects();
00127       if (!m_to_transfer.empty()) convertFromOld();
00128       sort();
00129       return m_triplets;
00130     }
00131     
00132     std::string const & userTextLog() const { return m_userTextLog; }
00133     
00134     //Implement to fill m_to_transfer vector and  m_userTextLog
00135     //use getOfflineInfo to get the contents of offline DB
00136     virtual void getNewObjects()=0;
00137     
00138     // return a string identifing the source
00139     virtual std::string id() const=0;
00140     
00141     void sort() {
00142       std::sort(m_triplets.begin(),m_triplets.end(),
00143                 boost::bind(std::less<cond::Time_t>(),
00144                             boost::bind(&Container::value_type::time,_1),
00145                             boost::bind(&Container::value_type::time,_2)
00146                             )
00147                 );
00148     }
00149     
00150     
00151     // make sure to create a new one each time...
00152     Summary * dummySummary(typename OldContainer::value_type const &) const {
00153       return new cond::GenericSummary("not supplied");
00154     }
00155     
00156     void convertFromOld() {
00157       std::for_each( m_to_transfer.begin(), m_to_transfer.end(),
00158                      boost::bind(&self::add, this,
00159                                  boost::bind(&OldContainer::value_type::first,_1),
00160                                  boost::bind(&self::dummySummary, this, _1),
00161                                  boost::bind(&OldContainer::value_type::second,_1)
00162                                  ));
00163     }
00164     
00165   protected:
00166     
00167     
00168     int add(value_type * payload, Summary * summary, Time_t time) {
00169       Triplet t = {payload,summary,time};
00170       m_triplets.push_back(t);
00171       return m_triplets.size();
00172     }
00173 
00174   private:
00175     
00176     mutable cond::DbSession m_session;
00177     
00178     cond::TagInfo const * m_tagInfo;
00179     
00180     cond::LogDBEntry const * m_logDBEntry;
00181     
00182 
00183   protected:
00184     
00185     //vector of payload objects and iovinfo to be transferred
00186     //class looses ownership of payload object
00187     OldContainer m_to_transfer;
00188 
00189     private:
00190     Container m_triplets;
00191 
00192   protected:
00193     std::string m_userTextLog;
00194 
00195 
00196   };
00197 }
00198 #endif