CMS 3D CMS Logo

PopConSourceHandler.h
Go to the documentation of this file.
1 #ifndef PopConSourceHandler_H
2 #define PopConSourceHandler_H
3 
6 
7 #include <boost/bind.hpp>
8 #include <algorithm>
9 #include <memory>
10 #include <vector>
11 #include <string>
12 
13 namespace cond {
14  class Summary;
15 }
16 
18 
19 namespace popcon {
20 
29  template <class T>
31  public:
32  typedef T value_type;
33  typedef PopConSourceHandler<T> self;
36 
37  struct Triplet {
38  value_type* payload;
39  Summary* summary;
40  Time_t time;
41  };
42 
43  typedef std::vector<Triplet> Container;
44 
45  typedef std::vector<std::pair<T*, cond::Time_t> > OldContainer;
46 
47  class Ref {
48  public:
49  Ref() : m_dbsession() {}
50  Ref(cond::persistency::Session& dbsession, const std::string& hash) : m_dbsession(dbsession) {
51  m_d = m_dbsession.fetchPayload<T>(hash);
52  }
53  ~Ref() {}
54 
55  Ref(const Ref& ref) : m_dbsession(ref.m_dbsession), m_d(ref.m_d) {}
56 
57  Ref& operator=(const Ref& ref) {
58  m_dbsession = ref.m_dbsession;
59  m_d = ref.m_d;
60  return *this;
61  }
62 
63  T const* ptr() const { return m_d.get(); }
64 
65  T const* operator->() const { return ptr(); }
66  // dereference operator
67  T const& operator*() const { return *ptr(); }
68 
69  private:
71  std::shared_ptr<T> m_d;
72  };
73 
74  PopConSourceHandler() : m_tagInfo(nullptr), m_logDBEntry(nullptr) {}
75 
76  virtual ~PopConSourceHandler() {}
77 
78  cond::TagInfo_t const& tagInfo() const { return *m_tagInfo; }
79 
80  // return last paylod of the tag
81  Ref lastPayload() const { return Ref(m_session, tagInfo().lastPayloadToken); }
82 
83  // return last successful log entry for the tag in question
84  cond::LogDBEntry_t const& logDBEntry() const { return *m_logDBEntry; }
85 
86  // FIX ME
87  void initialize(const cond::persistency::Session& dbSession,
88  cond::TagInfo_t const& tagInfo,
89  cond::LogDBEntry_t const& logDBEntry) {
90  m_session = dbSession;
91  m_tagInfo = &tagInfo;
92  m_logDBEntry = &logDBEntry;
93  }
94 
95  // this is the only mandatory interface
96  std::pair<Container const*, std::string const> operator()(const cond::persistency::Session& session,
97  cond::TagInfo_t const& tagInfo,
98  cond::LogDBEntry_t const& logDBEntry) const {
99  const_cast<self*>(this)->initialize(session, tagInfo, logDBEntry);
100  return std::pair<Container const*, std::string const>(&(const_cast<self*>(this)->returnData()), userTextLog());
101  }
102 
103  Container const& returnData() {
104  getNewObjects();
105  if (!m_to_transfer.empty())
106  convertFromOld();
107  sort();
108  return m_triplets;
109  }
110 
111  std::string const& userTextLog() const { return m_userTextLog; }
112 
113  //Implement to fill m_to_transfer vector and m_userTextLog
114  //use getOfflineInfo to get the contents of offline DB
115  virtual void getNewObjects() = 0;
116 
117  // return a string identifing the source
118  virtual std::string id() const = 0;
119 
120  void sort() {
121  std::sort(m_triplets.begin(),
122  m_triplets.end(),
123  boost::bind(std::less<cond::Time_t>(),
124  boost::bind(&Container::value_type::time, _1),
125  boost::bind(&Container::value_type::time, _2)));
126  }
127 
128  // make sure to create a new one each time...
129  Summary* dummySummary(typename OldContainer::value_type const&) const {
130  return new cond::GenericSummary("not supplied");
131  }
132 
133  void convertFromOld() {
134  std::for_each(m_to_transfer.begin(),
135  m_to_transfer.end(),
136  boost::bind(&self::add,
137  this,
138  boost::bind(&OldContainer::value_type::first, _1),
139  boost::bind(&self::dummySummary, this, _1),
140  boost::bind(&OldContainer::value_type::second, _1)));
141  }
142 
143  protected:
144  cond::persistency::Session& dbSession() const { return m_session; }
145 
146  int add(value_type* payload, Summary* summary, Time_t time) {
147  Triplet t = {payload, summary, time};
148  m_triplets.push_back(t);
149  return m_triplets.size();
150  }
151 
152  private:
154 
156 
158 
159  protected:
160  //vector of payload objects and iovinfo to be transferred
161  //class looses ownership of payload object
162  OldContainer m_to_transfer;
163 
164  private:
165  Container m_triplets;
166 
167  protected:
169  };
170 } // namespace popcon
171 #endif
cond::persistency::Session & dbSession() const
static AlgebraicMatrix initialize()
cond::persistency::Session m_dbsession
std::string const & userTextLog() const
#define nullptr
cond::LogDBEntry_t const * m_logDBEntry
std::vector< Triplet > Container
U second(std::pair< T, U > const &p)
unsigned long long Time_t
Definition: Time.h:14
cond::LogDBEntry_t const & logDBEntry() const
void initialize(const cond::persistency::Session &dbSession, cond::TagInfo_t const &tagInfo, cond::LogDBEntry_t const &logDBEntry)
Summary * dummySummary(typename OldContainer::value_type const &) const
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
Definition: plugin.cc:23
Ref(cond::persistency::Session &dbsession, const std::string &hash)
cond::TagInfo_t const * m_tagInfo
std::pair< Container const *, std::string const > operator()(const cond::persistency::Session &session, cond::TagInfo_t const &tagInfo, cond::LogDBEntry_t const &logDBEntry) const
cond::persistency::Session m_session
long double T
std::vector< std::pair< T *, cond::Time_t > > OldContainer
int add(value_type *payload, Summary *summary, Time_t time)
cond::TagInfo_t const & tagInfo() const