Class 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 of OrderByExpression 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 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 object
        orderSpec - 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 interface java.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 the TypeConverter.coerceComparison(java.lang.Object, java.lang.Object) method. Note that the schemas of the tuples are not considered.
        Parameters:
        t1 - the first tuple to compare
        t2 - 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 the TypeConverter.coerceComparison(java.lang.Object, java.lang.Object) method. Note that the schemas of the tuples are not considered.
        Parameters:
        t1 - the first tuple to compare
        t2 - the second tuple to compare
        epsilon - 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 be null.
        t2 - the second tuple to compare. Must not be null.
        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 allows t1 and t2 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 be null.
        t2 - the second tuple to compare. Must not be null.
        Returns:
        a negative, positive, or zero value indicating the ordering of the two inputs