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 java.util.Comparator;
021
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.yetus.audience.InterfaceStability;
024
025/**
026 * Comparator for comparing cells and has some specialized methods that allows comparing individual
027 * cell components like row, family, qualifier and timestamp
028 */
029@InterfaceAudience.Public
030@InterfaceStability.Evolving
031public interface CellComparator extends Comparator<Cell> {
032  /**
033   * A comparator for ordering cells in user-space tables. Useful when writing cells in sorted
034   * order as necessary for bulk import (i.e. via MapReduce)
035   * <p>
036   * CAUTION: This comparator may provide inaccurate ordering for cells from system tables,
037   * and should not be relied upon in that case.
038   */
039  static CellComparator getInstance() {
040    return CellComparatorImpl.COMPARATOR;
041  }
042
043  /**
044   * Lexographically compares two cells. The key part of the cell is taken for comparison which
045   * includes row, family, qualifier, timestamp and type
046   * @param leftCell the left hand side cell
047   * @param rightCell the right hand side cell
048   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
049   *         cells are equal
050   */
051  @Override
052  int compare(Cell leftCell, Cell rightCell);
053
054  /**
055   * Compare cells.
056   * @param ignoreSequenceid True if we are to compare the key portion only and ignore
057   * the sequenceid. Set to false to compare key and consider sequenceid.
058   * @return 0 if equal, -1 if a &lt; b, and +1 if a &gt; b.
059   */
060  int compare(Cell leftCell, Cell rightCell, boolean ignoreSequenceid);
061
062  /**
063   * Lexographically compares the rows of two cells.
064   * @param leftCell the left hand side cell
065   * @param rightCell the right hand side cell
066   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
067   *         cells are equal
068   */
069  int compareRows(Cell leftCell, Cell rightCell);
070
071  /**
072   * Compares the row part of the cell with a simple plain byte[] like the
073   * stopRow in Scan.
074   * @param cell the cell
075   * @param bytes the byte[] representing the row to be compared with
076   * @param offset the offset of the byte[]
077   * @param length the length of the byte[]
078   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
079   *         cells are equal
080   */
081  int compareRows(Cell cell, byte[] bytes, int offset, int length);
082
083  /**
084   * Lexographically compares the two cells excluding the row part. It compares family, qualifier,
085   * timestamp and the type
086   * @param leftCell the left hand side cell
087   * @param rightCell the right hand side cell
088   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
089   *         cells are equal
090   */
091  int compareWithoutRow(Cell leftCell, Cell rightCell);
092
093  /**
094   * Lexographically compares the families of the two cells
095   * @param leftCell the left hand side cell
096   * @param rightCell the right hand side cell
097   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
098   *         cells are equal
099   */
100  int compareFamilies(Cell leftCell, Cell rightCell);
101
102  /**
103   * Lexographically compares the qualifiers of the two cells
104   * @param leftCell the left hand side cell
105   * @param rightCell the right hand side cell
106   * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
107   *         cells are equal
108   */
109  int compareQualifiers(Cell leftCell, Cell rightCell);
110
111  /**
112   * Compares cell's timestamps in DESCENDING order. The below older timestamps sorting ahead of
113   * newer timestamps looks wrong but it is intentional. This way, newer timestamps are first found
114   * when we iterate over a memstore and newer versions are the first we trip over when reading from
115   * a store file.
116   * @param leftCell the left hand side cell
117   * @param rightCell the right hand side cell
118   * @return 1 if left's timestamp &lt; right's timestamp -1 if left's timestamp &gt; right's
119   *         timestamp 0 if both timestamps are equal
120   */
121  int compareTimestamps(Cell leftCell, Cell rightCell);
122
123  /**
124   * Compares cell's timestamps in DESCENDING order. The below older timestamps sorting ahead of
125   * newer timestamps looks wrong but it is intentional. This way, newer timestamps are first found
126   * when we iterate over a memstore and newer versions are the first we trip over when reading from
127   * a store file.
128   * @param leftCellts the left cell's timestamp
129   * @param rightCellts the right cell's timestamp
130   * @return 1 if left's timestamp &lt; right's timestamp -1 if left's timestamp &gt; right's
131   *         timestamp 0 if both timestamps are equal
132   */
133  int compareTimestamps(long leftCellts, long rightCellts);
134
135  /**
136   * @return A dumbed-down, fast comparator for hbase2 base-type, the {@link ByteBufferKeyValue}.
137   *   Create an instance when you make a new memstore, when you know only BBKVs will be passed.
138   *   Do not pollute with types other than BBKV if can be helped; the Comparator will slow.
139   */
140  Comparator getSimpleComparator();
141}