com.fasterxml.sort
Class Sorter<T>

java.lang.Object
  extended by com.fasterxml.sort.Sorter<T>
All Implemented Interfaces:
SortingState
Direct Known Subclasses:
TextFileSorter

public class Sorter<T>
extends Object
implements SortingState

Main entry point for sorting functionality; object that drives the sorting process from pre-sort to final output. Instances are not thread-safe, although they are reusable. Since the cost of creating new instances is trivial, there is usally no benefit from reusing instances, other than possible convenience.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.fasterxml.sort.SortingState
SortingState.Phase
 
Field Summary
protected  Exception _cancelForException
           
protected  AtomicBoolean _cancelRequest
           
protected  Comparator<T> _comparator
           
protected  SortConfig _config
           
protected  int _currentSortRound
           
protected  SortingState.Phase _phase
           
protected  int _presortFileCount
           
protected  DataReaderFactory<T> _readerFactory
          Factory used for reading intermediate sorted files.
protected  int _sortRoundCount
           
protected  DataWriterFactory<T> _writerFactory
          Factory used for writing intermediate sorted files.
 
Constructor Summary
Sorter(SortConfig config, DataReaderFactory<T> readerFactory, DataWriterFactory<T> writerFactory, Comparator<T> comparator)
           
 
Method Summary
protected static int _calculateRoundCount(int files, int mergeFactor)
           
protected  boolean _checkForCancel()
           
protected  boolean _checkForCancel(Collection<File> tmpFilesToDelete)
           
protected  File _merge(List<File> inputs)
           
protected  void _merge(List<File> inputs, DataWriter<T> writer)
           
protected  Comparator<Object> _rawComparator()
           
protected  void _writeAll(DataWriter<T> resultWriter, Object[] items)
           
protected  File _writePresorted(Object[] items)
           
 void cancel()
          Method that can be used to try to cancel executing sort operation.
 void cancel(IOException e)
          Method that can be used to try to cancel executing sort operation.
 void cancel(RuntimeException e)
          Method that can be used to try to cancel executing sort operation.
 int getNumberOfPreSortFiles()
          Accessor for checking how many pre-sort files were created during pre-sort phase.
 int getNumberOfSortRounds()
          Accessor for figuring out how many regular sorting rounds need to be taken to complete sorting, if known.
 SortingState.Phase getPhase()
           
 int getSortRound()
          Accessor for checking which sorting round sorter is doing: for pre-sort it basically means number of segment (0-based) that is being processed in-memory, for regular sort it is number of (0-based) sorting round.
 boolean isCompleted()
          Accessor for determining whether sorting has been succesfully completed or not.
 boolean isPreSorting()
          Accessor for determining whether sorter is in its in-memory pre-sorting phase.
 boolean isSorting()
          Accessor for determining whether sorter is in regular merge-sort phase or not.
protected  void merge(List<File> presorted, DataWriter<T> resultWriter)
          Main-level merge method called during once during sorting.
protected  List<File> presort(DataReader<T> inputReader, SegmentedBuffer buffer, Object[] firstSortedBatch, T nextValue)
           
 boolean sort(DataReader<T> inputReader, DataWriter<T> resultWriter)
          Method that will perform full sort on input data read using given DataReader, and written out using specified DataWriter.
 void sort(InputStream source, OutputStream destination)
          Method that will perform full sort on specified input, writing results into specified destination.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_config

protected final SortConfig _config

_readerFactory

protected final DataReaderFactory<T> _readerFactory
Factory used for reading intermediate sorted files.


_writerFactory

protected final DataWriterFactory<T> _writerFactory
Factory used for writing intermediate sorted files.


_comparator

protected final Comparator<T> _comparator

_phase

protected SortingState.Phase _phase

_presortFileCount

protected int _presortFileCount

_sortRoundCount

protected int _sortRoundCount

_currentSortRound

protected int _currentSortRound

_cancelRequest

protected final AtomicBoolean _cancelRequest

_cancelForException

protected Exception _cancelForException
Constructor Detail

Sorter

public Sorter(SortConfig config,
              DataReaderFactory<T> readerFactory,
              DataWriterFactory<T> writerFactory,
              Comparator<T> comparator)
Parameters:
config - Configuration for the sorter
readerFactory - Factory used for creating readers for pre-sorted data; as well as for input if an InputStream is passed as source
writerFactory - Factory used for creating writers for storing pre-sorted data; as well as for results if an OutputStream is passed as destination.
Method Detail

