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