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
24  // iov data
27  std::vector<cond::Time_t> sinceGroups;
29  size_t numberOfQueries = 0;
30  };
31 
33  m_current(),
34  m_end(),
35  m_timeType( cond::invalid ),
36  m_endOfValidity(cond::time::MAX_VAL) {
37  }
38 
39  IOVProxy::Iterator::Iterator( IOVContainer::const_iterator current, IOVContainer::const_iterator end,
41  m_current( current ),
42  m_end( end ),
43  m_timeType( timeType ),
44  m_endOfValidity( endOfValidity ){
45  }
46 
48  m_current( rhs.m_current ),
49  m_end( rhs.m_end ),
50  m_timeType( rhs.m_timeType ),
51  m_endOfValidity( rhs.m_endOfValidity ){
52  }
53 
55  if( this != &rhs ){
56  m_current = rhs.m_current;
57  m_end = rhs.m_end;
58  m_timeType = rhs.m_timeType;
59  m_endOfValidity = rhs.m_endOfValidity;
60  }
61  return *this;
62  }
63 
65  cond::Iov_t retVal;
66  retVal.since = std::get<0>(*m_current);
67  auto next = m_current;
68  next++;
69 
70  // default is the end of validity when set...
71  retVal.till = m_endOfValidity;
72  // for the till, the next element has to be verified!
73  if( next != m_end ){
74 
75  // the till has to be calculated according to the time type ( because of the packing for some types )
76  retVal.till = cond::time::tillTimeFromNextSince( std::get<0>(*next), m_timeType );
77  }
78  retVal.payloadId = std::get<1>(*m_current);
79  return retVal;
80  }
81 
83  m_current++;
84  return *this;
85  }
86 
88  Iterator tmp( *this );
89  operator++();
90  return tmp;
91  }
92 
93  bool IOVProxy::Iterator::operator==( const Iterator& rhs ) const {
94  if( m_current != rhs.m_current ) return false;
95  if( m_end != rhs.m_end ) return false;
96  return true;
97  }
98 
99  bool IOVProxy::Iterator::operator!=( const Iterator& rhs ) const {
100  return !operator==( rhs );
101  }
102 
104  m_data(),
105  m_session(){
106  }
107 
108  IOVProxy::IOVProxy( const std::shared_ptr<SessionImpl>& session ):
109  m_data( new IOVProxyData ),
110  m_session( session ){
111  }
112 
114  m_data( rhs.m_data ),
115  m_session( rhs.m_session ){
116  }
117 
119  m_data = rhs.m_data;
120  m_session = rhs.m_session;
121  return *this;
122  }
123 
124  void IOVProxy::load( const std::string& tag, bool full ){
125  // clear
126  reset();
127 
128  checkTransaction( "IOVProxy::load" );
129  std::string dummy;
130  if(!m_session->iovSchema().tagTable().select( tag, m_data->timeType, m_data->payloadType, m_data->synchronizationType,
131  m_data->endOfValidity, dummy, m_data->lastValidatedTime ) ){
132  throwException( "Tag \""+tag+"\" has not been found in the database.","IOVProxy::load");
133  }
134  m_data->tag = tag;
135 
136  // now get the iov sequence when required
137  if( full ) {
138 
139  // load the full iov sequence in this case!
140  m_session->iovSchema().iovTable().selectLatest( m_data->tag, m_data->iovSequence );
141  m_data->lowerGroup = cond::time::MIN_VAL;
142  m_data->higherGroup = cond::time::MAX_VAL;
143  } else {
144  m_session->iovSchema().iovTable().selectGroups( m_data->tag, m_data->sinceGroups );
145  }
146  }
147 
149  if(m_data.get() && !m_data->tag.empty()) load( m_data->tag );
150  }
151 
153  if( m_data.get() ){
154  m_data->lowerGroup = cond::time::MAX_VAL;
155  m_data->higherGroup = cond::time::MIN_VAL;
156  m_data->sinceGroups.clear();
157  m_data->iovSequence.clear();
158  m_data->numberOfQueries = 0;
159  }
160  }
161 
163  return m_data.get() ? m_data->tag : std::string("");
164  }
165 
167  return m_data.get() ? m_data->timeType : cond::invalid;
168  }
169 
171  return m_data.get() ? m_data->payloadType : std::string("");
172  }
173 
175  return m_data.get() ? m_data->synchronizationType : cond::SYNCHRONIZATION_UNKNOWN;
176  }
177 
179  return m_data.get() ? m_data->endOfValidity : cond::time::MIN_VAL;
180  }
181 
183  return m_data.get() ? m_data->lastValidatedTime : cond::time::MIN_VAL;
184  }
185 
186  bool IOVProxy::isEmpty() const {
187  return m_data.get() ? ( m_data->sinceGroups.size()==0 && m_data->iovSequence.size()==0 ) : true;
188  }
189 
190  void IOVProxy::checkTransaction( const std::string& ctx ) const {
191  if( !m_session.get() ) throwException("The session is not active.",ctx );
192  if( !m_session->isTransactionActive( false ) ) throwException("The transaction is not active.",ctx );
193  }
194 
195  void IOVProxy::fetchSequence( cond::Time_t lowerGroup, cond::Time_t higherGroup ){
196  m_data->iovSequence.clear();
197  m_session->iovSchema().iovTable().selectLatestByGroup( m_data->tag, lowerGroup, higherGroup, m_data->iovSequence );
198 
199  m_data->lowerGroup = lowerGroup;
200  m_data->higherGroup = higherGroup;
201 
202  m_data->numberOfQueries++;
203  }
204 
206  if( m_data.get() ){
207  return Iterator( m_data->iovSequence.begin(), m_data->iovSequence.end(),
208  m_data->timeType, m_data->endOfValidity );
209  }
210  return Iterator();
211  }
212 
214  if( m_data.get() ){
215  return Iterator( m_data->iovSequence.end(), m_data->iovSequence.end(), m_data->timeType, m_data->endOfValidity );
216  }
217  return Iterator();
218  }
219 
220  // comparison functor for iov tuples: Time_t only and Time_t,string
221  struct IOVComp {
222 
223  bool operator()( const cond::Time_t& x, const cond::Time_t& y ){ return (x < y); }
224 
225  bool operator()( const cond::Time_t& x, const std::tuple<cond::Time_t,cond::Hash>& y ){ return ( x < std::get<0>(y) ); }
226  };
227 
228  // function to search in the vector the target time
229  template <typename T> typename std::vector<T>::const_iterator search( const cond::Time_t& val, const std::vector<T>& container ){
230  if( !container.size() ) return container.end();
231  auto p = std::upper_bound( container.begin(), container.end(), val, IOVComp() );
232  return (p!= container.begin()) ? p-1 : container.end();
233  }
234 
236  checkTransaction( "IOVProxy::find" );
237  // first check the available iov cache:
238  // case 0 empty cache ( the first request )
239 
240  if( m_data->lowerGroup==cond::time::MAX_VAL ||
241  // case 1 : target outside
242  time < m_data->lowerGroup || time>= m_data->higherGroup ){
243 
244  // a new query required!
245  // first determine the groups
246  auto iGLow = search( time, m_data->sinceGroups );
247  if( iGLow == m_data->sinceGroups.end() ){
248  // so suitable group=no iov at all! exiting...
249  return end();
250  }
251  auto iGHigh = iGLow;
252  cond::Time_t lowG = 0;
253  // unless the low group is the first one available, move the previous one to fully cover the interval
254  if( iGLow != m_data->sinceGroups.begin() ){
255  iGLow--;
256  lowG = *iGLow;
257  }
258  // the upper group will be also extended to the next (covering in total up to three groups )
259  iGHigh++;
261  if( iGHigh != m_data->sinceGroups.end() ) {
262  iGHigh++;
263  if( iGHigh != m_data->sinceGroups.end() ) highG = *iGHigh;
264  }
265  // finally, get the iovs for the selected group interval!!
266  fetchSequence( lowG, highG );
267  }
268 
269  // the current iov set is a good one...
270  auto iIov = search( time, m_data->iovSequence );
271  return Iterator( iIov, m_data->iovSequence.end(), m_data->timeType, m_data->endOfValidity );
272  }
273 
275  Iterator valid = find( time );
276  if( valid == end() ){
277  throwException( "Can't find a valid interval for the specified time.","IOVProxy::getInterval");
278  }
279  return *valid;
280  }
281 
283  checkTransaction( "IOVProxy::getLast" );
285  if( m_session->iovSchema().iovTable().getLastIov( m_data->tag, ret.since, ret.payloadId ) ){
287  }
288  return ret;
289  }
290 
291  int IOVProxy::loadedSize() const {
292  return m_data.get()? m_data->iovSequence.size() : 0;
293  }
294 
296  checkTransaction( "IOVProxy::sequenceSize" );
297  size_t ret = 0;
298  m_session->iovSchema().iovTable().getSize( m_data->tag, ret );
299  return ret;
300  }
301 
302  size_t IOVProxy::numberOfQueries() const {
303  return m_data.get()? m_data->numberOfQueries : 0;
304  }
305 
306  std::pair<cond::Time_t,cond::Time_t> IOVProxy::loadedGroup() const {
307  return m_data.get()? std::make_pair( m_data->lowerGroup, m_data->higherGroup ): std::make_pair( cond::time::MAX_VAL, cond::time::MIN_VAL );
308  }
309 
310  const std::shared_ptr<SessionImpl>& IOVProxy::session() const {
311  return m_session;
312  }
313 
314  }
315 
316 }
void fetchSequence(cond::Time_t lowerGroup, cond::Time_t higherGroup)
Definition: IOVProxy.cc:195
void checkTransaction(const std::string &ctx) const
Definition: IOVProxy.cc:190
cond::SynchronizationType synchronizationType() const
Definition: IOVProxy.cc:174
std::shared_ptr< SessionImpl > m_session
Definition: IOVProxy.h:141
bool operator()(const cond::Time_t &x, const std::tuple< cond::Time_t, cond::Hash > &y)
Definition: IOVProxy.cc:225
std::string tag() const
Definition: IOVProxy.cc:162
bool operator()(const cond::Time_t &x, const cond::Time_t &y)
Definition: IOVProxy.cc:223
IOVProxy & operator=(const IOVProxy &rhs)
Definition: IOVProxy.cc:118
void load(const std::string &tag, bool full=false)
Definition: IOVProxy.cc:124
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:229
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:306
std::string payloadObjectType() const
Definition: IOVProxy.cc:170
bool operator!=(const Iterator &rhs) const
Definition: IOVProxy.cc:99
IOVProxy::IOVContainer iovSequence
Definition: IOVProxy.cc:28
TimeType
Definition: Time.h:21
Iterator begin() const
Definition: IOVProxy.cc:205
Iterator & operator=(const Iterator &rhs)
Definition: IOVProxy.cc:54
cond::TimeType timeType() const
Definition: IOVProxy.cc:166
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:235
Hash payloadId
Definition: Types.h:53
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:274
IOVContainer::const_iterator m_end
Definition: IOVProxy.h:56
cond::Time_t endOfValidity() const
Definition: IOVProxy.cc:178
cond::Time_t lastValidatedTime() const
Definition: IOVProxy.cc:182
std::shared_ptr< IOVProxyData > m_data
Definition: IOVProxy.h:140
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:21
Definition: DDAxes.h:10
bool operator==(const Iterator &rhs) const
Definition: IOVProxy.cc:93
std::vector< cond::Time_t > sinceGroups
Definition: IOVProxy.cc:27
size_t numberOfQueries() const
Definition: IOVProxy.cc:302
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:213
Time_t till
Definition: Types.h:52
const std::shared_ptr< SessionImpl > & session() const
Definition: IOVProxy.cc:310