diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ColumnFamilyDescriptor.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ColumnFamilyDescriptor.java index 76b53330e6..5cc71b7ee7 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ColumnFamilyDescriptor.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ColumnFamilyDescriptor.java @@ -52,6 +52,7 @@ public interface ColumnFamilyDescriptor { } return lhs.getConfiguration().hashCode() - rhs.getConfiguration().hashCode(); }; + Comparator x = Bytes.comparingBytes(ColumnFamilyDescriptor::getName).thenComparingInt((ColumnFamilyDescriptor cfd) -> cfd.getValues().hashCode()); /** * @return The storefile/hfile blocksize for this column family. diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java index 5e6500bcee..ae05d73d26 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java @@ -24,6 +24,7 @@ import static org.apache.hadoop.hbase.shaded.com.google.common.base.Precondition import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; @@ -35,6 +36,8 @@ import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.Objects; +import java.util.function.Function; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -349,6 +352,7 @@ public class Bytes implements Comparable { return LexicographicalComparerHolder.BEST_COMPARER. compareTo(b1, s1, l1, b2, s2, l2); } + } /** @@ -1421,6 +1425,26 @@ public class Bytes implements Comparable { compareTo(buffer1, offset1, length1, buffer2, offset2, length2); } + /** + * Accepts a function that extracts a {@code byte[]} sort key from a type + * {@code T}, and returns a {@code Comparator} that compares by that + * sort key. + * + *

The returned comparator is serializable if the specified function + * is also serializable. + * + * @param the type of element to be compared + * @param keyExtractor the function used to extract the integer sort key + * @return a comparator that compares by an extracted key + * @see #comparing(Function) + * @throws NullPointerException if the argument is null + */ + public static Comparator comparingBytes(Function keyExtractor) { + Objects.requireNonNull(keyExtractor); + return (Comparator & Serializable) + (c1, c2) -> Bytes.compareTo(keyExtractor.apply(c1), keyExtractor.apply(c2)); + } + interface Comparer { int compareTo( T buffer1, int offset1, int length1, T buffer2, int offset2, int length2