<commit_before>package com.twitter.mesos.scheduler.storage.mem;

import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;

import com.twitter.mesos.Tasks;
import com.twitter.mesos.gen.JobConfiguration;
import com.twitter.mesos.gen.JobKey;
import com.twitter.mesos.scheduler.storage.JobStore;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * An in-memory job store.
 */
class MemJobStore implements JobStore.Mutable {

  private static final Function<JobConfiguration, JobConfiguration> DEEP_COPY = Util.deepCopier();

  private final LoadingCache<String, Manager> managers = CacheBuilder.newBuilder()
      .build(new CacheLoader<String, Manager>() {
        @Override public Manager load(String key) {
          return new Manager();
        }
      });

  @Override
  public void saveAcceptedJob(String managerId, JobConfiguration jobConfig) {
    checkNotNull(managerId);
    checkNotNull(jobConfig);

    managers.getUnchecked(managerId).jobs.put(Tasks.jobKey(jobConfig), DEEP_COPY.apply(jobConfig));
  }

  @Override
  public void removeJob(String jobKey) {
    checkNotNull(jobKey);

    for (Manager manager : managers.asMap().values()) {
      manager.jobs.remove(jobKey);
    }
  }

  @Override
  public void removeJob(JobKey jobKey) {
    checkNotNull(jobKey);

    // TODO(ksweeney): Remove this delegation as part of MESOS-2403.
    removeJob(Tasks.jobKey(jobKey));
  }

  @Override
  public void deleteJobs() {
    managers.invalidateAll();
  }

  @Override
  public Iterable<JobConfiguration> fetchJobs(String managerId) {
    checkNotNull(managerId);

    @Nullable Manager manager = managers.getIfPresent(managerId);
    if (manager == null) {
      return ImmutableSet.of();
    }

    return FluentIterable.from(manager.jobs.values())
        .transform(DEEP_COPY)
        .toSet();
  }

  @Nullable
  private JobConfiguration fetchJob(String managerId, String jobKey) {
    checkNotNull(managerId);
    checkNotNull(jobKey);

    @Nullable Manager manager = managers.getIfPresent(managerId);
    if (manager == null) {
      return null;
    }

    return DEEP_COPY.apply(manager.jobs.get(jobKey));
  }

  @Override
  public Optional<JobConfiguration> fetchJob(String managerId, JobKey jobKey) {
    checkNotNull(jobKey);

    // TODO(ksweeney): Remove this delegation as part of MESOS-2403.
    return Optional.fromNullable(fetchJob(managerId, Tasks.jobKey(jobKey)));
  }

  @Override
  public Set<String> fetchManagerIds() {
    return ImmutableSet.copyOf(managers.asMap().keySet());
  }

  private static class Manager {
    private final Map<String, JobConfiguration> jobs = Maps.newConcurrentMap();
  }
}<commit_msg>Cleanup deprecated fetchJob in MemJobStore<commit_after>package com.twitter.mesos.scheduler.storage.mem;

import java.util.Map;
import java.util.Set;

import javax.annotation.Nullable;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;

import com.twitter.mesos.Tasks;
import com.twitter.mesos.gen.JobConfiguration;
import com.twitter.mesos.gen.JobKey;
import com.twitter.mesos.scheduler.storage.JobStore;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * An in-memory job store.
 */
class MemJobStore implements JobStore.Mutable {

  private static final Function<JobConfiguration, JobConfiguration> DEEP_COPY = Util.deepCopier();

  private final LoadingCache<String, Manager> managers = CacheBuilder.newBuilder()
      .build(new CacheLoader<String, Manager>() {
        @Override public Manager load(String key) {
          return new Manager();
        }
      });

  @Override
  public void saveAcceptedJob(String managerId, JobConfiguration jobConfig) {
    checkNotNull(managerId);
    checkNotNull(jobConfig);

    managers.getUnchecked(managerId).jobs.put(Tasks.jobKey(jobConfig), DEEP_COPY.apply(jobConfig));
  }

  @Override
  public void removeJob(String jobKey) {
    checkNotNull(jobKey);

    for (Manager manager : managers.asMap().values()) {
      manager.jobs.remove(jobKey);
    }
  }

  @Override
  public void removeJob(JobKey jobKey) {
    checkNotNull(jobKey);

    // TODO(ksweeney): Remove this delegation as part of MESOS-2403.
    removeJob(Tasks.jobKey(jobKey));
  }

  @Override
  public void deleteJobs() {
    managers.invalidateAll();
  }

  @Override
  public Iterable<JobConfiguration> fetchJobs(String managerId) {
    checkNotNull(managerId);

    @Nullable Manager manager = managers.getIfPresent(managerId);
    if (manager == null) {
      return ImmutableSet.of();
    }

    return FluentIterable.from(manager.jobs.values())
        .transform(DEEP_COPY)
        .toSet();
  }

  @Override
  public Optional<JobConfiguration> fetchJob(String managerId, JobKey jobKey) {
    checkNotNull(managerId);
    checkNotNull(jobKey);

    Optional<Manager> manager = Optional.fromNullable(managers.getIfPresent(managerId));
    if (!manager.isPresent()) {
      return Optional.absent();
    } else {
      return Optional.fromNullable(manager.get().jobs.get(Tasks.jobKey(jobKey)))
          .transform(DEEP_COPY);
    }
  }

  @Override
  public Set<String> fetchManagerIds() {
    return ImmutableSet.copyOf(managers.asMap().keySet());
  }

  private static class Manager {
    private final Map<String, JobConfiguration> jobs = Maps.newConcurrentMap();
  }
}
{'fraction_non_alphanumeric': 0.11251049538203191, 'fraction_numerical': 0.0020151133501259445, 'mean_word_length': 4.121238177128117, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '6816143', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1763, 'n_words': 351}