1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hbase.cell;
20
21 import java.io.Serializable;
22 import java.util.Comparator;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.classification.InterfaceStability;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.apache.hbase.Cell;
28
29 import com.google.common.primitives.Longs;
30
31
32
33
34
35
36 @InterfaceAudience.Private
37 @InterfaceStability.Evolving
38 public class CellComparator implements Comparator<Cell>, Serializable{
39 private static final long serialVersionUID = -8760041766259623329L;
40
41 @Override
42 public int compare(Cell a, Cell b) {
43 return compareStatic(a, b);
44 }
45
46
47 public static int compareStatic(Cell a, Cell b) {
48
49 int c = Bytes.compareTo(
50 a.getRowArray(), a.getRowOffset(), a.getRowLength(),
51 b.getRowArray(), b.getRowOffset(), b.getRowLength());
52 if (c != 0) return c;
53
54
55 c = Bytes.compareTo(
56 a.getFamilyArray(), a.getFamilyOffset(), a.getFamilyLength(),
57 b.getFamilyArray(), b.getFamilyOffset(), b.getFamilyLength());
58 if (c != 0) return c;
59
60
61 c = Bytes.compareTo(
62 a.getQualifierArray(), a.getQualifierOffset(), a.getQualifierLength(),
63 b.getQualifierArray(), b.getQualifierOffset(), b.getQualifierLength());
64 if (c != 0) return c;
65
66
67 c = -Longs.compare(a.getTimestamp(), b.getTimestamp());
68 if (c != 0) return c;
69
70
71 c = (0xff & a.getTypeByte()) - (0xff & b.getTypeByte());
72 if (c != 0) return c;
73
74
75 return -Longs.compare(a.getMvccVersion(), b.getMvccVersion());
76 }
77
78
79
80
81 public static boolean equals(Cell a, Cell b){
82 if (!areKeyLengthsEqual(a, b)) {
83 return false;
84 }
85
86 return 0 == compareStatic(a, b);
87 }
88
89 public static boolean equalsRow(Cell a, Cell b){
90 if(!areRowLengthsEqual(a, b)){
91 return false;
92 }
93 return 0 == Bytes.compareTo(
94 a.getRowArray(), a.getRowOffset(), a.getRowLength(),
95 b.getRowArray(), b.getRowOffset(), b.getRowLength());
96 }
97
98
99
100
101
102
103
104
105 public static int hashCode(Cell cell){
106 if (cell == null) {
107 return 0;
108 }
109
110
111 int rowHash = Bytes.hashCode(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
112 int familyHash = Bytes.hashCode(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
113 int qualifierHash = Bytes.hashCode(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
114
115
116 int hash = 31 * rowHash + familyHash;
117 hash = 31 * hash + qualifierHash;
118 hash = 31 * hash + (int)cell.getTimestamp();
119 hash = 31 * hash + cell.getTypeByte();
120 hash = 31 * hash + (int)cell.getMvccVersion();
121 return hash;
122 }
123
124
125
126
127 public static boolean areKeyLengthsEqual(Cell a, Cell b) {
128 return a.getRowLength() == b.getRowLength()
129 && a.getFamilyLength() == b.getFamilyLength()
130 && a.getQualifierLength() == b.getQualifierLength();
131 }
132
133 public static boolean areRowLengthsEqual(Cell a, Cell b) {
134 return a.getRowLength() == b.getRowLength();
135 }
136
137
138
139
140
141
142
143 private static int compareStaticIgnoreMvccVersion(Cell a, Cell b) {
144
145 int c = Bytes.compareTo(
146 a.getRowArray(), a.getRowOffset(), a.getRowLength(),
147 b.getRowArray(), b.getRowOffset(), b.getRowLength());
148 if (c != 0) return c;
149
150
151 c = Bytes.compareTo(
152 a.getFamilyArray(), a.getFamilyOffset(), a.getFamilyLength(),
153 b.getFamilyArray(), b.getFamilyOffset(), b.getFamilyLength());
154 if (c != 0) return c;
155
156
157 c = Bytes.compareTo(
158 a.getQualifierArray(), a.getQualifierOffset(), a.getQualifierLength(),
159 b.getQualifierArray(), b.getQualifierOffset(), b.getQualifierLength());
160 if (c != 0) return c;
161
162
163 c = -Longs.compare(a.getTimestamp(), b.getTimestamp());
164 if (c != 0) return c;
165
166
167 c = (0xff & a.getTypeByte()) - (0xff & b.getTypeByte());
168 return c;
169 }
170
171
172
173
174 public static boolean equalsIgnoreMvccVersion(Cell a, Cell b){
175 return 0 == compareStaticIgnoreMvccVersion(a, b);
176 }
177
178 }