Top Description Fields Constructors Methods
org.apache.derby.impl.store.access.sort

pack-priv Class MergeSort

extends Object
implements Sort
Class Inheritance
All Implemented Interfaces
org.apache.derby.iapi.store.access.conglomerate.Sort
Known Direct Subclasses
org.apache.derby.impl.store.access.sort.UniqueWithDuplicateNullsMergeSort
Imports
java.util.Enumeration, .Properties, .Vector, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.SQLState, org.apache.derby.iapi.services.io.FormatableBitSet, org.apache.derby.iapi.store.access.ColumnOrdering, .RowUtil, .SortController, .SortObserver, .TransactionController, org.apache.derby.iapi.store.access.conglomerate.ScanControllerRowSource, .ScanManager, .Sort, .TransactionManager, org.apache.derby.iapi.store.raw.RawStoreFactory, .StreamContainerHandle, .Transaction, org.apache.derby.iapi.types.DataValueDescriptor, org.apache.derby.shared.common.sanity.SanityManager

A sort implementation which does the sort in-memory if it can, but which can do an external merge sort so that it can sort an arbitrary number of rows.

Field Summary

Modifier and TypeField and Description
protected boolean
alreadyInOrder

Whether the rows are expected to be in order on insert, as passed in on create.

protected ColumnOrdering[]
columnOrdering

The column ordering as passed in on create.

protected boolean[]
columnOrderingAscendingMap

A lookup table to speed up lookup of Ascending state of a column,

protected int[]
columnOrderingMap

A lookup table to speed up lookup of a column associated with the i'th column to compare.

protected boolean[]
columnOrderingNullsLowMap

A lookup table to speed up lookup of nulls-low ordering of a column,

private MergeInserter
inserter

The inserter that's being used to insert rows into the sort.

private Vector<Long>
mergeRuns

A vector of merge runs, produced by the MergeInserter.

pack-priv static Properties
properties

Properties for mergeSort

private Scan
scan

The scan that's being used to return rows from the sort.

private SortBuffer
sortBuffer

An ordered set of the leftover rows that didn't go in the last merge run (might be all the rows if there are no merge runs).

pack-priv int
sortBufferMax

The maximum number of entries a sort buffer can hold.

pack-priv int
sortBufferMin

The minimum number of entries a sort buffer can hold.

pack-priv SortObserver
sortObserver

The sort observer.

private int
state

Maintains the current state of the sort as defined in the preceding values.

private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
protected DataValueDescriptor[]
template

The template as passed in on create.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

true if the ordering is valid, false if not.
checkColumnOrdering
(DataValueDescriptor[] template, ColumnOrdering[] columnOrdering)

Check the column ordering against the template, making sure that each column is present in the template, is not mentioned more than once, and that the columns isn't null.

pack-priv void
checkColumnTypes(DataValueDescriptor[] row)

Check that the columns in the row agree with the columns in the template, both in number and in type.

protected int
pack-priv long
createMergeRun(TransactionManager tran, SortBuffer sortBuffer)

Remove all the rows from the sort buffer and store them in a temporary conglomerate.

pack-priv void
doneInserting(MergeInserter inserter, SortBuffer sortBuffer, Vector<Long> mergeRuns)

An inserter is closing.

pack-priv void
doneScanning(Scan scan, SortBuffer sortBuffer)

pack-priv void
doneScanning(Scan scan, SortBuffer sortBuffer, Vector<Long> mergeRuns)

public void
pack-priv void
dropMergeRuns(TransactionManager tran)

Get rid of the merge runs, if there are any.

public void
initialize(DataValueDescriptor[] template, ColumnOrdering[] columnOrdering, SortObserver sortObserver, boolean alreadyInOrder, long estimatedRows, int sortBufferMax)

Go from the CLOSED to the INITIALIZED state.

private void
public SortController
public ScanControllerRowSource
public ScanManager
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alreadyInOrderback to summary
protected boolean alreadyInOrder

Whether the rows are expected to be in order on insert, as passed in on create.

columnOrderingback to summary
protected ColumnOrdering[] columnOrdering

The column ordering as passed in on create. Valid when the state is INITIALIZED through SCANNING, null otherwise. May be null if there is no column ordering - this means that all rows are considered to be duplicates, and the sort will only emit a single row.

columnOrderingAscendingMapback to summary
protected boolean[] columnOrderingAscendingMap

A lookup table to speed up lookup of Ascending state of a column,

columnOrderingMapback to summary
protected int[] columnOrderingMap

A lookup table to speed up lookup of a column associated with the i'th column to compare. To find the column id to compare as the i'th column look in columnOrderingMap[i].

columnOrderingNullsLowMapback to summary
protected boolean[] columnOrderingNullsLowMap

A lookup table to speed up lookup of nulls-low ordering of a column,

inserterback to summary
private MergeInserter inserter

The inserter that's being used to insert rows into the sort. This field is only valid when the state is INSERTING.

mergeRunsback to summary
private Vector<Long> mergeRuns

A vector of merge runs, produced by the MergeInserter. Might be null if no merge runs were produced. It is a vector of container ids.

