Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#include <memory>
#include "Alignment/Geners/interface/IOException.hh"
#include "Alignment/Geners/interface/WriteOnlyCatalog.hh"
#include "Alignment/Geners/interface/binaryIO.hh"

#include <memory>

namespace gs {
  WriteOnlyCatalog::WriteOnlyCatalog(std::ostream &os, const unsigned long long firstId)
      : AbsCatalog(), os_(os), count_(0), smallestId_(firstId ? firstId : 1ULL), largestId_(0) {}

  unsigned long long WriteOnlyCatalog::makeEntry(const ItemDescriptor &descriptor,
                                                 const unsigned compressionCode,
                                                 const unsigned long long itemLen,
                                                 const ItemLocation &loc,
                                                 const unsigned long long off) {
    const unsigned long long id = count_ ? largestId_ + 1 : smallestId_;
    lastEntry_ =
        std::unique_ptr<const CatalogEntry>(new CatalogEntry(descriptor, id, compressionCode, itemLen, loc, off));
    if (lastEntry_->write(os_)) {
      ++count_;
      largestId_ = id;
      return id;
    } else {
      lastEntry_ = nullptr;
      return 0ULL;
    }
  }

  // Version 1 write function
  // bool WriteOnlyCatalog::write(std::ostream& os) const
  // {
  //     return ClassId::makeId<CatalogEntry>().write(os) &&
  //            ClassId::makeId<ItemLocation>().write(os);
  // }

  bool WriteOnlyCatalog::write(std::ostream &os) const {
    long long dummy = -1;
    write_pod(os, dummy);
    return !os.fail() && ClassId::makeId<CatalogEntry>().write(os) && ClassId::makeId<ItemLocation>().write(os);
  }

  WriteOnlyCatalog *WriteOnlyCatalog::read(const ClassId &id, std::istream &in) {
    static const ClassId current(ClassId::makeId<WriteOnlyCatalog>());
    id.ensureSameName(current);
    id.ensureVersionInRange(1, version());

    if (id.version() > 1) {
      long long dummy;
      read_pod(in, &dummy);
    }

    ClassId rId(in, 1);
    ClassId locId(in, 1);

    std::unique_ptr<WriteOnlyCatalog> cat(new WriteOnlyCatalog(dynamic_cast<std::ostream &>(in)));
    bool firstEntry = true;
    for (in.peek(); !in.eof(); in.peek()) {
      CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
      if (rec) {
        bool ordered = true;
        const unsigned long long id = rec->id();
        if (firstEntry) {
          cat->smallestId_ = id;
          cat->count_ = 1;
          cat->largestId_ = id;
          firstEntry = false;
        } else {
          if (id < cat->smallestId_) {
            cat->smallestId_ = id;
            ++cat->count_;
          } else if (id > cat->largestId_) {
            cat->largestId_ = id;
            ++cat->count_;
          } else
            ordered = false;
        }
        delete rec;
        if (!ordered)
          throw IOInvalidData(
              "In gs::WriteOnlyCatalog::read: "
              "entry out of order. Catalog is "
              "likely to be corrupted.");
      } else
        throw IOInvalidData(
            "In gs::WriteOnlyCatalog::read: "
            "failed to read catalog entry");
    }
    return cat.release();
  }

  std::shared_ptr<const CatalogEntry> WriteOnlyCatalog::retrieveEntry(unsigned long long) const {
    throw IOReadFailure(
        "In gs::WriteOnlyCatalog::retrieveEntry: "
        "entries can not be retrieved "
        "from a write-only catalog");
    return std::shared_ptr<CatalogEntry>(reinterpret_cast<CatalogEntry *>(0));
  }

  bool WriteOnlyCatalog::retrieveStreampos(unsigned long long /* id */,
                                           unsigned * /* compressionCode */,
                                           unsigned long long * /* length */,
                                           std::streampos * /* pos */) const {
    throw IOReadFailure(
        "In gs::WriteOnlyCatalog::retrieveStreampos: "
        "stream positions can not be retrieved "
        "from a write-only catalog");
    return false;
  }

  void WriteOnlyCatalog::search(const SearchSpecifier &,
                                const SearchSpecifier &,
                                std::vector<unsigned long long> *) const {
    throw IOReadFailure(
        "In gs::WriteOnlyCatalog::search: "
        "entries can not be searched "
        "in a write-only catalog");
  }
}  // namespace gs