<commit_before>import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

import java.sql.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Created by sumeet
 * on 20/1/17.
 */
public abstract class Database {
    private final DatabaseConfig databaseConfig;

    private final static Map<String, DataSource> dataSourceMap = new HashMap<>();
    private final static Logger log = Logger.getLogger(Database.class.getName());

    public Database(DatabaseConfig databaseConfig) {
        this.databaseConfig = databaseConfig;

        if (databaseConfig.usePool) {
            databaseConfig.poolProperties.setUrl(databaseConfig.url);
            databaseConfig.poolProperties.setDriverClassName(databaseConfig.driver);
            databaseConfig.poolProperties.setUsername(databaseConfig.user);
            databaseConfig.poolProperties.setPassword(databaseConfig.password);
            instantiatePool(databaseConfig.poolProperties);
        }
    }

    private void instantiatePool(PoolProperties poolProperties) {
        DataSource dataSource = dataSourceMap.getOrDefault(databaseConfig.databaseName, null);
        if (dataSource == null) {
            synchronized (Database.class) {
                dataSource = dataSourceMap.getOrDefault(databaseConfig.databaseName, null);
                if (dataSource == null) {
                    log.log(Level.INFO, "INITIALIZING_DATASOURCE");
                    dataSource = new DataSource(poolProperties);
                    log.log(Level.INFO, "DATASOURCE_INITIALIZED");
                    dataSourceMap.put(databaseConfig.databaseName, dataSource);
                }
            }
        }
    }

    private Connection getSingleDirectConnection() throws ClassNotFoundException, SQLException {
        Class.forName(databaseConfig.driver);
        return DriverManager.getConnection(databaseConfig.url, databaseConfig.user, databaseConfig.password);
    }

    protected Connection getConnection() throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection;
        if (databaseConfig.usePool) {
            connection = dataSourceMap.get(databaseConfig.databaseName).getConnection();
        } else {
            connection = getSingleDirectConnection();
        }
        long timeTaken = System.currentTimeMillis() - startTime;
        log.log(Level.INFO, String.format("DB_CONNECT_TIME|%s|%d", databaseConfig.databaseName, timeTaken));
        return connection;
    }

    protected String getProcedureCallString(String storedProcedureName, List<Parameter> parameters) {
        List<String> paramList = parameters.stream().map(parameter -> parameter.getName() + " := ?").collect(Collectors.toList());
        String parameterPlaceHolders = Util.implode(",", paramList);
        String procedureCallString = String.format("{call %s (%s) }", storedProcedureName, parameterPlaceHolders);
        return procedureCallString;
    }

    protected PreparedStatement getPreparedStatement(Connection connection, String storedProcedureName, List<Parameter> parameters) throws Exception {
        PreparedStatement preparedStatement = connection.prepareStatement(getProcedureCallString(storedProcedureName, parameters));
        preparedStatement.setEscapeProcessing(true);
        int index = 0;
        for (Parameter parameter : parameters) {
            preparedStatement.setObject(++index, parameter.getValue(), parameter.getType());
        }
        return preparedStatement;
    }

    protected <T> List<T> executeQuery(StoredProcedureCall<T> storedProcedureCall) throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<T> rows = null;

        String storedProcedureName = storedProcedureCall.getStoredProcedureName();
        try {
            connection = getConnection();
            preparedStatement = getPreparedStatement(connection, storedProcedureName, storedProcedureCall.getParameters());
            resultSet = preparedStatement.executeQuery();

            RowMapper<T> rowMapper = storedProcedureCall.getRowMapper();
            rows = new LinkedList<>();

            while (resultSet.next()) {
                T row = rowMapper.map(resultSet);
                rows.add(row);
            }

        } catch (Exception e) {
            log.log(Level.SEVERE, storedProcedureName + "_FAILURE");
            throw new DatabaseException(databaseConfig.databaseName, storedProcedureName, e);
        } finally {
            closeDatabaseConnection(resultSet, preparedStatement, connection);
            long timeTaken = System.currentTimeMillis() - startTime;
            log.log(Level.INFO, String.format("SP_TIME|%s|%d", storedProcedureName, timeTaken));
        }
        return rows;
    }

    protected int executeUpdate(StoredProcedureCall<Integer> storedProcedureCall) throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int affectedRows = -1;

        String storedProcedureName = storedProcedureCall.getStoredProcedureName();
        try {
            connection = getConnection();
            preparedStatement = getPreparedStatement(connection, storedProcedureName, storedProcedureCall.getParameters());
            affectedRows = preparedStatement.executeUpdate();

        } catch (Exception e) {
            log.log(Level.SEVERE, storedProcedureName + "_FAILURE");
            throw new DatabaseException(databaseConfig.databaseName, storedProcedureName, e);
        } finally {
            closeDatabaseConnection(null, preparedStatement, connection);
            long timeTaken = System.currentTimeMillis() - startTime;
            log.log(Level.INFO, String.format("SP_TIME|%s|%d", storedProcedureName, timeTaken));
        }
        return affectedRows;
    }

    protected <T> List<T> executeQuery(PSMaker psMaker, RowMapper<T> rowMapper) throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<T> rows = null;

        try {
            connection = getConnection();
            preparedStatement = psMaker.getPreparedStatement(connection);
            resultSet = preparedStatement.executeQuery();

            rows = new LinkedList<>();

            while (resultSet.next()) {
                T row = rowMapper.map(resultSet);
                rows.add(row);
            }

        } catch (Exception e) {
            log.log(Level.SEVERE, "RAW_QUERY_FAILURE");
            throw new DatabaseException(databaseConfig.databaseName, "RAW_QUERY", e);
        } finally {
            closeDatabaseConnection(resultSet, preparedStatement, connection);
            long timeTaken = System.currentTimeMillis() - startTime;
            log.log(Level.INFO, String.format("RAW_QUERY|%d", timeTaken));
        }
        return rows;
    }

    protected void closeDatabaseConnection(ResultSet resultSet, PreparedStatement statement, Connection connection) {
        if (resultSet != null) try {
            resultSet.close();
        } catch (Exception e) {
            log.log(Level.SEVERE, "ERROR_IN_CLOSING_RESULT_SET", e);
        }
        if (statement != null) try {
            statement.close();
        } catch (Exception e) {
            log.log(Level.SEVERE, "ERROR_IN_CLOSING_STATEMENT", e);
        }
        if (connection != null) try {
            connection.close();
        } catch (Exception e) {
            log.log(Level.SEVERE, "ERROR_IN_CLOSING_CONNECTION", e);
        }
    }
}
<commit_msg>added function to turn off logging<commit_after>import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

