Class TupleComparator
- java.lang.Object
-
- edu.caltech.nanodb.expressions.TupleComparator
-
- All Implemented Interfaces:
java.util.Comparator<Tuple>
public class TupleComparator extends java.lang.Object implements java.util.Comparator<Tuple>
This class allows us to sort and compare tuples based on an order-by specification. The specification is simply a list ofOrderByExpression
objects, and the order of the expressions themselves matters. Tuples will be ordered by the first expression; if the tuples' values are the same then the tuples will be ordered by the second expression; etc.
-
-
Field Summary
Fields Modifier and Type Field Description private Environment
envTupleA
The environment to use for evaluating order-by expressions against the first tuple.private Environment
envTupleB
The environment to use for evaluating order-by expressions against the second tuple.private java.util.ArrayList<OrderByExpression>
orderSpec
The specification of how to order the tuples being compared.private Schema
schema
The schema of the tuples that will be compared by this comparator object.
-
Constructor Summary
Constructors Constructor Description TupleComparator(Schema schema, java.util.List<OrderByExpression> orderSpec)
Construct a new tuple-comparator with the given ordering specification.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int
_compareTuples(Tuple t1, Tuple t2, boolean allowSizeMismatch)
This is the private helper function that implements both thecompareTuples(edu.caltech.nanodb.relations.Tuple, edu.caltech.nanodb.relations.Tuple)
method and thecomparePartialTuples(edu.caltech.nanodb.relations.Tuple, edu.caltech.nanodb.relations.Tuple)
method.static boolean
areTuplesEqual(Tuple t1, Tuple t2)
This helper function returns true if two tuples have the same number of columns and the values compare as equal when coerced with theTypeConverter.coerceComparison(java.lang.Object, java.lang.Object)
method.static boolean
areTuplesEqual(Tuple t1, Tuple t2, double epsilon)
This helper function returns true if two tuples have the same number of columns and the values compare as equal when coerced with theTypeConverter.coerceComparison(java.lang.Object, java.lang.Object)
method.int
compare(Tuple a, Tuple b)
Performs the comparison of two tuples based on the configuration of this tuple-comparator object.static int
comparePartialTuples(Tuple t1, Tuple t2)
Compares all columns of the two input tuples, in the order they appear within the tuples, and a value is returned to indicate the ordering.static int
compareTuples(Tuple t1, Tuple t2)
Compares all columns of the two input tuples, in the order they appear within the tuples, and a value is returned to indicate the ordering.
-
-
-
Field Detail
-
schema
private Schema schema
The schema of the tuples that will be compared by this comparator object.
-
orderSpec
private java.util.ArrayList<OrderByExpression> orderSpec
The specification of how to order the tuples being compared.
-
envTupleA
private Environment envTupleA
The environment to use for evaluating order-by expressions against the first tuple.
-
envTupleB
private Environment envTupleB
The environment to use for evaluating order-by expressions against the second tuple.
-
-
Constructor Detail
-
TupleComparator
public TupleComparator(Schema schema, java.util.List<OrderByExpression> orderSpec)
Construct a new tuple-comparator with the given ordering specification.- Parameters:
schema
- the schema of the tuples that will be compared by this comparator objectorderSpec
- a series of order-by expressions used to order the tuples being compared
-
-
Method Detail
-
compare
public int compare(Tuple a, Tuple b)
Performs the comparison of two tuples based on the configuration of this tuple-comparator object.- Specified by:
compare
in interfacejava.util.Comparator<Tuple>
- Parameters:
a
- the first tuple to compare.b
- the second tuple to compare.- Returns:
- a negative, zero, or positive value, corresponding to whether tuple a is less than, equal to, or greater than tuple b.
- Design Note:
- (Donnie) We have to suppress "unchecked operation" warnings on
this code, since
Comparable
is a generic (and thus allows us to specify the type of object being compared), but we want to use it without specifying any types.
-
areTuplesEqual
public static boolean areTuplesEqual(Tuple t1, Tuple t2)
This helper function returns true if two tuples have the same number of columns and the values compare as equal when coerced with theTypeConverter.coerceComparison(java.lang.Object, java.lang.Object)
method. Note that the schemas of the tuples are not considered.- Parameters:
t1
- the first tuple to comparet2
- the second tuple to compare- Returns:
- true if the two tuples have the same number of columns, and the values from t1 and t2 compare equal.
-
areTuplesEqual
public static boolean areTuplesEqual(Tuple t1, Tuple t2, double epsilon)
This helper function returns true if two tuples have the same number of columns and the values compare as equal when coerced with theTypeConverter.coerceComparison(java.lang.Object, java.lang.Object)
method. Note that the schemas of the tuples are not considered.- Parameters:
t1
- the first tuple to comparet2
- the second tuple to compareepsilon
- how close two float or double arguments must be to each other to compare as "equal"- Returns:
- true if the two tuples have the same number of columns, and the values from t1 and t2 compare equal.
-
compareTuples
public static int compareTuples(Tuple t1, Tuple t2)
Compares all columns of the two input tuples, in the order they appear within the tuples, and a value is returned to indicate the ordering.
- Result < 0 if t1 < t2
- Result == 0 if t1 == t2
- Result > 0 if t1 > t2
This method performs type-coercion between the values being compared, so that it may be used for evaluating general predicates.
This method requires the tuples being compared to have the same number of columns. For a method that allows tuples with different numbers of columns to be compared, see the
comparePartialTuples(edu.caltech.nanodb.relations.Tuple, edu.caltech.nanodb.relations.Tuple)
method.- Parameters:
t1
- the first tuple to compare. Must not benull
.t2
- the second tuple to compare. Must not benull
.- Returns:
- a negative, positive, or zero value indicating the ordering of the two inputs
- Throws:
java.lang.IllegalArgumentException
- if the two input tuples are different sizes.
-
comparePartialTuples
public static int comparePartialTuples(Tuple t1, Tuple t2)
Compares all columns of the two input tuples, in the order they appear within the tuples, and a value is returned to indicate the ordering.
- Result < 0 if t1 < t2
- Result == 0 if t1 == t2
- Result > 0 if t1 > t2
This method performs type-coercion between the values being compared, so that it may be used for evaluating general predicates.
This method differs from the
compareTuples(edu.caltech.nanodb.relations.Tuple, edu.caltech.nanodb.relations.Tuple)
method in that it allowst1
andt2
to have different numbers of columns. Tuples are compared in a way similar to strings, where if two different-length strings have the same values in the overlapping portion, the shorter string is defined to be "less than" the longer string. This allows this comparison method to be used to do partial-key lookups against ordered indexes.- Parameters:
t1
- the first tuple to compare. Must not benull
.t2
- the second tuple to compare. Must not benull
.- Returns:
- a negative, positive, or zero value indicating the ordering of the two inputs
-
_compareTuples
private static int _compareTuples(Tuple t1, Tuple t2, boolean allowSizeMismatch)
This is the private helper function that implements both thecompareTuples(edu.caltech.nanodb.relations.Tuple, edu.caltech.nanodb.relations.Tuple)
method and thecomparePartialTuples(edu.caltech.nanodb.relations.Tuple, edu.caltech.nanodb.relations.Tuple)
method. Its behavior with respect to tuples with different numbers of columns is controlled with a Boolean argument.- Parameters:
t1
- the first tuple to compare. Must not benull
.t2
- the second tuple to compare. Must not benull
.allowSizeMismatch
- true if the two tuples are allowed to be different sizes, or false if they must be the same size.- Returns:
- a negative, positive, or zero value indicating the ordering of the two inputs
-
-