Class MergeSort
java.lang.Object
org.apache.derby.impl.store.access.sort.MergeSort
- All Implemented Interfaces:
Sort
- Direct Known Subclasses:
UniqueWithDuplicateNullsMergeSort
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
FieldsModifier and TypeFieldDescriptionprotected boolean
Whether the rows are expected to be in order on insert, as passed in on create.protected ColumnOrdering[]
The column ordering as passed in on create.protected boolean[]
A lookup table to speed up lookup of Ascending state of a column,protected int[]
A lookup table to speed up lookup of a column associated with the i'th column to compare.protected boolean[]
A lookup table to speed up lookup of nulls-low ordering of a column,private MergeInserter
The inserter that's being used to insert rows into the sort.A vector of merge runs, produced by the MergeInserter.(package private) static Properties
Properties for mergeSortprivate Scan
The scan that's being used to return rows from the sort.private 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).(package private) int
The maximum number of entries a sort buffer can hold.(package private) int
The minimum number of entries a sort buffer can hold.(package private) SortObserver
The sort observer.private int
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[]
The template as passed in on create. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate 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'tnull
.(package private) void
Check that the columns in the row agree with the columns in the template, both in number and in type.protected int
compare
(DataValueDescriptor[] r1, DataValueDescriptor[] r2) (package private) long
createMergeRun
(TransactionManager tran, SortBuffer sortBuffer) Remove all the rows from the sort buffer and store them in a temporary conglomerate.(package private) void
doneInserting
(MergeInserter inserter, SortBuffer sortBuffer, Vector<Long> mergeRuns) An inserter is closing.(package private) void
doneScanning
(Scan scan, SortBuffer sortBuffer) (package private) void
doneScanning
(Scan scan, SortBuffer sortBuffer, Vector<Long> mergeRuns) void
drop
(TransactionController tran) Drop the sort.(package private) void
Get rid of the merge runs, if there are any.void
initialize
(DataValueDescriptor[] template, ColumnOrdering[] columnOrdering, SortObserver sortObserver, boolean alreadyInOrder, long estimatedRows, int sortBufferMax) Go from the CLOSED to the INITIALIZED state.private void
open
(TransactionManager tran) Open a sort controller.Open a row source to get rows out of the sorter.openSortScan
(TransactionManager tran, boolean hold) Open a scan controller.
-
Field Details
-
STATE_CLOSED
private static final int STATE_CLOSED- See Also:
-
STATE_INITIALIZED
private static final int STATE_INITIALIZED- See Also:
-
STATE_INSERTING
private static final int STATE_INSERTING- See Also:
-
STATE_DONE_INSERTING
private static final int STATE_DONE_INSERTING- See Also:
-
STATE_SCANNING
private static final int STATE_SCANNING- See Also:
-
STATE_DONE_SCANNING
private static final int STATE_DONE_SCANNING- See Also:
-
state
private int stateMaintains the current state of the sort as defined in the preceding values. Sorts start off and end up closed. -
template
The template as passed in on create. Valid when the state is INITIALIZED through SCANNING, null otherwise. -
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. -
columnOrderingMap
protected int[] columnOrderingMapA 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]. -
columnOrderingAscendingMap
protected boolean[] columnOrderingAscendingMapA lookup table to speed up lookup of Ascending state of a column, -
columnOrderingNullsLowMap
protected boolean[] columnOrderingNullsLowMapA lookup table to speed up lookup of nulls-low ordering of a column, -
sortObserver
SortObserver sortObserverThe sort observer. May be null. Used as a callback. -
alreadyInOrder
protected boolean alreadyInOrderWhether the rows are expected to be in order on insert, as passed in on create. -
inserter
The inserter that's being used to insert rows into the sort. This field is only valid when the state is INSERTING. -
scan
The scan that's being used to return rows from the sort. This field is only valid when the state is SCANNING. -
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. -
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). -
sortBufferMax
int sortBufferMaxThe maximum number of entries a sort buffer can hold. -
sortBufferMin
int sortBufferMinThe minimum number of entries a sort buffer can hold. -
properties
Properties for mergeSort
-
-
Constructor Details
-
MergeSort
MergeSort()
-
-
Method Details
-
open
Open a sort controller.This implementation only supports a single sort controller per sort.
- Specified by:
open
in interfaceSort
- Throws:
StandardException
- Standard exception policy.- See Also:
-
openSortScan
Open a scan controller.- Specified by:
openSortScan
in interfaceSort
- Throws:
StandardException
- Standard exception policy.- See Also:
-
openSortRowSource
Open a row source to get rows out of the sorter.- Specified by:
openSortRowSource
in interfaceSort
- Throws:
StandardException
- Standard exception policy.- See Also:
-
drop
Drop the sort.- Specified by:
drop
in interfaceSort
- Throws:
StandardException
- See Also:
-
checkColumnOrdering
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'tnull
.Intended to be called as part of a sanity check. All columns are orderable, since
DataValueDescriptor
extendsOrderable
.- Returns:
true
if the ordering is valid,false
if not.
-
checkColumnTypes
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?
- Throws:
StandardException
-
compare
- Throws:
StandardException
-
initialize
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.- Throws:
StandardException
-
doneInserting
An inserter is closing. -
doneScanning
-
doneScanning
-
dropMergeRuns
Get rid of the merge runs, if there are any. Must not cause any errors because it's called during error processing. -
multiStageMerge
- Throws:
StandardException
-
createMergeRun
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.- Throws:
StandardException
-