CMS 3D CMS Logo

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