T - the type of element to be sortedpublic class ExternalSort<T> extends Object
add(Object, long) to add each element. Then call get()
the same number of times to get all the elements in order.
This class assumes at least 140MB of memory is free prior to sorting, and will aim to maintain at least 70MB of free memory at all times during the sort. We use 70MB because
Geneious warns about running low on memory at 50MB so we don't want to go below that. Only once free memory drops below 70MB does
the ExternalSort resort to using temporary files. If free memory does not drop below this, then the sort will be performed entirely in memory.| Modifier and Type | Class and Description |
|---|---|
static interface |
ExternalSort.Serializer<T>
Specifies how to serialize the elements to disk and read them back in again.
|
| Constructor and Description |
|---|
ExternalSort(ExternalSort.Serializer<T> serializer,
Comparator<T> comparator,
ProgressListener progressListener,
int maximumNumberOfElementsToSort) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(T element,
long upperBoundOnMemoryRequiredByElement)
Adds a new element to sort
|
void |
cleanup()
If the calling code decides not to call
get() for all sequences added, then it should call this method
to close and delete all temporary files. |
T |
get()
Gets the next sorted element
|
boolean |
isAllInMemory()
Returns true if all this data is in memory (i.e.
|
void |
setReturnElementsTwice()
Flags this sorter to return all the elements twice.
|
public ExternalSort(ExternalSort.Serializer<T> serializer, Comparator<T> comparator, ProgressListener progressListener, int maximumNumberOfElementsToSort)
serializer - specifies how to serialize the objects to be sortedcomparator - for comparing the objects to be sorted.progressListener - a progress listener for reporting progress to and cancelling. Note that calls will be made to this progress listener
as side effects of later calls on this sorter. If the progress listener requests a cancel during one of these later calls, they will throw an IOException with "Canceled" as the message. Since calls may be made later
to this progress listener to avoid progress interfering with later calls, it is strongly recommended you create split progress using ProgressListener.createSplitProgress(jebl.util.ProgressListener, int)maximumNumberOfElementsToSort - an upper bound number of elements to be sorted, or -1 if unknown. Used only for progress reporting purposes.public void setReturnElementsTwice()
get() has been called once per element,
get() may be called that many times again retrieve all the elements again.public void add(T element, long upperBoundOnMemoryRequiredByElement) throws IOException
element - the new elementupperBoundOnMemoryRequiredByElement - an upper bounds on the memory used by this object and any other data stored in memory since the previous call to this method. This estimate is used to determine how frequently to check free memory and resort to sorting on disk.IOException - if there is a problem writing to disk.public void cleanup()
throws IOException
get() for all sequences added, then it should call this method
to close and delete all temporary files. Normally, get() automatically cleans up temporary files when they are done with.IOException - if files can't be closed or deletedpublic boolean isAllInMemory()
throws IOException
IOException - if we can't flush the final block to disk if necessarypublic T get() throws IOException
IOException - if there is a problem reading from disk