CMS 3D CMS Logo

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