CMS 3D CMS Logo

GeneralCatalog.cc
Go to the documentation of this file.
1 #include <cassert>
2 #include <utility>
3 #include <algorithm>
4 
5 #include "Alignment/Geners/interface/CPP11_shared_ptr.hh"
6 #include "Alignment/Geners/interface/GeneralCatalog.hh"
7 #include "Alignment/Geners/interface/binaryIO.hh"
8 #include "Alignment/Geners/interface/IOException.hh"
9 
10 namespace gs {
11  GeneralCatalog::GeneralCatalog()
12  : smallestId_(1ULL),
13  largestId_(0)
14  {
15  }
16 
17  void GeneralCatalog::findByName(
18  const NameMap& m,
19  const SearchSpecifier& namePattern,
20  std::vector<unsigned long long>* found) const
21  {
22  typedef NameMap::const_iterator Nameiter;
23 
24  if (namePattern.useRegex())
25  {
26  const Nameiter itend = m.end();
27  for (Nameiter it = m.begin(); it != itend; ++it)
28  if (namePattern.matches(it->first))
29  found->push_back(it->second->id());
30  }
31  else
32  {
33  const std::pair<Nameiter, Nameiter> limits =
34  m.equal_range(namePattern.pattern());
35  for (Nameiter it = limits.first; it != limits.second; ++it)
36  found->push_back(it->second->id());
37  }
38  }
39 
40  bool GeneralCatalog::addEntry(const SPtr inptr)
41  {
42  assert(inptr.get());
43 
44  const bool first = records_.empty();
45  const unsigned long long id = inptr->id();
46  if (id && records_.insert(std::make_pair(id, inptr)).second)
47  {
48  recordMap_[inptr->category()].insert(
49  std::make_pair(inptr->name(), inptr));
50  if (first)
51  {
52  smallestId_ = id;
53  largestId_ = id;
54  }
55  else
56  {
57  if (id < smallestId_)
58  smallestId_ = id;
59  if (id > largestId_)
60  largestId_ = id;
61  }
62  return true;
63  }
64  else
65  return false;
66  }
67 
68  bool GeneralCatalog::removeEntry(const unsigned long long id)
69  {
70  typedef RecordMap::iterator Mapiter;
71  typedef NameMap::iterator Nameiter;
72 
73  IdMap::iterator rit = records_.find(id);
74  if (rit == records_.end())
75  return false;
76 
77  const SPtr item = rit->second;
78  records_.erase(rit);
79 
80  bool found = false;
81  const Mapiter mit = recordMap_.find(item->category());
82  assert(mit != recordMap_.end());
83  const std::pair<Nameiter, Nameiter> limits =
84  mit->second.equal_range(item->name());
85  for (Nameiter nit = limits.first; nit != limits.second; ++nit)
86  if (nit->second->id() == id)
87  {
88  mit->second.erase(nit);
89  found = true;
90  break;
91  }
92  assert(found);
93  if (mit->second.empty())
94  recordMap_.erase(mit);
95 
96  if (records_.empty())
97  {
98  recordMap_.clear();
99  smallestId_ = 0;
100  largestId_ = 0;
101  }
102  else if (id == smallestId_ || id == largestId_)
103  {
104  IdMap::const_iterator it = records_.begin();
105  smallestId_ = it->first;
106  largestId_ = it->first;
107  const IdMap::const_iterator itend = records_.end();
108  for (++it; it != itend; ++it)
109  if (it->first < smallestId_)
110  smallestId_ = it->first;
111  else if (it->first > largestId_)
112  largestId_ = it->first;
113  }
114  return true;
115  }
116 
117  unsigned long long GeneralCatalog::makeEntry(
118  const ItemDescriptor& descriptor,
119  const unsigned compressionCode,
120  const unsigned long long itemLength,
121  const ItemLocation& loc,
122  const unsigned long long offset)
123  {
124  const unsigned long long nextId = records_.empty() ? 1ULL :
125  largestId_ + 1;
126  lastEntry_ = SPtr(new CatalogEntry(
127  descriptor, nextId, compressionCode, itemLength, loc, offset));
128  assert(addEntry(lastEntry_));
129  return nextId;
130  }
131 
132  void GeneralCatalog::search(const SearchSpecifier& namePattern,
133  const SearchSpecifier& categoryPattern,
134  std::vector<unsigned long long>* found) const
135  {
136  typedef RecordMap::const_iterator Mapiter;
137 
138  assert(found);
139  found->clear();
140 
141  const Mapiter endMap = recordMap_.end();
142  if (categoryPattern.useRegex())
143  {
144  for (Mapiter it = recordMap_.begin(); it != endMap; ++it)
145  if (categoryPattern.matches(it->first))
146  findByName(it->second, namePattern, found);
147  }
148  else
149  {
150  Mapiter it = recordMap_.find(categoryPattern.pattern());
151  if (it != endMap)
152  findByName(it->second, namePattern, found);
153  }
154  std::sort(found->begin(), found->end());
155  }
156 
157  bool GeneralCatalog::isEqual(const AbsCatalog& other) const
158  {
159  if ((void*)this == (void*)(&other))
160  return true;
161  const GeneralCatalog& r = static_cast<const GeneralCatalog&>(other);
162  if (smallestId_ != r.smallestId_)
163  return false;
164  if (largestId_ != r.largestId_)
165  return false;
166  if (records_.size() != r.records_.size())
167  return false;
168  IdMap::const_iterator itend = records_.end();
169  IdMap::const_iterator itend2 = r.records_.end();
170  for (IdMap::const_iterator it = records_.begin();
171  it != itend; ++it)
172  {
173  IdMap::const_iterator it2 = r.records_.find(it->first);
174  if (it2 == itend2)
175  return false;
176  if (!(*it->second == *it2->second))
177  return false;
178  }
179  return true;
180  }
181 
182  // Version 1 write function
183  // bool GeneralCatalog::write(std::ostream& os) const
184  // {
185  // if (!ClassId::makeId<CatalogEntry>().write(os))
186  // return false;
187  // if (!ClassId::makeId<ItemLocation>().write(os))
188  // return false;
189 
190  // // Sort item ids in the increasing order first
191  // std::vector<unsigned long long> idlist;
192  // const unsigned long sz = records_.size();
193  // idlist.reserve(sz);
194  // const IdMap::const_iterator itend = records_.end();
195  // for (IdMap::const_iterator it = records_.begin(); it != itend; ++it)
196  // idlist.push_back(it->first);
197  // std::sort(idlist.begin(), idlist.end());
198 
199  // // Now, write the catalog records in the order of increasing ids
200  // for (unsigned long i=0; i<sz; ++i)
201  // {
202  // IdMap::const_iterator it = records_.find(idlist[i]);
203  // if (!it->second->write(os))
204  // return false;
205  // }
206 
207  // return true;
208  // }
209 
210  bool GeneralCatalog::write(std::ostream& os) const
211  {
212  const unsigned long sz = records_.size();
213  long long ltmp = sz;
214  write_pod(os, ltmp);
215  if (os.fail())
216  return false;
217  if (!ClassId::makeId<CatalogEntry>().write(os))
218  return false;
219  if (!ClassId::makeId<ItemLocation>().write(os))
220  return false;
221 
222  // Sort item ids in the increasing order first
223  std::vector<unsigned long long> idlist;
224  idlist.reserve(sz);
225  const IdMap::const_iterator itend = records_.end();
226  for (IdMap::const_iterator it = records_.begin(); it != itend; ++it)
227  idlist.push_back(it->first);
228  std::sort(idlist.begin(), idlist.end());
229 
230  // Now, write the catalog records in the order of increasing ids
231  for (unsigned long i=0; i<sz; ++i)
232  {
233  IdMap::const_iterator it = records_.find(idlist[i]);
234  if (!it->second->write(os))
235  return false;
236  }
237 
238  return true;
239  }
240 
241  GeneralCatalog* GeneralCatalog::read(const ClassId& id, std::istream& in)
242  {
243  static const ClassId current(ClassId::makeId<GeneralCatalog>());
244  id.ensureSameName(current);
245  id.ensureVersionInRange(1, version());
246 
247  if (id.version() == 1)
248  return read_v1(in);
249 
250  long long nRecords;
251  read_pod(in, &nRecords);
252  if (nRecords < 0)
253  return read_v1(in);
254 
255  ClassId rId(in, 1);
256  ClassId locId(in, 1);
257 
258  GeneralCatalog* catalog = new GeneralCatalog();
259  bool ok = true;
260  for (long long recnum=0; ok && recnum<nRecords; ++recnum)
261  {
262  CatalogEntry* rec = CatalogEntry::read(rId, locId, in);
263  if (rec)
264  {
265  if (!catalog->addEntry(
266  CPP11_shared_ptr<const CatalogEntry>(rec)))
267  ok = false;
268  }
269  else
270  ok = false;
271  }
272 
273  if (!ok)
274  {
275  delete catalog;
276  throw IOInvalidData("In gs::GeneralCatalog::read: "
277  "duplicate item id. "
278  "Catalog is corrupted.");
279  }
280  return catalog;
281  }
282 
283  GeneralCatalog* GeneralCatalog::read_v1(std::istream& in)
284  {
285  ClassId rId(in, 1);
286  ClassId locId(in, 1);
287 
288  GeneralCatalog* catalog = new GeneralCatalog();
289  bool ok = true;
290  for (in.peek(); ok && !in.eof(); in.peek())
291  {
292  CatalogEntry* rec = CatalogEntry::read(rId, locId, in);
293  if (rec)
294  {
295  if (!catalog->addEntry(
296  CPP11_shared_ptr<const CatalogEntry>(rec)))
297  ok = false;
298  }
299  else
300  ok = false;
301  }
302 
303  if (!ok)
304  {
305  delete catalog;
306  throw IOInvalidData("In gs::GeneralCatalog::read_v1: "
307  "duplicate item id. "
308  "Catalog is corrupted.");
309  }
310  return catalog;
311  }
312 
313  CPP11_shared_ptr<const CatalogEntry> GeneralCatalog::retrieveEntry(
314  const unsigned long long id) const
315  {
316  IdMap::const_iterator it = records_.find(id);
317  if (it == records_.end())
318  {
319  CatalogEntry* ptr = nullptr;
320  return CPP11_shared_ptr<const CatalogEntry>(ptr);
321  }
322  else
323  return it->second;
324  }
325 
326  bool GeneralCatalog::retrieveStreampos(
327  unsigned long long id, unsigned* compressionCode,
328  unsigned long long* length, std::streampos* pos) const
329  {
330  IdMap::const_iterator it = records_.find(id);
331  if (it == records_.end())
332  return false;
333 
334  assert(compressionCode);
335  assert(length);
336  assert(pos);
337 
338  *compressionCode = it->second->compressionCode();
339  *length = it->second->itemLength();
340  *pos = it->second->location().streamPosition();
341 
342  return true;
343  }
344 }
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:314
def addEntry(schema, datatableName, entryinfo, branchinfo)
Definition: revisionDML.py:342
Definition: AbsArchive.cc:53
def write(self, setup)