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 //
5 #include <boost/filesystem.hpp>
6 
7 namespace cond {
8 
9  namespace persistency {
10 
11  size_t copyTag( const std::string& sourceTag,
12  Session& sourceSession,
13  const std::string& destTag,
14  Session& destSession,
15  UpdatePolicy policy,
16  bool log,
17  bool forValidation ){
18  persistency::TransactionScope ssc( sourceSession.transaction() );
19  ssc.start();
20  if( log && !forValidation ) std::cout <<" Loading source iov..."<<std::endl;
21  persistency::IOVProxy p = sourceSession.readIov( sourceTag, true );
22  if( p.loadedSize()==0 ) {
23  if( log ) std::cout <<" Tag contains 0 iovs."<<std::endl;
24  return 0;
25  }
26  if( log && !forValidation ) std::cout <<" Copying tag. Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
27 
29  persistency::TransactionScope dsc( destSession.transaction() );
30  dsc.start( false );
31  bool exists = false;
32  if( !destSession.existsDatabase() ) {
33  destSession.createDatabase();
34  } else {
35  exists = destSession.existsIov( destTag );
36  }
37  if( exists ){
38  if( policy == REPLACE ){
39  destSession.clearIov( destTag );
40  } else if( policy == NEW ){
41  destSession.transaction().rollback();
42  throwException(" Tag \""+destTag+"\" already exists.","copyTag");
43  }
44  editor = destSession.editIov( destTag );
45  } else {
46  editor = destSession.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
47  }
48  if( forValidation ) {
49  editor.setValidationMode();
50  editor.setDescription("Validation");
51  } else {
52  editor.setDescription("Tag "+sourceTag+" migrated from "+sourceSession.connectionString());
53  }
54  size_t niovs = 0;
55  std::set<cond::Hash> pids;
56  std::set<cond::Time_t> sinces;
57  for( auto iov : p ){
58  // skip duplicated sinces
59  if( sinces.find( iov.since ) != sinces.end() ){
60  if( log && !forValidation ) std::cout <<" WARNING. Skipping duplicated since="<<iov.since<<std::endl;
61  continue;
62  }
63  sinces.insert( iov.since );
64  // make sure that we import the payload _IN_USE_
65  auto usedIov = p.getInterval( iov.since );
66  std::pair<std::string,boost::shared_ptr<void> > readBackPayload = fetch( usedIov.payloadId, sourceSession );
67  cond::Hash ph = import( readBackPayload.first, readBackPayload.second.get(), destSession );
68  editor.insert( iov.since, ph );
69  pids.insert( ph );
70  niovs++;
71  if( log && !forValidation && niovs && (niovs%1000==0) ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
72  }
73  if( log && !forValidation) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
74  if( log && !forValidation) std::cout <<" Flushing changes..."<<std::endl;
75  editor.flush();
76  dsc.commit();
77  ssc.commit();
78  return niovs;
79  }
80 
81  size_t importIovs( const std::string& sourceTag,
82  Session& sourceSession,
83  const std::string& destTag,
84  Session& destSession,
87  bool log ){
88  persistency::TransactionScope ssc( sourceSession.transaction() );
89  ssc.start();
90  if( log ) std::cout <<" Loading source iov..."<<std::endl;
91  persistency::IOVProxy p = sourceSession.readIov( sourceTag, true );
92  if( p.loadedSize()==0 ) {
93  if( log ) std::cout <<" Tag contains 0 iovs."<<std::endl;
94  return 0;
95  } else {
96  if( log ) std::cout <<" Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
97  }
99  persistency::TransactionScope dsc( destSession.transaction() );
100  dsc.start( false );
101  bool exists = false;
102  if( !destSession.existsDatabase() ) {
103  destSession.createDatabase();
104  } else {
105  exists = destSession.existsIov( destTag );
106  }
107  if( exists ){
108  editor = destSession.editIov( destTag );
109  if( editor.timeType() != p.timeType() )
110  throwException( "TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
111  if( editor.payloadType() != p.payloadObjectType() )
112  throwException( "PayloadType of the destination tag does not match with the source tag payloadType.", "importIovs");
113  } else {
114  editor = destSession.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
115  editor.setDescription( "Copied tag "+sourceTag+" from "+sourceSession.connectionString() );
116  }
117  size_t niovs = 0;
118  std::set<cond::Hash> pids;
119  std::set<cond::Time_t> sinces;
120  auto iiov = p.find( begin );
121  cond::Time_t newSince = begin;
122  while( iiov != p.end() ){
123  // skip duplicated sinces
124  if( sinces.find( newSince ) != sinces.end() ){
125  if( log ) std::cout <<" WARNING. Skipping duplicated since="<<newSince<<std::endl;
126  continue;
127  }
128  sinces.insert( newSince );
129  // make sure that we import the payload _IN_USE_
130  auto usedIov = p.getInterval( newSince );
131  std::pair<std::string,boost::shared_ptr<void> > readBackPayload = fetch( usedIov.payloadId, sourceSession );
132  cond::Hash ph = import( readBackPayload.first, readBackPayload.second.get(), destSession );
133  editor.insert( newSince, ph );
134  pids.insert( ph );
135  niovs++;
136  if( log && niovs && (niovs%1000==0) ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
137  iiov++;
138  if( iiov == p.end() || (*iiov).since > end ){
139  break;
140  } else {
141  newSince = (*iiov).since;
142  }
143  }
144  if( log ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
145  if( log ) std::cout <<" Flushing changes..."<<std::endl;
146  editor.flush();
147  dsc.commit();
148  ssc.commit();
149  return niovs;
150  }
151 
153  const std::string& sourceTag,
154  const std::string& destTag,
155  cond::Time_t sourceSince,
156  cond::Time_t destSince,
157  bool log ){
159  ssc.start( false );
160  if( log ) std::cout <<" Loading source iov..."<<std::endl;
161  persistency::IOVProxy p = session.readIov( sourceTag, true );
162  if( p.loadedSize()==0 ) {
163  if( log ) std::cout <<" Tag contains 0 iovs."<<std::endl;
164  return false;
165  } else {
166  if( log ) std::cout <<" Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
167  }
168 
169  auto iiov = p.find( sourceSince );
170  if( iiov == p.end() ){
171  if( log ) std::cout <<"ERROR: No Iov valid found for target time "<<sourceSince<<std::endl;
172  return false;
173  }
174 
176  if( session.existsIov( destTag ) ){
177  editor = session.editIov( destTag );
178  if( editor.timeType() != p.timeType() )
179  throwException( "TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
180  if( editor.payloadType() != p.payloadObjectType() )
181  throwException( "PayloadType of the destination tag does not match with the source tag payloadType.", "importIovs");
182  } else {
183  editor = session.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
184  editor.setDescription( "Copied tag "+sourceTag+" from "+session.connectionString() );
185  }
186 
187  editor.insert( destSince, (*iiov).payloadId );
188 
189  if( log ) std::cout <<" Flushing changes..."<<std::endl;
190  editor.flush();
191  ssc.commit();
192  return true;
193  }
194 
195  size_t exportTagToFile( const std::string& tag, const std::string& destTag, Session& session, const std::string fileName ){
196  ConnectionPool localPool;
197  Session writeSession = localPool.createSession( "sqlite:"+fileName, true );
198  size_t ret = copyTag( tag, session, destTag, writeSession, NEW, false,true );
199  return ret;
200  }
201 
202  bool compareTags( const std::string& firstTag,
203  Session& firstSession,
204  const std::string& firstFileName,
205  const std::string& secondTag,
206  Session& secondSession,
207  const std::string& secondFileName ){
208  size_t n1 = exportTagToFile( firstTag, firstTag, firstSession, firstFileName );
209  if( ! n1 ){
210  std::cout <<"Can't compare empty tag "<<firstTag<<std::endl;
211  return false;
212  }
213  size_t n2 = exportTagToFile( secondTag, firstTag, secondSession, secondFileName );
214  if( ! n2 ){
215  std::cout <<"Can't compare empty tag "<<secondTag<<std::endl;
216  return false;
217  }
218  if( n1 != n2 ) {
219  std::cout <<" Tag size is different. "<<firstSession.connectionString()<<":"<<firstTag<<": "<<n1<<" "<<
220  secondSession.connectionString()<<":"<<secondTag<<": "<<n2<<std::endl;
221  }
222 
223  FILE* file1 = fopen( firstFileName.c_str(), "r" );
224  if( file1 == NULL ){
225  throwException("Can't open file "+firstFileName, "compareTags" );
226  }
227  FILE* file2 = fopen( secondFileName.c_str(), "r" );
228  if( file2 == NULL ){
229  throwException("Can't open file "+secondFileName, "compareTags" );
230  }
231  int N = 10000;
232  char buf1[N];
233  char buf2[N];
234 
235  bool cmpOk = true;
236  size_t totSize = 0;
237  do {
238  size_t r1 = fread( buf1, 1, N, file1 );
239  size_t r2 = fread( buf2, 1, N, file2 );
240 
241  if( r1 != r2 || memcmp( buf1, buf2, r1)) {
242  cmpOk = false;
243  break;
244  }
245  totSize += r1;
246  } while(!feof(file2) || !feof(file2));
247 
248  std::cout <<" "<<totSize<<" bytes compared."<<std::endl;
249  fclose( file1 );
250  fclose( file2 );
251 
252  if( cmpOk ){
253  boost::filesystem::path fp1( firstFileName );
255  boost::filesystem::path fp2( secondFileName );
257  }
258 
259  return cmpOk;
260  }
261 
262  bool validateTag( const std::string& refTag,
263  Session& refSession,
264  const std::string& candTag,
265  Session& candSession ){
266  std::cout <<" Validating..."<<std::endl;
267  std::tuple<std::string,std::string,std::string> connPars = persistency::parseConnectionString( refSession.connectionString() );
268  std::string dbLabel = std::get<2>( connPars );
269  std::string tagLabel = dbLabel+"_"+refTag;
270  std::string refFile = tagLabel+"_ref.db";
271  std::string candFile = tagLabel+"_cand.db";
272  bool ret = compareTags( refTag, refSession, refFile, candTag, candSession, candFile );
273  if( ret ) {
274  boost::filesystem::path refF( refFile );
275  if( boost::filesystem::exists(refF) ) boost::filesystem::remove( refF );
276  boost::filesystem::path candF( candFile );
277  if( boost::filesystem::exists(candF) ) boost::filesystem::remove( candF );
278  }
279  return ret;
280  }
281 
282  }
283 }
284 
cond::SynchronizationType synchronizationType() const
Definition: IOVProxy.cc:174
void clearIov(const std::string &tag)
Definition: Session.cc:127
#define NULL
Definition: scimark2.h:8
void setDescription(const std::string &description)
Definition: IOVEditor.cc:113
std::string payloadObjectType() const
Definition: IOVProxy.cc:170
Transaction & transaction()
Definition: Session.cc:66
list sinces
Definition: EcalCondDB.py:80
std::tuple< std::string, std::string, std::string > parseConnectionString(const std::string &connectionString)
Definition: Utils.h:43
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
cond::TimeType timeType() const
Definition: IOVProxy.cc:166
unsigned long long Time_t
Definition: Time.h:16
tuple path
else: Piece not in the list, fine.
tuple iov
Definition: o2o.py:307
Session createSession(const std::string &connectionString, bool writeCapable=false, BackendType backType=DEFAULT_DB)
size_t copyTag(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, UpdatePolicy policy, bool log, bool forValidation)
Definition: CondDBTools.cc:11
Iterator find(cond::Time_t time)
Definition: IOVProxy.cc:235
size_t exportTagToFile(const std::string &tag, const std::string &destTag, Session &session, const std::string fileName)
Definition: CondDBTools.cc:195
void start(bool readOnly=true)
Definition: Session.cc:239
std::string Hash
Definition: Types.h:43
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:274
#define end
Definition: vmac.h:37
std::pair< std::string, boost::shared_ptr< void > > fetch(const cond::Hash &payloadId, Session &session)
std::string connectionString()
Definition: Session.cc:215
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:120
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:135
#define N
Definition: blowfish.cc:9
tuple editor
Definition: idDealer.py:73
bool existsIov(const std::string &tag)
Definition: Session.cc:88
cond::TimeType timeType() const
Definition: IOVEditor.cc:86
bool copyIov(Session &session, const std::string &sourceTag, const std::string &destTag, cond::Time_t souceSince, cond::Time_t destSince, bool log)
Definition: CondDBTools.cc:152
bool validateTag(const std::string &refTag, Session &refSession, const std::string &candTag, Session &candSession)
Definition: CondDBTools.cc:262
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::OFFLINE)
Definition: Session.h:178
#define begin
Definition: vmac.h:30
tuple cout
Definition: gather_cfg.py:121
std::string payloadType() const
Definition: IOVEditor.cc:90
bool compareTags(const std::string &firstTag, Session &firstSession, const std::string &firstFileName, const std::string &secondTag, Session &secondSession, const std::string &secondFileName)
Definition: CondDBTools.cc:202
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11
Iterator end() const
Definition: IOVProxy.cc:213
size_t importIovs(const std::string &sourceTag, Session &sourceSession, const std::string &destTag, Session &destSession, cond::Time_t begin, cond::Time_t end, bool log)
Definition: CondDBTools.cc:81