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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#include <algorithm>
#include <cassert>
#include <utility>

#include <memory>
#include "Alignment/Geners/interface/ContiguousCatalog.hh"
#include "Alignment/Geners/interface/IOException.hh"
#include "Alignment/Geners/interface/binaryIO.hh"

namespace gs {
  void ContiguousCatalog::findByName(const NameMap &m,
                                     const SearchSpecifier &namePattern,
                                     std::vector<unsigned long long> *found) const {
    typedef NameMap::const_iterator Nameiter;

    if (namePattern.useRegex()) {
      const Nameiter itend = m.end();
      for (Nameiter it = m.begin(); it != itend; ++it)
        if (namePattern.matches(it->first))
          found->push_back(it->second);
    } else {
      const std::pair<Nameiter, Nameiter> limits = m.equal_range(namePattern.pattern());
      for (Nameiter it = limits.first; it != limits.second; ++it)
        found->push_back(it->second);
    }
  }

  unsigned long long ContiguousCatalog::makeEntry(const ItemDescriptor &descriptor,
                                                  const unsigned compressionCode,
                                                  const unsigned long long itemLength,
                                                  const ItemLocation &loc,
                                                  const unsigned long long offset) {
    const unsigned long long nextId = records_.size() + firstId_;
    lastEntry_ = SPtr(new CatalogEntry(descriptor, nextId, compressionCode, itemLength, loc, offset));
    records_.push_back(lastEntry_);
    recordMap_[descriptor.category()].insert(std::make_pair(descriptor.name(), nextId));

    return nextId;
  }

  void ContiguousCatalog::search(const SearchSpecifier &namePattern,
                                 const SearchSpecifier &categoryPattern,
                                 std::vector<unsigned long long> *found) const {
    typedef RecordMap::const_iterator Mapiter;

    assert(found);
    found->clear();

    const Mapiter endMap = recordMap_.end();
    if (categoryPattern.useRegex()) {
      for (Mapiter it = recordMap_.begin(); it != endMap; ++it)
        if (categoryPattern.matches(it->first))
          findByName(it->second, namePattern, found);
    } else {
      Mapiter it = recordMap_.find(categoryPattern.pattern());
      if (it != endMap)
        findByName(it->second, namePattern, found);
    }
    std::sort(found->begin(), found->end());
  }

  bool ContiguousCatalog::isEqual(const AbsCatalog &other) const {
    if ((void *)this == (void *)(&other))
      return true;
    const ContiguousCatalog &r = static_cast<const ContiguousCatalog &>(other);
    if (firstId_ != r.firstId_)
      return false;
    if (recordMap_ != r.recordMap_)
      return false;
    const unsigned long nRecords = records_.size();
    if (nRecords != r.records_.size())
      return false;
    for (unsigned long i = 0; i < nRecords; ++i)
      if (*records_[i] != *r.records_[i])
        return false;
    return true;
  }

  // Version 1 write function
  // bool ContiguousCatalog::write(std::ostream& os) const
  // {
  //     bool status = ClassId::makeId<CatalogEntry>().write(os) &&
  //                   ClassId::makeId<ItemLocation>().write(os);
  //     const unsigned long long sz = records_.size();
  //     for (unsigned long long i=0; i<sz && status; ++i)
  //         status = records_[i]->write(os);
  //     return status;
  // }

  bool ContiguousCatalog::write(std::ostream &os) const {
    const unsigned long long sz = records_.size();
    long long nRecords = sz;
    write_pod(os, nRecords);
    bool status = !os.fail() && ClassId::makeId<CatalogEntry>().write(os) && ClassId::makeId<ItemLocation>().write(os);
    for (unsigned long long i = 0; i < sz && status; ++i)
      status = records_[i]->write(os);
    return status;
  }

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

    if (cid.version() == 1)
      return read_v1(in);

    long long nRecords;
    read_pod(in, &nRecords);
    if (nRecords < 0)
      return read_v1(in);

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

    std::unique_ptr<ContiguousCatalog> catalog(new ContiguousCatalog());
    bool firstEntry = true;
    for (long long recnum = 0; recnum < nRecords; ++recnum) {
      CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
      if (rec) {
        const unsigned long long id = rec->id();
        if (firstEntry) {
          catalog->firstId_ = id;
          firstEntry = false;
        } else {
          const unsigned long long nextId = catalog->records_.size() + catalog->firstId_;
          if (id != nextId) {
            delete rec;
            throw IOInvalidData(
                "In gs::ContiguousCatalog::read:"
                " unexpected item id. "
                "Catalog is corrupted.");
          }
        }
        catalog->records_.push_back(SPtr(rec));
        catalog->recordMap_[rec->category()].insert(std::make_pair(rec->name(), id));
      } else
        throw IOInvalidData(
            "In gs::ContiguousCatalog::read:"
            " failed to read catalog entry");
    }
    return catalog.release();
  }

  ContiguousCatalog *ContiguousCatalog::read_v1(std::istream &in) {
    ClassId rId(in, 1);
    ClassId locId(in, 1);

    std::unique_ptr<ContiguousCatalog> catalog(new ContiguousCatalog());
    bool firstEntry = true;
    for (in.peek(); !in.eof(); in.peek()) {
      CatalogEntry *rec = CatalogEntry::read(rId, locId, in);
      if (rec) {
        const unsigned long long id = rec->id();
        if (firstEntry) {
          catalog->firstId_ = id;
          firstEntry = false;
        } else {
          const unsigned long long nextId = catalog->records_.size() + catalog->firstId_;
          if (id != nextId) {
            delete rec;
            throw IOInvalidData(
                "In gs::ContiguousCatalog::read_v1:"
                " unexpected item id. "
                "Catalog is corrupted.");
          }
        }
        catalog->records_.push_back(SPtr(rec));
        catalog->recordMap_[rec->category()].insert(std::make_pair(rec->name(), id));
      } else
        throw IOInvalidData(
            "In gs::ContiguousCatalog::read_v1:"
            " failed to read catalog entry");
    }
    return catalog.release();
  }

  std::shared_ptr<const CatalogEntry> ContiguousCatalog::retrieveEntry(const unsigned long long id) const {
    if (id >= firstId_ && id < records_.size() + firstId_)
      return records_[id - firstId_];
    else {
      CatalogEntry *ptr = nullptr;
      return std::shared_ptr<const CatalogEntry>(ptr);
    }
  }

  bool ContiguousCatalog::retrieveStreampos(unsigned long long id,
                                            unsigned *compressionCode,
                                            unsigned long long *length,
                                            std::streampos *pos) const {
    assert(compressionCode);
    assert(length);
    assert(pos);

    if (id >= firstId_ && id < records_.size() + firstId_) {
      const std::shared_ptr<const CatalogEntry> &rec(records_[id - firstId_]);
      *compressionCode = rec->compressionCode();
      *length = rec->itemLength();
      *pos = rec->location().streamPosition();
      return true;
    } else
      return false;
  }
}  // namespace gs