private void init() {
        if (databasePath.toFile().exists()) {
            // we have to initialize chunk size and the map of chunks
            // and return
            initChunkMap();
            int chunkSize = readChunkSize();
            assert(this.chunkSize == chunkSize);
            if (chunkSize == -1) {
                throw new InternalError("Impossible to read chunk size from the database '" + databasePath + "'");
            }
            this.chunkSize = chunkSize;
            logger.debug("Database was initialized previously. Nothing to do.");
            return;
        }

        Statement stmt = null;
        Connection connection = null;

        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:" + databasePath);

            // Create tables
            stmt = connection.createStatement();
            String sql = "CREATE TABLE chunk "
                    + "(id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "chunk_id VARCHAR NOT NULL,"
                    + "chromosome VARCHAR NOT NULL, "
                    + "start INTEGER NOT NULL, "
                    + "end INTEGER NOT NULL); "
                    + "CREATE UNIQUE INDEX chunk_id_idx ON chunk (chunk_id);"
                    + "CREATE INDEX chrom_start_end_idx ON chunk (chromosome, start, end);";
            stmt.executeUpdate(sql);

            sql = "CREATE TABLE info "
                    + "(id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "name VARCHAR NOT NULL,"
                    + "value VARCHAR NOT NULL);"
                    + "CREATE UNIQUE INDEX name_idx ON info (name);";
            stmt.executeUpdate(sql);

            sql = "insert into info (name, value) values ('chunkSize', '" + chunkSize + "');";
            stmt.executeUpdate(sql);

            sql = "CREATE TABLE file "
                    + "(id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "path VARCHAR NOT NULL,"
                    + "name VARCHAR NOT NULL);"
                    + "CREATE UNIQUE INDEX path_idx ON file (path);";
            stmt.executeUpdate(sql);

            sql = "CREATE TABLE mean_coverage "
                    + "(chunk_id INTEGER,"
                    + "file_id INTEGER,"
                    + "v1 INTEGER, "
                    + "v2 INTEGER, "
                    + "v3 INTEGER, "
                    + "v4 INTEGER, "
                    + "v5 INTEGER, "
                    + "v6 INTEGER, "
                    + "v7 INTEGER, "
                    + "v8 INTEGER,"
                    + "PRIMARY KEY(chunk_id, file_id));";
            stmt.executeUpdate(sql);

            // close both resources
            stmt.close();
            connection.close();
        } catch (SQLException e) {
            logger.error(e.getClass().getName() + ": " + e.getMessage());
            cleanConnectionClose(connection, stmt);
        } catch (ClassNotFoundException e) {
            logger.error(e.getClass().getName() + ": " + e.getMessage());
            e.printStackTrace();
        }

        logger.debug("Initialized database successfully");
    }
private void init ( ) { if ( databasePath . toFile ( ) . exists ( ) ) { initChunkMap ( ) ; int chunkSize = readChunkSize ( ) ; assert ( this . chunkSize == chunkSize ) ; if ( chunkSize == - 1 ) { throw new InternalError ( "Impossible to read chunk size from the database '" + databasePath + "'" ) ; } this . chunkSize = chunkSize ; logger . debug ( "Database was initialized previously. Nothing to do." ) ; return ; } Statement stmt = null ; Connection connection = null ; try { Class . forName ( "org.sqlite.JDBC" ) ; connection = DriverManager . getConnection ( "jdbc:sqlite:" + databasePath ) ; stmt = connection . createStatement ( ) ; String sql = "CREATE TABLE chunk " + "(id INTEGER PRIMARY KEY AUTOINCREMENT," + "chunk_id VARCHAR NOT NULL," + "chromosome VARCHAR NOT NULL, " + "start INTEGER NOT NULL, " + "end INTEGER NOT NULL); " + "CREATE UNIQUE INDEX chunk_id_idx ON chunk (chunk_id);" + "CREATE INDEX chrom_start_end_idx ON chunk (chromosome, start, end);" ; stmt . executeUpdate ( sql ) ; sql = "CREATE TABLE info " + "(id INTEGER PRIMARY KEY AUTOINCREMENT," + "name VARCHAR NOT NULL," + "value VARCHAR NOT NULL);" + "CREATE UNIQUE INDEX name_idx ON info (name);" ; stmt . executeUpdate ( sql ) ; sql = "insert into info (name, value) values ('chunkSize', '" + chunkSize + "');" ; stmt . executeUpdate ( sql ) ; sql = "CREATE TABLE file " + "(id INTEGER PRIMARY KEY AUTOINCREMENT," + "path VARCHAR NOT NULL," + "name VARCHAR NOT NULL);" + "CREATE UNIQUE INDEX path_idx ON file (path);" ; stmt . executeUpdate ( sql ) ; sql = "CREATE TABLE mean_coverage " + "(chunk_id INTEGER," + "file_id INTEGER," + "v1 INTEGER, " + "v2 INTEGER, " + "v3 INTEGER, " + "v4 INTEGER, " + "v5 INTEGER, " + "v6 INTEGER, " + "v7 INTEGER, " + "v8 INTEGER," + "PRIMARY KEY(chunk_id, file_id));" ; stmt . executeUpdate ( sql ) ; stmt . close ( ) ; connection . close ( ) ; } catch ( SQLException e ) { logger . error ( e . getClass ( ) . getName ( ) + ": " + e . getMessage ( ) ) ; cleanConnectionClose ( connection , stmt ) ; } catch ( ClassNotFoundException e ) { logger . error ( e . getClass ( ) . getName ( ) + ": " + e . getMessage ( ) ) ; e . printStackTrace ( ) ; } logger . debug ( "Initialized database successfully" ) ; }
Initialize database creating tables.