Package com.carrotsearch.junitbenchmarks.h2

Source Code of com.carrotsearch.junitbenchmarks.h2.H2Consumer

package com.carrotsearch.junitbenchmarks.h2;

import com.carrotsearch.junitbenchmarks.BenchmarkOptionsSystemProperties;
import com.carrotsearch.junitbenchmarks.db.DbConsumer;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import org.h2.jdbcx.JdbcDataSource;

/**
* {@link DbConsumer} implementation for H2.
*/
public final class H2Consumer extends DbConsumer
{

    /**
     * The database file name.
     */
    private File dbFileName;

    /**
     * Creates a consumer with the default file name.
     */
    public H2Consumer()
    {
        this(getDefaultDbName());
    }

    /**
     * Creates a consumer with the default charts and custom key dirs.
     *
     * @param dbFileName the database file name
     */
    public H2Consumer(File dbFileName)
    {
        this(dbFileName, getDefaultChartsDir(), getDefaultCustomKey());
    }

    /**
     * Creates a consumer with the specified database file, charts directory,
     * and custom key value.
     *
     * @param dbFileName the database file
     * @param chartsDir the charts directory
     * @param customKeyValue the custom key value
     */
    public H2Consumer(File dbFileName, File chartsDir, String customKeyValue)
    {
        super(chartsDir, customKeyValue);
        this.dbFileName = dbFileName;
        try
        {
            checkSchema();
        }
        catch (SQLException e)
        {
            throw new RuntimeException("Cannot initialize H2 database.", e);
        }
    }

    /**
     * Return the global default DB name.
     */
    private static File getDefaultDbName()
    {
        final String dbPath = System.getProperty(BenchmarkOptionsSystemProperties.DB_FILE_PROPERTY);
        if (dbPath != null && !dbPath.trim().equals(""))
        {
            return new File(dbPath);
        }
        throw new IllegalArgumentException("Missing global property: "
                + BenchmarkOptionsSystemProperties.DB_FILE_PROPERTY);
    }

    @Override
    protected Connection createConnection() throws SQLException
    {
        final JdbcDataSource ds = new org.h2.jdbcx.JdbcDataSource();
        ds.setURL("jdbc:h2:" + dbFileName.getAbsolutePath() + ";DB_CLOSE_ON_EXIT=FALSE");
        ds.setUser("sa");
        Connection results = ds.getConnection();
        results.setAutoCommit(false);
        return results;
    }

    @Override
    public String getMethodChartResultsQuery()
    {
        return getResource(H2Consumer.class, "method-chart-results.sql");
    }

    @Override
    public String getMethodChartPropertiesQuery()
    {
        return getResource(H2Consumer.class, "method-chart-properties.sql");
    }

    @Override
    protected String getCreateRunsSql()
    {
        return getResource(H2Consumer.class, "000-create-runs.sql");
    }

    @Override
    protected String getCreateTestsSql()
    {
        return getResource(H2Consumer.class, "001-create-tests.sql");
    }

    @Override
    protected String getNewRunSql()
    {
        return getResource(H2Consumer.class, "002-new-run.sql");
    }

    @Override
    protected String getTestInsertSql()
    {
        return getResource(H2Consumer.class, "003-new-result.sql");
    }

    @Override
    protected String getCreateDbVersionSql()
    {
        return getResource(H2Consumer.class, "004-create-dbversion.sql");
    }

    @Override
    protected String getAddCustomKeySql()
    {
        return getResource(H2Consumer.class, "005-add-custom-key.sql");
    }
}
TOP

Related Classes of com.carrotsearch.junitbenchmarks.h2.H2Consumer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.