CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 namespace gs {
8  AbsArchive::AbsArchive(const char* name)
9  : name_(name ? name : ""),
10  lastItemId_(0),
11  lastItemLength_(0)
12  {
13  }
14 
15  void AbsArchive::addItemToReference(AbsReference& r,
16  const unsigned long long id) const
17  {
18  r.addItemId(id);
19  }
20 }
21 
22 static std::string local_error_message(gs::AbsArchive& ar,
23  const gs::AbsRecord& record,
24  const char* failedAction)
25 {
26  std::ostringstream err;
27  err << "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
28  << "failed to " << failedAction << " to the archive \""
29  << ar.name() << "\" for item with type \""
30  << record.type().name() << "\", name \""
31  << record.name() << "\", and category \""
32  << record.category() << '"';
33  return err.str();
34 }
35 
36 gs::AbsArchive& operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record)
37 {
38  // Do not reuse records
39  if (record.id()) throw gs::IOInvalidArgument(
40  "In operator<<(gs::AbsArchive& ar, const gs::AbsRecord& record): "
41  "records can not be reused");
42 
43  // Get the relevant streams. Do not change the order
44  // of the next three lines, some code which does
45  // not implement compression may actually rely on
46  // the fact that the "outputStream()" method
47  // is called before "compressedStream()".
48  std::ostream& os = ar.outputStream();
49  std::streampos base = os.tellp();
50  std::ostream& compressed = ar.compressedStream(os);
51 
52  // Write the data
53  if (!record.writeData(compressed))
54  throw gs::IOWriteFailure(local_error_message(ar, record, "write data"));
55 
56  const unsigned compressCode = ar.flushCompressedRecord(compressed);
57  if (os.fail())
58  throw gs::IOWriteFailure(local_error_message(
59  ar, record, "transfer compressed data"));
60 
61  // Figure out the record length. Naturally, can't have negative length.
62  std::streamoff off = os.tellp() - base;
63  long long delta = off;
64  assert(delta >= 0LL);
65 
66  // Add the record to the catalog
67  const unsigned long long id = ar.addToCatalog(record, compressCode, delta);
68  if (id == 0ULL)
69  throw gs::IOWriteFailure(local_error_message(
70  ar, record, "add catalog entry"));
71 
72  // Mark record as written and give feedback about item length
73  record.itemId_ = id;
74  record.itemLength_ = delta;
75 
76  // Same thing for the archive
77  ar.lastItemId_ = id;
78  ar.lastItemLength_ = delta;
79 
80  return ar;
81 }
dbl * delta
Definition: mlp_gen.cc:36
tuple base
Main Program
Definition: newFWLiteAna.py:92
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
static std::string local_error_message(gs::AbsArchive &ar, const gs::AbsRecord &record, const char *failedAction)
Definition: AbsArchive.cc:22