3 #include "Alignment/Geners/interface/BinaryFileArchive.hh"
5 #include "Alignment/Geners/interface/uriUtils.hh"
6 #include "Alignment/Geners/interface/ContiguousCatalog.hh"
7 #include "Alignment/Geners/interface/WriteOnlyCatalog.hh"
8 #include "Alignment/Geners/interface/IOException.hh"
9 #include "Alignment/Geners/interface/streamposIO.hh"
12 BinaryFileArchive::BinaryFileArchive(
const char* basename,
13 const char*
mode,
const char* ann,
14 const unsigned dataFileBufferSize,
15 const unsigned catalogFileBufferSize)
16 : BinaryArchiveBase(basename, mode),
20 dataFileName_(AbsArchive::
name() +
".0.gsbd"),
21 catalogFileName_(AbsArchive::
name() +
".gsbmf"),
25 catalogMergeLevel_(1),
26 annotationsMerged_(
false),
29 if (!modeValid())
return;
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(),
47 openmode() & ~std::ios_base::app);
48 if (!catStream_.is_open())
49 throw IOOpeningFailure(
"gs::BinaryFileArchive constructor",
57 if (isEmptyFile(catStream_))
58 setCatalog(
new ContiguousCatalog());
60 readCatalog<ContiguousCatalog>();
70 if (isEmptyFile(catStream_))
72 setCatalog(
new WriteOnlyCatalog(catStream_));
80 catStream_.open(catalogFileName_.c_str(),
82 if (!catStream_.is_open())
throw IOOpeningFailure(
83 "gs::BinaryFileArchive constructor", catalogFileName_);
84 readCatalog<WriteOnlyCatalog>();
93 errorStream() << e.what();
97 void BinaryFileArchive::releaseBuffers()
99 if (dataStream_.is_open()) dataStream_.close();
100 if (catStream_.is_open()) catStream_.close();
101 catStream_.rdbuf()->pubsetbuf(0, 0);
102 dataStream_.rdbuf()->pubsetbuf(0, 0);
103 delete [] catabuf_; catabuf_ = 0;
104 delete [] filebuf_; filebuf_ = 0;
107 BinaryFileArchive::~BinaryFileArchive()
113 void BinaryFileArchive::writeCatalog()
117 if (!annotationsMerged_)
119 if (annotation_.size())
120 catalogAnnotations_.push_back(annotation_);
121 annotationsMerged_ =
true;
123 const unsigned compress =
static_cast<unsigned>(compressionMode());
125 catalogAnnotations_, *
catalog()))
127 std::ostringstream os;
128 os <<
"In BinaryFileArchive::writeCatalog: "
129 <<
"failed to write catalog data to file "
131 throw IOWriteFailure(os.str());
136 std::istream& BinaryFileArchive::plainInputStream(
137 const unsigned long long id,
138 unsigned* compressionCode,
139 unsigned long long* length)
144 if (!
id)
throw gs::IOInvalidArgument(
145 "In gs::BinaryFileArchive::plainInputStream: invalid item id");
147 if (!
catalog()->retrieveStreampos(
148 id, compressionCode, length, &pos))
150 std::ostringstream os;
151 os <<
"In gs::BinaryFileArchive::plainInputStream: "
152 <<
"failed to locate item with id " <<
id
153 <<
"in the catalog stored in file " << catalogFileName_;
154 throw gs::IOInvalidArgument(os.str());
159 streamFlushed_ =
true;
161 dataStream_.seekg(pos);
166 unsigned long long BinaryFileArchive::addToCatalog(
167 const AbsRecord&
record,
const unsigned compressionCode,
168 const unsigned long long itemLength)
170 unsigned long long id = 0;
174 record, compressionCode, itemLength,
175 ItemLocation(lastpos_, dataFileURI_.c_str()));
176 if (
id && injectMetadata())
178 const CatalogEntry* entry =
catalog()->lastEntryMade();
181 std::streampos
now = dataStream_.tellp();
182 if (entry->write(dataStream_))
184 dataStream_.seekp(jumppos_);
185 write_pod(dataStream_, now);
195 std::ostream& BinaryFileArchive::plainOutputStream()
201 if (injectMetadata())
203 jumppos_ = dataStream_.tellp();
204 std::streampos catpos(0);
205 write_pod(dataStream_, catpos);
207 lastpos_ = dataStream_.tellp();
208 streamFlushed_ =
false;
213 void BinaryFileArchive::flush()
220 streamFlushed_ =
true;
225 if (dynamic_cast<WriteOnlyCatalog*>(
catalog()) == 0)
bool writeBinaryCatalog(std::ostream &os, const unsigned compressionCode, const unsigned mergeLevel, const std::vector< std::string > &annotations, const AbsCatalog &catalog, const unsigned formatId)
std::string localFileURI(const char *filename)
volatile std::atomic< bool > shutdown_flag false