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,
47  cond::Time_t begin,
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);
58  auto iovs = p.selectAll();
59  if (iovs.size() == 0) {
60  std::cout << " Tag contains 0 iovs." << std::endl;
61  return 0;
62  } else {
63  std::cout << " Iov size:" << iovs.size() << " timeType:" << p.tagInfo().timeType << " payloadObjectType=\""
64  << p.tagInfo().payloadType << "\"" << std::endl;
65  }
66  if ((*iovs.begin()).since > begin)
67  begin = (*iovs.begin()).since;
68  if (end < begin) {
69  std::cout << " No Iov in the selected range." << std::endl;
70  return 0;
71  }
73  persistency::TransactionScope dsc(destSession.transaction());
74  dsc.start(false);
75  bool exists = false;
76  if (!destSession.existsDatabase()) {
77  destSession.createDatabase();
78  } else {
79  exists = destSession.existsIov(destTag);
80  }
82  if (exists) {
83  dp = destSession.readIov(destTag);
84  editor = destSession.editIov(destTag);
85  if (!description.empty())
86  std::cout << " INFO. Destination Tag " << destTag
87  << " already exists. Provided description will be ignored." << std::endl;
88  if (editor.timeType() != p.tagInfo().timeType)
89  throwException("TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
90  if (editor.payloadType() != p.tagInfo().payloadType)
91  throwException("PayloadType of the destination tag does not match with the source tag payloadType.",
92  "importIovs");
93  } else {
94  editor = destSession.createIov(
95  p.tagInfo().payloadType, destTag, p.tagInfo().timeType, p.tagInfo().synchronizationType);
96  if (description.empty())
97  editor.setDescription("Created copying tag " + sourceTag + " from " + sourceSession.connectionString());
98  else
100  }
101  size_t niovs = 0;
102  std::set<cond::Hash> pids;
103  std::set<cond::Time_t> sinces;
104  auto iiov = iovs.find(begin);
105  cond::Time_t newSince = begin;
106  while (iiov != iovs.end()) {
107  // skip duplicated sinces
108  if (sinces.find(newSince) != sinces.end()) {
109  std::cout << " WARNING. Skipping duplicated since=" << newSince << std::endl;
110  continue;
111  }
112  // make sure that we import the payload _IN_USE_
113  auto usedIov = p.getInterval(newSince);
114  cond::Hash ph = importPayload(sourceSession, usedIov.payloadId, destSession, reserialize);
115  pids.insert(ph);
116  bool skip = false;
117  if (exists) {
118  // don't insert if the same entry is already there...
119  auto diovs = dp.selectAll();
120  auto ie = diovs.find(newSince);
121  if (ie != diovs.end()) {
122  if (((*ie).since == newSince) && ((*ie).payloadId == usedIov.payloadId)) {
123  skip = true;
124  }
125  }
126  }
127  if (!skip) {
128  editor.insert(newSince, ph);
129  sinces.insert(newSince);
130  niovs++;
131  if (niovs && (niovs % 1000 == 0))
132  std::cout << " Total of iov inserted: " << niovs << " payloads: " << pids.size() << std::endl;
133  }
134  iiov++;
135  if (iiov == iovs.end() || (*iiov).since > end) {
136  break;
137  } else {
138  newSince = (*iiov).since;
139  }
140  }
141  if (exists && override) {
142  std::cout << " Adding overlying iovs..." << std::endl;
143  persistency::IOVProxy dp = destSession.readIov(destTag);
144  auto diovs = dp.selectRange(begin, end);
145  std::set<cond::Time_t> extraSinces;
146  for (const auto& iov : diovs) {
147  auto siov = p.getInterval(iov.since);
148  if (siov.since != iov.since) {
149  if (extraSinces.find(iov.since) == extraSinces.end()) {
150  editor.insert(iov.since, siov.payloadId);
151  extraSinces.insert(iov.since);
152  niovs++;
153  if (niovs && (niovs % 1000 == 0))
154  std::cout << " Total of iov inserted: " << niovs << " payloads: " << pids.size() << std::endl;
155  }
156  }
157  }
158  }
159  std::cout << " Total of iov inserted: " << niovs << " payloads: " << pids.size() << std::endl;
160  std::cout << " Flushing changes..." << std::endl;
161  editor.flush(editingNote, forceInsert);
162  dsc.commit();
163  ssc.commit();
164  return niovs;
165  }
166 
167  bool copyIov(Session& session,
168  const std::string& sourceTag,
169  const std::string& destTag,
170  cond::Time_t sourceSince,
171  cond::Time_t destSince,
172  const std::string& description) {
174  ssc.start(false);
175  std::cout << " Loading source iov..." << std::endl;
177  auto iovs = p.selectAll();
178  if (iovs.size() == 0) {
179  std::cout << " Tag contains 0 iovs." << std::endl;
180  return false;
181  } else {
182  std::cout << " Iov size:" << iovs.size() << " timeType:" << p.tagInfo().timeType << " payloadObjectType=\""
183  << p.tagInfo().payloadType << "\"" << std::endl;
184  }
185 
186  auto iiov = iovs.find(sourceSince);
187  if (iiov == iovs.end()) {
188  std::cout << "ERROR: No Iov valid found for target time " << sourceSince << std::endl;
189  return false;
190  }
191 
192  persistency::IOVEditor editor;
193  if (session.existsIov(destTag)) {
194  if (!description.empty())
195  std::cout << " INFO. Destination Tag " << destTag
196  << " already exists. Provided description will be ignored." << std::endl;
197  editor = session.editIov(destTag);
198  if (editor.timeType() != p.tagInfo().timeType)
199  throwException("TimeType of the destination tag does not match with the source tag timeType.", "importIovs");
200  if (editor.payloadType() != p.tagInfo().payloadType)
201  throwException("PayloadType of the destination tag does not match with the source tag payloadType.",
202  "importIovs");
203  } else {
204  editor =
205  session.createIov(p.tagInfo().payloadType, destTag, p.tagInfo().timeType, p.tagInfo().synchronizationType);
206  if (description.empty())
207  editor.setDescription("Created copying iovs from tag " + sourceTag);
208  else
209  editor.setDescription(description);
210  }
211 
212  editor.insert(destSince, (*iiov).payloadId);
213 
214  std::cout << " Flushing changes..." << std::endl;
215  editor.flush();
216  ssc.commit();
217  return true;
218  }
219 
220  } // namespace persistency
221 } // namespace cond
cond::persistency::TransactionScope
Definition: Session.h:231
cond::persistency::IOVEditor::insert
void insert(cond::Time_t since, const cond::Hash &payloadHash, bool checkType=false)
Definition: IOVEditor.cc:160
ConnectionPool.h
cond::Hash
std::string Hash
Definition: Types.h:43
cond::persistency::Session::existsIov
bool existsIov(const std::string &tag)
Definition: Session.cc:77
cond::Binary
Definition: Binary.h:15
submitPVValidationJobs.now
now
Definition: submitPVValidationJobs.py:639
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
edmLumisInFiles.description
description
Definition: edmLumisInFiles.py:11
gather_cfg.cout
cout
Definition: gather_cfg.py:144
cond::persistency::IOVEditor
Definition: IOVEditor.h:28
cond::persistency::TransactionScope::start
void start(bool readOnly=true)
Definition: Session.cc:236
cond::persistency::fetch
std::pair< std::string, std::shared_ptr< void > > fetch(const cond::Hash &payloadId, Session &session)
Definition: CondDBFetch.cc:338
cond::persistency::Session::createDatabase
void createDatabase()
Definition: Session.cc:61
optionsL1T.skip
skip
Definition: optionsL1T.py:30
Calorimetry_cff.dp
dp
Definition: Calorimetry_cff.py:157
mps_fire.end
end
Definition: mps_fire.py:242
writeEcalDQMStatus.since
since
Definition: writeEcalDQMStatus.py:53
cond::persistency::importIovs
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
cond::persistency::Session::readIov
IOVProxy readIov(const std::string &tag)
Definition: Session.cc:63
cond::persistency::IOVEditor::setDescription
void setDescription(const std::string &description)
Definition: IOVEditor.cc:140
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond::persistency::IOVProxy
Definition: IOVProxy.h:92
cond
Definition: plugin.cc:23
cond::persistency::Session::storePayloadData
cond::Hash storePayloadData(const std::string &payloadObjectType, const std::pair< Binary, Binary > &payloadAndStreamerInfoData, const boost::posix_time::ptime &creationTime)
Definition: Session.cc:177
cond::persistency::Session::editIov
IOVEditor editIov(const std::string &tag)
Definition: Session.cc:130
cond::persistency::IOVEditor::timeType
cond::TimeType timeType() const
Definition: IOVEditor.cc:112
cond::persistency::Session
Definition: Session.h:63
cond::persistency::IOVComp::operator()
bool operator()(const cond::Time_t &x, const std::pair< cond::Time_t, boost::posix_time::ptime > &y)
Definition: CondDBTools.cc:38
cond::Time_t
unsigned long long Time_t
Definition: Time.h:14
cond::persistency::Session::connectionString
std::string connectionString()
Definition: Session.cc:216
DbCore.h
cond::persistency::Session::fetchPayloadData
bool fetchPayloadData(const cond::Hash &payloadHash, std::string &payloadType, cond::Binary &payloadData, cond::Binary &streamerInfoData)
Definition: Session.cc:185
RawToDigi_cff.sourceTag
sourceTag
Definition: RawToDigi_cff.py:78
cond::persistency::IOVEditor::payloadType
std::string payloadType() const
Definition: IOVEditor.cc:114
CondDBImport.h
cond::persistency::Session::transaction
Transaction & transaction()
Definition: Session.cc:52
HLT_FULL_cff.payloadType
payloadType
Definition: HLT_FULL_cff.py:9452
cond::persistency::Session::existsDatabase
bool existsDatabase()
Definition: Session.cc:55
cond::persistency::copyIov
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:167
cond::persistency::Session::createIov
IOVEditor createIov(const std::string &tag, cond::TimeType timeType, cond::SynchronizationType synchronizationType=cond::SYNCH_ANY)
Definition: Session.h:179
cond::persistency::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
cond::persistency::importPayload
cond::Hash importPayload(Session &sourceSession, const cond::Hash &sourcePayloadId, Session &destSession, bool reserialize)
Definition: CondDBTools.cc:16
CondDBTools.h
cond::persistency::IOVEditor::flush
bool flush()
Definition: IOVEditor.cc:316
cond::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18