<commit_before>package com.brighttag.agathon.dao.zerg;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.google.inject.Inject;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import com.ning.http.client.AsyncHttpClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.brighttag.agathon.dao.CassandraInstanceDao;
import com.brighttag.agathon.model.CassandraInstance;

/**
 * Zerg implementation of {@link CassandraInstanceDAO}.
 *
 * By convention, a Zerg host is considered to be part of a Cassandra ring if it contains
 * a role named "cassandra_<ringname>".
 *
 * Because Zerg uniquely identifies instances by hostname (which remain stable even if rebuilt),
 * this DAO uses the {@link String#hashCode() hashCode} of the hostname as the Cassandra instance ID.
 *
 * Finally, the Zerg DAO assumes you're running Cassandra on EC2 with the {@link Ec2MultiRegionSnitch}.
 * Therefore it translates the EC2 region ("us-east-1") and availability zone ("a") into the Cassandra
 * data center ("us-east") and rack ("1a"), as expected by this particular snitch.
 *
 * @author codyaray
 * @since 5/15/12
 */
public class ZergCassandraInstanceDao implements CassandraInstanceDao {

  private static final Logger LOG = LoggerFactory.getLogger(ZergCassandraInstanceDao.class);

  private final AsyncHttpClient client;
  private final String manifestUrl;
  private final Gson gson;

  @Inject
  public ZergCassandraInstanceDao(AsyncHttpClient client, Gson gson,
      @Named(ZergDaoModule.ZERG_MANIFEST_URL_PROPERTY) String manifestUrl) {
    this.client = client;
    this.gson = gson;
    this.manifestUrl = manifestUrl;
  }

  @Override
  public ImmutableSet<CassandraInstance> findAll(String ring) {
    Set<CassandraInstance> instances = Sets.newHashSet();
    for (Map.Entry<String, Map<String, Host>> regionEntry : getRegions().entrySet()) {
      Map<String, Host> region = regionEntry.getValue();
      for (Map.Entry<String, Host> hostEntry : region.entrySet()) {
        Host host = hostEntry.getValue();
        if (host.roles.contains(role(ring))) {
          instances.add(instance(hostEntry.getKey(), host));
        }
      }
    }
    return ImmutableSet.copyOf(instances);
  }

  @Override
  public @Nullable CassandraInstance findById(String ring, int id) {
    for (Map.Entry<String, Map<String, Host>> regionEntry : getRegions().entrySet()) {
      Map<String, Host> region = regionEntry.getValue();
      for (Map.Entry<String, Host> hostEntry : region.entrySet()) {
        String hostname = hostEntry.getKey();
        Host host = hostEntry.getValue();
        if (host.roles.contains(role(ring))) {
          int hostId = hostId(hostname);
          if (hostId == id) {
            return instance(hostname, host);
          }
        }
      }
    }
    return null;
  }

  @Override
  public void save(String ring, CassandraInstance instance) {
    throw new UnsupportedOperationException("Save is not supported for " + getClass().getSimpleName());
  }

  @Override
  public void delete(String ring, CassandraInstance instance) {
    throw new UnsupportedOperationException("Delete is not supported for " + getClass().getSimpleName());
  }

  Map<String, Map<String, Host>> getRegions() {
    try {
      return gson.fromJson(execute(manifestUrl), MAP_OF_REGIONS.getType());
    } catch (JsonSyntaxException e) {
      LOG.warn("Received bad JSON from Zerg {}: {}", manifestUrl, e);
    }
    return ImmutableMap.of();<commit_msg>[adhoc:agathon] Handle unexpected null pointer exception<commit_after>package com.brighttag.agathon.dao.zerg;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import javax.annotation.Nullable;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.google.inject.Inject;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import com.ning.http.client.AsyncHttpClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.brighttag.agathon.dao.CassandraInstanceDao;
import com.brighttag.agathon.model.CassandraInstance;

/**
 * Zerg implementation of {@link CassandraInstanceDAO}.
 *
 * By convention, a Zerg host is considered to be part of a Cassandra ring if it contains
 * a role named "cassandra_<ringname>".
 *
 * Because Zerg uniquely identifies instances by hostname (which remain stable even if rebuilt),
 * this DAO uses the {@link String#hashCode() hashCode} of the hostname as the Cassandra instance ID.
 *
 * Finally, the Zerg DAO assumes you're running Cassandra on EC2 with the {@link Ec2MultiRegionSnitch}.
 * Therefore it translates the EC2 region ("us-east-1") and availability zone ("a") into the Cassandra
 * data center ("us-east") and rack ("1a"), as expected by this particular snitch.
 *
 * @author codyaray
 * @since 5/15/12
 */
public class ZergCassandraInstanceDao implements CassandraInstanceDao {

  private static final Logger LOG = LoggerFactory.getLogger(ZergCassandraInstanceDao.class);

  private final AsyncHttpClient client;
  private final String manifestUrl;
  private final Gson gson;

  @Inject
  public ZergCassandraInstanceDao(AsyncHttpClient client, Gson gson,
      @Named(ZergDaoModule.ZERG_MANIFEST_URL_PROPERTY) String manifestUrl) {
    this.client = client;
    this.gson = gson;
    this.manifestUrl = manifestUrl;
  }

  @Override
  public ImmutableSet<CassandraInstance> findAll(String ring) {
    Set<CassandraInstance> instances = Sets.newHashSet();
    for (Map.Entry<String, Map<String, Host>> regionEntry : getRegions().entrySet()) {
      Map<String, Host> region = regionEntry.getValue();
      for (Map.Entry<String, Host> hostEntry : region.entrySet()) {
        Host host = hostEntry.getValue();
        if (host.roles.contains(role(ring))) {
          instances.add(instance(hostEntry.getKey(), host));
        }
      }
    }
    return ImmutableSet.copyOf(instances);
  }

  @Override
  public @Nullable CassandraInstance findById(String ring, int id) {
    for (Map.Entry<String, Map<String, Host>> regionEntry : getRegions().entrySet()) {
      Map<String, Host> region = regionEntry.getValue();
      for (Map.Entry<String, Host> hostEntry : region.entrySet()) {
        String hostname = hostEntry.getKey();
        Host host = hostEntry.getValue();
        if (host.roles.contains(role(ring))) {
          int hostId = hostId(hostname);
          if (hostId == id) {
            return instance(hostname, host);
          }
        }
      }
    }
    return null;
  }

  @Override
  public void save(String ring, CassandraInstance instance) {
    throw new UnsupportedOperationException("Save is not supported for " + getClass().getSimpleName());
  }

  @Override
  public void delete(String ring, CassandraInstance instance) {
    throw new UnsupportedOperationException("Delete is not supported for " + getClass().getSimpleName());
  }

  Map<String, Map<String, Host>> getRegions() {
    try {
      Map<String, Map<String, Host>> regions = gson.fromJson(execute(manifestUrl), MAP_OF_REGIONS.getType());
      return Objects.firstNonNull(regions, ImmutableMap.<String, Map<String, Host>>of());
    } catch (JsonSyntaxException e) {
      LOG.warn("Received bad JSON from Zerg {}: {}", manifestUrl, e);
    }
    return ImmutableMap.of();
{'fraction_non_alphanumeric': 0.1097622027534418, 'fraction_numerical': 0.003003754693366708, 'mean_word_length': 4.06721623335447, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5014347', 'n_tokens_mistral': 2376, 'n_tokens_neox': 2352, 'n_words': 618}