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