CMS 3D CMS Logo

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