<commit_before>package oasis.jongo.applications.v2;

import javax.inject.Inject;

import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.jongo.MongoCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.mongodb.DuplicateKeyException;

import oasis.jongo.JongoBootstrapper;
import oasis.model.InvalidVersionException;
import oasis.model.applications.v2.UserSubscription;
import oasis.model.applications.v2.UserSubscriptionRepository;

public class JongoUserSubscriptionRepository implements UserSubscriptionRepository, JongoBootstrapper {
  private static final Logger logger = LoggerFactory.getLogger(JongoUserSubscriptionRepository.class);

  private final Jongo jongo;

  @Inject JongoUserSubscriptionRepository(Jongo jongo) {
    this.jongo = jongo;
  }

  private MongoCollection getUserSubscriptionsCollection() {
    return jongo.getCollection("user_subscriptions");
  }

  @Override
  public UserSubscription createUserSubscription(UserSubscription subscription) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(subscription.getService_id()));

    subscription = new JongoUserSubscription(subscription);
    try {
      getUserSubscriptionsCollection()
          .insert(subscription);
    } catch (DuplicateKeyException dke) {
      return null;
    }
    return subscription;
  }

  @Override
  public UserSubscription getUserSubscription(String id) {
    return getUserSubscriptionsCollection()
        .findOne("{ id: # }", id)
        .as(JongoUserSubscription.class);
  }

  @Override
  public boolean deleteUserSubscription(String id, long[] versions) throws InvalidVersionException {
    int n = getUserSubscriptionsCollection()
        .remove("{ id: #, modified: { $in: # } }", id, versions)
        .getN();

    if (n == 0) {
      if (getUserSubscriptionsCollection().count("{ id: # }", id) != 0) {
        throw new InvalidVersionException("userSubscription", id);
      }
      return false;
    }

    if (n > 1) {
      logger.error("Deleted {} user subscriptions with ID {}, that shouldn't have happened", n, id);
    }
    return true;
  }

  @Override
  @SuppressWarnings("unchecked")
  public Iterable<UserSubscription> getUserSubscriptions(String userId) {
    return (Iterable<UserSubscription>) (Iterable<?>) getUserSubscriptionsCollection()
        .find("{ user_id: # }", userId)
        .as(JongoUserSubscription.class);
  }

  @Override
  @SuppressWarnings("unchecked")
  public Iterable<UserSubscription> getSubscriptionsForService(String serviceId) {
    return (Iterable<UserSubscription>) (Iterable<?>) getUserSubscriptionsCollection()
        .find("{ service_id: 1 }")
        .as(JongoUserSubscription.class);
  }

  @Override
  public void bootstrap() {
    getUserSubscriptionsCollection().ensureIndex("{ id: 1 }", "{ unique: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ service_id: 1, user_id: 1 }", "{ unique: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ user_id: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ service_id: 1 }");
  }
}<commit_msg>Fix empty response of Service subscription getter<commit_after>package oasis.jongo.applications.v2;

import javax.inject.Inject;

import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.jongo.MongoCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.mongodb.DuplicateKeyException;

import oasis.jongo.JongoBootstrapper;
import oasis.model.InvalidVersionException;
import oasis.model.applications.v2.UserSubscription;
import oasis.model.applications.v2.UserSubscriptionRepository;

public class JongoUserSubscriptionRepository implements UserSubscriptionRepository, JongoBootstrapper {
  private static final Logger logger = LoggerFactory.getLogger(JongoUserSubscriptionRepository.class);

  private final Jongo jongo;

  @Inject JongoUserSubscriptionRepository(Jongo jongo) {
    this.jongo = jongo;
  }

  private MongoCollection getUserSubscriptionsCollection() {
    return jongo.getCollection("user_subscriptions");
  }

  @Override
  public UserSubscription createUserSubscription(UserSubscription subscription) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(subscription.getService_id()));

    subscription = new JongoUserSubscription(subscription);
    try {
      getUserSubscriptionsCollection()
          .insert(subscription);
    } catch (DuplicateKeyException dke) {
      return null;
    }
    return subscription;
  }

  @Override
  public UserSubscription getUserSubscription(String id) {
    return getUserSubscriptionsCollection()
        .findOne("{ id: # }", id)
        .as(JongoUserSubscription.class);
  }

  @Override
  public boolean deleteUserSubscription(String id, long[] versions) throws InvalidVersionException {
    int n = getUserSubscriptionsCollection()
        .remove("{ id: #, modified: { $in: # } }", id, versions)
        .getN();

    if (n == 0) {
      if (getUserSubscriptionsCollection().count("{ id: # }", id) != 0) {
        throw new InvalidVersionException("userSubscription", id);
      }
      return false;
    }

    if (n > 1) {
      logger.error("Deleted {} user subscriptions with ID {}, that shouldn't have happened", n, id);
    }
    return true;
  }

  @Override
  @SuppressWarnings("unchecked")
  public Iterable<UserSubscription> getUserSubscriptions(String userId) {
    return (Iterable<UserSubscription>) (Iterable<?>) getUserSubscriptionsCollection()
        .find("{ user_id: # }", userId)
        .as(JongoUserSubscription.class);
  }

  @Override
  @SuppressWarnings("unchecked")
  public Iterable<UserSubscription> getSubscriptionsForService(String serviceId) {
    return (Iterable<UserSubscription>) (Iterable<?>) getUserSubscriptionsCollection()
        .find("{ service_id: # }", serviceId)
        .as(JongoUserSubscription.class);
  }

  @Override
  public void bootstrap() {
    getUserSubscriptionsCollection().ensureIndex("{ id: 1 }", "{ unique: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ service_id: 1, user_id: 1 }", "{ unique: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ user_id: 1 }");
    getUserSubscriptionsCollection().ensureIndex("{ service_id: 1 }");
  }
}
{'fraction_non_alphanumeric': 0.11301208601475436, 'fraction_numerical': 0.004865798147857479, 'mean_word_length': 4.53125, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '7155351', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1847, 'n_words': 372}