<commit_before>/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.data.sql;

import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import org.grouplens.lenskit.data.event.Rating;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.sql.Connection;
import java.util.List;

/**
 * Construct and configure a JDBC rating DAO. Get a builder with {@link JDBCRatingDAO#newBuilder()}.
 *
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 * @since 2.1
 */
public class JDBCRatingDAOBuilder {
    private SQLStatementFactory factory;
    private BasicSQLStatementFactory basicFactory;
    private boolean closeWhenClosed = true;
    private CacheBuilder<? super QueryKey, ? super List<?>> cacheBuilder;
    private Cache<QueryKey, List<Rating>> queryCache;

    JDBCRatingDAOBuilder() {
        factory = basicFactory = new BasicSQLStatementFactory();
        cacheBuilder = CacheBuilder.newBuilder().softValues().maximumSize(1000);
    }

    public String getTableName() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getTableName();
    }

    public JDBCRatingDAOBuilder setTableName(String table) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(table, "table name");
        basicFactory.setTableName(table);
        return this;
    }

    public String getUserColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getUserColumn();
    }

    public JDBCRatingDAOBuilder setUserColumn(@Nonnull String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(col, "user column name");
        basicFactory.setUserColumn(col);
        return this;
    }

    public String getItemColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getItemColumn();
    }

    public JDBCRatingDAOBuilder setItemColumn(@Nonnull String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(col, "item column name");
        basicFactory.setItemColumn(col);
        return this;
    }

    public String getRatingColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getRatingColumn();
    }

    public JDBCRatingDAOBuilder setRatingColumn(@Nonnull String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(col, "rating column name");
        basicFactory.setRatingColumn(col);
        return this;
    }

    public String getTimestampColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getTimestampColumn();
    }

    public JDBCRatingDAOBuilder setTimestampColumn(@Nullable String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        basicFactory.setTimestampColumn(col);
        return this;
    }

    public boolean isCloseWhenClosed() {
        return closeWhenClosed;
    }

    /**
     * Configure whether the the DAO should close the database connection.
     *
     * @param close {@code true} to close the database connection with the DAO, {@code false} to
     *              leave it open.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setCloseWhenClosed(boolean close) {
        this.closeWhenClosed = close;
        return this;
    }

    /**
     * Set the statement factory to be used by the DAO.
     * @param fac The statement factory.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setStatementFactory(SQLStatementFactory fac) {
        factory = fac;
        basicFactory = null;
        return this;
    }

    /**
     * Get the statement factory the DAO will use.
     * @return The statement factory the DAO will use.
     */
    public SQLStatementFactory getStatementFactory() {
        return factory;
    }

    /**
     * Set a cache builder to use for making the DAO's internal caches.  The default builder uses
     * soft values and a maximum size of 1000.
     *
     * @param cb The cache builder.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setCacheBuilder(CacheBuilder<? super QueryKey, ? super List<?>> cb) {
        cacheBuilder = cb;
        return this;
    }

    /**
     * Set a cache builder spec to use for making the DAO's internal caches.
     * @param spec A cache builder spec.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setCacheBuilder(CacheBuilderSpec spec) {
        return setCacheBuilder(CacheBuilder.from(spec));
    }

    /**
     * Set the cache builder spec to use for making the DAO's internal caches.<commit_msg>Add timeout to cache<commit_after>/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.data.sql;

import com.google.common.base.Preconditions;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import org.grouplens.lenskit.data.event.Rating;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.sql.Connection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Construct and configure a JDBC rating DAO. Get a builder with {@link JDBCRatingDAO#newBuilder()}.
 *
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 * @since 2.1
 */
public class JDBCRatingDAOBuilder {
    private SQLStatementFactory factory;
    private BasicSQLStatementFactory basicFactory;
    private boolean closeWhenClosed = true;
    private CacheBuilder<? super QueryKey, ? super List<?>> cacheBuilder;
    private Cache<QueryKey, List<Rating>> queryCache;

    JDBCRatingDAOBuilder() {
        factory = basicFactory = new BasicSQLStatementFactory();
        cacheBuilder = CacheBuilder.newBuilder()
                                   .softValues()
                                   .maximumSize(1000)
                                   .expireAfterWrite(5, TimeUnit.MINUTES);
    }

    public String getTableName() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getTableName();
    }

    public JDBCRatingDAOBuilder setTableName(String table) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(table, "table name");
        basicFactory.setTableName(table);
        return this;
    }

    public String getUserColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getUserColumn();
    }

    public JDBCRatingDAOBuilder setUserColumn(@Nonnull String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(col, "user column name");
        basicFactory.setUserColumn(col);
        return this;
    }

    public String getItemColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getItemColumn();
    }

    public JDBCRatingDAOBuilder setItemColumn(@Nonnull String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(col, "item column name");
        basicFactory.setItemColumn(col);
        return this;
    }

    public String getRatingColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getRatingColumn();
    }

    public JDBCRatingDAOBuilder setRatingColumn(@Nonnull String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        Preconditions.checkNotNull(col, "rating column name");
        basicFactory.setRatingColumn(col);
        return this;
    }

    public String getTimestampColumn() {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        return basicFactory.getTimestampColumn();
    }

    public JDBCRatingDAOBuilder setTimestampColumn(@Nullable String col) {
        Preconditions.checkState(basicFactory != null, "statement factory has been explicitly set");
        basicFactory.setTimestampColumn(col);
        return this;
    }

    public boolean isCloseWhenClosed() {
        return closeWhenClosed;
    }

    /**
     * Configure whether the the DAO should close the database connection.
     *
     * @param close {@code true} to close the database connection with the DAO, {@code false} to
     *              leave it open.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setCloseWhenClosed(boolean close) {
        this.closeWhenClosed = close;
        return this;
    }

    /**
     * Set the statement factory to be used by the DAO.
     * @param fac The statement factory.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setStatementFactory(SQLStatementFactory fac) {
        factory = fac;
        basicFactory = null;
        return this;
    }

    /**
     * Get the statement factory the DAO will use.
     * @return The statement factory the DAO will use.
     */
    public SQLStatementFactory getStatementFactory() {
        return factory;
    }

    /**
     * Set a cache builder to use for making the DAO's internal caches.  The default builder uses
     * soft value, a maximum size of 1000, and a timeout of 5 minutes after load.
     *
     * @param cb The cache builder.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setCacheBuilder(CacheBuilder<? super QueryKey, ? super List<?>> cb) {
        cacheBuilder = cb;
        return this;
    }

    /**
     * Set a cache builder spec to use for making the DAO's internal caches.
     * @param spec A cache builder spec.
     * @return The builder (for chaining).
     */
    public JDBCRatingDAOBuilder setCacheBuilder(CacheBuilderSpec spec) {
        return setCacheBuilder(CacheBuilder.from(spec));
    }

    /**
     * Set the cache builder spec to use for making the DAO's internal caches.
{'fraction_non_alphanumeric': 0.0824124513618677, 'fraction_numerical': 0.00933852140077821, 'mean_word_length': 3.168342523516056, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6651330', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3272, 'n_words': 1147}