4 #include "Alignment/Geners/interface/AbsArchive.hh"
5 #include "Alignment/Geners/interface/IOException.hh"
7 #define GS_STREAM_COPY_BUFFER_SIZE 65536
14 bool in_fail =
in.fail();
15 bool out_fail =
out.fail();
16 while (
count > bufsize && !in_fail && !out_fail) {
21 out_fail =
out.fail();
25 if (!in_fail && !out_fail) {
34 class NotWritableRecord :
public gs::AbsRecord {
36 inline NotWritableRecord(
const gs::ClassId &classId,
const char *ioPrototype,
const char *
name,
const char *
category)
40 NotWritableRecord() =
delete;
41 inline bool writeData(std::ostream &)
const override {
return false; }
46 AbsArchive::AbsArchive(
const char *
name) : name_(
name ?
name :
""), lastItemId_(0), lastItemLength_(0) {}
48 void AbsArchive::addItemToReference(AbsReference &
r,
const unsigned long long id)
const {
r.addItemId(
id); }
50 unsigned long long AbsArchive::copyItem(
const unsigned long long id,
53 const char *newCategory) {
55 throw gs::IOInvalidArgument(
56 "In gs::AbsArchive::copyItem: "
57 "origin archive is not readable");
60 throw gs::IOInvalidArgument(
61 "In gs::AbsArchive::copyItem: "
62 "origin and destination archives are the same");
65 throw gs::IOInvalidArgument(
66 "In gs::AbsArchive::copyItem: "
67 "destination archive is not writable");
68 std::shared_ptr<const CatalogEntry>
entry(catalogEntry(
id));
70 throw gs::IOInvalidArgument(
71 "In gs::AbsArchive::copyItem: no item "
72 "in the archive with the given id");
76 std::istream &is = inputStream(
id, &sz);
80 std::ostream &os = ar.outputStream();
81 std::streampos
base = os.tellp();
82 std::ostream &compressed = ar.compressedStream(os);
85 unsigned long long len;
89 len =
entry->itemLength();
93 "In gs::AbsArchive::copyItem: "
94 "input stream failure");
95 if (compressed.fail())
97 "In gs::AbsArchive::copyItem: "
98 "output stream failure");
99 const unsigned compressCode = ar.flushCompressedRecord(compressed);
101 throw IOWriteFailure(
102 "In gs::AbsArchive::copyItem: "
103 "failed to transfer compressed data");
106 std::streamoff off = os.tellp() -
base;
107 const long long delta = off;
120 const unsigned long long id2 = ar.addToCatalog(
record, compressCode,
delta);
122 throw IOWriteFailure(
123 "In gs::AbsArchive::copyItem: "
124 "failed to add catalog entry");
125 ar.lastItemId_ =
id2;
126 ar.lastItemLength_ =
delta;
132 std::ostringstream
err;
133 err <<
"In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
134 <<
"failed to " << failedAction <<
" to the archive \"" << ar.name() <<
"\" for item with type \""
135 <<
record.type().name() <<
"\", name \"" <<
record.name() <<
"\", and category \"" <<
record.category() <<
'"';
142 throw gs::IOInvalidArgument(
143 "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
144 "records can not be reused");
151 std::ostream &os = ar.outputStream();
152 std::streampos
base = os.tellp();
153 std::ostream &compressed = ar.compressedStream(os);
156 if (!
record.writeData(compressed))
159 const unsigned compressCode = ar.flushCompressedRecord(compressed);
164 std::streamoff off = os.tellp() -
base;
165 const long long delta = off;
169 const unsigned long long id = ar.addToCatalog(
record, compressCode,
delta);
179 ar.lastItemLength_ =
delta;