CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
101  IOVArray::Iterator IOVArray::find(cond::Time_t time) const { return Iterator(search(time, *m_array), this); }
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 
139  void IOVProxy::load(const std::string& tagName) {
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 
229  //void IOVProxy::reload(){
230  // if(m_data.get() && !m_data->tagInfo.empty()) {
231  // if(m_data->range) loadRange( m_data->tag, m_data->groupLowerIov, m_data->groupHigherIov, m_data->snapshotTime );
232  // else load( m_data->tag, m_data->snapshotTime, m_data->full );
233  // }
234  //}
235 
237  if (m_data.get()) {
238  m_data->groupLowerIov = cond::time::MAX_VAL;
239  m_data->groupHigherIov = cond::time::MIN_VAL;
240  m_data->sinceGroups.clear();
241  m_data->iovSequence.clear();
242  m_data->numberOfQueries = 0;
243  }
244  }
245 
247  if (m_data.get()) {
248  m_data->tagInfo.clear();
249  }
250  resetIOVCache();
251  }
252 
253  cond::Tag_t IOVProxy::tagInfo() const { return m_data.get() ? m_data->tagInfo : cond::Tag_t(); }
254 
256  if (endOfValidity < cond::time::MAX_VAL) {
257  if (target.since >= endOfValidity) {
258  target.clear();
259  } else {
260  target.till = endOfValidity;
261  }
262  } else {
263  target.till = cond::time::MAX_VAL;
264  }
265  }
266 
268  if (!m_data.get())
269  throwException("No tag has been loaded.", "IOVProxy::iovSequenceInfo");
270  checkTransaction("IOVProxy::iovSequenceInfo");
272  m_session->iovSchema().iovTable().getSize(m_data->tagInfo.name, m_data->snapshotTime, ret.size);
274  bool ok = m_session->iovSchema().iovTable().getLastIov(
275  m_data->tagInfo.name, m_data->snapshotTime, ret.lastInterval.since, ret.lastInterval.payloadId);
276  if (ok) {
277  setTillToLastIov(ret.lastInterval, m_data->tagInfo.endOfValidity);
278  }
279  return ret;
280  }
281 
282  std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime> IOVProxy::getMetadata() const {
283  if (!m_data.get())
284  throwException("No tag has been loaded.", "IOVProxy::getMetadata");
285  checkTransaction("IOVProxy::getMetadata");
286  std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime> ret;
287  if (!m_session->iovSchema().tagTable().getMetadata(
288  m_data->tagInfo.name, std::get<0>(ret), std::get<1>(ret), std::get<2>(ret))) {
289  throwException("Metadata for tag \"" + m_data->tagInfo.name + "\" have not been found in the database.",
290  "IOVProxy::getMetadata");
291  }
292  return ret;
293  }
294 
295  void IOVProxy::checkTransaction(const std::string& ctx) const {
296  if (!m_session.get())
297  throwException("The session is not active.", ctx);
298  if (!m_session->isTransactionActive(false))
299  throwException("The transaction is not active.", ctx);
300  }
301 
302  void IOVProxy::fetchSequence(cond::Time_t lowerGroup, cond::Time_t higherGroup) {
303  m_data->iovSequence.clear();
304  m_session->iovSchema().iovTable().select(
305  m_data->tagInfo.name, lowerGroup, higherGroup, m_data->snapshotTime, m_data->iovSequence);
306 
307  if (m_data->iovSequence.empty()) {
308  m_data->groupLowerIov = cond::time::MAX_VAL;
309  m_data->groupHigherIov = cond::time::MIN_VAL;
310  } else {
311  if (lowerGroup > cond::time::MIN_VAL) {
312  m_data->groupLowerIov = std::get<0>(m_data->iovSequence.front());
313  } else {
314  m_data->groupLowerIov = cond::time::MIN_VAL;
315  }
316  m_data->groupHigherIov = std::get<0>(m_data->iovSequence.back());
317  if (higherGroup < cond::time::MAX_VAL) {
318  m_data->groupHigherIov = cond::time::tillTimeFromNextSince(higherGroup, m_data->tagInfo.timeType);
319  } else {
320  m_data->groupHigherIov = cond::time::MAX_VAL;
321  }
322  }
323 
324  m_data->numberOfQueries++;
325  }
326 
328 
330  if (!m_data.get())
331  throwException("No tag has been loaded.", "IOVProxy::getInterval");
332  checkTransaction("IOVProxy::getInterval");
333  if (!m_data->cacheInitialized)
334  loadGroups();
335  cond::Iov_t retVal;
336  // organize iovs in pages...
337  // first check the available iov cache:
338  if (m_data->groupLowerIov == cond::time::MAX_VAL || // case 0 : empty cache ( the first request )
339  time < m_data->groupLowerIov || time >= m_data->groupHigherIov) { // case 1 : target outside
340 
341  // a new query required!
342  // first determine the groups
343  auto iGLow = search(time, m_data->sinceGroups);
344  if (iGLow == m_data->sinceGroups.end()) {
345  // no suitable group=no iov at all! exiting...
346  return retVal;
347  }
348  auto iGHigh = iGLow;
349  cond::Time_t lowG = *iGLow;
350  iGHigh++;
352  if (iGHigh != m_data->sinceGroups.end())
353  highG = *iGHigh;
354 
355  // finally, get the iovs for the selected group interval!!
356  fetchSequence(lowG, highG);
357  }
358 
359  // the current iov set is a good one...
360  auto iIov = search(time, m_data->iovSequence);
361  if (iIov == m_data->iovSequence.end()) {
362  return retVal;
363  }
364 
365  retVal.since = std::get<0>(*iIov);
366  auto next = iIov;
367  next++;
368 
369  // default is the end of validity when set...
370  retVal.till = m_data->tagInfo.endOfValidity;
371  // for the till, the next element of the sequence has to be looked up
372  cond::Time_t tillVal;
373  if (next != m_data->iovSequence.end()) {
374  tillVal = cond::time::tillTimeFromNextSince(std::get<0>(*next), m_data->tagInfo.timeType);
375  } else {
376  tillVal = m_data->groupHigherIov;
377  }
378  if (tillVal < retVal.till)
379  retVal.till = tillVal;
380  //
381  retVal.payloadId = std::get<1>(*iIov);
382  if (retVal.till == cond::time::MAX_VAL && defaultIovSize != cond::time::MAX_VAL) {
383  if (defaultIovSize == 0) {
384  // ???? why?
385  retVal.clear();
386  } else {
387  retVal.since = time;
388  retVal.till = retVal.since + defaultIovSize - 1;
389  if (time > retVal.till)
390  retVal.till = time;
391  }
392  }
393  return retVal;
394  }
395 
397  checkTransaction("IOVProxy::getLast");
399  bool ok = m_session->iovSchema().iovTable().getLastIov(
400  m_data->tagInfo.name, m_data->snapshotTime, ret.since, ret.payloadId);
401  if (ok) {
402  setTillToLastIov(ret, m_data->tagInfo.endOfValidity);
403  }
404  return ret;
405  }
406 
407  int IOVProxy::loadedSize() const { return m_data.get() ? m_data->iovSequence.size() : 0; }
408 
410  checkTransaction("IOVProxy::sequenceSize");
411  size_t ret = 0;
412  m_session->iovSchema().iovTable().getSize(m_data->tagInfo.name, m_data->snapshotTime, ret);
413 
414  return ret;
415  }
416 
417  size_t IOVProxy::numberOfQueries() const { return m_data.get() ? m_data->numberOfQueries : 0; }
418 
419  std::pair<cond::Time_t, cond::Time_t> IOVProxy::loadedGroup() const {
420  return m_data.get() ? std::make_pair(m_data->groupLowerIov, m_data->groupHigherIov)
421  : std::make_pair(cond::time::MAX_VAL, cond::time::MIN_VAL);
422  }
423 
424  const std::shared_ptr<SessionImpl>& IOVProxy::session() const { return m_session; }
425 
426  } // namespace persistency
427 
428 } // namespace cond
void fetchSequence(cond::Time_t lowerGroup, cond::Time_t higherGroup)
Definition: IOVProxy.cc:302
virtual void clear()
Definition: Types.cc:10
void checkTransaction(const std::string &ctx) const
Definition: IOVProxy.cc:295
tuple ret
prodAgent to be discontinued
cond::TagInfo_t iovSequenceInfo() const
Definition: IOVProxy.cc:267
std::shared_ptr< SessionImpl > m_session
Definition: IOVProxy.h:165
bool operator()(const cond::Time_t &x, const std::tuple< cond::Time_t, cond::Hash > &y)
Definition: IOVProxy.cc:14
Iov_t lastInterval
Definition: Types.h:73
bool operator()(const cond::Time_t &x, const cond::Time_t &y)
Definition: IOVProxy.cc:12
IOVProxy & operator=(const IOVProxy &rhs)
Definition: IOVProxy.cc:133
void setTillToLastIov(cond::Iov_t &target, cond::Time_t endOfValidity)
Definition: IOVProxy.cc:255
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:21
Iterator find(cond::Time_t time) const
Definition: IOVProxy.cc:101
Time_t since
Definition: Types.h:53
const Time_t MIN_VAL(0)
__host__ __device__ constexpr RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
std::unique_ptr< IOVContainer > m_array
Definition: IOVProxy.h:87
size_t size
Definition: Types.h:74
std::pair< cond::Time_t, cond::Time_t > loadedGroup() const
Definition: IOVProxy.cc:419
std::vector< std::tuple< cond::Time_t, cond::Hash > > IOVContainer
Definition: IOVProxy.h:25
Iterator begin() const
Definition: IOVProxy.cc:97
std::tuple< std::string, boost::posix_time::ptime, boost::posix_time::ptime > getMetadata() const
Definition: IOVProxy.cc:282
Iterator end() const
Definition: IOVProxy.cc:99
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.
Hash payloadId
Definition: Types.h:55
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:327
size_t size() const
Definition: IOVProxy.cc:103
boost::posix_time::ptime snapshotTime
Definition: IOVProxy.cc:117
std::shared_ptr< IOVProxyData > m_data
Definition: IOVProxy.h:164
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
IOVArray selectRange(const cond::Time_t &begin, const cond::Time_t &end)
Definition: IOVProxy.cc:197
cond::Tag_t tagInfo() const
Definition: IOVProxy.cc:253
string end
Definition: dataset.py:937
tuple last
Definition: dqmdumpme.py:56
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:73
size_t numberOfQueries() const
Definition: IOVProxy.cc:417
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
bool operator!=(const Iterator &rhs) const
Definition: IOVProxy.cc:81
Time_t sinceGroupSize(TimeType tp)
Definition: Time.cc:56
Time_t till
Definition: Types.h:54
const std::shared_ptr< SessionImpl > & session() const
Definition: IOVProxy.cc:424