3 #include "Alignment/Geners/interface/BinaryFileArchive.hh"
5 #include "Alignment/Geners/interface/ContiguousCatalog.hh"
6 #include "Alignment/Geners/interface/IOException.hh"
7 #include "Alignment/Geners/interface/WriteOnlyCatalog.hh"
8 #include "Alignment/Geners/interface/streamposIO.hh"
9 #include "Alignment/Geners/interface/uriUtils.hh"
12 BinaryFileArchive::BinaryFileArchive(
const char *basename,
15 const unsigned dataFileBufferSize,
16 const unsigned catalogFileBufferSize)
17 : BinaryArchiveBase(basename,
mode),
21 dataFileName_(AbsArchive::
name() +
".0.gsbd"),
22 catalogFileName_(AbsArchive::
name() +
".gsbmf"),
26 catalogMergeLevel_(1),
27 annotationsMerged_(
false),
28 streamFlushed_(
true) {
34 if (dataFileBufferSize)
35 filebuf_ =
new char[dataFileBufferSize];
36 dataStream_.rdbuf()->pubsetbuf(filebuf_, dataFileBufferSize);
37 openDataFile(dataStream_, dataFileName_.c_str());
43 if (catalogFileBufferSize)
44 catabuf_ =
new char[catalogFileBufferSize];
45 catStream_.rdbuf()->pubsetbuf(catabuf_, catalogFileBufferSize);
46 catStream_.open(catalogFileName_.c_str(), openmode() & ~
std::ios_base::app);
47 if (!catStream_.is_open())
48 throw IOOpeningFailure(
"gs::BinaryFileArchive constructor", catalogFileName_);
54 if (isEmptyFile(catStream_))
55 setCatalog(
new ContiguousCatalog());
57 readCatalog<ContiguousCatalog>();
65 if (isEmptyFile(catStream_)) {
66 setCatalog(
new WriteOnlyCatalog(catStream_));
73 if (!catStream_.is_open())
74 throw IOOpeningFailure(
"gs::BinaryFileArchive constructor", catalogFileName_);
75 readCatalog<WriteOnlyCatalog>();
82 errorStream() <<
e.what();
86 void BinaryFileArchive::releaseBuffers() {
87 if (dataStream_.is_open())
89 if (catStream_.is_open())
91 catStream_.rdbuf()->pubsetbuf(
nullptr, 0);
92 dataStream_.rdbuf()->pubsetbuf(
nullptr, 0);
99 BinaryFileArchive::~BinaryFileArchive() {
104 void BinaryFileArchive::writeCatalog() {
106 if (!annotationsMerged_) {
107 if (!annotation_.empty())
108 catalogAnnotations_.push_back(annotation_);
109 annotationsMerged_ =
true;
111 const unsigned compress = static_cast<unsigned>(compressionMode());
113 std::ostringstream os;
114 os <<
"In BinaryFileArchive::writeCatalog: "
115 <<
"failed to write catalog data to file " << catalogFileName_;
116 throw IOWriteFailure(os.str());
121 std::istream &BinaryFileArchive::plainInputStream(
const unsigned long long id,
122 unsigned *compressionCode,
123 unsigned long long *length) {
127 throw gs::IOInvalidArgument(
"In gs::BinaryFileArchive::plainInputStream: invalid item id");
129 if (!
catalog()->retrieveStreampos(
id, compressionCode, length, &
pos)) {
130 std::ostringstream os;
131 os <<
"In gs::BinaryFileArchive::plainInputStream: "
132 <<
"failed to locate item with id " <<
id <<
"in the catalog stored in file " << catalogFileName_;
133 throw gs::IOInvalidArgument(os.str());
135 if (!streamFlushed_) {
137 streamFlushed_ =
true;
139 dataStream_.seekg(
pos);
144 unsigned long long BinaryFileArchive::addToCatalog(
const AbsRecord &
record,
145 const unsigned compressionCode,
146 const unsigned long long itemLength) {
147 unsigned long long id = 0;
149 id =
catalog()->makeEntry(
record, compressionCode, itemLength, ItemLocation(lastpos_, dataFileURI_.c_str()));
150 if (
id && injectMetadata()) {
154 std::streampos
now = dataStream_.tellp();
155 if (
entry->write(dataStream_)) {
156 dataStream_.seekp(jumppos_);
157 write_pod(dataStream_,
now);
166 std::ostream &BinaryFileArchive::plainOutputStream() {
170 if (injectMetadata()) {
171 jumppos_ = dataStream_.tellp();
172 std::streampos catpos(0);
173 write_pod(dataStream_, catpos);
175 lastpos_ = dataStream_.tellp();
176 streamFlushed_ =
false;
181 void BinaryFileArchive::flush() {
183 if (!streamFlushed_) {
185 streamFlushed_ =
true;
189 if (dynamic_cast<WriteOnlyCatalog *>(
catalog()) ==
nullptr)