<commit_before>/**
 * Copyright (c) 2002-2011 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.ha;

import java.io.File;
import java.io.PrintStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Ignore;
import org.neo4j.kernel.HighlyAvailableGraphDatabase;
import org.neo4j.kernel.ha.AbstractBroker;
import org.neo4j.kernel.ha.Broker;
import org.neo4j.kernel.ha.CommunicationProtocol;
import org.neo4j.kernel.ha.FakeMasterBroker;
import org.neo4j.kernel.ha.FakeSlaveBroker;
import org.neo4j.kernel.ha.MasterClient;
import org.neo4j.kernel.ha.zookeeper.ZooKeeperException;
import org.neo4j.management.HighAvailability;
import org.neo4j.test.SubProcess;

import slavetest.Job;

@Ignore
public class StandaloneDatabase
{
    private final Controller process;

    public static StandaloneDatabase withDefaultBroker( String testMethodName, File path,
            int machineId, final LocalhostZooKeeperCluster zooKeeper, String haServer,
            String[] extraArgs )
    {
        return new StandaloneDatabase( testMethodName, new Bootstrap( path, machineId,//
                HighlyAvailableGraphDatabase.CONFIG_KEY_HA_SERVER, haServer,//
                HighlyAvailableGraphDatabase.CONFIG_KEY_HA_ZOO_KEEPER_SERVERS,
                zooKeeper.getConnectionString() )
        {
            @Override
            HighlyAvailableGraphDatabase start( String storeDir, Map<String, String> config )
            {
                HighlyAvailableGraphDatabase db = new HighlyAvailableGraphDatabase( storeDir,
                        config );
                System.out.println( "Started HA db (w/ zoo keeper)" );
                return db;
            }
        } )
        {
            @Override
            IllegalStateException format( StartupFailureException e )
            {
                return e.format( zooKeeper );
            }
        };
    }

    public static StandaloneDatabase withFakeBroker( String testMethodName, File path,
            int machineId, final int masterId, String[] extraArgs )
    {
        StandaloneDatabase standalone = new StandaloneDatabase( testMethodName, new Bootstrap(
                path, machineId )
        {
            @Override
            HighlyAvailableGraphDatabase start( String storeDir, Map<String, String> config )
            {
                final Broker broker;
                if ( machineId == masterId )
                {
                    broker = new FakeMasterBroker( machineId, storeDir );
                }
                else
                {
                    broker = new FakeSlaveBroker( new MasterClient( "localhost",
                            CommunicationProtocol.PORT, storeDir ), masterId, machineId, storeDir );
                }
                HighlyAvailableGraphDatabase db = new HighlyAvailableGraphDatabase( storeDir,
                        config, AbstractBroker.wrapSingleBroker( broker ) );
                System.out.println( "Started HA db (w/o zoo keeper)" );
                return db;
            }
        } );
        standalone.awaitStarted();
        return standalone;
    }

    private StandaloneDatabase( String testMethodName, Bootstrap bootstrap )
    {
        process = new HaDbProcess( testMethodName ).start( bootstrap );
    }

    @Override
    public String toString()
    {
        return getClass().getSimpleName() + process;
    }

    public void awaitStarted()
    {
        try
        {
            process.awaitStarted();
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    public <T> T executeJob( Job<T> job ) throws Exception
    {
        try
        {
            return process.executeJob( job );
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    public int getMachineId()
    {
        try
        {
            return process.getMachineId();
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    public void pullUpdates()
    {
        try
        {
            process.pullUpdates();
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    IllegalStateException format( StartupFailureException e )
    {
        throw new IllegalStateException( "database failed to start", e.getCause() );
    }

    public void shutdown()
    {
        SubProcess.stop( process );
    }

    // <IMPLEMENTATION>

    public interface Controller
    {
        void pullUpdates() throws StartupFailureException;

        void awaitStarted() throws StartupFailureException;

        int getMachineId() throws StartupFailureException;

        <T> T executeJob( Job<T> job ) throws Exception;
    }

    private static class StartupFailureException extends Exception
    {
        StartupFailureException( Throwable cause )
        {
            super( cause );<commit_msg>Made exception public to allow reflective use.<commit_after>/**
 * Copyright (c) 2002-2011 "<NAME>,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.ha;

import java.io.File;
import java.io.PrintStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Ignore;
import org.neo4j.kernel.HighlyAvailableGraphDatabase;
import org.neo4j.kernel.ha.AbstractBroker;
import org.neo4j.kernel.ha.Broker;
import org.neo4j.kernel.ha.CommunicationProtocol;
import org.neo4j.kernel.ha.FakeMasterBroker;
import org.neo4j.kernel.ha.FakeSlaveBroker;
import org.neo4j.kernel.ha.MasterClient;
import org.neo4j.kernel.ha.zookeeper.ZooKeeperException;
import org.neo4j.management.HighAvailability;
import org.neo4j.test.SubProcess;

import slavetest.Job;

@Ignore
public class StandaloneDatabase
{
    private final Controller process;

    public static StandaloneDatabase withDefaultBroker( String testMethodName, File path,
            int machineId, final LocalhostZooKeeperCluster zooKeeper, String haServer,
            String[] extraArgs )
    {
        return new StandaloneDatabase( testMethodName, new Bootstrap( path, machineId,//
                HighlyAvailableGraphDatabase.CONFIG_KEY_HA_SERVER, haServer,//
                HighlyAvailableGraphDatabase.CONFIG_KEY_HA_ZOO_KEEPER_SERVERS,
                zooKeeper.getConnectionString() )
        {
            @Override
            HighlyAvailableGraphDatabase start( String storeDir, Map<String, String> config )
            {
                HighlyAvailableGraphDatabase db = new HighlyAvailableGraphDatabase( storeDir,
                        config );
                System.out.println( "Started HA db (w/ zoo keeper)" );
                return db;
            }
        } )
        {
            @Override
            IllegalStateException format( StartupFailureException e )
            {
                return e.format( zooKeeper );
            }
        };
    }

    public static StandaloneDatabase withFakeBroker( String testMethodName, File path,
            int machineId, final int masterId, String[] extraArgs )
    {
        StandaloneDatabase standalone = new StandaloneDatabase( testMethodName, new Bootstrap(
                path, machineId )
        {
            @Override
            HighlyAvailableGraphDatabase start( String storeDir, Map<String, String> config )
            {
                final Broker broker;
                if ( machineId == masterId )
                {
                    broker = new FakeMasterBroker( machineId, storeDir );
                }
                else
                {
                    broker = new FakeSlaveBroker( new MasterClient( "localhost",
                            CommunicationProtocol.PORT, storeDir ), masterId, machineId, storeDir );
                }
                HighlyAvailableGraphDatabase db = new HighlyAvailableGraphDatabase( storeDir,
                        config, AbstractBroker.wrapSingleBroker( broker ) );
                System.out.println( "Started HA db (w/o zoo keeper)" );
                return db;
            }
        } );
        standalone.awaitStarted();
        return standalone;
    }

    private StandaloneDatabase( String testMethodName, Bootstrap bootstrap )
    {
        process = new HaDbProcess( testMethodName ).start( bootstrap );
    }

    @Override
    public String toString()
    {
        return getClass().getSimpleName() + process;
    }

    public void awaitStarted()
    {
        try
        {
            process.awaitStarted();
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    public <T> T executeJob( Job<T> job ) throws Exception
    {
        try
        {
            return process.executeJob( job );
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    public int getMachineId()
    {
        try
        {
            return process.getMachineId();
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    public void pullUpdates()
    {
        try
        {
            process.pullUpdates();
        }
        catch ( StartupFailureException e )
        {
            throw format( e );
        }
    }

    IllegalStateException format( StartupFailureException e )
    {
        throw new IllegalStateException( "database failed to start", e.getCause() );
    }

    public void shutdown()
    {
        SubProcess.stop( process );
    }

    // <IMPLEMENTATION>

    public interface Controller
    {
        void pullUpdates() throws StartupFailureException;

        void awaitStarted() throws StartupFailureException;

        int getMachineId() throws StartupFailureException;

        <T> T executeJob( Job<T> job ) throws Exception;
    }

    public static class StartupFailureException extends Exception
    {
        StartupFailureException( Throwable cause )
        {
            super( cause );
{'fraction_non_alphanumeric': 0.07571868583162218, 'fraction_numerical': 0.0037645448323066393, 'mean_word_length': 2.081729501713683, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '4891635', 'n_tokens_mistral': 3126, 'n_tokens_neox': 2995, 'n_words': 861}