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