<commit_before>package org.intermine.objectstore.intermine;

/*
 * Copyright (C) 2002-2009 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.intermine.metadata.AttributeDescriptor;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.CollectionDescriptor;
import org.intermine.metadata.FieldDescriptor;
import org.intermine.metadata.ReferenceDescriptor;
import org.intermine.model.InterMineObject;
import org.intermine.modelproduction.MetadataManager;
import org.intermine.objectstore.ObjectStoreException;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.BAGID_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.BAGVAL_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOB_TABLE_NAME;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOBID_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOBPAGE_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOBVAL_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.INT_BAG_TABLE_NAME;
import org.intermine.sql.DatabaseUtil;

import org.apache.log4j.Logger;

/**
 * Map InterMine metadata to a Torque database schema in InterMine format
 *
 * @author <NAME>
 * @author <NAME>
 */
public class TorqueModelOutput
{
    private static final Logger LOG = Logger.getLogger(TorqueModelOutput.class);

    /** The version number of the database format */
    public static final int FORMAT_VERSION = 1;
    protected static final String INDENT = "    ";
    protected static final String ENDL = System.getProperty("line.separator");

    protected DatabaseSchema schema;
    protected File file;
    protected Set indirections = new HashSet();
    protected String className = "";
    protected Set columns = new HashSet();

    private static final String LONG_VAR_BINARY_TYPE = "LONGVARBINARY";

    /**
     * Constructor for this class
     *
     * @param schema a DatabaseSchema
     * @param file the file to which the torque data should be written
     */
    public TorqueModelOutput(DatabaseSchema schema, File file) {
        this.schema = schema;
        this.file = file;
    }

    /**
     * Process the schema and put the output in the file.
     */
    public void process() {
        LOG.info("Generating " + file.getPath());
        BufferedWriter fos = null;
        try {
            fos = new BufferedWriter(new FileWriter(file, false));
            fos.write(generate());
        } catch (IOException e) {
            LOG.error("Failed to output torque data to file " + file.getPath());
        } catch (ObjectStoreException e) {
            LOG.error("Schema invalid");
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                LOG.warn("Failed to close file " + file.getPath());
            }
        }
    }

    /**
     * Generate a string that contains the torque data for the given schema.
     *
     * @return a String containing torque data
     * @throws ObjectStoreException if the schema is invalid
     */
    protected String generate() throws ObjectStoreException {<commit_msg>Don't swallow exception while outputting torque<commit_after>package org.intermine.objectstore.intermine;

/*
 * Copyright (C) 2002-2009 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.intermine.metadata.AttributeDescriptor;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.CollectionDescriptor;
import org.intermine.metadata.FieldDescriptor;
import org.intermine.metadata.ReferenceDescriptor;
import org.intermine.model.InterMineObject;
import org.intermine.modelproduction.MetadataManager;
import org.intermine.objectstore.ObjectStoreException;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.BAGID_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.BAGVAL_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOB_TABLE_NAME;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOBID_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOBPAGE_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.CLOBVAL_COLUMN;
import static org.intermine.objectstore.intermine.ObjectStoreInterMineImpl.INT_BAG_TABLE_NAME;
import org.intermine.sql.DatabaseUtil;

import org.apache.log4j.Logger;

/**
 * Map InterMine metadata to a Torque database schema in InterMine format
 *
 * @author <NAME>
 * @author <NAME>
 */
public class TorqueModelOutput
{
    private static final Logger LOG = Logger.getLogger(TorqueModelOutput.class);

    /** The version number of the database format */
    public static final int FORMAT_VERSION = 1;
    protected static final String INDENT = "    ";
    protected static final String ENDL = System.getProperty("line.separator");

    protected DatabaseSchema schema;
    protected File file;
    protected Set indirections = new HashSet();
    protected String className = "";
    protected Set columns = new HashSet();

    private static final String LONG_VAR_BINARY_TYPE = "LONGVARBINARY";

    /**
     * Constructor for this class
     *
     * @param schema a DatabaseSchema
     * @param file the file to which the torque data should be written
     */
    public TorqueModelOutput(DatabaseSchema schema, File file) {
        this.schema = schema;
        this.file = file;
    }

    /**
     * Process the schema and put the output in the file.
     */
    public void process() {
        LOG.info("Generating " + file.getPath());
        BufferedWriter fos = null;
        try {
            fos = new BufferedWriter(new FileWriter(file, false));
            fos.write(generate());
        } catch (IOException e) {
            LOG.error("Failed to output torque data to file " + file.getPath());
            throw new RuntimeException("Failed to output torque data to file " + file.getPath());
        } catch (ObjectStoreException e) {
            LOG.error("Schema invalid while writing to file " + file.getPath(), e);
            throw new RuntimeException("Schema invalid while writing to file " + file.getPath(),
                    e);
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                LOG.warn("Failed to close file " + file.getPath());
            }
        }
    }

    /**
     * Generate a string that contains the torque data for the given schema.
     *
     * @return a String containing torque data
     * @throws ObjectStoreException if the schema is invalid
     */
    protected String generate() throws ObjectStoreException {
{'fraction_non_alphanumeric': 0.08610895946294862, 'fraction_numerical': 0.002581977794990963, 'mean_word_length': 3.263621353880022, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1570360', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2086, 'n_words': 595}