CMS 3D CMS Logo

WriteOnlyCatalog.cc
Go to the documentation of this file.
1 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
2 #include "Alignment/Geners/interface/CPP11_shared_ptr.hh"
3 #include "Alignment/Geners/interface/IOException.hh"
4 #include "Alignment/Geners/interface/WriteOnlyCatalog.hh"
5 #include "Alignment/Geners/interface/binaryIO.hh"
6 
7 namespace gs {
8  WriteOnlyCatalog::WriteOnlyCatalog(std::ostream &os, const unsigned long long firstId)
9  : AbsCatalog(), os_(os), count_(0), smallestId_(firstId ? firstId : 1ULL), largestId_(0) {}
10 
11  unsigned long long WriteOnlyCatalog::makeEntry(const ItemDescriptor &descriptor,
12  const unsigned compressionCode,
13  const unsigned long long itemLen,
14  const ItemLocation &loc,
15  const unsigned long long off) {
16  const unsigned long long id = count_ ? largestId_ + 1 : smallestId_;
17  lastEntry_ =
18  CPP11_auto_ptr<const CatalogEntry>(new CatalogEntry(descriptor, id, compressionCode, itemLen, loc, off));
19  if (lastEntry_->write(os_)) {
20  ++count_;
21  largestId_ = id;
22  return id;
23  } else {
24  lastEntry_ = nullptr;
25  return 0ULL;
26  }
27  }
28 
29  // Version 1 write function
30  // bool WriteOnlyCatalog::write(std::ostream& os) const
31  // {
32  // return ClassId::makeId<CatalogEntry>().write(os) &&
33  // ClassId::makeId<ItemLocation>().write(os);
34  // }
35 
36  bool WriteOnlyCatalog::write(std::ostream &os) const {
37  long long dummy = -1;
38  write_pod(os, dummy);
39  return !os.fail() && ClassId::makeId<CatalogEntry>().write(os) && ClassId::makeId<ItemLocation>().write(os);
40  }
41 
42  WriteOnlyCatalog *WriteOnlyCatalog::read(const ClassId &id, std::istream &in) {
43  static const ClassId current(ClassId::makeId<WriteOnlyCatalog>());
44  id.ensureSameName(current);
45  id.ensureVersionInRange(1, version());
46 
47  if (id.version() > 1) {
48  long long dummy;
49  read_pod(in, &dummy);
50  }
51 
52  ClassId rId(in, 1);
53  ClassId locId(in, 1);
54 
55  CPP11_auto_ptr<WriteOnlyCatalog> cat(new WriteOnlyCatalog(dynamic_cast<std::ostream &>(in)));
56  bool firstEntry = true;
57  for (in.peek(); !in.eof(); in.peek()) {
58  CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
59  if (rec) {
60  bool ordered = true;
61  const unsigned long long id = rec->id();
62  if (firstEntry) {
63  cat->smallestId_ = id;
64  cat->count_ = 1;
65  cat->largestId_ = id;
66  firstEntry = false;
67  } else {
68  if (id < cat->smallestId_) {
69  cat->smallestId_ = id;
70  ++cat->count_;
71  } else if (id > cat->largestId_) {
72  cat->largestId_ = id;
73  ++cat->count_;
74  } else
75  ordered = false;
76  }
77  delete rec;
78  if (!ordered)
79  throw IOInvalidData(
80  "In gs::WriteOnlyCatalog::read: "
81  "entry out of order. Catalog is "
82  "likely to be corrupted.");
83  } else
84  throw IOInvalidData(
85  "In gs::WriteOnlyCatalog::read: "
86  "failed to read catalog entry");
87  }
88  return cat.release();
89  }
90 
91  CPP11_shared_ptr<const CatalogEntry> WriteOnlyCatalog::retrieveEntry(unsigned long long) const {
92  throw IOReadFailure(
93  "In gs::WriteOnlyCatalog::retrieveEntry: "
94  "entries can not be retrieved "
95  "from a write-only catalog");
96  return CPP11_shared_ptr<CatalogEntry>(reinterpret_cast<CatalogEntry *>(0));
97  }
98 
99  bool WriteOnlyCatalog::retrieveStreampos(unsigned long long /* id */,
100  unsigned * /* compressionCode */,
101  unsigned long long * /* length */,
102  std::streampos * /* pos */) const {
103  throw IOReadFailure(
104  "In gs::WriteOnlyCatalog::retrieveStreampos: "
105  "stream positions can not be retrieved "
106  "from a write-only catalog");
107  return false;
108  }
109 
110  void WriteOnlyCatalog::search(const SearchSpecifier &,
111  const SearchSpecifier &,
112  std::vector<unsigned long long> *) const {
113  throw IOReadFailure(
114  "In gs::WriteOnlyCatalog::search: "
115  "entries can not be searched "
116  "in a write-only catalog");
117  }
118 } // namespace gs
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:314
def cat(path)
Definition: eostools.py:401
Definition: AbsArchive.cc:45
def write(self, setup)