CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CondDBTools.cc
Go to the documentation of this file.
4 //
6 //
7 #include <boost/filesystem.hpp>
8 #include <boost/regex.hpp>
9 #include <boost/bind.hpp>
10 
11 namespace cond {
12 
13  namespace persistency {
14 
15  size_t copyTag( const std::string& sourceTag,
16  Session& sourceSession,
17  const std::string& destTag,
18  Session& destSession,
19  UpdatePolicy policy,
20  bool log ){
21  persistency::TransactionScope ssc( sourceSession.transaction() );
22  ssc.start();
23  if( log ) std::cout <<" Loading source iov..."<<std::endl;
24  persistency::IOVProxy p = sourceSession.readIov( sourceTag, true );
25  if( p.loadedSize()==0 ) {
26  if( log ) std::cout <<" Tag contains 0 iovs."<<std::endl;
27  return 0;
28  }
29  if( log ) std::cout <<" Copying tag. Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
30 
32  persistency::TransactionScope dsc( destSession.transaction() );
33  dsc.start( false );
34  bool exists = false;
35  if( !destSession.existsDatabase() ) {
36  destSession.createDatabase();
37  } else {
38  exists = destSession.existsIov( destTag );
39  }
40  if( exists ){
41  if( policy == REPLACE ){
42  destSession.clearIov( destTag );
43  } else if( policy == NEW ){
44  destSession.transaction().rollback();
45  throwException(" Tag \""+destTag+"\" already exists.","copyTag");
46  }
47  editor = destSession.editIov( destTag );
48  } else {
49  editor = destSession.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
50  }
51  editor.setDescription("Tag "+sourceTag+" migrated from "+sourceSession.connectionString());
52 
53  size_t niovs = 0;
54  std::set<cond::Hash> pids;
55  std::set<cond::Time_t> sinces;
56  for( auto iov : p ){
57  // skip duplicated sinces
58  if( sinces.find( iov.since ) != sinces.end() ){
59  if( log ) std::cout <<" WARNING. Skipping duplicated since="<<iov.since<<std::endl;
60  continue;
61  }
62  sinces.insert( iov.since );
63  // make sure that we import the payload _IN_USE_
64  auto usedIov = p.getInterval( iov.since );
65  std::pair<std::string,boost::shared_ptr<void> > readBackPayload = fetch( usedIov.payloadId, sourceSession );
66  cond::Hash ph = import( sourceSession, usedIov.payloadId, readBackPayload.first, readBackPayload.second.get(), destSession );
67  editor.insert( iov.since, ph );
68  pids.insert( ph );
69  niovs++;
70  if( log && niovs && (niovs%1000==0) ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
71  }
72  if( log ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
73  if( log ) std::cout <<" Flushing changes..."<<std::endl;
74  editor.flush();
75  dsc.commit();
76  ssc.commit();
77  return niovs;
78  }
79 
80  // comparison functor for iov tuples: Time_t only and Time_t,string
81  struct IOVComp {
82  bool operator()( const cond::Time_t& x, const std::pair<cond::Time_t,boost::posix_time::ptime>& y ){ return ( x < y.first ); }
83  };
84 
85  size_t importIovs( const std::string& sourceTag,
86  Session& sourceSession,
87  const std::string& destTag,
88  Session& destSession,
91  const std::string& description,
92  bool log ){
93  persistency::TransactionScope ssc( sourceSession.transaction() );
94  ssc.start();
95  if( log ) std::cout <<" Loading source iov..."<<std::endl;
96  persistency::IOVProxy p = sourceSession.readIov( sourceTag, true );
97  if( p.loadedSize()==0 ) {
98  if( log ) std::cout <<" Tag contains 0 iovs."<<std::endl;
99  return 0;
100  } else {
101  if( log ) std::cout <<" Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
102  }
103  if( (*p.begin()).since > begin ) begin = (*p.begin()).since;
104  if( end < begin ) {
105  if( log ) std::cout <<" No Iov in the selected range."<<std::endl;
106  return 0;
107  }
109  persistency::TransactionScope dsc( destSession.transaction() );
110  dsc.start( false );
111  bool exists = false;
112  if( !destSession.existsDatabase() ) {
113  destSession.createDatabase();
114  } else {
115  exists = destSession.existsIov( destTag );
116  }
117  if( exists ){
118  editor = destSession.editIov( destTag );
119  if( editor.timeType() != p.timeType() )
120  throwException( "TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
121  if( editor.payloadType() != p.payloadObjectType() )
122  throwException( "PayloadType of the destination tag does not match with the source tag payloadType.", "importIovs");
123  } else {
124  editor = destSession.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
125  if( description.empty() ) editor.setDescription( "Created copying tag "+sourceTag+" from "+sourceSession.connectionString() );
126  else editor.setDescription( description );
127  }
128  size_t niovs = 0;
129  std::set<cond::Hash> pids;
130  std::set<cond::Time_t> sinces;
131  auto iiov = p.find( begin );
132  cond::Time_t newSince = begin;
133  while( iiov != p.end() ){
134  // skip duplicated sinces
135  if( sinces.find( newSince ) != sinces.end() ){
136  if( log ) std::cout <<" WARNING. Skipping duplicated since="<<newSince<<std::endl;
137  continue;
138  }
139  sinces.insert( newSince );
140  // make sure that we import the payload _IN_USE_
141  auto usedIov = p.getInterval( newSince );
142  std::pair<std::string,boost::shared_ptr<void> > readBackPayload = fetch( usedIov.payloadId, sourceSession );
143  cond::Hash ph = import( sourceSession, usedIov.payloadId, readBackPayload.first, readBackPayload.second.get(), destSession );
144  editor.insert( newSince, ph );
145  pids.insert( ph );
146  niovs++;
147  if( log && niovs && (niovs%1000==0) ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
148  iiov++;
149  if( iiov == p.end() || (*iiov).since > end ){
150  break;
151  } else {
152  newSince = (*iiov).since;
153  }
154  }
155  if( log ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
156  if( log ) std::cout <<" Flushing changes..."<<std::endl;
157  editor.flush();
158  dsc.commit();
159  ssc.commit();
160  return niovs;
161  }
162 
164  const std::string& sourceTag,
165  const std::string& destTag,
166  cond::Time_t sourceSince,
167  cond::Time_t destSince,
168  const std::string& description,
169  bool log ){
171  ssc.start( false );
172  if( log ) std::cout <<" Loading source iov..."<<std::endl;
173  persistency::IOVProxy p = session.readIov( sourceTag, true );
174  if( p.loadedSize()==0 ) {
175  if( log ) std::cout <<" Tag contains 0 iovs."<<std::endl;
176  return false;
177  } else {
178  if( log ) std::cout <<" Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
179  }
180 
181  auto iiov = p.find( sourceSince );
182  if( iiov == p.end() ){
183  if( log ) std::cout <<"ERROR: No Iov valid found for target time "<<sourceSince<<std::endl;
184  return false;
185  }
186 
188  if( session.existsIov( destTag ) ){
189  editor = session.editIov( destTag );
190  if( editor.timeType() != p.timeType() )
191  throwException( "TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
192  if( editor.payloadType() != p.payloadObjectType() )
193  throwException( "PayloadType of the destination tag does not match with the source tag payloadType.", "importIovs");
194  } else {
195  editor = session.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
196  if( description.empty() ) editor.setDescription( "Created copying iovs from tag "+sourceTag );
197  else editor.setDescription( description );
198  }
199 
200  editor.insert( destSince, (*iiov).payloadId );
201 
202  if( log ) std::cout <<" Flushing changes..."<<std::endl;
203  editor.flush();
204  ssc.commit();
205  return true;
206  }
207 
208  }
209 }
210 
cond::SynchronizationType synchronizationType() const
Definition: IOVProxy.cc:199
void clearIov(const std::string &tag)
Definition: Session.cc:148
size_t importIovs(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, cond::Time_t begin, cond::Time_t end, const std::string &description, bool log)
Definition: CondDBTools.cc:85
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:173
void setDescription(const std::string &description)
Definition: IOVEditor.cc:117
std::string payloadObjectType() const
Definition: IOVProxy.cc:195
Transaction & transaction()
Definition: Session.cc:66
list sinces
Definition: EcalCondDB.py:80
Iterator begin() const
Definition: IOVProxy.cc:257
size_t copyTag(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, UpdatePolicy policy, bool log)
Definition: CondDBTools.cc:15
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
cond::TimeType timeType() const
Definition: IOVProxy.cc:191
unsigned long long Time_t
Definition: Time.h:16
tuple iov
Definition: o2o.py:307
Iterator find(cond::Time_t time)
Definition: IOVProxy.cc:288
void start(bool readOnly=true)
Definition: Session.cc:227
std::string Hash
Definition: Types.h:45
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:317
#define end
Definition: vmac.h:37
std::pair< std::string, boost::shared_ptr< void > > fetch(const cond::Hash &payloadId, Session &session)
Definition: CondDBFetch.cc:311
std::string connectionString()
Definition: Session.cc:203
bool copyIov(Session &session, const std::string &sourceTag, const std::string &destTag, cond::Time_t souceSince, cond::Time_t destSince, const std::string &description, bool log)
Definition: CondDBTools.cc:163
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:141
tuple description
Definition: idDealer.py:66
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:139
tuple editor
Definition: idDealer.py:73
bool existsIov(const std::string &tag)
Definition: Session.cc:97
cond::TimeType timeType() const
Definition: IOVEditor.cc:90
#define begin
Definition: vmac.h:30
tuple cout
Definition: gather_cfg.py:145
session
Definition: models.py:201
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, boost::posix_time::ptime > &y)
Definition: CondDBTools.cc:82
std::string payloadType() const
Definition: IOVEditor.cc:94
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
Iterator end() const
Definition: IOVProxy.cc:265