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_dbsession.transaction().start(true);
00059 m_d = m_dbsession.getTypedObject<T>(token);
00060 }
00061 ~Ref() {
00062 if(m_dbsession.isOpen())
00063 m_dbsession.transaction().commit();
00064 }
00065
00066 Ref(const Ref & ref) :
00067 m_dbsession(ref.m_dbsession), m_d(ref.m_d) {
00068
00069 }
00070
00071 Ref & operator=(const Ref & ref) {
00072 m_dbsession = ref.m_dbsession;
00073 m_d = ref.m_d;
00074
00075 return *this;
00076 }
00077
00078 T const * ptr() const {
00079 return m_d.get();
00080 }
00081
00082 T const * operator->() const {
00083 return ptr();
00084 }
00085
00086 T const & operator*() const {
00087 return *ptr();
00088 }
00089
00090
00091 private:
00092
00093 cond::DbSession m_dbsession;
00094 boost::shared_ptr<T> m_d;
00095 };
00096
00097
00098 PopConSourceHandler(){}
00099
00100 virtual ~PopConSourceHandler(){
00101 }
00102
00103
00104 cond::TagInfo const & tagInfo() const { return *m_tagInfo; }
00105
00106
00107 Ref lastPayload() const {
00108 return Ref(m_session,tagInfo().lastPayloadToken);
00109 }
00110
00111
00112 cond::LogDBEntry const & logDBEntry() const { return *m_logDBEntry; }
00113
00114
00115 void initialize (cond::DbSession dbSession,
00116 cond::TagInfo const & tagInfo, cond::LogDBEntry const & logDBEntry) {
00117 m_session = dbSession;
00118 m_tagInfo = &tagInfo;
00119 m_logDBEntry = &logDBEntry;
00120 }
00121
00122
00123 std::pair<Container const *, std::string const> operator()(cond::DbSession session,
00124 cond::TagInfo const & tagInfo,
00125 cond::LogDBEntry const & logDBEntry) const {
00126 const_cast<self*>(this)->initialize(session, tagInfo, logDBEntry);
00127 return std::pair<Container const *, std::string const>(&(const_cast<self*>(this)->returnData()), userTextLog());
00128 }
00129
00130 Container const & returnData() {
00131 getNewObjects();
00132 if (!m_to_transfer.empty()) convertFromOld();
00133 sort();
00134 return m_triplets;
00135 }
00136
00137 std::string const & userTextLog() const { return m_userTextLog; }
00138
00139
00140
00141 virtual void getNewObjects()=0;
00142
00143
00144 virtual std::string id() const=0;
00145
00146 void sort() {
00147 std::sort(m_triplets.begin(),m_triplets.end(),
00148 boost::bind(std::less<cond::Time_t>(),
00149 boost::bind(&Container::value_type::time,_1),
00150 boost::bind(&Container::value_type::time,_2)
00151 )
00152 );
00153 }
00154
00155
00156
00157 Summary * dummySummary(typename OldContainer::value_type const &) const {
00158 return new cond::GenericSummary("not supplied");
00159 }
00160
00161 void convertFromOld() {
00162 std::for_each( m_to_transfer.begin(), m_to_transfer.end(),
00163 boost::bind(&self::add, this,
00164 boost::bind(&OldContainer::value_type::first,_1),
00165 boost::bind(&self::dummySummary, this, _1),
00166 boost::bind(&OldContainer::value_type::second,_1)
00167 ));
00168 }
00169
00170 protected:
00171
00172
00173 int add(value_type * payload, Summary * summary, Time_t time) {
00174 Triplet t = {payload,summary,time};
00175 m_triplets.push_back(t);
00176 return m_triplets.size();
00177 }
00178
00179 private:
00180
00181 mutable cond::DbSession m_session;
00182
00183 cond::TagInfo const * m_tagInfo;
00184
00185 cond::LogDBEntry const * m_logDBEntry;
00186
00187
00188 protected:
00189
00190
00191
00192 OldContainer m_to_transfer;
00193
00194 private:
00195 Container m_triplets;
00196
00197 protected:
00198 std::string m_userTextLog;
00199
00200
00201 };
00202 }
00203 #endif