propertiesback to summary
pack-priv static Properties properties

Properties for mergeSort

scanback to summary
private Scan scan

The scan that's being used to return rows from the sort. This field is only valid when the state is SCANNING.

sortBufferback to summary
private SortBuffer sortBuffer

An ordered set of the leftover rows that didn't go in the last merge run (might be all the rows if there are no merge runs).

sortBufferMaxback to summary
pack-priv int sortBufferMax

The maximum number of entries a sort buffer can hold.

sortBufferMinback to summary
pack-priv int sortBufferMin

The minimum number of entries a sort buffer can hold.

sortObserverback to summary
pack-priv SortObserver sortObserver

The sort observer. May be null. Used as a callback.

stateback to summary
private int state

Maintains the current state of the sort as defined in the preceding values. Sorts start off and end up closed.

STATE_CLOSEDback to summary
private static final int STATE_CLOSED
STATE_DONE_INSERTINGback to summary
private static final int STATE_DONE_INSERTING
STATE_DONE_SCANNINGback to summary
private static final int STATE_DONE_SCANNING
STATE_INITIALIZEDback to summary
private static final int STATE_INITIALIZED
STATE_INSERTINGback to summary
private static final int STATE_INSERTING
STATE_SCANNINGback to summary
private static final int STATE_SCANNING
templateback to summary
protected DataValueDescriptor[] template

The template as passed in on create. Valid when the state is INITIALIZED through SCANNING, null otherwise.

Constructor Detail

MergeSortback to summary
pack-priv MergeSort()

Method Detail

checkColumnOrderingback to summary
private boolean checkColumnOrdering(DataValueDescriptor[] template, ColumnOrdering[] columnOrdering)

Check the column ordering against the template, making sure that each column is present in the template, is not mentioned more than once, and that the columns isn't null.

Intended to be called as part of a sanity check. All columns are orderable, since DataValueDescriptor extends Orderable.

Returns:boolean

true if the ordering is valid, false if not.

checkColumnTypesback to summary
pack-priv void checkColumnTypes(DataValueDescriptor[] row) throws StandardException

Check that the columns in the row agree with the columns in the template, both in number and in type.

XXX (nat) Currently checks that the classes implementing each column are the same -- is this right?

compareback to summary
protected int compare(DataValueDescriptor[] r1, DataValueDescriptor[] r2) throws StandardException
createMergeRunback to summary
pack-priv long createMergeRun(TransactionManager tran, SortBuffer sortBuffer) throws StandardException

Remove all the rows from the sort buffer and store them in a temporary conglomerate. The temporary conglomerate is a "merge run". Returns the container id of the merge run.

doneInsertingback to summary
pack-priv void doneInserting(MergeInserter inserter, SortBuffer sortBuffer, Vector<Long> mergeRuns)

An inserter is closing.

doneScanningback to summary
pack-priv void doneScanning(Scan scan, SortBuffer sortBuffer)
doneScanningback to summary
pack-priv void doneScanning(Scan scan, SortBuffer sortBuffer, Vector<Long> mergeRuns)
dropback to summary
public void drop(TransactionController tran) throws StandardException

Implements org.apache.derby.iapi.store.access.conglomerate.Sort.drop.

Drop the sort.

See Also
Sort#drop
dropMergeRunsback to summary
pack-priv void dropMergeRuns(TransactionManager tran)

Get rid of the merge runs, if there are any. Must not cause any errors because it's called during error processing.

initializeback to summary
public void initialize(DataValueDescriptor[] template, ColumnOrdering[] columnOrdering, SortObserver sortObserver, boolean alreadyInOrder, long estimatedRows, int sortBufferMax) throws StandardException

Go from the CLOSED to the INITIALIZED state.

multiStageMergeback to summary
private void multiStageMerge(TransactionManager tran) throws StandardException
openback to summary
public SortController open(TransactionManager tran) throws StandardException

Implements org.apache.derby.iapi.store.access.conglomerate.Sort.open.

Open a sort controller.

This implementation only supports a single sort controller per sort.

Exceptions
StandardException:

Doc from org.apache.derby.iapi.store.access.conglomerate.Sort.open.

Standard exception policy.

See Also
Sort#open
openSortRowSourceback to summary
public ScanControllerRowSource openSortRowSource(TransactionManager tran) throws StandardException

Implements org.apache.derby.iapi.store.access.conglomerate.Sort.openSortRowSource.

Open a row source to get rows out of the sorter.

Exceptions
StandardException:

Doc from org.apache.derby.iapi.store.access.conglomerate.Sort.openSortRowSource.

Standard exception policy.

See Also
Sort#openSortRowSource
openSortScanback to summary
public ScanManager openSortScan(TransactionManager tran, boolean hold) throws StandardException

Implements org.apache.derby.iapi.store.access.conglomerate.Sort.openSortScan.

Open a scan controller.

Exceptions
StandardException:

Doc from org.apache.derby.iapi.store.access.conglomerate.Sort.openSortScan.

Standard exception policy.

See Also
Sort#openSortScan