Class TupleComparator<T extends Tuple>
- java.lang.Object
-
- org.apache.flink.api.common.typeutils.TypeComparator<T>
-
- org.apache.flink.api.common.typeutils.CompositeTypeComparator<T>
-
- org.apache.flink.api.java.typeutils.runtime.TupleComparatorBase<T>
-
- org.apache.flink.api.java.typeutils.runtime.TupleComparator<T>
-
- All Implemented Interfaces:
Serializable
@Internal public final class TupleComparator<T extends Tuple> extends TupleComparatorBase<T>
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class org.apache.flink.api.java.typeutils.runtime.TupleComparatorBase
comparators, deserializedFields1, deserializedFields2, HASH_SALT, invertNormKey, keyPositions, normalizableKeyPrefixLen, normalizedKeyLengths, numLeadingNormalizableKeys, serializers
-
-
Constructor Summary
Constructors Constructor Description TupleComparator(int[] keyPositions, TypeComparator<?>[] comparators, TypeSerializer<?>[] serializers)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompare(T first, T second)Compares two records in object form.TypeComparator<T>duplicate()Creates a copy of this class.booleanequalToReference(T candidate)Checks, whether the given element is equal to the element that has been set as the comparison reference in this comparator instance.intextractKeys(Object record, Object[] target, int index)Extracts the key fields from a record.inthash(T value)Computes a hash value for the given record.voidputNormalizedKey(T value, MemorySegment target, int offset, int numBytes)Writes a normalized key for the given record into the target byte array, starting at the specified position and writing exactly the given number of bytes.voidsetReference(T toCompare)Sets the given element as the comparison reference for future calls toTypeComparator.equalToReference(Object)andTypeComparator.compareToReference(TypeComparator).-
Methods inherited from class org.apache.flink.api.java.typeutils.runtime.TupleComparatorBase
compareSerialized, compareToReference, getFlatComparator, getKeyPositions, getNormalizeKeyLen, instantiateDeserializationUtils, invertNormalizedKey, isNormalizedKeyPrefixOnly, privateDuplicate, readWithKeyDenormalization, supportsNormalizedKey, supportsSerializationWithKeyNormalization, writeWithKeyNormalization
-
Methods inherited from class org.apache.flink.api.common.typeutils.CompositeTypeComparator
getFlatComparators
-
Methods inherited from class org.apache.flink.api.common.typeutils.TypeComparator
compareAgainstReference, supportsCompareAgainstReference
-
-
-
-
Constructor Detail
-
TupleComparator
public TupleComparator(int[] keyPositions, TypeComparator<?>[] comparators, TypeSerializer<?>[] serializers)
-
-
Method Detail
-
hash
public int hash(T value)
Description copied from class:TypeComparatorComputes a hash value for the given record. The hash value should include all fields in the record relevant to the comparison.The hash code is typically not used as it is in hash tables and for partitioning, but it is further scrambled to make sure that a projection of the hash values to a lower cardinality space is as results in a rather uniform value distribution. However, any collisions produced by this method cannot be undone. While it is NOT important to create hash codes that cover the full spectrum of bits in the integer, it IS important to avoid collisions when combining two value as much as possible.
- Specified by:
hashin classTypeComparator<T extends Tuple>- Parameters:
value- The record to be hashed.- Returns:
- A hash value for the record.
- See Also:
Object.hashCode()
-
setReference
public void setReference(T toCompare)
Description copied from class:TypeComparatorSets the given element as the comparison reference for future calls toTypeComparator.equalToReference(Object)andTypeComparator.compareToReference(TypeComparator). This method must set the given element into this comparator instance's state. If the comparison happens on a subset of the fields from the record, this method may extract those fields.A typical example for checking the equality of two elements is the following:
The rational behind this method is that elements are typically compared using certain features that are extracted from them, (such de-serializing as a subset of fields). When setting the reference, this extraction happens. The extraction needs happen only once per element, even though an element is often compared to multiple other elements, such as when finding equal elements in the process of grouping the elements.E e1 = ...; E e2 = ...; TypeComparator<E> acc = ...; acc.setReference(e1); boolean equal = acc.equalToReference(e2);- Specified by:
setReferencein classTypeComparator<T extends Tuple>- Parameters:
toCompare- The element to set as the comparison reference.
-
equalToReference
public boolean equalToReference(T candidate)
Description copied from class:TypeComparatorChecks, whether the given element is equal to the element that has been set as the comparison reference in this comparator instance.- Specified by:
equalToReferencein classTypeComparator<T extends Tuple>- Parameters:
candidate- The candidate to check.- Returns:
- True, if the element is equal to the comparison reference, false otherwise.
- See Also:
TypeComparator.setReference(Object)
-
compare
public int compare(T first, T second)
Description copied from class:TypeComparatorCompares two records in object form. The return value indicates the order of the two in the same way as defined byComparator.compare(Object, Object).- Specified by:
comparein classTypeComparator<T extends Tuple>- Parameters:
first- The first record.second- The second record.- Returns:
- An integer defining the oder among the objects in the same way as
Comparator.compare(Object, Object). - See Also:
Comparator.compare(Object, Object)
-
putNormalizedKey
public void putNormalizedKey(T value, MemorySegment target, int offset, int numBytes)
Description copied from class:TypeComparatorWrites a normalized key for the given record into the target byte array, starting at the specified position and writing exactly the given number of bytes. Note that the comparison of the bytes is treating the bytes as unsigned bytes:int byteI = bytes[i] & 0xFF;If the meaningful part of the normalized key takes less than the given number of bytes, then it must be padded. Padding is typically required for variable length data types, such as strings. The padding uses a special character, either
0or0xff, depending on whether shorter values are sorted to the beginning or the end.This method is similar to
NormalizableKey.copyNormalizedKey(MemorySegment, int, int). In the case that multiple fields of a record contribute to the normalized key, it is crucial that the fields align on the byte field, i.e. that every field always takes up the exact same number of bytes.- Specified by:
putNormalizedKeyin classTypeComparator<T extends Tuple>- Parameters:
value- The record for which to create the normalized key.target- The byte array into which to write the normalized key bytes.offset- The offset in the byte array, where to start writing the normalized key bytes.numBytes- The number of bytes to be written exactly.- See Also:
NormalizableKey.copyNormalizedKey(MemorySegment, int, int)
-
extractKeys
public int extractKeys(Object record, Object[] target, int index)
Description copied from class:TypeComparatorExtracts the key fields from a record. This is for use by the PairComparator to provide interoperability between different record types. Note, that at least one key should be extracted.- Specified by:
extractKeysin classTypeComparator<T extends Tuple>- Parameters:
record- The record that contains the key(s)target- The array to write the key(s) into.index- The offset of the target array to start writing into.- Returns:
- the number of keys added to target.
-
duplicate
public TypeComparator<T> duplicate()
Description copied from class:TypeComparatorCreates a copy of this class. The copy must be deep such that no state set in the copy affects this instance of the comparator class.- Specified by:
duplicatein classTypeComparator<T extends Tuple>- Returns:
- A deep copy of this comparator instance.
-
-