2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
;
20 import java
.nio
.ByteBuffer
;
21 import java
.util
.Comparator
;
22 import org
.apache
.hadoop
.hbase
.util
.ByteBufferUtils
;
23 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
24 import org
.apache
.yetus
.audience
.InterfaceAudience
;
25 import org
.apache
.yetus
.audience
.InterfaceStability
;
28 * Comparator for comparing cells and has some specialized methods that allows comparing individual
29 * cell components like row, family, qualifier and timestamp
31 @InterfaceAudience.Public
32 @InterfaceStability.Evolving
33 public interface CellComparator
extends Comparator
<Cell
> {
35 * A comparator for ordering cells in user-space tables. Useful when writing cells in sorted
36 * order as necessary for bulk import (i.e. via MapReduce).
38 * CAUTION: This comparator may provide inaccurate ordering for cells from system tables,
39 * and should not be relied upon in that case.
41 // For internal use, see CellComparatorImpl utility methods.
42 static CellComparator
getInstance() {
43 return CellComparatorImpl
.COMPARATOR
;
47 * Lexographically compares two cells. The key part of the cell is taken for comparison which
48 * includes row, family, qualifier, timestamp and type
49 * @param leftCell the left hand side cell
50 * @param rightCell the right hand side cell
51 * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
55 int compare(Cell leftCell
, Cell rightCell
);
59 * @param ignoreSequenceid True if we are to compare the key portion only and ignore
60 * the sequenceid. Set to false to compare key and consider sequenceid.
61 * @return 0 if equal, -1 if a < b, and +1 if a > b.
63 int compare(Cell leftCell
, Cell rightCell
, boolean ignoreSequenceid
);
66 * Lexographically compares the rows of two cells.
67 * @param leftCell the left hand side cell
68 * @param rightCell the right hand side cell
69 * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
72 int compareRows(Cell leftCell
, Cell rightCell
);
75 * Compares the row part of the cell with a simple plain byte[] like the
77 * @param cell the cell
78 * @param bytes the byte[] representing the row to be compared with
79 * @param offset the offset of the byte[]
80 * @param length the length of the byte[]
81 * @return greater than 0 if leftCell is bigger, less than 0 if rightCell is bigger, 0 if both
84 int compareRows(Cell cell
, byte[] bytes
, int offset
, int length
);
87 * Compares two row bytes
88 * @param leftRow the byte array of the left row
89 * @param rightRow the byte array of the right row
90 * @return greater than 0 if leftRow is bigger, less than 0 if rightRow is bigger, 0 if both
93 default int compareRows(byte[] leftRow
, byte[] rightRow
) {
94 return Bytes
.compareTo(leftRow
, rightRow
);
98 * @param row ByteBuffer that wraps a row; will read from current position and will reading all
99 * 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
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());
110 return ByteBufferUtils
.compareTo(row
, row
.position(), row
.remaining(),
111 cell
.getRowArray(), cell
.getRowOffset(),
112 cell
.getRowLength());
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
123 int compareWithoutRow(Cell leftCell
, Cell rightCell
);
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
132 int compareFamilies(Cell leftCell
, Cell rightCell
);
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
141 int compareQualifiers(Cell leftCell
, Cell rightCell
);
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
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
153 int compareTimestamps(Cell leftCell
, Cell rightCell
);
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
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
165 int compareTimestamps(long leftCellts
, long rightCellts
);
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.
172 Comparator
getSimpleComparator();