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