CMS 3D CMS Logo

PopConSourceHandler.h

Go to the documentation of this file.
00001 #ifndef  PopConSourceHandler_H
00002 #define  PopConSourceHandler_H
00003 
00004 #include "CondCore/DBCommon/interface/PoolTransaction.h"
00005 #include "CondCore/DBCommon/interface/TypedRef.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 popcon {
00017 
00026   template <class T>
00027     class PopConSourceHandler{
00028     public: 
00029     typedef T value_type;
00030     typedef PopConSourceHandler<T> self;
00031     typedef std::vector<std::pair<T*, cond::Time_t> > Container;
00032     typedef cond::Time_t Time_t;
00033 
00034     class Ref : public cond::TypedRef<value_type>  {
00035     public:
00036       Ref() : m_pooldb(0){}
00037       Ref(cond::PoolTransaction& pooldb, std::string token) : 
00038         m_pooldb(&pooldb){
00039         m_pooldb->start(true);
00040         (cond::TypedRef<value_type>&)(*this) = cond::TypedRef<value_type>(pooldb,token);
00041       }
00042       ~Ref() {
00043         if (m_pooldb)
00044           m_pooldb->commit();
00045       }
00046 
00047       Ref(const Ref & ref) : 
00048         cond::TypedRef<value_type>(ref), m_pooldb(ref.m_pooldb) {
00049         ref.m_pooldb=0; // avoid commit;
00050       }
00051 
00052       Ref & operator=(const Ref & ref) {
00053         cond::TypedRef<value_type>::operator=(ref);
00054         m_pooldb = ref.m_pooldb;
00055         ref.m_pooldb=0; // avoid commit;
00056         return *this;
00057       }
00058 
00059       mutable cond::PoolTransaction *m_pooldb;
00060 
00061     };
00062 
00063  
00064     PopConSourceHandler(){}
00065     
00066     virtual ~PopConSourceHandler(){
00067     }
00068     
00069 
00070     cond::TagInfo const & tagInfo() const { return  *m_tagInfo; }
00071 
00072     // return last paylod of the tag
00073     Ref lastPayload() const {
00074       return Ref(m_connection->poolTransaction(),tagInfo().lastPayloadToken);
00075     }
00076 
00077     // return last successful log entry for the tag in question
00078     cond::LogDBEntry const & logDBEntry() const { return *m_logDBEntry; }
00079 
00080 
00081     void initialize (cond::Connection* connection,
00082                      cond::TagInfo const & tagInfo, cond::LogDBEntry const & logDBEntry) { 
00083       m_connection = connection;
00084       m_tagInfo = &tagInfo;
00085       m_logDBEntry = &logDBEntry;
00086     }
00087 
00088     // this is the only mandatory interface
00089     std::pair<Container const *, std::string const>  operator()(cond::Connection* connection,
00090                                  cond::TagInfo const & tagInfo, 
00091                                  cond::LogDBEntry const & logDBEntry) const {
00092       const_cast<self*>(this)->initialize(connection, tagInfo, logDBEntry);
00093       return std::pair<Container const *, std::string const>(&(const_cast<self*>(this)->returnData()), userTextLog());
00094     }
00095     
00096     Container const &  returnData() {
00097       getNewObjects();
00098       sort();
00099       return m_to_transfer;
00100     }
00101     
00102     std::string const & userTextLog() const { return m_userTextLog; }
00103 
00104 
00105     //Implement to fill m_to_transfer vector and  m_userTextLog
00106     //use getOfflineInfo to get the contents of offline DB
00107     virtual void getNewObjects()=0;
00108 
00109     // return a string identifing the source
00110     virtual std::string id() const=0;
00111 
00112     void sort() {
00113       std::sort(m_to_transfer.begin(),m_to_transfer.end(),
00114                 boost::bind(std::less<cond::Time_t>(),
00115                             boost::bind(&Container::value_type::second,_1),
00116                             boost::bind(&Container::value_type::second,_2)
00117                             )
00118                 );
00119     }
00120 
00121 
00122     
00123   private:
00124     
00125     cond::Connection* m_connection;
00126 
00127     cond::TagInfo const * m_tagInfo;
00128     
00129     cond::LogDBEntry const * m_logDBEntry;
00130     
00131 
00132   protected:
00133     
00134     //vector of payload objects and iovinfo to be transferred
00135     //class looses ownership of payload object
00136     Container m_to_transfer;
00137 
00138     std::string m_userTextLog;
00139 
00140 
00141   };
00142 }
00143 #endif

Generated on Tue Jun 9 17:26:14 2009 for CMSSW by  doxygen 1.5.4