<commit_before>#include "ostree_repo.h"

#include "logging/logging.h"

OSTreeObject::ptr OSTreeRepo::GetObject(const uint8_t sha256[32], const OstreeObjectType type) const {
  return GetObject(OSTreeHash(sha256), type);
}

OSTreeObject::ptr OSTreeRepo::GetObject(const OSTreeHash hash, const OstreeObjectType type) const {
  otable::const_iterator it;
  it = ObjectTable.find(hash);
  if (it != ObjectTable.end()) {
    return it->second;
  }

  const std::string exts[] = {".filez", ".dirtree", ".dirmeta", ".commit"};
  const std::string objpath = hash.string().insert(2, 1, '/');
  OSTreeObject::ptr object;
  int ext_index = -1;
  switch (type) {
    case OstreeObjectType::OSTREE_OBJECT_TYPE_FILE:
      ext_index = 0;
      break;
    case OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_TREE:
      ext_index = 1;
      break;
    case OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META:
      ext_index = 2;
      break;
    case OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT:
      ext_index = 3;
      break;
    case OstreeObjectType::OSTREE_OBJECT_TYPE_UNKNOWN:
    default:
      break;
  }

  for (int i = 0; i < 3; ++i) {
    if (i > 0) {
      LOG_WARNING << "OSTree hash " << hash << " not found. Retrying (attempt " << i << " of 3)";
    }
    if (type != OstreeObjectType::OSTREE_OBJECT_TYPE_UNKNOWN) {
      if (CheckForObject(hash, objpath + exts[ext_index], object)) {
        return object;
      }
    } else {
      for (const std::string &ext : exts) {
        if (CheckForObject(hash, objpath + ext, object)) {
          return object;
        }
      }
    }
  }
  throw OSTreeObjectMissing(hash);
}

bool OSTreeRepo::CheckForObject(const OSTreeHash &hash, const std::string &path, OSTreeObject::ptr &object) const {
  if (FetchObject(std::string("objects/") + path)) {
    object = OSTreeObject::ptr(new OSTreeObject(*this, path));
    ObjectTable[hash] = object;
    LOG_DEBUG << "Fetched OSTree object " << path;
    return true;
  }
  return false;
}
<commit_msg>Refactor object type discovery to use a map.<commit_after>#include "ostree_repo.h"

#include "logging/logging.h"

OSTreeObject::ptr OSTreeRepo::GetObject(const uint8_t sha256[32], const OstreeObjectType type) const {
  return GetObject(OSTreeHash(sha256), type);
}

OSTreeObject::ptr OSTreeRepo::GetObject(const OSTreeHash hash, const OstreeObjectType type) const {
  otable::const_iterator obj_it = ObjectTable.find(hash);
  if (obj_it != ObjectTable.cend()) {
    return obj_it->second;
  }

  const std::map<OstreeObjectType, std::string> exts{{OstreeObjectType::OSTREE_OBJECT_TYPE_FILE, ".filez"},
                                                     {OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_TREE, ".dirtree"},
                                                     {OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META, ".dirmeta"},
                                                     {OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT, ".commit"}};
  const std::string objpath = hash.string().insert(2, 1, '/');
  OSTreeObject::ptr object;

  for (int i = 0; i < 3; ++i) {
    if (i > 0) {
      LOG_WARNING << "OSTree hash " << hash << " not found. Retrying (attempt " << i << " of 3)";
    }
    if (type != OstreeObjectType::OSTREE_OBJECT_TYPE_UNKNOWN) {
      if (CheckForObject(hash, objpath + exts.at(type), object)) {
        return object;
      }
    } else {
      for (auto it = exts.cbegin(); it != exts.cend(); ++it) {
        if (CheckForObject(hash, objpath + it->second, object)) {
          return object;
        }
      }
    }
  }
  throw OSTreeObjectMissing(hash);
}

bool OSTreeRepo::CheckForObject(const OSTreeHash &hash, const std::string &path, OSTreeObject::ptr &object) const {
  if (FetchObject(std::string("objects/") + path)) {
    object = OSTreeObject::ptr(new OSTreeObject(*this, path));
    ObjectTable[hash] = object;
    LOG_DEBUG << "Fetched OSTree object " << path;
    return true;
  }
  return false;
}

{'fraction_non_alphanumeric': 0.13479703854991065, 'fraction_numerical': 0.008935409752361501, 'mean_word_length': 3.1504237288135593, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6620289', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1312, 'n_words': 281}