mavibot/src/main/java/org/apache/directory/mavibot/btree/memory/BulkDataSorter.java [46:179]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class BulkDataSorter<K, V>
{
    private File workDir;

    private int splitAfter = 1000;

    private Comparator<Tuple<K, V>> tupleComparator;

    private TupleReaderWriter<K, V> readerWriter;

    private boolean sorted;


    public BulkDataSorter( TupleReaderWriter<K, V> readerWriter, Comparator<Tuple<K, V>> tupleComparator,
        int splitAfter )
    {
        if ( splitAfter <= 0 )
        {
            throw new IllegalArgumentException( "Value of splitAfter parameter cannot be null" );
        }

        this.splitAfter = splitAfter;

        this.workDir = new File( System.getProperty( "java.io.tmpdir" ), System.currentTimeMillis() + "-sort" );
        workDir.mkdir();

        this.readerWriter = readerWriter;
        this.tupleComparator = tupleComparator;
    }


    public void sort( File dataFile ) throws IOException
    {
        int i = 0;

        Tuple<K, V>[] arr = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, splitAfter );

        Tuple<K, V> t = null;

        DataInputStream in = new DataInputStream( new FileInputStream( dataFile ) );

        while ( ( t = readerWriter.readUnsortedTuple( in ) ) != null )
        {
            arr[i++] = t;

            if ( ( i % splitAfter ) == 0 )
            {
                i = 0;
                Arrays.sort( arr, tupleComparator );

                storeSortedData( arr );
            }
        }

        if ( i != 0 )
        {
            Tuple<K, V>[] tmp = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, i );
            System.arraycopy( arr, 0, tmp, 0, i );
            Arrays.sort( tmp, tupleComparator );

            storeSortedData( tmp );
        }

        sorted = true;
    }


    private void storeSortedData( Tuple<K, V>[] arr ) throws IOException
    {
        File tempFile = File.createTempFile( UUID.randomUUID().toString(), ".batch", workDir );
        DataOutputStream out = new DataOutputStream( new FileOutputStream( tempFile ) );

        for ( Tuple<K, V> t : arr )
        {
            readerWriter.storeSortedTuple( t, out );
        }

        out.flush();
        out.close();
    }


    public File getWorkDir()
    {
        return workDir;
    }


    public Iterator<Tuple<K, V>> getMergeSortedTuples() throws IOException
    {
        if ( !sorted )
        {
            throw new IllegalStateException( "Data is not sorted" );
        }

        File[] batches = workDir.listFiles();

        if ( batches.length == 0 )
        {
            return Collections.EMPTY_LIST.iterator();
        }

        final DataInputStream[] streams = new DataInputStream[batches.length];

        for ( int i = 0; i < batches.length; i++ )
        {
            streams[i] = new DataInputStream( new FileInputStream( batches[i] ) );
        }

        Iterator<Tuple<K, V>> itr = new Iterator<Tuple<K, V>>()
        {
            private Tuple<K, V>[] heads = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, streams.length );

            private Tuple<K, V> candidate = null;

            private boolean closed;

            private int candidatePos = -1;


            @Override
            public boolean hasNext()
            {

                if ( closed )
                {
                    throw new IllegalStateException( "No elements to read" );
                }

                Tuple<K, V> available = null;

                for ( int i = 0; i < streams.length; i++ )
                {
                    if ( heads[i] == null )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



mavibot/src/main/java/org/apache/directory/mavibot/btree/persisted/BulkDataSorter.java [46:179]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class BulkDataSorter<K, V>
{
    private File workDir;

    private int splitAfter = 1000;

    private Comparator<Tuple<K, V>> tupleComparator;

    private TupleReaderWriter<K, V> readerWriter;

    private boolean sorted;


    public BulkDataSorter( TupleReaderWriter<K, V> readerWriter, Comparator<Tuple<K, V>> tupleComparator,
        int splitAfter )
    {
        if ( splitAfter <= 0 )
        {
            throw new IllegalArgumentException( "Value of splitAfter parameter cannot be null" );
        }

        this.splitAfter = splitAfter;

        this.workDir = new File( System.getProperty( "java.io.tmpdir" ), System.currentTimeMillis() + "-sort" );
        workDir.mkdir();

        this.readerWriter = readerWriter;
        this.tupleComparator = tupleComparator;
    }


    public void sort( File dataFile ) throws IOException
    {
        int i = 0;

        Tuple<K, V>[] arr = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, splitAfter );

        Tuple<K, V> t = null;

        DataInputStream in = new DataInputStream( new FileInputStream( dataFile ) );

        while ( ( t = readerWriter.readUnsortedTuple( in ) ) != null )
        {
            arr[i++] = t;

            if ( ( i % splitAfter ) == 0 )
            {
                i = 0;
                Arrays.sort( arr, tupleComparator );

                storeSortedData( arr );
            }
        }

        if ( i != 0 )
        {
            Tuple<K, V>[] tmp = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, i );
            System.arraycopy( arr, 0, tmp, 0, i );
            Arrays.sort( tmp, tupleComparator );

            storeSortedData( tmp );
        }

        sorted = true;
    }


    private void storeSortedData( Tuple<K, V>[] arr ) throws IOException
    {
        File tempFile = File.createTempFile( UUID.randomUUID().toString(), ".batch", workDir );
        DataOutputStream out = new DataOutputStream( new FileOutputStream( tempFile ) );

        for ( Tuple<K, V> t : arr )
        {
            readerWriter.storeSortedTuple( t, out );
        }

        out.flush();
        out.close();
    }


    public File getWorkDir()
    {
        return workDir;
    }


    public Iterator<Tuple<K, V>> getMergeSortedTuples() throws IOException
    {
        if ( !sorted )
        {
            throw new IllegalStateException( "Data is not sorted" );
        }

        File[] batches = workDir.listFiles();

        if ( batches.length == 0 )
        {
            return Collections.EMPTY_LIST.iterator();
        }

        final DataInputStream[] streams = new DataInputStream[batches.length];

        for ( int i = 0; i < batches.length; i++ )
        {
            streams[i] = new DataInputStream( new FileInputStream( batches[i] ) );
        }

        Iterator<Tuple<K, V>> itr = new Iterator<Tuple<K, V>>()
        {
            private Tuple<K, V>[] heads = ( Tuple<K, V>[] ) Array.newInstance( Tuple.class, streams.length );

            private Tuple<K, V> candidate = null;

            private boolean closed;

            private int candidatePos = -1;


            @Override
            public boolean hasNext()
            {

                if ( closed )
                {
                    throw new IllegalStateException( "No elements to read" );
                }

                Tuple<K, V> available = null;

                for ( int i = 0; i < streams.length; i++ )
                {
                    if ( heads[i] == null )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



