CMS 3D CMS Logo

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