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