CMS 3D CMS Logo

IOVProxy.cc
Go to the documentation of this file.
1 #include <memory>
2 
4 #include "SessionImpl.h"
5 
6 namespace cond {
7 
8  namespace persistency {
9 
10  // comparison functor for iov tuples: Time_t only and Time_t,string
11  struct IOVComp {
12  bool operator()(const cond::Time_t& x, const cond::Time_t& y) { return (x < y); }
13 
14  bool operator()(const cond::Time_t& x, const std::tuple<cond::Time_t, cond::Hash>& y) {
15  return (x < std::get<0>(y));
16  }
17  };
18 
19  // function to search in the vector the target time
20  template <typename T>
21  typename std::vector<T>::const_iterator search(const cond::Time_t& val, const std::vector<T>& container) {
22  if (container.empty())
23  return container.end();
24  auto p = std::upper_bound(container.begin(), container.end(), val, IOVComp());
25  return (p != container.begin()) ? p - 1 : container.end();
26  }
27 
28  IOVArray::Iterator::Iterator() : m_current(), m_parent(nullptr) {}
29 
30  IOVArray::Iterator::Iterator(IOVContainer::const_iterator current, const IOVArray* parent)
31  : m_current(current), m_parent(parent) {}
32 
33  IOVArray::Iterator::Iterator(const Iterator& rhs) : m_current(rhs.m_current), m_parent(rhs.m_parent) {}
34 
36  if (this != &rhs) {
37  m_current = rhs.m_current;
38  m_parent = rhs.m_parent;
39  }
40  return *this;
41  }
42 
44  cond::Iov_t retVal;
45  retVal.since = std::get<0>(*m_current);
46  auto next = m_current;
47  next++;
48  if (next == m_parent->m_array->end()) {
49  retVal.till = cond::time::MAX_VAL;
50  } else {
51  retVal.till = cond::time::tillTimeFromNextSince(std::get<0>(*next), m_parent->m_tagInfo.timeType);
52  }
53  // default is the end of validity when set...
54  if (retVal.till > m_parent->m_tagInfo.endOfValidity) {
55  retVal.till = m_parent->m_tagInfo.endOfValidity;
56  }
57  retVal.payloadId = std::get<1>(*m_current);
58 
59  return retVal;
60  }
61 
63  m_current++;
64  return *this;
65  }
66 
68  Iterator tmp(*this);
69  operator++();
70  return tmp;
71  }
72 
73  bool IOVArray::Iterator::operator==(const Iterator& rhs) const {
74  if (m_current != rhs.m_current)
75  return false;
76  if (m_parent != rhs.m_parent)
77  return false;
78  return true;
79  }
80 
81  bool IOVArray::Iterator::operator!=(const Iterator& rhs) const { return !operator==(rhs); }
82 
84 
85  IOVArray::IOVArray(const IOVArray& rhs) : m_array(), m_tagInfo(rhs.m_tagInfo) {
86  m_array = std::make_unique<IOVContainer>(*rhs.m_array);
87  }
88 
90  m_array = std::make_unique<IOVContainer>(*rhs.m_array);
91  m_tagInfo = rhs.m_tagInfo;
92  return *this;
93  }
94 
95  const cond::Tag_t& IOVArray::tagInfo() const { return m_tagInfo; }
96 
97  IOVArray::Iterator IOVArray::begin() const { return Iterator(m_array->begin(), this); }
98 
99  IOVArray::Iterator IOVArray::end() const { return Iterator(m_array->end(), this); }
100 
102 
103  size_t IOVArray::size() const { return m_array->size(); }
104 
105  // returns true if at least one IOV is in the sequence.
106  bool IOVArray::isEmpty() const { return m_array->empty(); }
107 
108  // implementation details...
109  // only hosting data in this case
110  class IOVProxyData {
111  public:
113 
114  // tag data
116  // iov data
117  boost::posix_time::ptime snapshotTime;
120  bool cacheInitialized = false;
121  std::vector<cond::Time_t> sinceGroups;
123  // monitoring data
124  size_t numberOfQueries = 0;
125  };
126 
127  IOVProxy::IOVProxy() : m_data(), m_session() {}
128 
129  IOVProxy::IOVProxy(const std::shared_ptr<SessionImpl>& session) : m_data(new IOVProxyData), m_session(session) {}
130 
131  IOVProxy::IOVProxy(const IOVProxy& rhs) : m_data(rhs.m_data), m_session(rhs.m_session) {}
132 
134  m_data = rhs.m_data;
135  m_session = rhs.m_session;
136  return *this;
137  }
138 
140  boost::posix_time::ptime notime;
141  load(tagName, notime);
142  }
143 
144  void IOVProxy::load(const std::string& tagName, const boost::posix_time::ptime& snapshotTime) {
145  if (!m_data.get())
146  return;
147 
148  // clear
149  reset();
150 
151  checkTransaction("IOVProxyNew::load");
152 
153  int dummy;
154  if (!m_session->iovSchema().tagTable().select(tagName,
155  m_data->tagInfo.timeType,
156  m_data->tagInfo.payloadType,
157  m_data->tagInfo.synchronizationType,
158  m_data->tagInfo.endOfValidity,
159  m_data->tagInfo.lastValidatedTime,
160  dummy)) {
161  throwException("Tag \"" + tagName + "\" has not been found in the database.", "IOVProxy::load");
162  }
163  m_data->tagInfo.name = tagName;
164  m_data->snapshotTime = snapshotTime;
165  }
166 
168  //if( !m_data.get() ) return;
169 
170  // clear
171  resetIOVCache();
172 
173  //checkTransaction( "IOVProxyNew::load" );
174  m_session->iovSchema().iovTable().getGroups(m_data->tagInfo.name,
175  m_data->snapshotTime,
176  cond::time::sinceGroupSize(m_data->tagInfo.timeType),
177  m_data->sinceGroups);
178  m_data->cacheInitialized = true;
179  }
180 
182  boost::posix_time::ptime no_time;
183  return selectAll(no_time);
184  }
185 
186  IOVArray IOVProxy::selectAll(const boost::posix_time::ptime& snapshottime) {
187  if (!m_data.get())
188  throwException("No tag has been loaded.", "IOVProxy::selectAll");
189  checkTransaction("IOVProxy::selectAll");
190  IOVArray ret;
191  ret.m_tagInfo = m_data->tagInfo;
192  m_session->iovSchema().iovTable().select(
193  m_data->tagInfo.name, cond::time::MIN_VAL, cond::time::MAX_VAL, snapshottime, *ret.m_array);
194  return ret;
195  }
196 
198  boost::posix_time::ptime no_time;
199  return selectRange(begin, end, no_time);
200  }
201 
203  const cond::Time_t& end,
204  const boost::posix_time::ptime& snapshotTime) {
205  if (!m_data.get())
206  throwException("No tag has been loaded.", "IOVProxy::selectRange");
207 
208  checkTransaction("IOVProxy::selectRange");
209 
210  IOVArray ret;
211  ret.m_tagInfo = m_data->tagInfo;
212  m_session->iovSchema().iovTable().getRange(m_data->tagInfo.name, begin, end, snapshotTime, *ret.m_array);
213  return ret;
214  }
215 
217  if (!m_data.get())
218  throwException("No tag has been loaded.", "IOVProxy::selectRange");
219 
220  checkTransaction("IOVProxy::selectRange");
221 
222  boost::posix_time::ptime no_time;
223  size_t prevSize = destination.size();
224  m_session->iovSchema().iovTable().getRange(m_data->tagInfo.name, begin, end, no_time, destination);
225  size_t niov = destination.size() - prevSize;
226  return niov > 0;
227  }
228 
230  if (m_data.get()) {
231  m_data->groupLowerIov = cond::time::MAX_VAL;
232  m_data->groupHigherIov = cond::time::MIN_VAL;
233  m_data->sinceGroups.clear();
234  m_data->iovSequence.clear();
235  m_data->numberOfQueries = 0;
236  }
237  }
238 
240  if (m_data.get()) {
241  m_data->tagInfo.clear();
242  }
243  resetIOVCache();
244  }
245 
246  cond::Tag_t IOVProxy::tagInfo() const { return m_data.get() ? m_data->tagInfo : cond::Tag_t(); }
247 
249  if (endOfValidity < cond::time::MAX_VAL) {
250  if (target.since >= endOfValidity) {
251  target.clear();
252  } else {
253  target.till = endOfValidity;
254  }
255  } else {
257  }
258  }
259 
261  if (!m_data.get())
262  throwException("No tag has been loaded.", "IOVProxy::iovSequenceInfo");
263  checkTransaction("IOVProxy::iovSequenceInfo");
265  m_session->iovSchema().iovTable().getSize(m_data->tagInfo.name, m_data->snapshotTime, ret.size);
267  bool ok = m_session->iovSchema().iovTable().getLastIov(
268  m_data->tagInfo.name, m_data->snapshotTime, ret.lastInterval.since, ret.lastInterval.payloadId);
269  if (ok) {
270  setTillToLastIov(ret.lastInterval, m_data->tagInfo.endOfValidity);
271  }
272  return ret;
273  }
274 
275  std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime> IOVProxy::getMetadata() const {
276  if (!m_data.get())
277  throwException("No tag has been loaded.", "IOVProxy::getMetadata");
278  checkTransaction("IOVProxy::getMetadata");
279  std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime> ret;
280  if (!m_session->iovSchema().tagTable().getMetadata(
281  m_data->tagInfo.name, std::get<0>(ret), std::get<1>(ret), std::get<2>(ret))) {
282  throwException("Metadata for tag \"" + m_data->tagInfo.name + "\" have not been found in the database.",
283  "IOVProxy::getMetadata");
284  }
285  return ret;
286  }
287 
288  void IOVProxy::checkTransaction(const std::string& ctx) const {
289  if (!m_session.get())
290  throwException("The session is not active.", ctx);
291  if (!m_session->isTransactionActive(false))
292  throwException("The transaction is not active.", ctx);
293  }
294 
295  void IOVProxy::fetchSequence(cond::Time_t lowerGroup, cond::Time_t higherGroup) {
296  m_data->iovSequence.clear();
297  m_session->iovSchema().iovTable().select(
298  m_data->tagInfo.name, lowerGroup, higherGroup, m_data->snapshotTime, m_data->iovSequence);
299 
300  if (m_data->iovSequence.empty()) {
301  m_data->groupLowerIov = cond::time::MAX_VAL;
302  m_data->groupHigherIov = cond::time::MIN_VAL;
303  } else {
304  if (lowerGroup > cond::time::MIN_VAL) {
305  m_data->groupLowerIov = std::get<0>(m_data->iovSequence.front());
306  } else {
307  m_data->groupLowerIov = cond::time::MIN_VAL;
308  }
309  m_data->groupHigherIov = std::get<0>(m_data->iovSequence.back());
310  if (higherGroup < cond::time::MAX_VAL) {
311  m_data->groupHigherIov = cond::time::tillTimeFromNextSince(higherGroup, m_data->tagInfo.timeType);
312  } else {
313  m_data->groupHigherIov = cond::time::MAX_VAL;
314  }
315  }
316 
317  m_data->numberOfQueries++;
318  }
319 
321  if (!m_data.get())
322  throwException("No tag has been loaded.", "IOVProxy::getInterval");
323  checkTransaction("IOVProxy::getInterval");
324  if (!m_data->cacheInitialized)
325  loadGroups();
326  cond::Iov_t retVal;
327  // organize iovs in pages...
328  // first check the available iov cache:
329  if (m_data->groupLowerIov == cond::time::MAX_VAL || // case 0 : empty cache ( the first request )
330  time < m_data->groupLowerIov || time >= m_data->groupHigherIov) { // case 1 : target outside
331 
332  // a new query required!
333  // first determine the groups
334  auto iGLow = search(time, m_data->sinceGroups);
335  if (iGLow == m_data->sinceGroups.end()) {
336  // no suitable group=no iov at all! exiting...
337  return retVal;
338  }
339  auto iGHigh = iGLow;
340  cond::Time_t lowG = *iGLow;
341  iGHigh++;
343  if (iGHigh != m_data->sinceGroups.end())
344  highG = *iGHigh;
345 
346  // finally, get the iovs for the selected group interval!!
347  fetchSequence(lowG, highG);
348  }
349 
350  // the current iov set is a good one...
351  auto iIov = search(time, m_data->iovSequence);
352  if (iIov == m_data->iovSequence.end()) {
353  return retVal;
354  }
355 
356  retVal.since = std::get<0>(*iIov);
357  auto next = iIov;
358  next++;
359 
360  // default is the end of validity when set...
361  retVal.till = m_data->tagInfo.endOfValidity;
362  // for the till, the next element of the sequence has to be looked up
363  cond::Time_t tillVal;
364  if (next != m_data->iovSequence.end()) {
365  tillVal = cond::time::tillTimeFromNextSince(std::get<0>(*next), m_data->tagInfo.timeType);
366  } else {
367  tillVal = m_data->groupHigherIov;
368  }
369  if (tillVal < retVal.till)
370  retVal.till = tillVal;
371  //
372  retVal.payloadId = std::get<1>(*iIov);
373  return retVal;
374  }
375 
377  checkTransaction("IOVProxy::getLast");
379  bool ok = m_session->iovSchema().iovTable().getLastIov(
380  m_data->tagInfo.name, m_data->snapshotTime, ret.since, ret.payloadId);
381  if (ok) {
382  setTillToLastIov(ret, m_data->tagInfo.endOfValidity);
383  }
384  return ret;
385  }
386 
387  int IOVProxy::loadedSize() const { return m_data.get() ? m_data->iovSequence.size() : 0; }
388 
390  checkTransaction("IOVProxy::sequenceSize");
391  size_t ret = 0;
392  m_session->iovSchema().iovTable().getSize(m_data->tagInfo.name, m_data->snapshotTime, ret);
393 
394  return ret;
395  }
396 
397  size_t IOVProxy::numberOfQueries() const { return m_data.get() ? m_data->numberOfQueries : 0; }
398 
399  std::pair<cond::Time_t, cond::Time_t> IOVProxy::loadedGroup() const {
400  return m_data.get() ? std::make_pair(m_data->groupLowerIov, m_data->groupHigherIov)
401  : std::make_pair(cond::time::MAX_VAL, cond::time::MIN_VAL);
402  }
403 
404  const std::shared_ptr<SessionImpl>& IOVProxy::session() const { return m_session; }
405 
406  } // namespace persistency
407 
408 } // namespace cond
void fetchSequence(cond::Time_t lowerGroup, cond::Time_t higherGroup)
Definition: IOVProxy.cc:295
std::shared_ptr< SessionImpl > m_session
Definition: IOVProxy.h:164
bool operator()(const cond::Time_t &x, const std::tuple< cond::Time_t, cond::Hash > &y)
Definition: IOVProxy.cc:14
bool operator()(const cond::Time_t &x, const cond::Time_t &y)
Definition: IOVProxy.cc:12
std::tuple< std::string, boost::posix_time::ptime, boost::posix_time::ptime > getMetadata() const
Definition: IOVProxy.cc:275
IOVProxy & operator=(const IOVProxy &rhs)
Definition: IOVProxy.cc:133
std::pair< cond::Time_t, cond::Time_t > loadedGroup() const
Definition: IOVProxy.cc:399
cond::TagInfo_t iovSequenceInfo() const
Definition: IOVProxy.cc:260
void setTillToLastIov(cond::Iov_t &target, cond::Time_t endOfValidity)
Definition: IOVProxy.cc:248
ret
prodAgent to be discontinued
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:21
size_t numberOfQueries() const
Definition: IOVProxy.cc:397
Iterator begin() const
Definition: IOVProxy.cc:97
Time_t since
Definition: Types.h:53
const Time_t MIN_VAL(0)
constexpr Process operator++(Process p)
Definition: DataFormats.h:68
std::unique_ptr< IOVContainer > m_array
Definition: IOVProxy.h:87
Iterator end() const
Definition: IOVProxy.cc:99
std::vector< std::tuple< cond::Time_t, cond::Hash > > IOVContainer
Definition: IOVProxy.h:25
bool operator==(const Iterator &rhs) const
Definition: IOVProxy.cc:73
unsigned long long Time_t
Definition: Time.h:14
const cond::Tag_t & tagInfo() const
Definition: IOVProxy.cc:95
bool operator==(const QGLikelihoodParameters &lhs, const QGLikelihoodCategory &rhs)
Test if parameters are compatible with category.
const std::shared_ptr< SessionImpl > & session() const
Definition: IOVProxy.cc:404
Hash payloadId
Definition: Types.h:55
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:320
Iterator find(cond::Time_t time) const
Definition: IOVProxy.cc:101
void checkTransaction(const std::string &ctx) const
Definition: IOVProxy.cc:288
cond::Tag_t tagInfo() const
Definition: IOVProxy.cc:246
boost::posix_time::ptime snapshotTime
Definition: IOVProxy.cc:117
std::shared_ptr< IOVProxyData > m_data
Definition: IOVProxy.h:163
Time_t tillTimeFromNextSince(Time_t nextSince, TimeType timeType)
Definition: Time.cc:34
void load(const std::string &tag)
Definition: IOVProxy.cc:139
IOVContainer::const_iterator m_current
Definition: IOVProxy.h:60
Definition: plugin.cc:23
IOVArray selectRange(const cond::Time_t &begin, const cond::Time_t &end)
Definition: IOVProxy.cc:197
float x
tmp
align.sh
Definition: createJobs.py:716
std::vector< cond::Time_t > sinceGroups
Definition: IOVProxy.cc:121
bool operator!=(const Iterator &rhs) const
Definition: IOVProxy.cc:81
IOVArray & operator=(const IOVArray &rhs)
Definition: IOVProxy.cc:89
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
Iterator & operator=(const Iterator &rhs)
Definition: IOVProxy.cc:35
Time_t sinceGroupSize(TimeType tp)
Definition: Time.cc:68
Time_t till
Definition: Types.h:54