CMS 3D CMS Logo

AbsArchive.cc
Go to the documentation of this file.
1 #include <cassert>
2 #include <sstream>
3 
4 #include "Alignment/Geners/interface/AbsArchive.hh"
5 #include "Alignment/Geners/interface/IOException.hh"
6 
7 #define GS_STREAM_COPY_BUFFER_SIZE 65536
8 
9 static void archive_stream_copy(std::istream &in, std::size_t count, std::ostream &out) {
10  if (count) {
11  const std::size_t bufsize = GS_STREAM_COPY_BUFFER_SIZE;
12  char buffer[bufsize];
13 
14  bool in_fail = in.fail();
15  bool out_fail = out.fail();
16  while (count > bufsize && !in_fail && !out_fail) {
17  in.read(buffer, bufsize);
18  in_fail = in.fail();
19  if (!in_fail) {
20  out.write(buffer, bufsize);
21  out_fail = out.fail();
22  }
23  count -= bufsize;
24  }
25  if (!in_fail && !out_fail) {
26  in.read(buffer, count);
27  if (!in.fail())
28  out.write(buffer, count);
29  }
30  }
31 }
32 
33 namespace {
34  class NotWritableRecord : public gs::AbsRecord {
35  public:
36  inline NotWritableRecord(const gs::ClassId &classId, const char *ioPrototype, const char *name, const char *category)
37  : gs::AbsRecord(classId, ioPrototype, name, category) {}
38 
39  NotWritableRecord() = delete;
40 
41  private:
42  inline bool writeData(std::ostream &) const override { return false; }
43  };
44 } // namespace
45 
46 namespace gs {
47  AbsArchive::AbsArchive(const char *name) : name_(name ? name : ""), lastItemId_(0), lastItemLength_(0) {}
48 
49  void AbsArchive::addItemToReference(AbsReference &r, const unsigned long long id) const { r.addItemId(id); }
50 
51  unsigned long long AbsArchive::copyItem(const unsigned long long id,
52  AbsArchive *destination,
53  const char *newName,
54  const char *newCategory) {
55  if (!isReadable())
56  throw gs::IOInvalidArgument(
57  "In gs::AbsArchive::copyItem: "
58  "origin archive is not readable");
60  if (this == destination)
61  throw gs::IOInvalidArgument(
62  "In gs::AbsArchive::copyItem: "
63  "origin and destination archives are the same");
64  AbsArchive &ar(*destination);
65  if (!ar.isWritable())
66  throw gs::IOInvalidArgument(
67  "In gs::AbsArchive::copyItem: "
68  "destination archive is not writable");
69  std::shared_ptr<const CatalogEntry> entry(catalogEntry(id));
70  if (!entry)
71  throw gs::IOInvalidArgument(
72  "In gs::AbsArchive::copyItem: no item "
73  "in the archive with the given id");
74 
75  // Position the input stream
76  long long sz = 0;
77  std::istream &is = inputStream(id, &sz);
78 
79  // The following code is similar to the one in the "operator<<"
80  // below w.r.t. operations with the output archive
81  std::ostream &os = ar.outputStream();
82  std::streampos base = os.tellp();
83  std::ostream &compressed = ar.compressedStream(os);
84 
85  // Transfer the data between the streams
86  unsigned long long len;
87  if (sz >= 0)
88  len = sz;
89  else
90  len = entry->itemLength();
91  archive_stream_copy(is, len, compressed);
92  if (is.fail())
93  throw IOReadFailure(
94  "In gs::AbsArchive::copyItem: "
95  "input stream failure");
96  if (compressed.fail())
97  throw IOWriteFailure(
98  "In gs::AbsArchive::copyItem: "
99  "output stream failure");
100  const unsigned compressCode = ar.flushCompressedRecord(compressed);
101  if (os.fail())
102  throw IOWriteFailure(
103  "In gs::AbsArchive::copyItem: "
104  "failed to transfer compressed data");
105 
106  // Figure out the record length. Naturally, can't have negative length.
107  std::streamoff off = os.tellp() - base;
108  const long long delta = off;
109  assert(delta >= 0LL);
110 
111  // We need to create a record out of the catalog entry we have found
112  const char *name = newName;
113  if (!name)
114  name = entry->name().c_str();
115  const char *category = newCategory;
116  if (!category)
117  category = entry->category().c_str();
118  NotWritableRecord record(entry->type(), entry->ioPrototype().c_str(), name, category);
119 
120  // Add the record to the catalog
121  const unsigned long long id2 = ar.addToCatalog(record, compressCode, delta);
122  if (id == 0ULL)
123  throw IOWriteFailure(
124  "In gs::AbsArchive::copyItem: "
125  "failed to add catalog entry");
126  ar.lastItemId_ = id2;
127  ar.lastItemLength_ = delta;
128  return id2;
129  }
130 } // namespace gs
131 
132 static std::string local_error_message(gs::AbsArchive &ar, const gs::AbsRecord &record, const char *failedAction) {
133  std::ostringstream err;
134  err << "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
135  << "failed to " << failedAction << " to the archive \"" << ar.name() << "\" for item with type \""
136  << record.type().name() << "\", name \"" << record.name() << "\", and category \"" << record.category() << '"';
137  return err.str();
138 }
139 
140 gs::AbsArchive &operator<<(gs::AbsArchive &ar, const gs::AbsRecord &record) {
141  // Do not reuse records
142  if (record.id())
143  throw gs::IOInvalidArgument(
144  "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
145  "records can not be reused");
146 
147  // Get the relevant streams. Do not change the order
148  // of the next three lines, some code which does
149  // not implement compression may actually rely on
150  // the fact that the "outputStream()" method
151  // is called before "compressedStream()".
152  std::ostream &os = ar.outputStream();
153  std::streampos base = os.tellp();
154  std::ostream &compressed = ar.compressedStream(os);
155 
156  // Write the data
157  if (!record.writeData(compressed))
158  throw gs::IOWriteFailure(local_error_message(ar, record, "write data"));
159 
160  const unsigned compressCode = ar.flushCompressedRecord(compressed);
161  if (os.fail())
162  throw gs::IOWriteFailure(local_error_message(ar, record, "transfer compressed data"));
163 
164  // Figure out the record length. Naturally, can't have negative length.
165  std::streamoff off = os.tellp() - base;
166  const long long delta = off;
167  assert(delta >= 0LL);
168 
169  // Add the record to the catalog
170  const unsigned long long id = ar.addToCatalog(record, compressCode, delta);
171  if (id == 0ULL)
172  throw gs::IOWriteFailure(local_error_message(ar, record, "add catalog entry"));
173 
174  // Mark record as written and give feedback about item length
175  record.itemId_ = id;
176  record.itemLength_ = delta;
177 
178  // Same thing for the archive
179  ar.lastItemId_ = id;
180  ar.lastItemLength_ = delta;
181 
182  return ar;
183 }
static void archive_stream_copy(std::istream &in, std::size_t count, std::ostream &out)
Definition: AbsArchive.cc:9
base
Main Program
Definition: newFWLiteAna.py:92
#define GS_STREAM_COPY_BUFFER_SIZE
Definition: AbsArchive.cc:7
string newName
Definition: mps_merge.py:86
assert(be >=bs)
gs::AbsArchive & operator<<(gs::AbsArchive &ar, const gs::AbsRecord &record)
Definition: AbsArchive.cc:140
static std::string local_error_message(gs::AbsArchive &ar, const gs::AbsRecord &record, const char *failedAction)
Definition: AbsArchive.cc:132
Definition: AbsArchive.cc:46