cancel

public void cancel()
Description copied from interface: SortingState
Method that can be used to try to cancel executing sort operation. No exception will be thrown; sorting will just be stopped as soon as sorting thread notices request.

Specified by:
cancel in interface SortingState

cancel

public void cancel(RuntimeException e)
Description copied from interface: SortingState
Method that can be used to try to cancel executing sort operation. Exception object can be specified; if non-null instance is given, it will be thrown to indicate erroneous result, otherwise sorting is just interrupted but execution returns normally.

Specified by:
cancel in interface SortingState

cancel

public void cancel(IOException e)
Description copied from interface: SortingState
Method that can be used to try to cancel executing sort operation. Exception object can be specified; if non-null instance is given, it will be thrown to indicate erroneous result, otherwise sorting is just interrupted but execution returns normally.

Specified by:
cancel in interface SortingState

getPhase

public SortingState.Phase getPhase()
Specified by:
getPhase in interface SortingState

getNumberOfSortRounds

public int getNumberOfSortRounds()
Description copied from interface: SortingState
Accessor for figuring out how many regular sorting rounds need to be taken to complete sorting, if known. If information is not known, will return -1. This information generally becomes available after pre-sorting round.

Specified by:
getNumberOfSortRounds in interface SortingState

getNumberOfPreSortFiles

public int getNumberOfPreSortFiles()
Description copied from interface: SortingState
Accessor for checking how many pre-sort files were created during pre-sort phase. Can be zero if the whole data fit in memory during pre-sorting.

Specified by:
getNumberOfPreSortFiles in interface SortingState

getSortRound

public int getSortRound()
Description copied from interface: SortingState
Accessor for checking which sorting round sorter is doing: for pre-sort it basically means number of segment (0-based) that is being processed in-memory, for regular sort it is number of (0-based) sorting round.

Specified by:
getSortRound in interface SortingState

isCompleted

public boolean isCompleted()
Description copied from interface: SortingState
Accessor for determining whether sorting has been succesfully completed or not.

Specified by:
isCompleted in interface SortingState

isPreSorting

public boolean isPreSorting()
Description copied from interface: SortingState
Accessor for determining whether sorter is in its in-memory pre-sorting phase.

Specified by:
isPreSorting in interface SortingState

isSorting

public boolean isSorting()
Description copied from interface: SortingState
Accessor for determining whether sorter is in regular merge-sort phase or not.

Specified by:
isSorting in interface SortingState

sort

public void sort(InputStream source,
                 OutputStream destination)
          throws IOException
Method that will perform full sort on specified input, writing results into specified destination. Data conversions needed are done using DataReaderFactory and DataWriterFactory configured for this sorter.

Throws:
IOException

sort

public boolean sort(DataReader<T> inputReader,
                    DataWriter<T> resultWriter)
             throws IOException
Method that will perform full sort on input data read using given DataReader, and written out using specified DataWriter. Conversions to and from intermediate sort files is done using DataReaderFactory and DataWriterFactory configured for this sorter.

Returns:
true if sorting completed succesfully; false if it was cancelled
Throws:
IOException

presort

protected List<File> presort(DataReader<T> inputReader,
                             SegmentedBuffer buffer,
                             Object[] firstSortedBatch,
                             T nextValue)
                      throws IOException
Throws:
IOException

_writePresorted

protected File _writePresorted(Object[] items)
                        throws IOException
Throws:
IOException

merge

protected void merge(List<File> presorted,
                     DataWriter<T> resultWriter)
              throws IOException
Main-level merge method called during once during sorting.

Throws:
IOException

_writeAll

protected void _writeAll(DataWriter<T> resultWriter,
                         Object[] items)
                  throws IOException
Throws:
IOException

_merge

protected File _merge(List<File> inputs)
               throws IOException
Throws:
IOException

_merge

protected void _merge(List<File> inputs,
                      DataWriter<T> writer)
               throws IOException
Throws:
IOException

_calculateRoundCount

protected static int _calculateRoundCount(int files,
                                          int mergeFactor)

_checkForCancel

protected boolean _checkForCancel()
                           throws IOException
Throws:
IOException

_checkForCancel

protected boolean _checkForCancel(Collection<File> tmpFilesToDelete)
                           throws IOException
Throws:
IOException

_rawComparator

protected Comparator<Object> _rawComparator()


Copyright © 2011. All Rights Reserved.