com.amazon.carbonado.cursor
Class MergeSortBuffer<S extends Storable>

java.lang.Object
  extended by java.util.AbstractCollection<S>
      extended by com.amazon.carbonado.cursor.MergeSortBuffer<S>
All Implemented Interfaces:
SortBuffer<S>, Iterable<S>, Collection<S>

public class MergeSortBuffer<S extends Storable>
extends AbstractCollection<S>
implements SortBuffer<S>

Sort buffer implemented via a merge sort algorithm. If there are too many storables to fit in the reserved memory buffer, they are sorted and serialized to temporary files.

The following system properties can be set to change the default performance characteristics of the merge sort. Each property name must be prefixed with "com.amazon.carbonado.cursor.MergeSortBuffer."

 Property            Default    Notes
 ------------------- ---------- ----------------------------------------------
 maxArrayCapacity    8192       Larger value greatly improves performance, but
                                more memory is used for each running sort.

 maxOpenFileCount    100        Larger value may reduce the amount of file
                                merges, but there is an increased risk of
                                running out of file descriptors.

 outputBufferSize    10000      Larger value may improve performance of file
                                writing, but not by much.

 tmpdir                         Merge sort files by default are placed in the
                                Java temp directory. Override to place them
                                somewhere else.
 

Author:
Brian S O'Neill
See Also:
SortedCursor

Constructor Summary
MergeSortBuffer()
           
MergeSortBuffer(Storage<S> storage)
           
MergeSortBuffer(Storage<S> storage, String tempDir)
           
MergeSortBuffer(Storage<S> storage, String tempDir, int maxArrayCapacity)
           
 
Method Summary
 boolean add(S storable)
           
 void clear()
           
 void close()
          Clear and close buffer.
 Iterator<S> iterator()
           
 void prepare(Comparator<S> comparator)
          Clears buffer and assigns a comparator for sorting.
 int size()
           
 void sort()
          Finish sorting buffer.
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

MergeSortBuffer

public MergeSortBuffer()
Since:
1.2

MergeSortBuffer

public MergeSortBuffer(Storage<S> storage)
Parameters:
storage - storage for elements; if null use first Storable to prepare reloaded Storables

MergeSortBuffer

public MergeSortBuffer(Storage<S> storage,
                       String tempDir)
Parameters:
storage - storage for elements; if null use first Storable to prepare reloaded Storables
tempDir - directory to store temp files for merging, or null for default

MergeSortBuffer

public MergeSortBuffer(Storage<S> storage,
                       String tempDir,
                       int maxArrayCapacity)
Parameters:
storage - storage for elements; if null use first Storable to prepare reloaded Storables
tempDir - directory to store temp files for merging, or null for default
maxArrayCapacity - maximum amount of storables to keep in an array before serializing to a file
Throws:
IllegalArgumentException - if storage is null
Method Detail

prepare

public void prepare(Comparator<S> comparator)
Description copied from interface: SortBuffer
Clears buffer and assigns a comparator for sorting.

Specified by:
prepare in interface SortBuffer<S extends Storable>

add

public boolean add(S storable)
Specified by:
add in interface Collection<S extends Storable>
Overrides:
add in class AbstractCollection<S extends Storable>

size

public int size()
Specified by:
size in interface Collection<S extends Storable>
Specified by:
size in class AbstractCollection<S extends Storable>

iterator

public Iterator<S> iterator()
Specified by:
iterator in interface Iterable<S extends Storable>
Specified by:
iterator in interface Collection<S extends Storable>
Specified by:
iterator in class AbstractCollection<S extends Storable>

clear

public void clear()
Specified by:
clear in interface Collection<S extends Storable>
Overrides:
clear in class AbstractCollection<S extends Storable>

sort

public void sort()
Description copied from interface: SortBuffer
Finish sorting buffer.

Specified by:
sort in interface SortBuffer<S extends Storable>

close

public void close()
Description copied from interface: SortBuffer
Clear and close buffer.

Specified by:
close in interface SortBuffer<S extends Storable>


Copyright © 2006-2009 Amazon Technologies, Inc.. All Rights Reserved.