Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:21

0001 #include <memory>
0002 #include "Alignment/Geners/interface/IOException.hh"
0003 #include "Alignment/Geners/interface/WriteOnlyCatalog.hh"
0004 #include "Alignment/Geners/interface/binaryIO.hh"
0005 
0006 #include <memory>
0007 
0008 namespace gs {
0009   WriteOnlyCatalog::WriteOnlyCatalog(std::ostream &os, const unsigned long long firstId)
0010       : AbsCatalog(), os_(os), count_(0), smallestId_(firstId ? firstId : 1ULL), largestId_(0) {}
0011 
0012   unsigned long long WriteOnlyCatalog::makeEntry(const ItemDescriptor &descriptor,
0013                                                  const unsigned compressionCode,
0014                                                  const unsigned long long itemLen,
0015                                                  const ItemLocation &loc,
0016                                                  const unsigned long long off) {
0017     const unsigned long long id = count_ ? largestId_ + 1 : smallestId_;
0018     lastEntry_ =
0019         std::unique_ptr<const CatalogEntry>(new CatalogEntry(descriptor, id, compressionCode, itemLen, loc, off));
0020     if (lastEntry_->write(os_)) {
0021       ++count_;
0022       largestId_ = id;
0023       return id;
0024     } else {
0025       lastEntry_ = nullptr;
0026       return 0ULL;
0027     }
0028   }
0029 
0030   // Version 1 write function
0031   // bool WriteOnlyCatalog::write(std::ostream& os) const
0032   // {
0033   //     return ClassId::makeId<CatalogEntry>().write(os) &&
0034   //            ClassId::makeId<ItemLocation>().write(os);
0035   // }
0036 
0037   bool WriteOnlyCatalog::write(std::ostream &os) const {
0038     long long dummy = -1;
0039     write_pod(os, dummy);
0040     return !os.fail() && ClassId::makeId<CatalogEntry>().write(os) && ClassId::makeId<ItemLocation>().write(os);
0041   }
0042 
0043   WriteOnlyCatalog *WriteOnlyCatalog::read(const ClassId &id, std::istream &in) {
0044     static const ClassId current(ClassId::makeId<WriteOnlyCatalog>());
0045     id.ensureSameName(current);
0046     id.ensureVersionInRange(1, version());
0047 
0048     if (id.version() > 1) {
0049       long long dummy;
0050       read_pod(in, &dummy);
0051     }
0052 
0053     ClassId rId(in, 1);
0054     ClassId locId(in, 1);
0055 
0056     std::unique_ptr<WriteOnlyCatalog> cat(new WriteOnlyCatalog(dynamic_cast<std::ostream &>(in)));
0057     bool firstEntry = true;
0058     for (in.peek(); !in.eof(); in.peek()) {
0059       CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
0060       if (rec) {
0061         bool ordered = true;
0062         const unsigned long long id = rec->id();
0063         if (firstEntry) {
0064           cat->smallestId_ = id;
0065           cat->count_ = 1;
0066           cat->largestId_ = id;
0067           firstEntry = false;
0068         } else {
0069           if (id < cat->smallestId_) {
0070             cat->smallestId_ = id;
0071             ++cat->count_;
0072           } else if (id > cat->largestId_) {
0073             cat->largestId_ = id;
0074             ++cat->count_;
0075           } else
0076             ordered = false;
0077         }
0078         delete rec;
0079         if (!ordered)
0080           throw IOInvalidData(
0081               "In gs::WriteOnlyCatalog::read: "
0082               "entry out of order. Catalog is "
0083               "likely to be corrupted.");
0084       } else
0085         throw IOInvalidData(
0086             "In gs::WriteOnlyCatalog::read: "
0087             "failed to read catalog entry");
0088     }
0089     return cat.release();
0090   }
0091 
0092   std::shared_ptr<const CatalogEntry> WriteOnlyCatalog::retrieveEntry(unsigned long long) const {
0093     throw IOReadFailure(
0094         "In gs::WriteOnlyCatalog::retrieveEntry: "
0095         "entries can not be retrieved "
0096         "from a write-only catalog");
0097     return std::shared_ptr<CatalogEntry>(reinterpret_cast<CatalogEntry *>(0));
0098   }
0099 
0100   bool WriteOnlyCatalog::retrieveStreampos(unsigned long long /* id */,
0101                                            unsigned * /* compressionCode */,
0102                                            unsigned long long * /* length */,
0103                                            std::streampos * /* pos */) const {
0104     throw IOReadFailure(
0105         "In gs::WriteOnlyCatalog::retrieveStreampos: "
0106         "stream positions can not be retrieved "
0107         "from a write-only catalog");
0108     return false;
0109   }
0110 
0111   void WriteOnlyCatalog::search(const SearchSpecifier &,
0112                                 const SearchSpecifier &,
0113                                 std::vector<unsigned long long> *) const {
0114     throw IOReadFailure(
0115         "In gs::WriteOnlyCatalog::search: "
0116         "entries can not be searched "
0117         "in a write-only catalog");
0118   }
0119 }  // namespace gs