CMS 3D CMS Logo

ContiguousCatalog.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cassert>
3 #include <utility>
4 
5 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
6 #include "Alignment/Geners/interface/ContiguousCatalog.hh"
7 #include "Alignment/Geners/interface/IOException.hh"
8 #include "Alignment/Geners/interface/binaryIO.hh"
9 
10 namespace gs {
11  void ContiguousCatalog::findByName(const NameMap &m,
12  const SearchSpecifier &namePattern,
13  std::vector<unsigned long long> *found) const {
14  typedef NameMap::const_iterator Nameiter;
15 
16  if (namePattern.useRegex()) {
17  const Nameiter itend = m.end();
18  for (Nameiter it = m.begin(); it != itend; ++it)
19  if (namePattern.matches(it->first))
20  found->push_back(it->second);
21  } else {
22  const std::pair<Nameiter, Nameiter> limits = m.equal_range(namePattern.pattern());
23  for (Nameiter it = limits.first; it != limits.second; ++it)
24  found->push_back(it->second);
25  }
26  }
27 
28  unsigned long long ContiguousCatalog::makeEntry(const ItemDescriptor &descriptor,
29  const unsigned compressionCode,
30  const unsigned long long itemLength,
31  const ItemLocation &loc,
32  const unsigned long long offset) {
33  const unsigned long long nextId = records_.size() + firstId_;
34  lastEntry_ = SPtr(new CatalogEntry(descriptor, nextId, compressionCode, itemLength, loc, offset));
35  records_.push_back(lastEntry_);
36  recordMap_[descriptor.category()].insert(std::make_pair(descriptor.name(), nextId));
37 
38  return nextId;
39  }
40 
41  void ContiguousCatalog::search(const SearchSpecifier &namePattern,
42  const SearchSpecifier &categoryPattern,
43  std::vector<unsigned long long> *found) const {
44  typedef RecordMap::const_iterator Mapiter;
45 
46  assert(found);
47  found->clear();
48 
49  const Mapiter endMap = recordMap_.end();
50  if (categoryPattern.useRegex()) {
51  for (Mapiter it = recordMap_.begin(); it != endMap; ++it)
52  if (categoryPattern.matches(it->first))
53  findByName(it->second, namePattern, found);
54  } else {
55  Mapiter it = recordMap_.find(categoryPattern.pattern());
56  if (it != endMap)
57  findByName(it->second, namePattern, found);
58  }
59  std::sort(found->begin(), found->end());
60  }
61 
62  bool ContiguousCatalog::isEqual(const AbsCatalog &other) const {
63  if ((void *)this == (void *)(&other))
64  return true;
65  const ContiguousCatalog &r = static_cast<const ContiguousCatalog &>(other);
66  if (firstId_ != r.firstId_)
67  return false;
68  if (recordMap_ != r.recordMap_)
69  return false;
70  const unsigned long nRecords = records_.size();
71  if (nRecords != r.records_.size())
72  return false;
73  for (unsigned long i = 0; i < nRecords; ++i)
74  if (*records_[i] != *r.records_[i])
75  return false;
76  return true;
77  }
78 
79  // Version 1 write function
80  // bool ContiguousCatalog::write(std::ostream& os) const
81  // {
82  // bool status = ClassId::makeId<CatalogEntry>().write(os) &&
83  // ClassId::makeId<ItemLocation>().write(os);
84  // const unsigned long long sz = records_.size();
85  // for (unsigned long long i=0; i<sz && status; ++i)
86  // status = records_[i]->write(os);
87  // return status;
88  // }
89 
90  bool ContiguousCatalog::write(std::ostream &os) const {
91  const unsigned long long sz = records_.size();
92  long long nRecords = sz;
93  write_pod(os, nRecords);
94  bool status = !os.fail() && ClassId::makeId<CatalogEntry>().write(os) && ClassId::makeId<ItemLocation>().write(os);
95  for (unsigned long long i = 0; i < sz && status; ++i)
96  status = records_[i]->write(os);
97  return status;
98  }
99 
100  ContiguousCatalog *ContiguousCatalog::read(const ClassId &cid, std::istream &in) {
101  static const ClassId current(ClassId::makeId<ContiguousCatalog>());
102  cid.ensureSameName(current);
103  cid.ensureVersionInRange(1, version());
104 
105  if (cid.version() == 1)
106  return read_v1(in);
107 
108  long long nRecords;
109  read_pod(in, &nRecords);
110  if (nRecords < 0)
111  return read_v1(in);
112 
113  ClassId rId(in, 1);
114  ClassId locId(in, 1);
115 
116  CPP11_auto_ptr<ContiguousCatalog> catalog(new ContiguousCatalog());
117  bool firstEntry = true;
118  for (long long recnum = 0; recnum < nRecords; ++recnum) {
119  CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
120  if (rec) {
121  const unsigned long long id = rec->id();
122  if (firstEntry) {
123  catalog->firstId_ = id;
124  firstEntry = false;
125  } else {
126  const unsigned long long nextId = catalog->records_.size() + catalog->firstId_;
127  if (id != nextId) {
128  delete rec;
129  throw IOInvalidData(
130  "In gs::ContiguousCatalog::read:"
131  " unexpected item id. "
132  "Catalog is corrupted.");
133  }
134  }
135  catalog->records_.push_back(SPtr(rec));
136  catalog->recordMap_[rec->category()].insert(std::make_pair(rec->name(), id));
137  } else
138  throw IOInvalidData(
139  "In gs::ContiguousCatalog::read:"
140  " failed to read catalog entry");
141  }
142  return catalog.release();
143  }
144 
145  ContiguousCatalog *ContiguousCatalog::read_v1(std::istream &in) {
146  ClassId rId(in, 1);
147  ClassId locId(in, 1);
148 
149  CPP11_auto_ptr<ContiguousCatalog> catalog(new ContiguousCatalog());
150  bool firstEntry = true;
151  for (in.peek(); !in.eof(); in.peek()) {
152  CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
153  if (rec) {
154  const unsigned long long id = rec->id();
155  if (firstEntry) {
156  catalog->firstId_ = id;
157  firstEntry = false;
158  } else {
159  const unsigned long long nextId = catalog->records_.size() + catalog->firstId_;
160  if (id != nextId) {
161  delete rec;
162  throw IOInvalidData(
163  "In gs::ContiguousCatalog::read_v1:"
164  " unexpected item id. "
165  "Catalog is corrupted.");
166  }
167  }
168  catalog->records_.push_back(SPtr(rec));
169  catalog->recordMap_[rec->category()].insert(std::make_pair(rec->name(), id));
170  } else
171  throw IOInvalidData(
172  "In gs::ContiguousCatalog::read_v1:"
173  " failed to read catalog entry");
174  }
175  return catalog.release();
176  }
177 
178  std::shared_ptr<const CatalogEntry> ContiguousCatalog::retrieveEntry(const unsigned long long id) const {
179  if (id >= firstId_ && id < records_.size() + firstId_)
180  return records_[id - firstId_];
181  else {
182  CatalogEntry *ptr = nullptr;
183  return std::shared_ptr<const CatalogEntry>(ptr);
184  }
185  }
186 
187  bool ContiguousCatalog::retrieveStreampos(unsigned long long id,
188  unsigned *compressionCode,
189  unsigned long long *length,
190  std::streampos *pos) const {
191  assert(compressionCode);
192  assert(length);
193  assert(pos);
194 
195  if (id >= firstId_ && id < records_.size() + firstId_) {
196  const std::shared_ptr<const CatalogEntry> &rec(records_[id - firstId_]);
197  *compressionCode = rec->compressionCode();
198  *length = rec->itemLength();
199  *pos = rec->location().streamPosition();
200  return true;
201  } else
202  return false;
203  }
204 } // namespace gs
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:21
assert(be >=bs)
Definition: AbsArchive.cc:46