<commit_before>package com.opower.persistence.jpile.loader;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.opower.persistence.jpile.infile.InfileDataBuffer;
import com.opower.persistence.jpile.reflection.CachedProxy;
import com.opower.persistence.jpile.reflection.PersistenceAnnotationInspector;
import com.opower.persistence.jpile.util.JdbcUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import java.io.Closeable;
import java.io.Flushable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static com.google.common.collect.Sets.newHashSet;

/**
 * Save any type of data using a collection of SingleInfileObjectLoaders. A common use case would be to do something like
 * <p/>
 * <pre>
 *     Connection connection = ...
 *     HierarchicalInfileObjectLoader objectLoader = new HierarchicalInfileObjectLoader();
 *     objectLoader.setConnection(connection);
 *     try {
 *         objectLoader.persist(foo, bar);
 *     } finally {
 *         objectLoader.close();
 *         connection.close();
 *     }
 * </pre>
 * Note that because the connection is passed in, it is up to the caller to close the connection correctly. Otherwise the
 * connection will never be closed.
 *
 * @author amir.raminfar
 * @since 1.0
 */
public class HierarchicalInfileObjectLoader implements Flushable, Closeable {
    private static Logger logger = LoggerFactory.getLogger(HierarchicalInfileObjectLoader.class);

    private PersistenceAnnotationInspector persistenceAnnotationInspector =
            CachedProxy.create(new PersistenceAnnotationInspector());

    private CallBack eventCallback = new NoOpCallBack();
    private Connection connection;

    // linked for consistent error message
    private Map<Class<?>, SingleInfileObjectLoader<Object>> primaryObjectLoaders = newLinkedHashMap();
    private Map<Class<?>, SingleInfileObjectLoader<Object>> secondaryTableObjectLoaders = newLinkedHashMap();
    private Map<Class<?>, Set<Method>> parentDependent = newHashMap();
    private Map<Class<?>, Set<Method>> childDependent = newHashMap();
    private Set<Class> classesToIgnore = ImmutableSet.of();
    private Set<String> secondaryClassesToIgnore = ImmutableSet.of();
    private boolean useReplace = false;


    /**
     * Disables fk (if not already disabled) and saves each object
     *
     * @param firstObject the first object to save
     * @param moreObjects optional more objects
     */
    public void persist(Object firstObject, Object... moreObjects) {
        persist(concat(of(firstObject), copyOf(moreObjects)));
    }

    /**
     * Disables fk (if not already disabled) and saves each object
     *
     * @param objects the objects to save
     */
    public void persist(Iterable<Object> objects) {
        Preconditions.checkNotNull(connection, "Connection is null, did you call setConnection()?");
        for (Object o : objects) {
            persistWithCyclicCheck(o, new HashSet<Object>());
        }
    }

    private void persistWithCyclicCheck(Object entity, Set<Object> cyclicCheck) {
        Preconditions.checkNotNull(entity, "Cannot persist null");

        // If we already saved this object then ignore
        if (cyclicCheck.contains(entity)) {
            logger.debug("Skipping in file persist on [{}] because it has already been saved.", entity);<commit_msg>use generic<commit_after>package com.opower.persistence.jpile.loader;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.opower.persistence.jpile.infile.InfileDataBuffer;
import com.opower.persistence.jpile.reflection.CachedProxy;
import com.opower.persistence.jpile.reflection.PersistenceAnnotationInspector;
import com.opower.persistence.jpile.util.JdbcUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import java.io.Closeable;
import java.io.Flushable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static com.google.common.collect.Sets.newHashSet;

/**
 * Save any type of data using a collection of SingleInfileObjectLoaders. A common use case would be to do something like
 * <p/>
 * <pre>
 *     Connection connection = ...
 *     HierarchicalInfileObjectLoader objectLoader = new HierarchicalInfileObjectLoader();
 *     objectLoader.setConnection(connection);
 *     try {
 *         objectLoader.persist(foo, bar);
 *     } finally {
 *         objectLoader.close();
 *         connection.close();
 *     }
 * </pre>
 * Note that because the connection is passed in, it is up to the caller to close the connection correctly. Otherwise the
 * connection will never be closed.
 *
 * @author amir.raminfar
 * @since 1.0
 */
public class HierarchicalInfileObjectLoader implements Flushable, Closeable {
    private static Logger logger = LoggerFactory.getLogger(HierarchicalInfileObjectLoader.class);

    private PersistenceAnnotationInspector persistenceAnnotationInspector =
            CachedProxy.create(new PersistenceAnnotationInspector());

    private CallBack eventCallback = new NoOpCallBack();
    private Connection connection;

    // linked for consistent error message
    private Map<Class<?>, SingleInfileObjectLoader<Object>> primaryObjectLoaders = newLinkedHashMap();
    private Map<Class<?>, SingleInfileObjectLoader<Object>> secondaryTableObjectLoaders = newLinkedHashMap();
    private Map<Class<?>, Set<Method>> parentDependent = newHashMap();
    private Map<Class<?>, Set<Method>> childDependent = newHashMap();
    private Set<Class> classesToIgnore = ImmutableSet.of();
    private Set<String> secondaryClassesToIgnore = ImmutableSet.of();
    private boolean useReplace = false;


    /**
     * Disables fk (if not already disabled) and saves each object
     *
     * @param firstObject the first object to save
     * @param moreObjects optional more objects
     */
    public void persist(Object firstObject, Object... moreObjects) {
        persist(concat(of(firstObject), copyOf(moreObjects)));
    }

    /**
     * Disables fk (if not already disabled) and saves each object
     *
     * @param objects the objects to save
     */
    public void persist(Iterable<?> objects) {
        Preconditions.checkNotNull(connection, "Connection is null, did you call setConnection()?");
        for (Object o : objects) {
            persistWithCyclicCheck(o, new HashSet<Object>());
        }
    }

    private void persistWithCyclicCheck(Object entity, Set<Object> cyclicCheck) {
        Preconditions.checkNotNull(entity, "Cannot persist null");

        // If we already saved this object then ignore
        if (cyclicCheck.contains(entity)) {
            logger.debug("Skipping in file persist on [{}] because it has already been saved.", entity);
{'fraction_non_alphanumeric': 0.09653787073141912, 'fraction_numerical': 0.000945291267871913, 'mean_word_length': 4.283395755305867, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '6038615', 'n_tokens_mistral': 2324, 'n_tokens_neox': 2306, 'n_words': 600}