CMS 3D CMS Logo

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