CMS 3D CMS Logo

IOVProxy.cc
Go to the documentation of this file.
2 #include "SessionImpl.h"
3 
4 namespace cond {
5 
6  namespace persistency {
7 
8  // implementation details...
9  // only hosting data in this case
10  class IOVProxyData {
11  public:
12 
14  iovSequence(){
15  }
16 
17  // tag data
19  boost::posix_time::ptime snapshotTime;
25  // iov data
28  std::vector<cond::Time_t> sinceGroups;
30  bool full = false;
31  bool range = false;
32  size_t numberOfQueries = 0;
33  };
34 
36  m_current(),
37  m_end(),
38  m_timeType( cond::invalid ),
39  m_lastTill(cond::time::MIN_VAL),
40  m_endOfValidity(cond::time::MAX_VAL){
41  }
42 
43  IOVProxy::Iterator::Iterator( IOVContainer::const_iterator current, IOVContainer::const_iterator end,
45  m_current( current ),
46  m_end( end ),
47  m_timeType( timeType ),
48  m_lastTill( lastTill ),
49  m_endOfValidity( endOfValidity ){
50  }
51 
53  m_current( rhs.m_current ),
54  m_end( rhs.m_end ),
55  m_timeType( rhs.m_timeType ),
56  m_lastTill( rhs.m_lastTill ),
58  }
59 
61  if( this != &rhs ){
62  m_current = rhs.m_current;
63  m_end = rhs.m_end;
64  m_timeType = rhs.m_timeType;
65  m_lastTill = rhs.m_lastTill;
67  }
68  return *this;
69  }
70 
72  cond::Iov_t retVal;
73  retVal.since = std::get<0>(*m_current);
74  auto next = m_current;
75  next++;
76 
77  // default is the end of validity when set...
78  retVal.till = m_endOfValidity;
79  // for the till, the next element has to be verified!
80  if( next != m_end ){
81  // the till has to be calculated according to the time type ( because of the packing for some types )
82  retVal.till = cond::time::tillTimeFromNextSince( std::get<0>(*next), m_timeType );
83  } else {
84  retVal.till = m_lastTill;
85  }
86  retVal.payloadId = std::get<1>(*m_current);
87  return retVal;
88  }
89 
91  m_current++;
92  return *this;
93  }
94 
96  Iterator tmp( *this );
97  operator++();
98  return tmp;
99  }
100 
101  bool IOVProxy::Iterator::operator==( const Iterator& rhs ) const {
102  if( m_current != rhs.m_current ) return false;
103  if( m_end != rhs.m_end ) return false;
104  return true;
105  }
106 
107  bool IOVProxy::Iterator::operator!=( const Iterator& rhs ) const {
108  return !operator==( rhs );
109  }
110 
112  m_data(),
113  m_session(){
114  }
115 
116  IOVProxy::IOVProxy( const std::shared_ptr<SessionImpl>& session ):
117  m_data( new IOVProxyData ),
118  m_session( session ){
119  }
120 
122  m_data( rhs.m_data ),
123  m_session( rhs.m_session ){
124  }
125 
127  m_data = rhs.m_data;
128  m_session = rhs.m_session;
129  return *this;
130  }
131 
133  bool full ){
134  boost::posix_time::ptime notime;
135  load( tag, notime, full );
136  }
137 
139  const boost::posix_time::ptime& snapshotTime,
140  bool full ){
141  if( !m_data.get() ) return;
142 
143  // clear
144  reset();
145 
146  checkTransaction( "IOVProxy::load" );
147 
149  if(!m_session->iovSchema().tagTable().select( tag, m_data->timeType, m_data->payloadType, m_data->synchronizationType,
150  m_data->endOfValidity, dummy, m_data->lastValidatedTime ) ){
151  throwException( "Tag \""+tag+"\" has not been found in the database.","IOVProxy::load");
152  }
153  m_data->tag = tag;
154 
155  // now get the iov sequence when required
156  if( full ) {
157  // load the full iov sequence in this case!
158  m_data->groupLowerIov = cond::time::MIN_VAL;
159  m_data->groupHigherIov = cond::time::MAX_VAL;
160  m_session->iovSchema().iovTable().select( m_data->tag, m_data->groupLowerIov, m_data->groupHigherIov, snapshotTime, m_data->iovSequence );
161  m_data->full = true;
162  } else {
163  m_session->iovSchema().iovTable().getGroups( m_data->tag, snapshotTime, m_data->sinceGroups );
164  m_data->full = false;
165  }
166  m_data->range = false;
167  m_data->snapshotTime = snapshotTime;
168  }
169 
171  const cond::Time_t& begin,
172  const cond::Time_t& end ){
173  boost::posix_time::ptime no_time;
174  loadRange( tag, begin, end, no_time );
175  }
176 
178  const cond::Time_t& begin,
179  const cond::Time_t& end,
180  const boost::posix_time::ptime& snapshotTime ){
181  if( !m_data.get() ) return;
182 
183  // clear
184  reset();
185 
186  checkTransaction( "IOVProxy::loadRange" );
187 
189  if(!m_session->iovSchema().tagTable().select( tag, m_data->timeType, m_data->payloadType, m_data->synchronizationType,
190  m_data->endOfValidity, dummy, m_data->lastValidatedTime ) ){
191  throwException( "Tag \""+tag+"\" has not been found in the database "+m_session->connectionString,"IOVProxy::loadRange");
192  }
193  m_data->tag = tag;
194 
195  m_session->iovSchema().iovTable().getRange( m_data->tag, begin, end, snapshotTime, m_data->iovSequence );
196 
197  m_data->full = false;
198  m_data->range = true;
199  m_data->groupLowerIov = begin;
200  m_data->groupHigherIov = end;
201 
202  }
203 
205  if(m_data.get() && !m_data->tag.empty()) {
206  if(m_data->range) loadRange( m_data->tag, m_data->groupLowerIov, m_data->groupHigherIov, m_data->snapshotTime );
207  else load( m_data->tag, m_data->snapshotTime, m_data->full );
208  }
209  }
210 
212  if( m_data.get() ){
213  m_data->groupLowerIov = cond::time::MAX_VAL;
214  m_data->groupHigherIov = cond::time::MIN_VAL;
215  m_data->sinceGroups.clear();
216  m_data->iovSequence.clear();
217  m_data->numberOfQueries = 0;
218  m_data->full = false;
219  m_data->range = false;
220  }
221  }
222 
224  return m_data.get() ? m_data->tag : std::string("");
225  }
226 
228  return m_data.get() ? m_data->timeType : cond::invalid;
229  }
230 
232  return m_data.get() ? m_data->payloadType : std::string("");
233  }
234 
236  return m_data.get() ? m_data->synchronizationType : cond::SYNCH_ANY;
237  }
238 
240  return m_data.get() ? m_data->endOfValidity : cond::time::MIN_VAL;
241  }
242 
244  return m_data.get() ? m_data->lastValidatedTime : cond::time::MIN_VAL;
245  }
246 
247  std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime > IOVProxy::getMetadata() const {
248  if(!m_data.get()) throwException( "No tag has been loaded.","IOVProxy::getMetadata()");
249  checkTransaction( "IOVProxy::getMetadata" );
250  std::tuple<std::string, boost::posix_time::ptime, boost::posix_time::ptime > ret;
251  if(!m_session->iovSchema().tagTable().getMetadata( m_data->tag, std::get<0>( ret ), std::get<1>( ret ), std::get<2>( ret ) ) ){
252  throwException( "Metadata for tag \""+m_data->tag+"\" have not been found in the database.","IOVProxy::getMetadata()");
253  }
254  return ret;
255  }
256 
257  bool IOVProxy::isEmpty() const {
258  return m_data.get() ? ( m_data->sinceGroups.empty() && m_data->iovSequence.empty() ) : true;
259  }
260 
261  void IOVProxy::checkTransaction( const std::string& ctx ) const {
262  if( !m_session.get() ) throwException("The session is not active.",ctx );
263  if( !m_session->isTransactionActive( false ) ) throwException("The transaction is not active.",ctx );
264  }
265 
266  void IOVProxy::fetchSequence( cond::Time_t lowerGroup, cond::Time_t higherGroup ){
267  m_data->iovSequence.clear();
268  m_session->iovSchema().iovTable().select( m_data->tag, lowerGroup, higherGroup, m_data->snapshotTime, m_data->iovSequence );
269 
270  if( m_data->iovSequence.empty() ){
271  m_data->groupLowerIov = cond::time::MAX_VAL;
272  m_data->groupHigherIov = cond::time::MIN_VAL;
273  } else {
274  if( lowerGroup > cond::time::MIN_VAL ) {
275  m_data->groupLowerIov = std::get<0>( m_data->iovSequence.front() );
276  } else {
277  m_data->groupLowerIov = cond::time::MIN_VAL;
278  }
279  if( higherGroup < cond::time::MAX_VAL ) {
280  m_data->groupHigherIov = higherGroup-1;
281  } else {
282  m_data->groupHigherIov = cond::time::MAX_VAL;
283  }
284  }
285 
286  m_data->numberOfQueries++;
287  }
288 
290  if( m_data.get() ){
291  return Iterator( m_data->iovSequence.begin(), m_data->iovSequence.end(),
292  m_data->timeType, m_data->groupHigherIov, m_data->endOfValidity );
293  }
294  return Iterator();
295  }
296 
298  if( m_data.get() ){
299  return Iterator( m_data->iovSequence.end(), m_data->iovSequence.end(),
300  m_data->timeType, m_data->groupHigherIov, m_data->endOfValidity );
301  }
302  return Iterator();
303  }
304 
305  // comparison functor for iov tuples: Time_t only and Time_t,string
306  struct IOVComp {
307 
308  bool operator()( const cond::Time_t& x, const cond::Time_t& y ){ return (x < y); }
309 
310  bool operator()( const cond::Time_t& x, const std::tuple<cond::Time_t,cond::Hash>& y ){ return ( x < std::get<0>(y) ); }
311  };
312 
313  // function to search in the vector the target time
314  template <typename T> typename std::vector<T>::const_iterator search( const cond::Time_t& val, const std::vector<T>& container ){
315  if( container.empty() ) return container.end();
316  auto p = std::upper_bound( container.begin(), container.end(), val, IOVComp() );
317  return (p!= container.begin()) ? p-1 : container.end();
318  }
319 
321  checkTransaction( "IOVProxy::find" );
322  // organize iovs in pages...
323  // first check the available iov cache:
324  if( m_data->groupLowerIov == cond::time::MAX_VAL || // case 0 : empty cache ( the first request )
325  time < m_data->groupLowerIov || time >= m_data->groupHigherIov ){ // case 1 : target outside
326 
327  // a new query required!
328  // first determine the groups
329  auto iGLow = search( time, m_data->sinceGroups );
330  if( iGLow == m_data->sinceGroups.end() ){
331  // no suitable group=no iov at all! exiting...
332  return end();
333  }
334  auto iGHigh = iGLow;
335  cond::Time_t lowG = *iGLow;
336  iGHigh++;
338  if( iGHigh != m_data->sinceGroups.end() ) highG = *iGHigh;
339 
340  // finally, get the iovs for the selected group interval!!
341  fetchSequence( lowG, highG );
342  }
343 
344  // the current iov set is a good one...
345  auto iIov = search( time, m_data->iovSequence );
346  return Iterator( iIov, m_data->iovSequence.end(), m_data->timeType, m_data->groupHigherIov, m_data->endOfValidity );
347  }
348 
350  Iterator valid = find( time );
351  if( valid == end() ){
352  throwException( "Can't find a valid interval for the specified time.","IOVProxy::getInterval");
353  }
354  return *valid;
355  }
356 
358  checkTransaction( "IOVProxy::getLast" );
359  cond::Iov_t ret;
360  bool ok = m_session->iovSchema().iovTable().getLastIov( m_data->tag, m_data->snapshotTime, ret.since, ret.payloadId );
361 
362  if(ok) ret.till = cond::time::MAX_VAL;
363  return ret;
364  }
365 
366  int IOVProxy::loadedSize() const {
367  return m_data.get()? m_data->iovSequence.size() : 0;
368  }
369 
371  checkTransaction( "IOVProxy::sequenceSize" );
372  size_t ret = 0;
373  m_session->iovSchema().iovTable().getSize( m_data->tag, m_data->snapshotTime, ret );
374 
375  return ret;
376  }
377 
378  size_t IOVProxy::numberOfQueries() const {
379  return m_data.get()? m_data->numberOfQueries : 0;
380  }
381 
382  std::pair<cond::Time_t,cond::Time_t> IOVProxy::loadedGroup() const {
383  return m_data.get()? std::make_pair( m_data->groupLowerIov, m_data->groupHigherIov ): std::make_pair( cond::time::MAX_VAL, cond::time::MIN_VAL );
384  }
385 
386  const std::shared_ptr<SessionImpl>& IOVProxy::session() const {
387  return m_session;
388  }
389 
390  }
391 
392 }
void fetchSequence(cond::Time_t lowerGroup, cond::Time_t higherGroup)
Definition: IOVProxy.cc:266
void checkTransaction(const std::string &ctx) const
Definition: IOVProxy.cc:261
cond::SynchronizationType synchronizationType() const
Definition: IOVProxy.cc:235
std::shared_ptr< SessionImpl > m_session
Definition: IOVProxy.h:153
bool operator()(const cond::Time_t &x, const std::tuple< cond::Time_t, cond::Hash > &y)
Definition: IOVProxy.cc:310
std::string tag() const
Definition: IOVProxy.cc:223
bool operator()(const cond::Time_t &x, const cond::Time_t &y)
Definition: IOVProxy.cc:308
IOVProxy & operator=(const IOVProxy &rhs)
Definition: IOVProxy.cc:126
void load(const std::string &tag, bool full=false)
Definition: IOVProxy.cc:132
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:314
Time_t since
Definition: Types.h:55
const Time_t MIN_VAL(0)
std::pair< cond::Time_t, cond::Time_t > loadedGroup() const
Definition: IOVProxy.cc:382
std::string payloadObjectType() const
Definition: IOVProxy.cc:231
bool operator!=(const Iterator &rhs) const
Definition: IOVProxy.cc:107
IOVProxy::IOVContainer iovSequence
Definition: IOVProxy.cc:29
TimeType
Definition: Time.h:21
Iterator begin() const
Definition: IOVProxy.cc:289
Iterator & operator=(const Iterator &rhs)
Definition: IOVProxy.cc:60
std::tuple< std::string, boost::posix_time::ptime, boost::posix_time::ptime > getMetadata() const
Definition: IOVProxy.cc:247
cond::TimeType timeType() const
Definition: IOVProxy.cc:227
unsigned long long Time_t
Definition: Time.h:16
IOVContainer::const_iterator m_current
Definition: IOVProxy.h:55
std::vector< std::tuple< cond::Time_t, cond::Hash > > IOVContainer
Definition: IOVProxy.h:30
Definition: GenABIO.cc:180
Iterator find(cond::Time_t time)
Definition: IOVProxy.cc:320
Hash payloadId
Definition: Types.h:57
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:349
IOVContainer::const_iterator m_end
Definition: IOVProxy.h:56
cond::Time_t endOfValidity() const
Definition: IOVProxy.cc:239
boost::posix_time::ptime snapshotTime
Definition: IOVProxy.cc:19
void loadRange(const std::string &tag, const cond::Time_t &begin, const cond::Time_t &end)
Definition: IOVProxy.cc:170
cond::Time_t lastValidatedTime() const
Definition: IOVProxy.cc:243
std::shared_ptr< IOVProxyData > m_data
Definition: IOVProxy.h:152
Time_t tillTimeFromNextSince(Time_t nextSince, TimeType timeType)
Definition: Time.cc:33
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
Definition: plugin.cc:24
SynchronizationType
Definition: Types.h:29
cond::SynchronizationType synchronizationType
Definition: IOVProxy.cc:22
bool operator==(const Iterator &rhs) const
Definition: IOVProxy.cc:101
std::vector< cond::Time_t > sinceGroups
Definition: IOVProxy.cc:28
size_t numberOfQueries() const
Definition: IOVProxy.cc:378
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
Iterator end() const
Definition: IOVProxy.cc:297
Time_t till
Definition: Types.h:56
const std::shared_ptr< SessionImpl > & session() const
Definition: IOVProxy.cc:386