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"
11 GeneralCatalog::GeneralCatalog()
17 void GeneralCatalog::findByName(
19 const SearchSpecifier& namePattern,
20 std::vector<unsigned long long>*
found)
const
22 typedef NameMap::const_iterator Nameiter;
24 if (namePattern.useRegex())
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());
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());
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)
48 recordMap_[inptr->category()].insert(
49 std::make_pair(inptr->name(), inptr));
68 bool GeneralCatalog::removeEntry(
const unsigned long long id)
70 typedef RecordMap::iterator Mapiter;
71 typedef NameMap::iterator Nameiter;
73 IdMap::iterator rit = records_.find(
id);
74 if (rit == records_.end())
77 const SPtr item = rit->second;
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)
88 mit->second.erase(nit);
93 if (mit->second.empty())
94 recordMap_.erase(mit);
102 else if (
id == smallestId_ ||
id == largestId_)
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;
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)
124 const unsigned long long nextId = records_.empty() ? 1ULL :
126 lastEntry_ = SPtr(
new CatalogEntry(
127 descriptor, nextId, compressionCode, itemLength, loc, offset));
133 const SearchSpecifier& categoryPattern,
134 std::vector<unsigned long long>* found)
const
136 typedef RecordMap::const_iterator Mapiter;
141 const Mapiter endMap = recordMap_.end();
142 if (categoryPattern.useRegex())
144 for (Mapiter it = recordMap_.begin(); it != endMap; ++it)
145 if (categoryPattern.matches(it->first))
146 findByName(it->second, namePattern, found);
150 Mapiter it = recordMap_.find(categoryPattern.pattern());
152 findByName(it->second, namePattern, found);
157 bool GeneralCatalog::isEqual(
const AbsCatalog& other)
const
159 if ((
void*)
this == (
void*)(&other))
161 const GeneralCatalog&
r =
static_cast<const GeneralCatalog&
>(other);
162 if (smallestId_ != r.smallestId_)
164 if (largestId_ != r.largestId_)
166 if (records_.size() != r.records_.size())
168 IdMap::const_iterator itend = records_.end();
169 IdMap::const_iterator itend2 = r.records_.end();
170 for (IdMap::const_iterator it = records_.begin();
173 IdMap::const_iterator it2 = r.records_.find(it->first);
176 if (!(*it->second == *it2->second))
212 const unsigned long sz = records_.size();
217 if (!ClassId::makeId<CatalogEntry>().write(os))
219 if (!ClassId::makeId<ItemLocation>().write(os))
223 std::vector<unsigned long long> idlist;
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);
231 for (
unsigned long i=0;
i<sz; ++
i)
233 IdMap::const_iterator it = records_.find(idlist[
i]);
234 if (!it->second->write(os))
243 static const ClassId
current(ClassId::makeId<GeneralCatalog>());
245 id.ensureVersionInRange(1,
version());
251 read_pod(in, &nRecords);
256 ClassId locId(in, 1);
258 GeneralCatalog*
catalog =
new GeneralCatalog();
260 for (
long long recnum=0; ok && recnum<nRecords; ++recnum)
265 if (!catalog->addEntry(
266 CPP11_shared_ptr<const CatalogEntry>(rec)))
276 throw IOInvalidData(
"In gs::GeneralCatalog::read: "
277 "duplicate item id. "
278 "Catalog is corrupted.");
283 GeneralCatalog* GeneralCatalog::read_v1(std::istream& in)
286 ClassId locId(in, 1);
288 GeneralCatalog* catalog =
new GeneralCatalog();
290 for (in.peek(); ok && !in.eof(); in.peek())
295 if (!catalog->addEntry(
296 CPP11_shared_ptr<const CatalogEntry>(rec)))
306 throw IOInvalidData(
"In gs::GeneralCatalog::read_v1: "
307 "duplicate item id. "
308 "Catalog is corrupted.");
313 CPP11_shared_ptr<const CatalogEntry> GeneralCatalog::retrieveEntry(
314 const unsigned long long id)
const
316 IdMap::const_iterator it = records_.find(
id);
317 if (it == records_.end())
319 CatalogEntry* ptr = 0;
320 return CPP11_shared_ptr<const CatalogEntry>(ptr);
326 bool GeneralCatalog::retrieveStreampos(
327 unsigned long long id,
unsigned* compressionCode,
328 unsigned long long* length, std::streampos* pos)
const
330 IdMap::const_iterator it = records_.find(
id);
331 if (it == records_.end())
334 assert(compressionCode);
338 *compressionCode = it->second->compressionCode();
339 *length = it->second->itemLength();
340 *pos = it->second->location().streamPosition();
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
unsigned int offset(bool)