import java.sql.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Created by sumeet
 * on 20/1/17.
 */
public abstract class Database {
    private final DatabaseConfig databaseConfig;

    private final static Map<String, DataSource> dataSourceMap = new HashMap<>();
    private final static Logger log = Logger.getLogger(Database.class.getName());

    public Database(DatabaseConfig databaseConfig) {
        this.databaseConfig = databaseConfig;

        if (databaseConfig.usePool) {
            databaseConfig.poolProperties.setUrl(databaseConfig.url);
            databaseConfig.poolProperties.setDriverClassName(databaseConfig.driver);
            databaseConfig.poolProperties.setUsername(databaseConfig.user);
            databaseConfig.poolProperties.setPassword(databaseConfig.password);
            instantiatePool(databaseConfig.poolProperties);
        }
    }

    private void instantiatePool(PoolProperties poolProperties) {
        DataSource dataSource = dataSourceMap.getOrDefault(databaseConfig.databaseName, null);
        if (dataSource == null) {
            synchronized (Database.class) {
                dataSource = dataSourceMap.getOrDefault(databaseConfig.databaseName, null);
                if (dataSource == null) {
                    log.log(Level.INFO, "INITIALIZING_DATASOURCE");
                    dataSource = new DataSource(poolProperties);
                    log.log(Level.INFO, "DATASOURCE_INITIALIZED");
                    dataSourceMap.put(databaseConfig.databaseName, dataSource);
                }
            }
        }
    }

    private Connection getSingleDirectConnection() throws ClassNotFoundException, SQLException {
        Class.forName(databaseConfig.driver);
        return DriverManager.getConnection(databaseConfig.url, databaseConfig.user, databaseConfig.password);
    }

