public class IndexSelectionWorker.IndexHelper
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private class |
IndexSelectionWorker.IndexHelper.IndexInfo
Class containing index information: list of indexes and the WHOLE-INDEX flag which
indicates if the index or indexes have bracketing or 4GL walks through the whole
index(es).
|
Modifier and Type | Field and Description |
---|---|
private IndexSelectionWorker.RecordPhraseData |
rpData
Current record phrase data
|
private java.util.Deque<IndexSelectionWorker.RecordPhraseData> |
stack
Stack of record phrase data objects; first one is not used
|
Constructor and Description |
---|
IndexHelper() |
Modifier and Type | Method and Description |
---|---|
void |
addBeginsMatch(java.lang.String field)
Add an begins match (for the given index component) to the list of
matches.
|
void |
addEqualityMatch(java.lang.String field)
Add an equality match (for the given index component) to the list of
matches.
|
void |
addRangeMatch(java.lang.String field)
Add an range match (for the given index component) to the list of
matches.
|
void |
addSortMatch(java.lang.String field,
java.lang.Boolean descending)
Add an sort match (for the given index component) to the list of
matches (and maintains the order of specificity based on the
order of calls to this method).
|
void |
addWordMatch(java.lang.String field)
Add an word match (for the given index component) to the list of
matches.
|
private boolean |
allComponentsMatch(P2JIndex idx,
java.util.Set<java.lang.String> matches)
Calculates if all index components of the given index are found in
the match set.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
breakTie(IndexSelectionWorker.IndexHelper.IndexInfo matches,
boolean ascend)
Select one index from the given list of indexes that are selectable, if there are more
than 1 in the list.
|
java.lang.String |
calcIndexSelection()
Use the recorded index component matches to determine the implicitly
selected index for this record phrase.
|
java.lang.String |
calcOrderByCriteria(java.util.LinkedHashMap<java.lang.String,java.lang.Boolean> criteria,
java.util.Map<java.lang.String,java.lang.String> bufNames)
Converts the sort criteria into an equivalent order by statement.
|
java.lang.String |
calcOrderByIndex(java.lang.String selected,
java.lang.String bufName,
boolean reverse)
Converts the given index into an equivalent order by statement.
|
int |
calcSortOverlap(java.lang.String selected)
Compares the given selected index with the stored sort criteria to
determine if the the complete sort criteria is an exact match or a
compatible subset match to the selected index.
|
boolean |
detectJumbleSort(java.util.LinkedList<java.lang.Long> buffers,
java.util.LinkedList<java.lang.Long> sortCriteria)
Compares the order of tables (only important in a multi-table query)
with the order of the sort criteria.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
findBeginsIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Uses the "begins" match data and the list of selectable indexes for the current record to
determine if there is an index that would be matched.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
findLeadingIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Uses the equality and range match data and the list of selectable indexes for the current
record to determine if there is an index that would be matched based on the largest
number of sequential leading index components.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
findRangeIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Uses the range match data and the list of selectable indexes for the current record to
determine if there is an index that would be matched.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
findSortIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Uses the sort match data and the list of selectable indexes for the current record to
determine if there is an index that would be matched.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
findUniqueIndexMatch()
Uses the stored equality match data and the list of all indexes for the current record to
determine if there is a unique index that would be matched (the index must be unique and
all index components must have equality matches).
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
findWordIndexMatch()
Uses the stored word match data and the list of indexes for the current record to
determine if there is a word index that would be matched.
|
java.util.List<P2JIndex> |
getFullIndexList()
Creates a list of all possible indexes for the current table.
|
P2JIndex |
getIndex(java.lang.String name)
Return the given named index for the current table.
|
java.lang.String |
getIndexNameByPrefix(java.lang.String prefix)
Returns the name of the index which corresponds the given prefix.
|
P2JIndex |
getPrimaryIndex()
Return the index set as primary for the current table.
|
java.lang.String |
getWholeIndexPrefix()
Get "WHOLE-INDEX," prefix for index information.
|
boolean |
isAbbreviatedField(java.lang.String name)
Tests the given field name to determine if it is the last component
of an index and is flagged for abbreviation support.
|
private int |
leadingComponentsMatch(P2JIndex idx,
int start,
java.util.Set<java.lang.String> matches)
Calculates the number of sequential leading index components of the
given index are found in the match set starting at the specified
position in the index component list.
|
private int |
leadingSortComponentsMatch(P2JIndex idx)
Calculates the number of sequential leading index components of the
given index are found in the sort match set (tests component name,
the specificity of the sort matches and the sort direction for
compatibility).
|
java.lang.String |
mergeOrderByCriteria(java.util.LinkedHashMap<java.lang.String,java.lang.Boolean> criteria,
java.util.Map<java.lang.String,java.lang.String> bufNames,
java.lang.String selected,
java.lang.String indexBufName)
Merges the sort criteria and the selected index into an equivalent
order by statement.
|
private int |
numComponentsMatch(P2JIndex idx,
java.util.Set<java.lang.String> matches)
Calculates how many index components of the given index are to be
found in the match set.
|
void |
popRecordPhrase()
Pop the previous record phrase data object from the stack and make it the current
record phrase data.
|
void |
pushRecordPhrase(java.lang.String schemaname)
Initialize data structures related to processing the specified record name.
|
private IndexSelectionWorker.IndexHelper.IndexInfo |
readSingleIndex(IndexSelectionWorker.IndexHelper.IndexInfo indexes)
If the given index information object has only a single entry, return this object,
otherwise return
null . |
private final java.util.Deque<IndexSelectionWorker.RecordPhraseData> stack
private IndexSelectionWorker.RecordPhraseData rpData
public void pushRecordPhrase(java.lang.String schemaname)
schemaname
- The fully qualified schema name of the record being processed.public void popRecordPhrase()
public java.lang.String calcIndexSelection()
The following is the precedence order:
See Schema Package Summary for complete details.
""
(the
empty string) in the case where there are no indexes
defined for this table and thus there is no primary
index to use as a default.public int calcSortOverlap(java.lang.String selected)
true
. Any incompatibility between
the selected index and the sort criteria will generate a return of
false
.
All sort criteria (the list of index components that are sort matches) must match in name, specificity (order of occurrance) and sort direction. If any of the sort matches does not exist in the selected index, exists in a different sequential position or differs in direction (ascending/descending), then this is not compatible.
If the selected index has more index components than the sort criteria AND all the sort criteria are a leading sequential subset, this is still an overlap. At this time, this routine will consider any gaps as incompatibilities (it is unknown if this is different from the Progress compiler's approach).
selected
- The name of the index to compare against.0
if the previously
recorded sort criteria are compatible with all or a leading
subset of the selected index in the direct order. A value
less than 0
if the previously recorded sort
criteria are compatible with all or a leading subset of the
selected index, but in the reverse order. 0
in
any other condition, including the case where there is a
null
, empty string or invalid index name
provided as the selected index.public java.lang.String calcOrderByIndex(java.lang.String selected, java.lang.String bufName, boolean reverse) throws SchemaException
selected
- The name of the index from which to generate the order by
clause.bufName
- Name of the Java variable which represents the buffer.reverse
- If true
then the index should be taken in the
reverse order.SchemaException
public java.lang.String calcOrderByCriteria(java.util.LinkedHashMap<java.lang.String,java.lang.Boolean> criteria, java.util.Map<java.lang.String,java.lang.String> bufNames) throws SchemaException
The string keys of both the criteria
map (or the
internal map of sort matches, if criteria
is
null
) and the bufNames
map will end with
an array dereferencing trailer (e.g., "[0]"), IF the associated sort
component refers to a specific element within an extent field. This
trailer already will be adjusted to assume a 0-based array.
criteria
- An ordered list of the sort criteria with the key as the
fully qualified schema name of the field and the value
as a Boolean
that specifies that the sort
direction is descending if true
, otherwise
that field will have an ascending sort direction. The
list MUST be ordered in the same order (from least
granular to most granular) of the original BY clauses.
If null
, the previously recorded sort
match criteria will be used.bufNames
- Map of fully qualified schema names of fields to the Java
names of the buffers which they reference.SchemaException
public java.lang.String mergeOrderByCriteria(java.util.LinkedHashMap<java.lang.String,java.lang.Boolean> criteria, java.util.Map<java.lang.String,java.lang.String> bufNames, java.lang.String selected, java.lang.String indexBufName) throws SchemaException
null
, the empty string will be returned.
Merging is performed in the following way: first, sort criteria are emitted; then the sort criteria specified by index which are not already present in the emitted criteria (into direct or reverse way) are appended to the output.
The string keys of both the criteria
map (or the
internal map of sort matches, if criteria
is
null
) and the bufNames
map will end with
an array dereferencing trailer (e.g., "[0]"), IF the associated sort
component refers to a specific element within an extent field. This
trailer already will be adjusted to assume a 0-based array.
criteria
- An ordered list of the sort criteria with the key as the
fully qualified schema name of the field and the value
as a Boolean
that specifies that the sort
direction is descending if true
, otherwise
that field will have an ascending sort direction. The
list MUST be ordered in the same order (from least
granular to most granular) of the original BY clauses.
If null
, the previously recorded sort
match criteria will be used.bufNames
- Map of fully qualified schema names of fields to the Java
names of the buffers which they reference.selected
- The name of the index which is merged with the order by
clause. Can be null
.indexBufName
- Name of the Java variable which represents the buffer for
the specified index.null
.SchemaException
public boolean detectJumbleSort(java.util.LinkedList<java.lang.Long> buffers, java.util.LinkedList<java.lang.Long> sortCriteria)
true
) if the fields from two or more tables are
intermixed (fields from one table are processed both before AND
after a field or fields of another table) AND/OR are out of order
(the fields of a later occurring table are less granular sort
criteria than fields of an earlier occurring table). If there is
only 1 table or if there are no sort criteria, jumble sorting will
be false
.buffers
- A list of the tables by node ID, in the order of
occurrence.sortCriteria
- A list of the tables referenced by each sort criteria,
in the order of occurrence.true
if jumble sorting is detected.public P2JIndex getIndex(java.lang.String name)
name
- The name of the index to obtain.null
if there are no
indexes explicitly defined for this table or if there are
no matches.public java.lang.String getIndexNameByPrefix(java.lang.String prefix)
null
is returned.prefix
- Prefix of the index name.null
if there is no corresponding index.public P2JIndex getPrimaryIndex()
null
if there are no
indexes explicitly defined for this table (as long as
there is at least 1 explicit index, there is always
a primary index).public boolean isAbbreviatedField(java.lang.String name)
name
- The field name.true
if this field is the last index
component of an index AND is flagged for abbreviation
support.public void addEqualityMatch(java.lang.String field)
field
- The index component (field) name which was matched.public void addBeginsMatch(java.lang.String field)
field
- The index component (field) name which was matched.public void addRangeMatch(java.lang.String field)
field
- The index component (field) name which was matched.public void addSortMatch(java.lang.String field, java.lang.Boolean descending)
field
- The index component (field) name which was matched.descending
- true
if the sort is a descending sort,
false
for an ascending sort.public void addWordMatch(java.lang.String field)
field
- The index component (field) name which was matched.public java.util.List<P2JIndex> getFullIndexList()
public java.lang.String getWholeIndexPrefix()
private IndexSelectionWorker.IndexHelper.IndexInfo findUniqueIndexMatch()
breakTie(com.goldencode.p2j.convert.IndexSelectionWorker.IndexHelper.IndexInfo, boolean)
method (in descending order).null
if no unique indexes are selectable by this criteria.private IndexSelectionWorker.IndexHelper.IndexInfo findWordIndexMatch()
breakTie(com.goldencode.p2j.convert.IndexSelectionWorker.IndexHelper.IndexInfo, boolean)
method (in ascending order) is used to
select a single result.null
if no indexes were selectable by this criteria.private IndexSelectionWorker.IndexHelper.IndexInfo findLeadingIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Only indexes that have sequential leading index components matching can be selectable by this method. The method first checks for equality matches and if multiple equivalent equality match choices exist (multiple choices with the same number of leading sequential equality matches), then a single subsequent field is checked for a range match. This provides a very specific/tightly coupled tie break process.
The selection process removes indexes from the selectable list only in the case where matches occur. This means that those selectable indexes will remain in the list and thus the returned list cannot be empty unless there are no indexes at all for a given table.
selectable
- Index information object containing the list of index candidates for the
current table. This may be equivalent to the full list of all possible indexes
or it may be a subset if prior processing removed indexes form the selection
list.private IndexSelectionWorker.IndexHelper.IndexInfo findBeginsIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Only indexes that have the first (leading) index component matching can be selectable. All other begins matches are ignored. There is no concept of the "most" begins matches.
The selection process removes indexes from the selectable list only in the case where matches occur. This means that those selectable indexes will remain in the list and thus the returned list cannot be empty unless there are no indexes at all for a given table.
selectable
- Index information object containing the list of index candidates for the
current table. This may be equivalent to the full list of all possible indexes
or it may be a subset if prior processing removed indexes form the selection
list.private IndexSelectionWorker.IndexHelper.IndexInfo findRangeIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
Only indexes that have the first (leading) index component matching can be selectable. All other range matches are ignored. There is no concept of the "most" range matches.
The selection process removes indexes from the selectable list only in the case where matches occur. This means that those selectable indexes will remain in the list and thus the returned list cannot be empty unless there are no indexes at all for a given table.
selectable
- Index information object containing the list of index candidates for the
current table. This may be equivalent to the full list of all possible indexes
or it may be a subset if prior processing removed indexes form the selection
list.private IndexSelectionWorker.IndexHelper.IndexInfo findSortIndexMatch(IndexSelectionWorker.IndexHelper.IndexInfo selectable)
breakTie(com.goldencode.p2j.convert.IndexSelectionWorker.IndexHelper.IndexInfo, boolean)
method (in ascending order) is used to select
a single result.
Only indexes that have sequential leading index components matching can be selectable. Sort matches must also agree on the direction of the sort for the given index component (ascending/descending).
The selection process removes indexes from the selectable list only in the case where matches occur. This means that those selectable indexes will remain in the list and thus the returned list cannot be empty unless there are no indexes at all for a given table.
selectable
- Index information object containing the list of index candidates for the
current table. This may be equivalent to the full list of all possible indexes
or it may be a subset if prior processing removed indexes form the selection
list.null
if no indexes were selectable by this criteria.private boolean allComponentsMatch(P2JIndex idx, java.util.Set<java.lang.String> matches)
idx
- The index to test.matches
- The list of matches that were found.true
if all index components were matched.private int numComponentsMatch(P2JIndex idx, java.util.Set<java.lang.String> matches)
idx
- The index to test.matches
- The list of matches that were found.private int leadingComponentsMatch(P2JIndex idx, int start, java.util.Set<java.lang.String> matches)
idx
- The index to test.start
- The 0-based position of the first component to compare.matches
- The list of matches that were found.private int leadingSortComponentsMatch(P2JIndex idx)
idx
- The index to test.0
then
components were matched in the direct way. If the returned
value is less than 0
then components were
matched in the reverse way. If the returned value is
0
then no leading components were matched.private IndexSelectionWorker.IndexHelper.IndexInfo readSingleIndex(IndexSelectionWorker.IndexHelper.IndexInfo indexes)
null
.indexes
- Index information object.null
if
no indexes or more than 1 index are in the list of indexes.private IndexSelectionWorker.IndexHelper.IndexInfo breakTie(IndexSelectionWorker.IndexHelper.IndexInfo matches, boolean ascend)
ascend
parameter).matches
- Index information object containing the list of indexes to select from.ascend
- true
to compare lexicographically (ascending order) and
false
to compare in reverse lexicographical order (descending
order).null
if no indexes are in the list.