001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import org.apache.hadoop.hbase.util.Bytes;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.yetus.audience.InterfaceStability;
023
024/**
025 * Compare two HBase cells inner store, skip compare family for better performance. Important!!! we
026 * should not make fake cell with fake family which length greater than zero inner store, otherwise
027 * this optimization cannot be used.
028 */
029@InterfaceAudience.Private
030@InterfaceStability.Evolving
031public class InnerStoreCellComparator extends CellComparatorImpl {
032
033  private static final long serialVersionUID = 8186411895799094989L;
034
035  public static final InnerStoreCellComparator INNER_STORE_COMPARATOR =
036    new InnerStoreCellComparator();
037
038  @Override
039  protected int compareFamilies(Cell left, int leftFamilyLength, Cell right,
040    int rightFamilyLength) {
041    return leftFamilyLength - rightFamilyLength;
042  }
043
044  @Override
045  protected int compareFamilies(KeyValue left, int leftFamilyPosition, int leftFamilyLength,
046    KeyValue right, int rightFamilyPosition, int rightFamilyLength) {
047    return leftFamilyLength - rightFamilyLength;
048  }
049
050  @Override
051  protected int compareFamilies(ByteBufferKeyValue left, int leftFamilyPosition,
052    int leftFamilyLength, ByteBufferKeyValue right, int rightFamilyPosition,
053    int rightFamilyLength) {
054    return leftFamilyLength - rightFamilyLength;
055  }
056
057  @Override
058  protected int compareFamilies(KeyValue left, int leftFamilyPosition, int leftFamilyLength,
059    ByteBufferKeyValue right, int rightFamilyPosition, int rightFamilyLength) {
060    return leftFamilyLength - rightFamilyLength;
061  }
062
063  /**
064   * Utility method that makes a guess at comparator to use based off passed tableName. Use in
065   * extreme when no comparator specified.
066   * @return CellComparator to use going off the {@code tableName} passed.
067   */
068  public static CellComparator getInnerStoreCellComparator(TableName tableName) {
069    return getInnerStoreCellComparator(tableName.toBytes());
070  }
071
072  /**
073   * Utility method that makes a guess at comparator to use based off passed tableName. Use in
074   * extreme when no comparator specified.
075   * @return CellComparator to use going off the {@code tableName} passed.
076   */
077  public static CellComparator getInnerStoreCellComparator(byte[] tableName) {
078    return Bytes.equals(tableName, TableName.META_TABLE_NAME.toBytes())
079      ? MetaCellComparator.META_COMPARATOR
080      : InnerStoreCellComparator.INNER_STORE_COMPARATOR;
081  }
082}