1 #include "Alignment/Geners/interface/GeneralCatalog.hh" 2 #include "Alignment/Geners/interface/IOException.hh" 3 #include "Alignment/Geners/interface/binaryIO.hh" 11 GeneralCatalog::GeneralCatalog() : smallestId_(1ULL), largestId_(0) {}
13 void GeneralCatalog::findByName(
const NameMap &
m,
14 const SearchSpecifier &namePattern,
15 std::vector<unsigned long long> *
found)
const {
16 typedef NameMap::const_iterator Nameiter;
18 if (namePattern.useRegex()) {
19 const Nameiter itend =
m.end();
20 for (Nameiter it =
m.begin(); it != itend; ++it)
21 if (namePattern.matches(it->first))
22 found->push_back(it->second->id());
24 const std::pair<Nameiter, Nameiter>
limits =
m.equal_range(namePattern.pattern());
25 for (Nameiter it =
limits.first; it !=
limits.second; ++it)
26 found->push_back(it->second->id());
30 bool GeneralCatalog::addEntry(
const SPtr inptr) {
33 const bool first = records_.empty();
34 const unsigned long long id = inptr->id();
35 if (
id && records_.insert(std::make_pair(
id, inptr)).second) {
36 recordMap_[inptr->category()].insert(std::make_pair(inptr->name(), inptr));
51 bool GeneralCatalog::removeEntry(
const unsigned long long id) {
52 typedef RecordMap::iterator Mapiter;
53 typedef NameMap::iterator Nameiter;
55 IdMap::iterator rit = records_.find(
id);
56 if (rit == records_.end())
59 const SPtr
item = rit->second;
63 const Mapiter mit = recordMap_.find(
item->category());
64 assert(mit != recordMap_.end());
65 const std::pair<Nameiter, Nameiter>
limits = mit->second.equal_range(
item->name());
66 for (Nameiter nit =
limits.first; nit !=
limits.second; ++nit)
67 if (nit->second->id() ==
id) {
68 mit->second.erase(nit);
73 if (mit->second.empty())
74 recordMap_.erase(mit);
76 if (records_.empty()) {
80 }
else if (
id == smallestId_ ||
id == largestId_) {
81 IdMap::const_iterator it = records_.begin();
82 smallestId_ = it->first;
83 largestId_ = it->first;
84 const IdMap::const_iterator itend = records_.end();
85 for (++it; it != itend; ++it)
86 if (it->first < smallestId_)
87 smallestId_ = it->first;
88 else if (it->first > largestId_)
89 largestId_ = it->first;
94 unsigned long long GeneralCatalog::makeEntry(
const ItemDescriptor &descriptor,
95 const unsigned compressionCode,
96 const unsigned long long itemLength,
97 const ItemLocation &loc,
98 const unsigned long long offset) {
99 const unsigned long long nextId = records_.empty() ? 1ULL : largestId_ + 1;
100 lastEntry_ = SPtr(
new CatalogEntry(descriptor, nextId, compressionCode, itemLength, loc,
offset));
101 assert(addEntry(lastEntry_));
106 const SearchSpecifier &categoryPattern,
107 std::vector<unsigned long long> *
found)
const {
108 typedef RecordMap::const_iterator Mapiter;
113 const Mapiter endMap = recordMap_.end();
114 if (categoryPattern.useRegex()) {
115 for (Mapiter it = recordMap_.begin(); it != endMap; ++it)
116 if (categoryPattern.matches(it->first))
117 findByName(it->second, namePattern,
found);
119 Mapiter it = recordMap_.find(categoryPattern.pattern());
121 findByName(it->second, namePattern,
found);
126 bool GeneralCatalog::isEqual(
const AbsCatalog &
other)
const {
127 if ((
void *)
this == (
void *)(&
other))
129 const GeneralCatalog &
r =
static_cast<const GeneralCatalog &
>(
other);
130 if (smallestId_ !=
r.smallestId_)
132 if (largestId_ !=
r.largestId_)
134 if (records_.size() !=
r.records_.size())
136 IdMap::const_iterator itend = records_.end();
137 IdMap::const_iterator itend2 =
r.records_.end();
138 for (IdMap::const_iterator it = records_.begin(); it != itend; ++it) {
139 IdMap::const_iterator it2 =
r.records_.find(it->first);
142 if (!(*it->second == *it2->second))
177 const unsigned long sz = records_.size();
182 if (!ClassId::makeId<CatalogEntry>().write(os))
184 if (!ClassId::makeId<ItemLocation>().write(os))
188 std::vector<unsigned long long> idlist;
190 const IdMap::const_iterator itend = records_.end();
191 for (IdMap::const_iterator it = records_.begin(); it != itend; ++it)
192 idlist.push_back(it->first);
196 for (
unsigned long i = 0;
i < sz; ++
i) {
197 IdMap::const_iterator it = records_.find(idlist[
i]);
198 if (!it->second->write(os))
206 static const ClassId current(ClassId::makeId<GeneralCatalog>());
207 id.ensureSameName(current);
208 id.ensureVersionInRange(1,
version());
214 read_pod(
in, &nRecords);
219 ClassId locId(
in, 1);
221 GeneralCatalog *
catalog =
new GeneralCatalog();
223 for (
long long recnum = 0;
ok && recnum < nRecords; ++recnum) {
226 if (!
catalog->addEntry(std::shared_ptr<const CatalogEntry>(rec)))
235 "In gs::GeneralCatalog::read: " 236 "duplicate item id. " 237 "Catalog is corrupted.");
242 GeneralCatalog *GeneralCatalog::read_v1(std::istream &
in) {
244 ClassId locId(
in, 1);
246 GeneralCatalog *
catalog =
new GeneralCatalog();
248 for (
in.peek();
ok && !
in.eof();
in.peek()) {
251 if (!
catalog->addEntry(std::shared_ptr<const CatalogEntry>(rec)))
260 "In gs::GeneralCatalog::read_v1: " 261 "duplicate item id. " 262 "Catalog is corrupted.");
267 std::shared_ptr<const CatalogEntry> GeneralCatalog::retrieveEntry(
const unsigned long long id)
const {
268 IdMap::const_iterator it = records_.find(
id);
269 if (it == records_.end()) {
270 CatalogEntry *ptr =
nullptr;
271 return std::shared_ptr<const CatalogEntry>(ptr);
276 bool GeneralCatalog::retrieveStreampos(
unsigned long long id,
277 unsigned *compressionCode,
278 unsigned long long *length,
279 std::streampos *
pos)
const {
280 IdMap::const_iterator it = records_.find(
id);
281 if (it == records_.end())
288 *compressionCode = it->second->compressionCode();
289 *length = it->second->itemLength();
290 *
pos = it->second->location().streamPosition();
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)