CMS 3D CMS Logo

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 #include <memory>
11 
12 namespace cond {
13 
14  namespace persistency {
15 
16  cond::Hash importPayload( Session& sourceSession, const cond::Hash& sourcePayloadId, Session& destSession, bool reserialize ){
17  if( reserialize ){
18  std::pair<std::string,std::shared_ptr<void> > readBackPayload = fetch( sourcePayloadId, sourceSession );
19  return import( sourceSession, sourcePayloadId, readBackPayload.first, readBackPayload.second.get(), destSession );
20  } else {
21  std::string payloadType("");
22  cond::Binary payloadData;
23  cond::Binary streamerInfoData;
24  if( !sourceSession.fetchPayloadData( sourcePayloadId, payloadType, payloadData, streamerInfoData ) ){
25  cond::throwException( "Payload with hash"+sourcePayloadId+" has not been found in the source database.","importPayload");
26  }
27  boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
28  return destSession.storePayloadData( payloadType, std::make_pair( payloadData, streamerInfoData ),now );
29  }
30  }
31 
32  // comparison functor for iov tuples: Time_t only and Time_t,string
33  struct IOVComp {
34  bool operator()( const cond::Time_t& x, const std::pair<cond::Time_t,boost::posix_time::ptime>& y ){ return ( x < y.first ); }
35  };
36 
37  size_t importIovs( const std::string& sourceTag,
38  Session& sourceSession,
39  const std::string& destTag,
40  Session& destSession,
43  const std::string& description,
44  const std::string& editingNote,
45  bool override,
46  bool reserialize,
47  bool forceInsert ){
48  persistency::TransactionScope ssc( sourceSession.transaction() );
49  ssc.start();
50  std::cout <<" Loading source iov..."<<std::endl;
51  persistency::IOVProxy p = sourceSession.readIov( sourceTag, true );
52  if( p.loadedSize()==0 ) {
53  std::cout <<" Tag contains 0 iovs."<<std::endl;
54  return 0;
55  } else {
56  std::cout <<" Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
57  }
58  if( (*p.begin()).since > begin ) begin = (*p.begin()).since;
59  if( end < begin ) {
60  std::cout <<" No Iov in the selected range."<<std::endl;
61  return 0;
62  }
64  persistency::TransactionScope dsc( destSession.transaction() );
65  dsc.start( false );
66  bool exists = false;
67  if( !destSession.existsDatabase() ) {
68  destSession.createDatabase();
69  } else {
70  exists = destSession.existsIov( destTag );
71  }
73  if( exists ){
74  dp = destSession.readIov( destTag );
75  editor = destSession.editIov( destTag );
76  if( !description.empty() ) std::cout <<" INFO. Destination Tag "<<destTag<<" already exists. Provided description will be ignored."<<std::endl;
77  if( editor.timeType() != p.timeType() )
78  throwException( "TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
79  if( editor.payloadType() != p.payloadObjectType() )
80  throwException( "PayloadType of the destination tag does not match with the source tag payloadType.", "importIovs");
81  } else {
82  editor = destSession.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
83  if( description.empty() ) editor.setDescription( "Created copying tag "+sourceTag+" from "+sourceSession.connectionString() );
84  else editor.setDescription( description );
85  }
86  size_t niovs = 0;
87  std::set<cond::Hash> pids;
88  std::set<cond::Time_t> sinces;
89  auto iiov = p.find( begin );
90  cond::Time_t newSince = begin;
91  while( iiov != p.end() ){
92  // skip duplicated sinces
93  if( sinces.find( newSince ) != sinces.end() ){
94  std::cout <<" WARNING. Skipping duplicated since="<<newSince<<std::endl;
95  continue;
96  }
97  // make sure that we import the payload _IN_USE_
98  auto usedIov = p.getInterval( newSince );
99  cond::Hash ph = importPayload( sourceSession, usedIov.payloadId, destSession, reserialize );
100  pids.insert( ph );
101  bool skip = false;
102  if( exists ){
103  // don't insert if the same entry is already there...
104  auto ie = dp.find( newSince );
105  if( ie != dp.end() ){
106  if( ((*ie).since == newSince) && ((*ie).payloadId == usedIov.payloadId) ) {
107  skip = true;
108  }
109  }
110  }
111  if( !skip ){
112  editor.insert( newSince, ph );
113  sinces.insert( newSince );
114  niovs++;
115  if( niovs && (niovs%1000==0) ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
116  }
117  iiov++;
118  if( iiov == p.end() || (*iiov).since > end ){
119  break;
120  } else {
121  newSince = (*iiov).since;
122  }
123  }
124  if( exists && override ){
125  std::cout <<" Adding overlying iovs..."<<std::endl;
127  dp = destSession.iovProxy();
128  dp.loadRange( destTag, begin, end );
129  std::set<cond::Time_t> extraSinces;
130  for( auto iov : dp ){
131  auto siov = p.getInterval( iov.since );
132  if( siov.since != iov.since ) {
133  if( extraSinces.find( iov.since )==extraSinces.end() ){
134  editor.insert( iov.since, siov.payloadId );
135  extraSinces.insert( iov.since );
136  niovs++;
137  if( niovs && (niovs%1000==0) ) std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
138  }
139  }
140  }
141  }
142  std::cout <<" Total of iov inserted: "<<niovs<<" payloads: "<<pids.size()<<std::endl;
143  std::cout <<" Flushing changes..."<<std::endl;
144  editor.flush( editingNote, forceInsert );
145  dsc.commit();
146  ssc.commit();
147  return niovs;
148  }
149 
151  const std::string& sourceTag,
152  const std::string& destTag,
153  cond::Time_t sourceSince,
154  cond::Time_t destSince,
155  const std::string& description ){
157  ssc.start( false );
158  std::cout <<" Loading source iov..."<<std::endl;
159  persistency::IOVProxy p = session.readIov( sourceTag, true );
160  if( p.loadedSize()==0 ) {
161  std::cout <<" Tag contains 0 iovs."<<std::endl;
162  return false;
163  } else {
164  std::cout <<" Iov size:"<<p.loadedSize()<<" timeType:"<<p.timeType()<<" payloadObjectType=\""<<p.payloadObjectType()<<"\""<<std::endl;
165  }
166 
167  auto iiov = p.find( sourceSince );
168  if( iiov == p.end() ){
169  std::cout <<"ERROR: No Iov valid found for target time "<<sourceSince<<std::endl;
170  return false;
171  }
172 
174  if( session.existsIov( destTag ) ){
175  if( !description.empty() ) std::cout <<" INFO. Destination Tag "<<destTag<<" already exists. Provided description will be ignored."<<std::endl;
176  editor = session.editIov( destTag );
177  if( editor.timeType() != p.timeType() )
178  throwException( "TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
179  if( editor.payloadType() != p.payloadObjectType() )
180  throwException( "PayloadType of the destination tag does not match with the source tag payloadType.", "importIovs");
181  } else {
182  editor = session.createIov( p.payloadObjectType(), destTag, p.timeType(), p.synchronizationType() );
183  if( description.empty() ) editor.setDescription( "Created copying iovs from tag "+sourceTag );
184  else editor.setDescription( description );
185  }
186 
187  editor.insert( destSince, (*iiov).payloadId );
188 
189  std::cout <<" Flushing changes..."<<std::endl;
190  editor.flush();
191  ssc.commit();
192  return true;
193  }
194 
195  }
196 }
197 
cond::SynchronizationType synchronizationType() const
Definition: IOVProxy.cc:235
bool copyIov(Session &session, const std::string &sourceTag, const std::string &destTag, cond::Time_t souceSince, cond::Time_t destSince, const std::string &description)
Definition: CondDBTools.cc:150
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, const std::string &editingNote, bool override, bool serialize, bool forceInsert)
Definition: CondDBTools.cc:37
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:188
void setDescription(const std::string &description)
Definition: IOVEditor.cc:128
std::string payloadObjectType() const
Definition: IOVProxy.cc:231
Transaction & transaction()
Definition: Session.cc:66
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
Iterator begin() const
Definition: IOVProxy.cc:289
IOVProxy readIov(const std::string &tag, bool full=false)
Definition: Session.cc:81
cond::TimeType timeType() const
Definition: IOVProxy.cc:227
unsigned long long Time_t
Definition: Time.h:16
Iterator find(cond::Time_t time)
Definition: IOVProxy.cc:320
void start(bool readOnly=true)
Definition: Session.cc:256
std::string Hash
Definition: Types.h:45
bool fetchPayloadData(const cond::Hash &payloadHash, std::string &payloadType, cond::Binary &payloadData, cond::Binary &streamerInfoData)
Definition: Session.cc:210
cond::Iov_t getInterval(cond::Time_t time)
Definition: IOVProxy.cc:349
#define end
Definition: vmac.h:39
cond::Hash importPayload(Session &sourceSession, const cond::Hash &sourcePayloadId, Session &destSession, bool reserialize)
Definition: CondDBTools.cc:16
std::string connectionString()
Definition: Session.cc:232
cond::Hash storePayloadData(const std::string &payloadObjectType, const std::pair< Binary, Binary > &payloadAndStreamerInfoData, const boost::posix_time::ptime &creationTime)
Definition: Session.cc:202
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:156
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:152
void loadRange(const std::string &tag, const cond::Time_t &begin, const cond::Time_t &end)
Definition: IOVProxy.cc:170
bool existsIov(const std::string &tag)
Definition: Session.cc:97
cond::TimeType timeType() const
Definition: IOVEditor.cc:92
Definition: plugin.cc:24
#define begin
Definition: vmac.h:32
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, boost::posix_time::ptime > &y)
Definition: CondDBTools.cc:34
std::pair< std::string, std::shared_ptr< void > > fetch(const cond::Hash &payloadId, Session &session)
Definition: CondDBFetch.cc:329
std::string payloadType() const
Definition: IOVEditor.cc:96
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14
Iterator end() const
Definition: IOVProxy.cc:297