<commit_before>package me.therin.brovine

import me.therin.brovine.Settings
import me.therin.mining.itemsets.data.BasketIterator
import me.therin.mining.itemsets.data.Basket
import java.sql.ResultSet
import java.sql.Statement
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException

/**
 * TransfacBaskets.java
 *
 * @author tcirwin
 * @date 03 09, 2013
 */
class TransfacBaskets implements BasketIterator<Transfac> {
    private Connection conn
    private static final String UNIQUE_ITEMS =
"""
SELECT DISTINCT(tf_cart) as matchid, transfac, g.cnt
FROM apriori_staging
INNER JOIN factor_matches ON (tf_cart = matchid)
INNER JOIN (select tf, cnt from (
select tf_cart as tf, count(*) as cnt
from apriori_staging
group by tf_cart
) f) g ON (tf = tf_cart)
ORDER BY matchid;
"""

    private static final String FILL_BASKETS =
"""
SELECT geneid, tf_cart, transfac
FROM (select tf_cart
from (
select tf_cart, count(*) as cnt
from apriori_staging
group by tf_cart
) f) g
INNER JOIN apriori_staging USING (tf_cart)
INNER JOIN factor_matches ON (tf_cart = matchid)
ORDER BY geneid, tf_cart
"""

    private static final String NUM_BASKETS =
"""
SELECT COUNT(DISTINCT geneid)
FROM apriori_staging
"""

    private List<Basket<Transfac>> baskets
    private Iterator<Basket<Transfac>> itr

    public TransfacBaskets() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            conn =
                DriverManager.getConnection("jdbc:mysql://localhost/brovine?" +
                        "user=" + Settings.USER + "&password=" + Settings.PASS);

            fillBaskets()
        }
        catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception ex) {
            ex.printStackTrace()
        }
    }

    @Override
    List<Transfac> getUniqueItems() {
        try {
            Statement state
            ResultSet rs
            List<Transfac> tfs = new ArrayList<Transfac>()

            state = conn.createStatement()
            rs = state.executeQuery(UNIQUE_ITEMS)

            while (rs.next()) {
                tfs.add(new Transfac(rs.getInt(1), rs.getString(2)))
            }

            return tfs
        }
        catch (SQLException e) { e.printStackTrace() }
    }

    @Override
    //TODO: implement includeOnly
    BasketIterator<Transfac> includeOnly(List<Basket<Transfac>> baskets) {
        return null
    }

    @Override
    //TODO: implement update
    boolean update() {
        return false
    }

    @Override
    boolean reset() {
        if (fillBaskets()) {
            itr = baskets.iterator()
            return true
        }

        return false
    }

    @Override
    int size() {
        try {
            Statement state
            ResultSet rs

            state = conn.createStatement()
            rs = state.executeQuery(NUM_BASKETS)

            rs.next() ? rs.getInt(1) : 0
        }
        catch (SQLException e) { e.printStackTrace() }
    }

    @Override
    boolean hasNext() { (fillBaskets()) ? itr.hasNext() : false }

    @Override
    Basket<Transfac> next() { (fillBaskets()) ? itr.next() : null }

    private boolean fillBaskets() {
        if (itr == null) {
            try {
                int curGene = -1
                Statement state
                ResultSet rs
                def basket = null
                baskets = new ArrayList<Basket<Transfac>>()

                state = conn.createStatement()
                rs = state.executeQuery(FILL_BASKETS)

                while (rs.next()) {
                    if (basket == null || curGene != rs.getInt(1)) {<commit_msg>Reinitialize MySQL connection if it's been closed<commit_after>package me.therin.brovine

import me.therin.brovine.Settings
import me.therin.mining.itemsets.data.BasketIterator
import me.therin.mining.itemsets.data.Basket
import java.sql.ResultSet
import java.sql.Statement
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException

/**
 * TransfacBaskets.java
 *
 * @author tcirwin
 * @date 03 09, 2013
 */
class TransfacBaskets implements BasketIterator<Transfac> {
    private Connection conn
    private static final String UNIQUE_ITEMS =
"""
SELECT DISTINCT(tf_cart) as matchid, transfac, g.cnt
FROM apriori_staging
INNER JOIN factor_matches ON (tf_cart = matchid)
INNER JOIN (select tf, cnt from (
select tf_cart as tf, count(*) as cnt
from apriori_staging
group by tf_cart
) f) g ON (tf = tf_cart)
ORDER BY matchid;
"""

    private static final String FILL_BASKETS =
"""
SELECT geneid, tf_cart, transfac
FROM (select tf_cart
from (
select tf_cart, count(*) as cnt
from apriori_staging
group by tf_cart
) f) g
INNER JOIN apriori_staging USING (tf_cart)
INNER JOIN factor_matches ON (tf_cart = matchid)
ORDER BY geneid, tf_cart
"""

    private static final String NUM_BASKETS =
"""
SELECT COUNT(DISTINCT geneid)
FROM apriori_staging
"""

    private List<Basket<Transfac>> baskets
    private Iterator<Basket<Transfac>> itr

    public TransfacBaskets() {
        initConnection()
        fillBaskets()
    }

    private void initConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance()

            conn =
                DriverManager.getConnection("jdbc:mysql://localhost/brovine?" +
                        "user=" + Settings.USER + "&password=" + Settings.PASS);
        }
        catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception ex) {
            ex.printStackTrace()
        }
    }

    @Override
    List<Transfac> getUniqueItems() {
        try {
            Statement state
            ResultSet rs
            List<Transfac> tfs = new ArrayList<Transfac>()

            if (conn.isClosed())
                initConnection()

            state = conn.createStatement()
            rs = state.executeQuery(UNIQUE_ITEMS)

            while (rs.next()) {
                tfs.add(new Transfac(rs.getInt(1), rs.getString(2)))
            }

            return tfs
        }
        catch (SQLException e) { e.printStackTrace() }
    }

    @Override
    //TODO: implement includeOnly
    BasketIterator<Transfac> includeOnly(List<Basket<Transfac>> baskets) {
        return null
    }

    @Override
    //TODO: implement update
    boolean update() {
        return false
    }

    @Override
    boolean reset() {
        if (fillBaskets()) {
            itr = baskets.iterator()
            return true
        }

        return false
    }

    @Override
    int size() {
        try {
            Statement state
            ResultSet rs

            if (conn.isClosed())
                initConnection()

            state = conn.createStatement()
            rs = state.executeQuery(NUM_BASKETS)

            rs.next() ? rs.getInt(1) : 0
        }
        catch (SQLException e) { e.printStackTrace() }
    }

    @Override
    boolean hasNext() { (fillBaskets()) ? itr.hasNext() : false }

    @Override
    Basket<Transfac> next() { (fillBaskets()) ? itr.next() : null }

    private boolean fillBaskets() {
        if (itr == null) {
            try {
                int curGene = -1
                Statement state
                ResultSet rs
                def basket = null
                baskets = new ArrayList<Basket<Transfac>>()

                if (conn.isClosed())
                    initConnection()

                state = conn.createStatement()
                rs = state.executeQuery(FILL_BASKETS)

                while (rs.next()) {
                    if (basket == null || curGene != rs.getInt(1)) {
{'fraction_non_alphanumeric': 0.10300911474591085, 'fraction_numerical': 0.0034960669247097015, 'mean_word_length': 2.0855161787365177, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '6806001', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2310, 'n_words': 573}