CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
IOVEditor.cc
Go to the documentation of this file.
6 
7 namespace cond {
8 
9  boost::shared_ptr<cond::IOVSequence> loadIOV( cond::DbSession& dbSess,
10  const std::string& iovToken ){
11  if( iovToken.empty()){
12  throw cond::Exception("IOVEditor::loadIOV Error: token is empty.");
13  }
14  boost::shared_ptr<cond::IOVSequence> iov = dbSess.getTypedObject<cond::IOVSequence>( iovToken );
15  // loading the lazy-loading Queryable vector...
16  iov->loadAll();
17  //**** temporary for the schema transition
18  if( dbSess.isOldSchema() ){
19  PoolTokenParser parser( dbSess.storage() );
20  iov->swapTokens( parser );
21  }
22  //****
23  return iov;
24  }
25 
26  std::string insertIOV( cond::DbSession& dbSess,
27  const boost::shared_ptr<IOVSequence>& data,
28  bool swapOIds=false ){
29  // ***** TEMPORARY FOR TRANSITION PHASE
30  if( swapOIds && dbSess.isOldSchema() ){
31  PoolTokenWriter writer( dbSess.storage() );
32  data->swapOIds( writer );
33  }
34  // *****
35  return dbSess.storeObject( data.get(), cond::IOVNames::container());
36  }
37 
38  void updateIOV( cond::DbSession& dbSess,
39  const boost::shared_ptr<IOVSequence>& data,
40  const std::string& token ){
41  // ***** TEMPORARY FOR TRANSITION PHASE
42  if( dbSess.isOldSchema() ){
43  PoolTokenWriter writer( dbSess.storage() );
44  data->swapOIds( writer );
45  }
46  // *****
47  dbSess.updateObject( data.get(), token );
48  }
49 
50  IOVImportIterator::IOVImportIterator( boost::shared_ptr<cond::IOVProxyData>& destIov ):
51  m_sourceIov(),
52  m_destIov( destIov ),
53  m_lastSince( 0 ),
54  m_bulkSize( 0 ),
55  m_cursor(),
56  m_till()
57  {
58  }
59 
61  }
62 
64  cond::Time_t since,
65  cond::Time_t till,
66  bool outOfOrder,
67  size_t bulkSize ){
68  m_sourceIov = sourceIov;
69  const IOVSequence& siov = m_sourceIov.iov();
70  cond::Time_t dsince = std::max(since, siov.firstSince());
71  IOVSequence::const_iterator ifirstTill = siov.find(dsince);
72  IOVSequence::const_iterator isecondTill = siov.find(till);
73  if( isecondTill != siov.iovs().end() ) isecondTill++;
74 
75  if (ifirstTill==isecondTill)
76  throw cond::Exception("IOVImportIterator::setUp Error: empty input range");
77 
78  IOVSequence& diov = *m_destIov->data;
79  if ( diov.iovs().empty()) ; // do not waist time
80  else if (outOfOrder) {
81  for( IOVSequence::const_iterator it=ifirstTill;
82  it!=isecondTill; ++it)
83  if (diov.exist(it->sinceTime()))
84  throw cond::Exception("IOVImportIterator::setUp Error: since time already exists");
85  } else if (dsince <= diov.iovs().back().sinceTime()) {
86  std::ostringstream errStr;
87  errStr << "IOVImportIterator::setUp Error: trying to append a since time " << dsince
88  << " which is not larger than last since " << diov.iovs().back().sinceTime();
89  throw cond::Exception(errStr.str());
90  }
91 
92  m_lastSince = dsince;
93  m_cursor = ifirstTill;
94  m_till = isecondTill;
95  m_bulkSize = bulkSize;
96  }
97 
99  const std::string& sourceIovToken,
100  cond::Time_t since,
101  cond::Time_t till,
102  bool outOfOrder,
103  size_t bulkSize ){
104  IOVProxy sourceIov( sourceSess, sourceIovToken );
105  setUp( sourceIov, since, till, outOfOrder, bulkSize );
106  }
107 
109  size_t bulkSize ){
110 
111  m_sourceIov = sourceIov;
112  const IOVSequence& siov = m_sourceIov.iov();
113  cond::Time_t dsince = siov.firstSince();
114 
115  IOVSequence::const_iterator ifirstTill = siov.iovs().begin();
116  IOVSequence::const_iterator isecondTill = siov.iovs().end();
117 
118  IOVSequence& diov = *m_destIov->data;
119  if (!diov.iovs().empty()) { // do not waist time
120  if (dsince <= diov.iovs().back().sinceTime()) {
121  std::ostringstream errStr;
122  errStr << "IOVImportIterator::setUp Error: trying to append a since time " << dsince
123  << " which is not larger than last since " << diov.iovs().back().sinceTime();
124  throw cond::Exception(errStr.str());
125  }
126  }
127 
128  m_lastSince = dsince;
129  m_cursor = ifirstTill;
130  m_till = isecondTill;
131  m_bulkSize = bulkSize;
132  }
133 
135  const std::string& sourceIovToken,
136  size_t bulkSize ){
137  IOVProxy sourceIov( sourceSess, sourceIovToken );
138  setUp( sourceIov, bulkSize );
139  }
140 
142  return m_cursor != m_till;
143  }
144 
146  size_t i = 0;
147  boost::shared_ptr<IOVSequence>& diov = m_destIov->data;
148  for( ; i<m_bulkSize && m_cursor != m_till; ++i, ++m_cursor, m_lastSince=m_cursor->sinceTime() ){
149  std::string newPtoken = m_destIov->dbSession.importObject( m_sourceIov.db(),m_cursor->token());
150  ora::OId poid;
151  poid.fromString( newPtoken );
152  ora::Container cont = m_destIov->dbSession.storage().containerHandle( poid.containerId() );
153  diov->add(m_lastSince, newPtoken, cont.className());
154  }
155  if( m_cursor == m_till ) diov->stamp(cond::userInfo(),false);
156  updateIOV( m_destIov->dbSession, diov, m_destIov->token );
157  return i;
158  }
159 
161  size_t total = 0;
162  while( hasMoreElements() ){
163  total += importMoreElements();
164  }
165  return total;
166  }
167 
169 
171  m_isLoaded(false),
172  m_iov( new IOVProxyData( dbSess ) ){
173  }
174 
176  const std::string& token ):
177  m_isLoaded(false),
178  m_iov( new IOVProxyData( dbSess, token )){
179  }
180 
182  m_iov->refresh();
183  m_isLoaded = true;
184  }
185 
186  void IOVEditor::load( const std::string& token ){
187  m_iov->token = token;
188  m_iov->refresh();
189  m_isLoaded = true;
190  }
191 
192  void IOVEditor::debugInfo(std::ostream & co) const {
193  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
194  co << "IOVEditor: ";
195  co << "db " << m_iov->dbSession.connectionString();
196  if(m_iov->token.empty()) {
197  co << " no token"; return;
198  }
199  if (!m_iov->data.get() ) {
200  co << " no iov for token " << m_iov->token;
201  return;
202  }
203  co << " iov token " << m_iov->token;
204  co << "\nStamp: " << iov->comment()
205  << "; time " << iov->timestamp()
206  << "; revision " << iov->revision();
207  co <<". TimeType " << cond::timeTypeSpecs[ iov->timeType()].name;
208  if( iov->iovs().empty() )
209  co << ". empty";
210  else
211  co << ". size " << iov->iovs().size()
212  << "; last since " << iov->iovs().back().sinceTime();
213  }
214 
215  void IOVEditor::reportError(std::string message) const {
216  std::ostringstream out;
217  out << "Error in ";
218  debugInfo(out);
219  out << "\n" << message;
220  throw cond::Exception(out.str());
221  }
222 
223  void IOVEditor::reportError( std::string message,
224  cond::Time_t time) const {
225  std::ostringstream out;
226  out << "Error in";
227  debugInfo(out);
228  out << "\n" << message << " for time: " << time;
229  throw cond::Exception(out.str());
230  }
231 
232 
234  bool ret = false;
235  cond::IOVSchemaUtility schemaUtil( m_iov->dbSession );
236  if( !schemaUtil.existsIOVContainer() ){
237  schemaUtil.createIOVContainer();
238  ret = true;
239  }
240  return ret;
241  }
242 
243  // create empty default sequence
245  m_iov->data.reset( new cond::IOVSequence(timetype) );
246  m_iov->token = insertIOV( m_iov->dbSession, m_iov->data );
247  m_isLoaded=true;
248  return m_iov->token;
249  }
250 
252  cond::Time_t lastTill,
253  const std::string& metadata ) {
254  m_iov->data.reset( new cond::IOVSequence((int)timetype,lastTill, metadata) );
255  m_iov->token = insertIOV( m_iov->dbSession, m_iov->data );
256  m_isLoaded=true;
257  return m_iov->token;
258  }
259 
260  // ####### TO BE REOMOVED ONLY USED IN TESTS
262  m_iov->data.reset( new cond::IOVSequence((int)timetype,lastTill, std::string(" ")) );
263  m_iov->token = insertIOV( m_iov->dbSession, m_iov->data );
264  m_isLoaded=true;
265  return m_iov->token;
266  }
267 
269  cond::TimeType timetype) const {
271 
272  }
273 
275  return validTime(time,m_iov->data->timeType());
276  }
277 
278 
279 
280  unsigned int
282  const std::string& payloadToken ){
283  if( !m_isLoaded ){
284  reload();
285  }
286  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
287  if( iov->iovs().empty() )
288  reportError("cond::IOVEditor::insert cannot inser into empty IOV sequence",tillTime);
289 
290  if(!validTime(tillTime))
291  reportError("cond::IOVEditor::insert time not in global range",tillTime);
292 
293  if(tillTime<=iov->lastTill() )
294  reportError("cond::IOVEditor::insert IOV not in range",tillTime);
295 
296  cond::Time_t newSince=iov->lastTill()+1;
297  std::string payloadClassName = m_iov->dbSession.classNameForItem( payloadToken );
298  unsigned int ret = iov->add(newSince, payloadToken, payloadClassName);
299  iov->updateLastTill(tillTime);
300  updateIOV( m_iov->dbSession, iov, m_iov->token );
301  return ret;
302  }
303 
304  void
305  IOVEditor::bulkAppend(std::vector< std::pair<cond::Time_t, std::string > >& values){
306  if (values.empty()) return;
307  if( !m_isLoaded ){
308  reload();
309  }
310  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
311  cond::Time_t firstTime = values.front().first;
312  cond::Time_t lastTime = values.back().first;
313  if(!validTime(firstTime))
314  reportError("cond::IOVEditor::bulkInsert first time not in global range",firstTime);
315 
316  if(!validTime(lastTime))
317  reportError("cond::IOVEditor::bulkInsert last time not in global range",lastTime);
318 
319  if(lastTime>= iov->lastTill() ||
320  ( !iov->iovs().empty() && firstTime<=iov->iovs().back().sinceTime())
321  )
322  reportError("cond::IOVEditor::bulkInsert IOV not in range",firstTime);
323 
324  for(std::vector< std::pair<cond::Time_t,std::string> >::const_iterator it=values.begin(); it!=values.end(); ++it){
325  std::string payloadClassName = m_iov->dbSession.classNameForItem( it->second );
326  iov->add(it->first,it->second,payloadClassName );
327  }
328  updateIOV( m_iov->dbSession, iov, m_iov->token );
329  }
330 
331  void
332  IOVEditor::bulkAppend(std::vector< cond::IOVElement >& values){
333  if (values.empty()) return;
334  if( !m_isLoaded ){
335  reload();
336  }
337  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
338  cond::Time_t firstTime = values.front().sinceTime();
339  cond::Time_t lastTime = values.back().sinceTime();
340  if(!validTime(firstTime))
341  reportError("cond::IOVEditor::bulkInsert first time not in global range",firstTime);
342 
343  if(!validTime(lastTime))
344  reportError("cond::IOVEditor::bulkInsert last time not in global range",lastTime);
345 
346  if(lastTime>=iov->lastTill() ||
347  ( !iov->iovs().empty() && firstTime<=iov->iovs().back().sinceTime())
348  ) reportError("cond::IOVEditor::bulkInsert IOV not in range",firstTime);
349 
350  for(std::vector< cond::IOVElement >::const_iterator it=values.begin(); it!=values.end(); ++it){
351  std::string payloadClassName = m_iov->dbSession.classNameForItem( it->token() );
352  iov->add(it->sinceTime(),it->token(),payloadClassName );
353  }
354 
355  updateIOV( m_iov->dbSession, iov, m_iov->token );
356  }
357 
358  void
359  IOVEditor::stamp( std::string const & icomment,
360  bool append) {
361  if( !m_isLoaded ){
362  reload();
363  }
364  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
365  iov->stamp(icomment, append);
366  updateIOV( m_iov->dbSession, iov, m_iov->token );
367  }
368 
369  void IOVEditor::editMetadata( std::string const & metadata,
370  bool append ){
371  if( !m_isLoaded ){
372  reload();
373  }
374  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
375  iov->updateMetadata( metadata, append);
376  updateIOV( m_iov->dbSession, iov, m_iov->token );
377  }
378 
380  if( !m_isLoaded ){
381  reload();
382  }
383  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
384  iov->setScope( scope );
385  updateIOV( m_iov->dbSession, iov, m_iov->token );
386  }
387 
388  void
390  if( m_iov->token.empty() ) reportError("cond::IOVEditor::updateClosure cannot change non-existing IOV index");
391  if( !m_isLoaded ){
392  reload();
393  }
394  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
395  iov->updateLastTill(newtillTime);
396  updateIOV( m_iov->dbSession, iov, m_iov->token );
397  }
398 
399  unsigned int
401  const std::string& payloadToken ){
402  if( m_iov->token.empty() ) {
403  reportError("cond::IOVEditor::appendIOV cannot append to non-existing IOV index");
404  }
405 
406  if( !m_isLoaded ){
407  reload();
408  }
409  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
410 
411  if(!validTime(sinceTime))
412  reportError("cond::IOVEditor::append time not in global range",sinceTime);
413 
414  if( !iov->iovs().empty() ){
415  //range check in case
416  cond::Time_t lastValidSince=iov->iovs().back().sinceTime();
417  if( sinceTime<= lastValidSince){
418  std::ostringstream errStr;
419  errStr << "IOVEditor::append Error: trying to append a since time " << lastValidSince
420  << " which is not larger than last since";
421  reportError(errStr.str(), sinceTime);
422  }
423  }
424 
425  // does it make sense? (in case of mixed till and since insertions...)
426  if (iov->lastTill()<=sinceTime) iov->updateLastTill( timeTypeSpecs[iov->timeType()].endValue );
427  std::string payloadClassName = m_iov->dbSession.classNameForItem( payloadToken );
428  unsigned int ret = iov->add(sinceTime,payloadToken, payloadClassName );
429  updateIOV( m_iov->dbSession, iov, m_iov->token );
430  return ret;
431  }
432 
433 
434  unsigned int
436  const std::string& payloadToken ){
437  if( m_iov->token.empty() ) {
438  reportError("cond::IOVEditor::freeInsert cannot append to non-existing IOV index");
439  }
440 
441  if( !m_isLoaded ){
442  reload();
443  }
444  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
445 
446  // if( m_iov->data->iov.empty() ) reportError("cond::IOVEditor::freeInsert cannot insert to empty IOV index");
447 
448 
449  if(!validTime(sinceTime))
450  reportError("cond::IOVEditor::freeInsert time not in global range",sinceTime);
451 
452 
453  // we do not support multiple iov with identical since...
454  if (m_iov->data->exist(sinceTime))
455  reportError("cond::IOVEditor::freeInsert sinceTime already existing",sinceTime);
456 
457 
458 
459  // does it make sense? (in case of mixed till and since insertions...)
460  if (iov->lastTill()<sinceTime) iov->updateLastTill( timeTypeSpecs[iov->timeType()].endValue );
461  std::string payloadClassName = m_iov->dbSession.classNameForItem( payloadToken );
462  unsigned int ret = iov->add(sinceTime,payloadToken, payloadClassName );
463  updateIOV( m_iov->dbSession, iov, m_iov->token );
464  return ret;
465  }
466 
467 
468  // remove last entry
469  unsigned int IOVEditor::truncate(bool withPayload) {
470  if( m_iov->token.empty() ) reportError("cond::IOVEditor::truncate cannot delete to non-existing IOV sequence");
471  if( !m_isLoaded ){
472  reload();
473  }
474  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
475  if (iov->piovs().empty()) return 0;
476  if(withPayload){
477  std::string tokenStr = iov->piovs().back().token();
478  m_iov->dbSession.deleteObject( tokenStr );
479  }
480  unsigned int ret = iov->truncate();
481  updateIOV( m_iov->dbSession, iov, m_iov->token );
482  return ret;
483 
484  }
485 
486 
487  void
488  IOVEditor::deleteEntries(bool withPayload){
489  if( m_iov->token.empty() ) reportError("cond::IOVEditor::deleteEntries cannot delete to non-existing IOV sequence");
490  if( !m_isLoaded ){
491  reload();
492  }
493  boost::shared_ptr<IOVSequence>& iov = m_iov->data;
494  if(withPayload){
495  std::string tokenStr;
496  IOVSequence::const_iterator payloadIt;
497  IOVSequence::const_iterator payloadItEnd=iov->piovs().end();
498  for(payloadIt=iov->piovs().begin();payloadIt!=payloadItEnd;++payloadIt){
499  tokenStr=payloadIt->token();
500  m_iov->dbSession.deleteObject( tokenStr );
501  }
502  }
503  m_iov->dbSession.deleteObject( m_iov->token );
504  iov->piovs().clear();
505  }
506 
507  size_t IOVEditor::import( cond::DbSession& sourceSess,
508  const std::string& sourceIovToken ){
509  boost::shared_ptr<IOVImportIterator> importer = importIterator();
510  importer->setUp( sourceSess, sourceIovToken );
511  return importer->importAll();
512  }
513 
514  boost::shared_ptr<IOVImportIterator>
516  if( !m_isLoaded ){
517  reload();
518  }
519  return boost::shared_ptr<IOVImportIterator>( new IOVImportIterator( m_iov ));
520  }
521 
523  return m_iov->data->timeType();
524  }
525 
526  std::string const & IOVEditor::token() const {
527  return m_iov->token;
528  }
529 
531  return IOVProxy( m_iov );
532  }
533 
534 }
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:22
int i
Definition: DBlmapReader.cc:9
std::string create(cond::TimeType timetype)
Definition: IOVEditor.cc:244
void debugInfo(std::ostream &co) const
Definition: IOVEditor.cc:192
cond::IOVProxy proxy()
Definition: IOVEditor.cc:530
void load(const std::string &token)
Definition: IOVEditor.cc:186
void reportError(std::string message) const
Definition: IOVEditor.cc:215
Time_t beginValue
Definition: Time.h:45
const_iterator find(cond::Time_t time) const
Definition: IOVSequence.cc:110
void updateIOV(cond::DbSession &dbSess, const boost::shared_ptr< IOVSequence > &data, const std::string &token)
Definition: IOVEditor.cc:38
bool validTime(cond::Time_t time, cond::TimeType timetype) const
Definition: IOVEditor.cc:268
DbSession & db() const
Definition: IOVProxy.cc:311
cond::Time_t firstSince() const
Definition: IOVSequence.h:62
Container const & iovs() const
Definition: IOVSequence.cc:76
Definition: OId.h:8
size_t import(cond::DbSession &sourceSess, const std::string &sourceIovToken)
Definition: IOVEditor.cc:507
std::string const & token() const
Definition: IOVEditor.cc:526
boost::shared_ptr< cond::IOVSequence > loadIOV(cond::DbSession &dbSess, const std::string &iovToken)
Definition: IOVEditor.cc:9
void setScope(cond::IOVSequence::ScopeType scope)
set the scope
Definition: IOVEditor.cc:379
Container::const_iterator const_iterator
Definition: IOVSequence.h:28
unsigned int freeInsert(cond::Time_t sinceTime, const std::string &payloadToken)
insert a payload with known since in any position
Definition: IOVEditor.cc:435
cond::IOVProxy m_sourceIov
Definition: IOVEditor.h:48
IOVSequence::const_iterator m_cursor
Definition: IOVEditor.h:52
TimeType
Definition: Time.h:21
IOVImportIterator(boost::shared_ptr< cond::IOVProxyData > &destIov)
Definition: IOVEditor.cc:50
const std::string & className()
Definition: Container.cc:75
unsigned int truncate(bool withPayload=false)
Definition: IOVEditor.cc:469
bool exist(cond::Time_t time) const
Definition: IOVSequence.cc:127
unsigned long long Time_t
Definition: Time.h:16
TimeType timetype() const
Definition: IOVEditor.cc:522
void loadAll() const
Definition: IOVSequence.cc:69
bool firstTime
Definition: QTestHandle.cc:18
tuple iov
Definition: o2o.py:307
const T & max(const T &a, const T &b)
int containerId() const
Definition: OId.cc:50
IOVSequence const & iov() const
Definition: IOVProxy.cc:276
std::string storeObject(const T *object, const std::string &containerName)
Definition: DbSession.h:131
tuple out
Definition: dbtoconf.py:99
unsigned int insert(cond::Time_t tillTime, const std::string &payloadToken)
Assign a payload with till time. Returns the payload index in the iov sequence.
Definition: IOVEditor.cc:281
bool createIOVContainerIfNecessary()
Definition: IOVEditor.cc:233
IOVSequence::const_iterator m_till
Definition: IOVEditor.h:53
bool isOldSchema()
Definition: DbSession.cc:215
void updateClosure(cond::Time_t newtillTime)
Update the closure of the iov sequence.
Definition: IOVEditor.cc:389
unsigned int append(cond::Time_t sinceTime, const std::string &payloadToken)
Definition: IOVEditor.cc:400
cond::Time_t m_lastSince
Definition: IOVEditor.h:50
std::string name
Definition: Time.h:43
std::string insertIOV(cond::DbSession &dbSess, const boost::shared_ptr< IOVSequence > &data, bool swapOIds=false)
Definition: IOVEditor.cc:26
int cont
static std::string container()
Definition: IOVNames.h:7
virtual ~IOVImportIterator()
Definition: IOVEditor.cc:60
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void bulkAppend(std::vector< std::pair< cond::Time_t, std::string > > &values)
Bulk append of iov chunck.
Definition: IOVEditor.cc:305
void stamp(std::string const &icomment, bool append=false)
Definition: IOVEditor.cc:359
std::string userInfo()
Definition: IOVInfo.cc:9
void deleteEntries(bool withPayload=false)
Definition: IOVEditor.cc:488
bool createIOVContainer()
create iov tables
Time_t endValue
Definition: Time.h:46
IOVEditor(cond::DbSession &dbSess)
Definition: IOVEditor.cc:170
boost::shared_ptr< IOVImportIterator > importIterator()
Definition: IOVEditor.cc:515
bool fromString(const std::string &s)
Definition: OId.cc:64
bool updateObject(const T *object, const std::string &objectId)
Definition: DbSession.h:142
boost::shared_ptr< T > getTypedObject(const std::string &objectId)
Definition: DbSession.h:125
ora::Database & storage()
Definition: DbSession.cc:198
void setUp(cond::IOVProxy &sourceIov, cond::Time_t since, cond::Time_t till, bool outOfOrder, size_t bulkSize=1)
Definition: IOVEditor.cc:63
boost::shared_ptr< cond::IOVProxyData > m_destIov
Definition: IOVEditor.h:49
boost::shared_ptr< cond::IOVProxyData > m_iov
Definition: IOVEditor.h:140
~IOVEditor()
Destructor.
Definition: IOVEditor.cc:168
void editMetadata(std::string const &metadata, bool append=false)
edit metadata
Definition: IOVEditor.cc:369