CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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       m_tagInfo(0),
00095       m_logDBEntry(0){}
00096     
00097     virtual ~PopConSourceHandler(){
00098     }
00099     
00100     
00101     cond::TagInfo const & tagInfo() const { return  *m_tagInfo; }
00102     
00103     // return last paylod of the tag
00104     Ref lastPayload() const {
00105       return Ref(m_session,tagInfo().lastPayloadToken);
00106     }
00107     
00108     // return last successful log entry for the tag in question
00109     cond::LogDBEntry const & logDBEntry() const { return *m_logDBEntry; }
00110     
00111     
00112     void initialize (cond::DbSession dbSession,
00113                      cond::TagInfo const & tagInfo, cond::LogDBEntry const & logDBEntry) { 
00114       m_session = dbSession;
00115       m_tagInfo = &tagInfo;
00116       m_logDBEntry = &logDBEntry;
00117     }
00118     
00119     // this is the only mandatory interface
00120     std::pair<Container const *, std::string const>  operator()(cond::DbSession session,
00121                                                                 cond::TagInfo const & tagInfo, 
00122                                                                 cond::LogDBEntry const & logDBEntry) const {
00123       const_cast<self*>(this)->initialize(session, tagInfo, logDBEntry);
00124       return std::pair<Container const *, std::string const>(&(const_cast<self*>(this)->returnData()), userTextLog());
00125     }
00126     
00127     Container const &  returnData() {
00128       getNewObjects();
00129       if (!m_to_transfer.empty()) convertFromOld();
00130       sort();
00131       return m_triplets;
00132     }
00133     
00134     std::string const & userTextLog() const { return m_userTextLog; }
00135     
00136     //Implement to fill m_to_transfer vector and  m_userTextLog
00137     //use getOfflineInfo to get the contents of offline DB
00138     virtual void getNewObjects()=0;
00139     
00140     // return a string identifing the source
00141     virtual std::string id() const=0;
00142     
00143     void sort() {
00144       std::sort(m_triplets.begin(),m_triplets.end(),
00145                 boost::bind(std::less<cond::Time_t>(),
00146                             boost::bind(&Container::value_type::time,_1),
00147                             boost::bind(&Container::value_type::time,_2)
00148                             )
00149                 );
00150     }
00151     
00152     
00153     // make sure to create a new one each time...
00154     Summary * dummySummary(typename OldContainer::value_type const &) const {
00155       return new cond::GenericSummary("not supplied");
00156     }
00157     
00158     void convertFromOld() {
00159       std::for_each( m_to_transfer.begin(), m_to_transfer.end(),
00160                      boost::bind(&self::add, this,
00161                                  boost::bind(&OldContainer::value_type::first,_1),
00162                                  boost::bind(&self::dummySummary, this, _1),
00163                                  boost::bind(&OldContainer::value_type::second,_1)
00164                                  ));
00165     }
00166     
00167   protected:
00168     
00169     
00170     int add(value_type * payload, Summary * summary, Time_t time) {
00171       Triplet t = {payload,summary,time};
00172       m_triplets.push_back(t);
00173       return m_triplets.size();
00174     }
00175 
00176   private:
00177     
00178     mutable cond::DbSession m_session;
00179     
00180     cond::TagInfo const * m_tagInfo;
00181     
00182     cond::LogDBEntry const * m_logDBEntry;
00183     
00184 
00185   protected:
00186     
00187     //vector of payload objects and iovinfo to be transferred
00188     //class looses ownership of payload object
00189     OldContainer m_to_transfer;
00190 
00191     private:
00192     Container m_triplets;
00193 
00194   protected:
00195     std::string m_userTextLog;
00196 
00197 
00198   };
00199 }
00200 #endif