8 #include "Alignment/Geners/interface/BinaryArchiveBase.hh"
9 #include "Alignment/Geners/interface/CatalogIO.hh"
10 #include "Alignment/Geners/interface/binaryIO.hh"
12 #ifdef GENERS_BINARY_ARCHIVE_FORMAT_ID
13 #undef GENERS_BINARY_ARCHIVE_FORMAT_ID
15 #define GENERS_BINARY_ARCHIVE_FORMAT_ID (0x1f2e3d4c)
18 const char *
c,
unsigned *
result)
22 const unsigned long value = strtoul(c, &endptr, 0);
23 if (errno || *endptr !=
'\0')
25 err <<
"expected an unsigned integer, got \"" << c <<
'"';
26 if (errno) err <<
", " << strerror(errno);
31 err <<
"unsigned value \"" << c <<
"\" is out of range";
43 const long value = strtol(c, &endptr, 0);
44 if (errno || *endptr !=
'\0')
46 err <<
"expected an integer, got \"" << c <<
'"';
47 if (errno) err <<
", " << strerror(errno);
50 if (value < INT_MIN || value > INT_MAX)
52 err <<
"integer value \"" << c <<
"\" is out of range";
60 BinaryArchiveBase::BinaryArchiveBase(
const char*
name,
const char*
mode)
62 mode_(parseMode(mode)),
69 addCatalogToData_(
false)
71 CStringStream::CompressionMode
m = CStringStream::NOT_COMPRESSED;
72 int compressionLevel = -1;
73 unsigned minSizeToCompress = 1024U;
76 std::ostringstream err;
77 modeIsValid_ = parseArchiveOptions(err, mode, &m, &compressionLevel,
78 &minSizeToCompress, &bufSize,
81 cStream_ =
new CStringStream(m, compressionLevel,
82 minSizeToCompress, bufSize);
85 errorStream() <<
"In BinaryArchiveBase constructor: "
86 <<
"invalid archive opening mode \"" << mode <<
'"';
89 errorStream() <<
": " << errInfo;
94 void BinaryArchiveBase::releaseClassIds()
96 delete storedEntryId_; storedEntryId_ = 0;
97 delete storedLocationId_; storedLocationId_ = 0;
101 BinaryArchiveBase::~BinaryArchiveBase()
113 write_pod(os, format);
116 const unsigned multiplex = addCatalogToData_ ? 1 : 0;
117 const unsigned sizeoflong =
sizeof(long);
118 const unsigned infoword = (sizeoflong << 1) | multiplex;
119 write_pod(os, infoword);
125 storedEntryId_ =
new ClassId(ClassId::makeId<CatalogEntry>());
126 storedEntryId_->write(os);
127 storedLocationId_ =
new ClassId(ClassId::makeId<ItemLocation>());
128 storedLocationId_->write(os);
136 is.seekg(0, std::ios_base::beg);
138 read_pod(is, &format);
139 if (format != expectedFormat)
142 unsigned infoword = 0xffffffff;
143 read_pod(is, &infoword);
144 const unsigned multiplex = infoword & 0x1U;
145 const unsigned sizeoflong = infoword >> 1;
150 if (sizeoflong !=
sizeof(
long))
153 addCatalogToData_ = multiplex;
154 if (addCatalogToData_)
157 storedEntryId_ =
new ClassId(is, 1);
158 storedLocationId_ =
new ClassId(is, 1);
165 const ClassId& entryId = ClassId::makeId<CatalogEntry>();
166 const ClassId& locId = ClassId::makeId<ItemLocation>();
167 if (entryId != *storedEntryId_ || locId != *storedLocationId_)
169 "In gs::BinaryArchiveBase::readHeader: this "
170 "archive can no longer be open for update as it was "
171 "created using an older version of I/O software");
178 void BinaryArchiveBase::openDataFile(std::fstream& stream,
182 if (stream.is_open())
185 stream.open(filename, mode_);
186 if (!stream.is_open())
187 throw IOOpeningFailure(
"gs::BinaryArchiveBase::openDataFile",
191 bool writeHead =
false;
192 if (mode_ & std::ios_base::out)
196 else if (isEmptyFile(stream))
206 std::string e =
"In gs::BinaryArchiveBase::openDataFile: "
207 "failed to write archive header to file \"";
210 throw IOWriteFailure(e);
217 const bool failed = stream.fail();
219 std::string e =
"In gs::BinaryArchiveBase::openDataFile: ";
222 e +=
"could not read archive header from file \"";
225 throw IOReadFailure(e);
229 e +=
"no valid archive header in file \"";
232 throw IOInvalidData(e);
239 void BinaryArchiveBase::setCatalog(AbsCatalog*
c)
244 catalogIsSet_ =
true;
251 void BinaryArchiveBase::itemSearch(
252 const SearchSpecifier& namePattern,
253 const SearchSpecifier& categoryPattern,
254 std::vector<unsigned long long>* idsFound)
const
257 catalog_->search(namePattern, categoryPattern, idsFound);
266 bool BinaryArchiveBase::parseArchiveOptions(
267 std::ostringstream& err,
268 const char* modeIn, CStringStream::CompressionMode* m,
269 int* compressionLevel,
unsigned* minSizeToCompress,
270 unsigned* bufSize,
bool* multiplexCatalog)
277 char* mode =
const_cast<char*
>(cmode.c_str());
280 for (
char* opt = strtok(mode,
":"); opt; opt = strtok(0,
":"), ++cnt)
285 char*
eq = strchr(opt,
'=');
290 while (isspace(*optname) && optname < eq)
294 err <<
"invalid binary archive option \"\"";
297 char* optend = eq - 1;
298 while (isspace(*optend))
304 char* optval = eq + 1;
305 while (*optval && isspace(*optval))
309 err <<
"invalid binary archive option value \"\"";
312 char* valend = opt + strlen(opt) - 1;
313 while (isspace(*valend))
317 if (strlen(optval) == 0)
319 err <<
"invalid binary archive option value \"\"";
324 if (!strcasecmp(optname,
"z"))
327 if (!CStringStream::getCompressionModeByName(optval, m))
329 err <<
"invalid compression type \"" << optval <<
'"';
333 else if (!strcasecmp(optname,
"cl"))
336 if (!
parse_int(err, optval, compressionLevel))
338 if (*compressionLevel < -1 || *compressionLevel > 9)
340 err <<
"compression level is out of range";
344 else if (!strcasecmp(optname,
"cb"))
350 else if (!strcasecmp(optname,
"cm"))
356 else if (!strcasecmp(optname,
"cat"))
359 if (optval[0] ==
'i' || optval[0] ==
'I')
360 *multiplexCatalog =
true;
361 else if (optval[0] ==
's' || optval[0] ==
'S')
362 *multiplexCatalog =
false;
365 err <<
"invalid catalog mode \"" << optval <<
'"';
372 err <<
"unrecognized binary archive option \""
379 err <<
"invalid binary archive option \"" << opt <<
'"';
387 std::ios_base::openmode BinaryArchiveBase::parseMode(
const char* mode)
389 std::ios_base::openmode m = std::ios_base::binary;
392 const unsigned len = strlen(mode);
393 for (
unsigned i=0;
i<len; ++
i)
399 else if (mode[
i] ==
'w')
401 else if (mode[
i] ==
'a')
402 m |= (std::ios_base::out | std::ios_base::app);
403 else if (mode[
i] ==
'+')
405 else if (mode[
i] ==
':')
421 std::vector<unsigned long long> idlist;
422 catalog_->search(reference.namePattern(),
423 reference.categoryPattern(),
425 const unsigned long nfound = idlist.size();
428 CPP11_shared_ptr<const CatalogEntry> pentry =
429 catalog_->retrieveEntry(idlist[
i]);
430 if (reference.isIOCompatible(*pentry))
431 addItemToReference(reference, idlist[i]);
437 bool BinaryArchiveBase::isEmptyFile(std::fstream&
s)
440 return s.tellg() == std::streampos(0);
444 std::istream& BinaryArchiveBase::inputStream(
const unsigned long long id,
447 unsigned long long length = 0;
448 unsigned compressionCode = 0;
449 std::istream& is = plainInputStream(
id, &compressionCode, &length);
450 if (cStream_->compressionMode() == CStringStream::NOT_COMPRESSED)
458 cStream_->readCompressed(is, compressionCode, length);
461 std::streamoff off = cStream_->tellp();
469 std::ostream& BinaryArchiveBase::outputStream()
471 return plainOutputStream();
475 std::ostream& BinaryArchiveBase::compressedStream(std::ostream& os)
477 if (cStream_->compressionMode() == CStringStream::NOT_COMPRESSED)
482 cStream_->setSink(os);
488 unsigned BinaryArchiveBase::flushCompressedRecord(std::ostream&)
490 CStringStream::CompressionMode m = cStream_->compressionMode();
491 if (m != CStringStream::NOT_COMPRESSED)
494 m = cStream_->writeCompressed();
496 return static_cast<unsigned>(
m);
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
static bool parse_unsigned(std::ostringstream &err, const char *c, unsigned *result)
void readHeader(xercesc::DOMNode *parentNode, EcalCondHeader &header)
read header from
void writeHeader(xercesc::DOMNode *parentNode, const EcalCondHeader &header)
write
static bool parse_int(std::ostringstream &err, const char *c, int *result)
#define GENERS_BINARY_ARCHIVE_FORMAT_ID
volatile std::atomic< bool > shutdown_flag false