    protected Connection getConnection() throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection;
        if (databaseConfig.usePool) {
            connection = dataSourceMap.get(databaseConfig.databaseName).getConnection();
        } else {
            connection = getSingleDirectConnection();
        }
        long timeTaken = System.currentTimeMillis() - startTime;
        log.log(Level.INFO, String.format("DB_CONNECT_TIME|%s|%d", databaseConfig.databaseName, timeTaken));
        return connection;
    }

    protected String getProcedureCallString(String storedProcedureName, List<Parameter> parameters) {
        List<String> paramList = parameters.stream().map(parameter -> parameter.getName() + " := ?").collect(Collectors.toList());
        String parameterPlaceHolders = Util.implode(",", paramList);
        String procedureCallString = String.format("{call %s (%s) }", storedProcedureName, parameterPlaceHolders);
        return procedureCallString;
    }

    protected PreparedStatement getPreparedStatement(Connection connection, String storedProcedureName, List<Parameter> parameters) throws Exception {
        PreparedStatement preparedStatement = connection.prepareStatement(getProcedureCallString(storedProcedureName, parameters));
        preparedStatement.setEscapeProcessing(true);
        int index = 0;
        for (Parameter parameter : parameters) {
            preparedStatement.setObject(++index, parameter.getValue(), parameter.getType());
        }
        return preparedStatement;
    }

    protected <T> List<T> executeQuery(StoredProcedureCall<T> storedProcedureCall) throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<T> rows = null;

        String storedProcedureName = storedProcedureCall.getStoredProcedureName();
        try {
            connection = getConnection();
            preparedStatement = getPreparedStatement(connection, storedProcedureName, storedProcedureCall.getParameters());
            resultSet = preparedStatement.executeQuery();

            RowMapper<T> rowMapper = storedProcedureCall.getRowMapper();
            rows = new LinkedList<>();

            while (resultSet.next()) {
                T row = rowMapper.map(resultSet);
                rows.add(row);
            }

        } catch (Exception e) {
            log.log(Level.SEVERE, storedProcedureName + "_FAILURE");
            throw new DatabaseException(databaseConfig.databaseName, storedProcedureName, e);
        } finally {
            closeDatabaseConnection(resultSet, preparedStatement, connection);
            long timeTaken = System.currentTimeMillis() - startTime;
            log.log(Level.INFO, String.format("SP_TIME|%s|%d", storedProcedureName, timeTaken));
        }
        return rows;
    }

    protected int executeUpdate(StoredProcedureCall<Integer> storedProcedureCall) throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int affectedRows = -1;

        String storedProcedureName = storedProcedureCall.getStoredProcedureName();
        try {
            connection = getConnection();
            preparedStatement = getPreparedStatement(connection, storedProcedureName, storedProcedureCall.getParameters());
            affectedRows = preparedStatement.executeUpdate();

        } catch (Exception e) {
            log.log(Level.SEVERE, storedProcedureName + "_FAILURE");
            throw new DatabaseException(databaseConfig.databaseName, storedProcedureName, e);
        } finally {
            closeDatabaseConnection(null, preparedStatement, connection);
            long timeTaken = System.currentTimeMillis() - startTime;
            log.log(Level.INFO, String.format("SP_TIME|%s|%d", storedProcedureName, timeTaken));
        }
        return affectedRows;
    }

    protected <T> List<T> executeQuery(PSMaker psMaker, RowMapper<T> rowMapper) throws Exception {
        long startTime = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<T> rows = null;

        try {
            connection = getConnection();
            preparedStatement = psMaker.getPreparedStatement(connection);
            resultSet = preparedStatement.executeQuery();

            rows = new LinkedList<>();

            while (resultSet.next()) {
                T row = rowMapper.map(resultSet);
                rows.add(row);
            }

        } catch (Exception e) {
            log.log(Level.SEVERE, "RAW_QUERY_FAILURE");
            throw new DatabaseException(databaseConfig.databaseName, "RAW_QUERY", e);
        } finally {
            closeDatabaseConnection(resultSet, preparedStatement, connection);
            long timeTaken = System.currentTimeMillis() - startTime;
            log.log(Level.INFO, String.format("RAW_QUERY|%d", timeTaken));
        }
        return rows;
    }

    protected void closeDatabaseConnection(ResultSet resultSet, PreparedStatement statement, Connection connection) {
        if (resultSet != null) try {
            resultSet.close();
        } catch (Exception e) {
            log.log(Level.SEVERE, "ERROR_IN_CLOSING_RESULT_SET", e);
        }
        if (statement != null) try {
            statement.close();
        } catch (Exception e) {
            log.log(Level.SEVERE, "ERROR_IN_CLOSING_STATEMENT", e);
        }
        if (connection != null) try {
            connection.close();
        } catch (Exception e) {
            log.log(Level.SEVERE, "ERROR_IN_CLOSING_CONNECTION", e);
        }
    }

    public static void setLoggingLevel(Level level) {
        log.setLevel(level);
    }
}

{'fraction_non_alphanumeric': 0.09504827156649019, 'fraction_numerical': 0.0008720024914356899, 'mean_word_length': 2.806543385490754, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '4010778', 'n_tokens_mistral': 4031, 'n_tokens_neox': 3969, 'n_words': 823}