CMS 3D CMS Logo

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