View Javadoc

1   /*
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import java.io.Serializable;
22  import java.util.Comparator;
23  
24  import org.apache.hadoop.hbase.KeyValue.Type;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  import com.google.common.primitives.Longs;
30  
31  /**
32   * Compare two HBase cells.  Do not use this method comparing <code>-ROOT-</code> or
33   * <code>hbase:meta</code> cells.  Cells from these tables need a specialized comparator, one that
34   * takes account of the special formatting of the row where we have commas to delimit table from
35   * regionname, from row.  See KeyValue for how it has a special comparator to do hbase:meta cells
36   * and yet another for -ROOT-.
37   */
38  @edu.umd.cs.findbugs.annotations.SuppressWarnings(
39      value="UNKNOWN",
40      justification="Findbugs doesn't like the way we are negating the result of a compare in below")
41  @InterfaceAudience.Private
42  @InterfaceStability.Evolving
43  public class CellComparator implements Comparator<Cell>, Serializable {
44    private static final long serialVersionUID = -8760041766259623329L;
45  
46    @Override
47    public int compare(Cell a, Cell b) {
48      return compare(a, b, false);
49    }
50  
51    /**
52     * Compare cells.
53     * TODO: Replace with dynamic rather than static comparator so can change comparator
54     * implementation.
55     * @param a
56     * @param b
57     * @param ignoreSequenceid True if we are to compare the key portion only and ignore
58     * the sequenceid. Set to false to compare key and consider sequenceid.
59     * @return 0 if equal, -1 if a < b, and +1 if a > b.
60     */
61    public static int compare(final Cell a, final Cell b, boolean ignoreSequenceid) {
62      // row
63      int c = compareRows(a, b);
64      if (c != 0) return c;
65  
66      c = compareWithoutRow(a, b);
67      if(c != 0) return c;
68  
69      if (!ignoreSequenceid) {
70        // Negate following comparisons so later edits show up first
71        // mvccVersion: later sorts first
72        return Longs.compare(b.getMvccVersion(), a.getMvccVersion());
73      } else {
74        return c;
75      }
76    }
77  
78    public static int findCommonPrefixInRowPart(Cell left, Cell right, int rowCommonPrefix) {
79      return findCommonPrefix(left.getRowArray(), right.getRowArray(), left.getRowLength()
80          - rowCommonPrefix, right.getRowLength() - rowCommonPrefix, left.getRowOffset()
81          + rowCommonPrefix, right.getRowOffset() + rowCommonPrefix);
82    }
83  
84    private static int findCommonPrefix(byte[] left, byte[] right, int leftLength, int rightLength,
85        int leftOffset, int rightOffset) {
86      int length = Math.min(leftLength, rightLength);
87      int result = 0;
88  
89      while (result < length && left[leftOffset + result] == right[rightOffset + result]) {
90        result++;
91      }
92      return result;
93    }
94  
95    public static int findCommonPrefixInFamilyPart(Cell left, Cell right, int familyCommonPrefix) {
96      return findCommonPrefix(left.getFamilyArray(), right.getFamilyArray(), left.getFamilyLength()
97          - familyCommonPrefix, right.getFamilyLength() - familyCommonPrefix, left.getFamilyOffset()
98          + familyCommonPrefix, right.getFamilyOffset() + familyCommonPrefix);
99    }
100 
101   public static int findCommonPrefixInQualifierPart(Cell left, Cell right,
102       int qualifierCommonPrefix) {
103     return findCommonPrefix(left.getQualifierArray(), right.getQualifierArray(),
104         left.getQualifierLength() - qualifierCommonPrefix, right.getQualifierLength()
105             - qualifierCommonPrefix, left.getQualifierOffset() + qualifierCommonPrefix,
106         right.getQualifierOffset() + qualifierCommonPrefix);
107   }
108 
109   /**************** equals ****************************/
110 
111   public static boolean equals(Cell a, Cell b){
112     return equalsRow(a, b)
113         && equalsFamily(a, b)
114         && equalsQualifier(a, b)
115         && equalsTimestamp(a, b)
116         && equalsType(a, b);
117   }
118 
119   public static boolean equalsRow(Cell a, Cell b){
120     return Bytes.equals(
121       a.getRowArray(), a.getRowOffset(), a.getRowLength(),
122       b.getRowArray(), b.getRowOffset(), b.getRowLength());
123   }
124 
125   public static boolean equalsFamily(Cell a, Cell b){
126     return Bytes.equals(
127       a.getFamilyArray(), a.getFamilyOffset(), a.getFamilyLength(),
128       b.getFamilyArray(), b.getFamilyOffset(), b.getFamilyLength());
129   }
130 
131   public static boolean equalsQualifier(Cell a, Cell b){
132     return Bytes.equals(
133       a.getQualifierArray(), a.getQualifierOffset(), a.getQualifierLength(),
134       b.getQualifierArray(), b.getQualifierOffset(), b.getQualifierLength());
135   }
136 
137   public static boolean equalsTimestamp(Cell a, Cell b){
138     return a.getTimestamp() == b.getTimestamp();
139   }
140 
141   public static boolean equalsType(Cell a, Cell b){
142     return a.getTypeByte() == b.getTypeByte();
143   }
144 
145   public static int compareColumns(final Cell left, final Cell right) {
146     int lfoffset = left.getFamilyOffset();
147     int rfoffset = right.getFamilyOffset();
148     int lclength = left.getQualifierLength();
149     int rclength = right.getQualifierLength();
150     int lfamilylength = left.getFamilyLength();
151     int rfamilylength = right.getFamilyLength();
152     int diff = compare(left.getFamilyArray(), lfoffset, lfamilylength, right.getFamilyArray(),
153         rfoffset, rfamilylength);
154     if (diff != 0) {
155       return diff;
156     } else {
157       return compare(left.getQualifierArray(), left.getQualifierOffset(), lclength,
158           right.getQualifierArray(), right.getQualifierOffset(), rclength);
159     }
160   }
161 
162   public static int compareFamilies(Cell left, Cell right) {
163     return Bytes.compareTo(left.getFamilyArray(), left.getFamilyOffset(), left.getFamilyLength(),
164         right.getFamilyArray(), right.getFamilyOffset(), right.getFamilyLength());
165   }
166 
167   public static int compareQualifiers(Cell left, Cell right) {
168     return Bytes.compareTo(left.getQualifierArray(), left.getQualifierOffset(),
169         left.getQualifierLength(), right.getQualifierArray(), right.getQualifierOffset(),
170         right.getQualifierLength());
171   }
172 
173   public int compareFlatKey(Cell left, Cell right) {
174     int compare = compareRows(left, right);
175     if (compare != 0) {
176       return compare;
177     }
178     return compareWithoutRow(left, right);
179   }
180 
181   /**
182    * Do not use comparing rows from hbase:meta. Meta table Cells have schema (table,startrow,hash)
183    * so can't be treated as plain byte arrays as this method does.
184    */
185   public static int compareRows(final Cell left, final Cell right) {
186     return Bytes.compareTo(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
187         right.getRowArray(), right.getRowOffset(), right.getRowLength());
188   }
189 
190   /**
191    * Do not use comparing rows from hbase:meta. Meta table Cells have schema (table,startrow,hash)
192    * so can't be treated as plain byte arrays as this method does.
193    */
194   public static int compareRows(byte[] left, int loffset, int llength, byte[] right, int roffset,
195       int rlength) {
196     return Bytes.compareTo(left, loffset, llength, right, roffset, rlength);
197   }
198 
199   public static int compareWithoutRow(final Cell leftCell, final Cell rightCell) {
200     // If the column is not specified, the "minimum" key type appears the
201     // latest in the sorted order, regardless of the timestamp. This is used
202     // for specifying the last key/value in a given row, because there is no
203     // "lexicographically last column" (it would be infinitely long). The
204     // "maximum" key type does not need this behavior.
205     // Copied from KeyValue. This is bad in that we can't do memcmp w/ special rules like this.
206     // TODO
207     if (leftCell.getFamilyLength() + leftCell.getQualifierLength() == 0
208           && leftCell.getTypeByte() == Type.Minimum.getCode()) {
209       // left is "bigger", i.e. it appears later in the sorted order
210       return 1;
211     }
212     if (rightCell.getFamilyLength() + rightCell.getQualifierLength() == 0
213         && rightCell.getTypeByte() == Type.Minimum.getCode()) {
214       return -1;
215     }
216     boolean sameFamilySize = (leftCell.getFamilyLength() == rightCell.getFamilyLength());
217     if (!sameFamilySize) {
218       // comparing column family is enough.
219 
220       return Bytes.compareTo(leftCell.getFamilyArray(), leftCell.getFamilyOffset(),
221           leftCell.getFamilyLength(), rightCell.getFamilyArray(), rightCell.getFamilyOffset(),
222           rightCell.getFamilyLength());
223     }
224     int diff = compareColumns(leftCell, rightCell);
225     if (diff != 0) return diff;
226 
227     diff = compareTimestamps(leftCell, rightCell);
228     if (diff != 0) return diff;
229 
230     // Compare types. Let the delete types sort ahead of puts; i.e. types
231     // of higher numbers sort before those of lesser numbers. Maximum (255)
232     // appears ahead of everything, and minimum (0) appears after
233     // everything.
234     return (0xff & rightCell.getTypeByte()) - (0xff & leftCell.getTypeByte());
235   }
236 
237   public static int compareTimestamps(final Cell left, final Cell right) {
238     long ltimestamp = left.getTimestamp();
239     long rtimestamp = right.getTimestamp();
240     return compareTimestamps(ltimestamp, rtimestamp);
241   }
242 
243   /********************* hashCode ************************/
244 
245   /**
246    * Returns a hash code that is always the same for two Cells having a matching equals(..) result.
247    * Currently does not guard against nulls, but it could if necessary.
248    */
249   public static int hashCode(Cell cell){
250     if (cell == null) {// return 0 for empty Cell
251       return 0;
252     }
253 
254     int hash = calculateHashForKeyValue(cell);
255     hash = 31 * hash + (int)cell.getMvccVersion();
256     return hash;
257   }
258 
259   /**
260    * Returns a hash code that is always the same for two Cells having a matching
261    * equals(..) result. Currently does not guard against nulls, but it could if
262    * necessary. Note : Ignore mvcc while calculating the hashcode
263    * 
264    * @param cell
265    * @return hashCode
266    */
267   public static int hashCodeIgnoreMvcc(Cell cell) {
268     if (cell == null) {// return 0 for empty Cell
269       return 0;
270     }
271 
272     int hash = calculateHashForKeyValue(cell);
273     return hash;
274   }
275 
276   private static int calculateHashForKeyValue(Cell cell) {
277     //pre-calculate the 3 hashes made of byte ranges
278     int rowHash = Bytes.hashCode(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
279     int familyHash =
280       Bytes.hashCode(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
281     int qualifierHash = Bytes.hashCode(cell.getQualifierArray(), cell.getQualifierOffset(),
282       cell.getQualifierLength());
283 
284     //combine the 6 sub-hashes
285     int hash = 31 * rowHash + familyHash;
286     hash = 31 * hash + qualifierHash;
287     hash = 31 * hash + (int)cell.getTimestamp();
288     hash = 31 * hash + cell.getTypeByte();
289     return hash;
290   }
291 
292 
293   /******************** lengths *************************/
294 
295   public static boolean areKeyLengthsEqual(Cell a, Cell b) {
296     return a.getRowLength() == b.getRowLength()
297         && a.getFamilyLength() == b.getFamilyLength()
298         && a.getQualifierLength() == b.getQualifierLength();
299   }
300 
301   public static boolean areRowLengthsEqual(Cell a, Cell b) {
302     return a.getRowLength() == b.getRowLength();
303   }
304 
305 
306   /*********************common prefixes*************************/
307 
308   private static int compare(byte[] left, int leftOffset, int leftLength, byte[] right,
309       int rightOffset, int rightLength) {
310     return Bytes.compareTo(left, leftOffset, leftLength, right, rightOffset, rightLength);
311   }
312 
313   public static int compareCommonRowPrefix(Cell left, Cell right, int rowCommonPrefix) {
314     return compare(left.getRowArray(), left.getRowOffset() + rowCommonPrefix, left.getRowLength()
315         - rowCommonPrefix, right.getRowArray(), right.getRowOffset() + rowCommonPrefix,
316         right.getRowLength() - rowCommonPrefix);
317   }
318 
319   public static int compareCommonFamilyPrefix(Cell left, Cell right,
320       int familyCommonPrefix) {
321     return compare(left.getFamilyArray(), left.getFamilyOffset() + familyCommonPrefix,
322         left.getFamilyLength() - familyCommonPrefix, right.getFamilyArray(),
323         right.getFamilyOffset() + familyCommonPrefix,
324         right.getFamilyLength() - familyCommonPrefix);
325   }
326 
327   public static int compareCommonQualifierPrefix(Cell left, Cell right,
328       int qualCommonPrefix) {
329     return compare(left.getQualifierArray(), left.getQualifierOffset() + qualCommonPrefix,
330         left.getQualifierLength() - qualCommonPrefix, right.getQualifierArray(),
331         right.getQualifierOffset() + qualCommonPrefix, right.getQualifierLength()
332             - qualCommonPrefix);
333   }
334 
335   /***************** special cases ****************************/
336   /**
337    * special case for KeyValue.equals
338    */
339   public static boolean equalsIgnoreMvccVersion(Cell a, Cell b){
340     return 0 == compareStaticIgnoreMvccVersion(a, b);
341   }
342 
343   private static int compareStaticIgnoreMvccVersion(Cell a, Cell b) {
344     // row
345     int c = compareRows(a, b);
346     if (c != 0) return c;
347 
348     // family
349     c = compareColumns(a, b);
350     if (c != 0) return c;
351 
352     // timestamp: later sorts first
353     c = compareTimestamps(a, b);
354     if (c != 0) return c;
355 
356     //type
357     c = (0xff & b.getTypeByte()) - (0xff & a.getTypeByte());
358     return c;
359   }
360 
361   private static int compareTimestamps(final long ltimestamp, final long rtimestamp) {
362     // The below older timestamps sorting ahead of newer timestamps looks
363     // wrong but it is intentional. This way, newer timestamps are first
364     // found when we iterate over a memstore and newer versions are the
365     // first we trip over when reading from a store file.
366     if (ltimestamp < rtimestamp) {
367       return 1;
368     } else if (ltimestamp > rtimestamp) {
369       return -1;
370     }
371     return 0;
372   }
373 
374   /**
375    * Counter part for the KeyValue.RowOnlyComparator
376    */
377   public static class RowComparator extends CellComparator {
378     @Override
379     public int compare(Cell a, Cell b) {
380       return compareRows(a, b);
381     }
382   }
383 
384   /**
385    * Try to return a Cell that falls between <code>left</code> and <code>right</code> but that is
386    * shorter; i.e. takes up less space. This trick is used building HFile block index.
387    * Its an optimization. It does not always work.  In this case we'll just return the
388    * <code>right</code> cell.
389    * @param comparator Comparator to use.
390    * @param left
391    * @param right
392    * @return A cell that sorts between <code>left</code> and <code>right</code>.
393    */
394   public static Cell getMidpoint(final KeyValue.KVComparator comparator, final Cell left,
395       final Cell right) {
396     // TODO: Redo so only a single pass over the arrays rather than one to compare and then a
397     // second composing midpoint.
398     if (right == null) {
399       throw new IllegalArgumentException("right cell can not be null");
400     }
401     if (left == null) {
402       return right;
403     }
404     // If Cells from meta table, don't mess around. meta table Cells have schema
405     // (table,startrow,hash) so can't be treated as plain byte arrays. Just skip out without
406     // trying to do this optimization.
407     if (comparator != null && comparator instanceof KeyValue.MetaComparator) {
408       return right;
409     }
410     int diff = compareRows(left, right);
411     if (diff > 0) {
412       throw new IllegalArgumentException("Left row sorts after right row; left=" +
413         CellUtil.getCellKeyAsString(left) + ", right=" + CellUtil.getCellKeyAsString(right));
414     }
415     if (diff < 0) {
416       // Left row is < right row.
417       byte [] midRow = getMinimumMidpointArray(left.getRowArray(), left.getRowOffset(),
418           left.getRowLength(),
419         right.getRowArray(), right.getRowOffset(), right.getRowLength());
420       // If midRow is null, just return 'right'.  Can't do optimization.
421       if (midRow == null) return right;
422       return CellUtil.createCell(midRow);
423     }
424     // Rows are same. Compare on families.
425     diff = compareFamilies(left, right);
426     if (diff > 0) {
427       throw new IllegalArgumentException("Left family sorts after right family; left=" +
428           CellUtil.getCellKeyAsString(left) + ", right=" + CellUtil.getCellKeyAsString(right));
429     }
430     if (diff < 0) {
431       byte [] midRow = getMinimumMidpointArray(left.getFamilyArray(), left.getFamilyOffset(),
432           left.getFamilyLength(),
433         right.getFamilyArray(), right.getFamilyOffset(), right.getFamilyLength());
434       // If midRow is null, just return 'right'.  Can't do optimization.
435       if (midRow == null) return right;
436       // Return new Cell where we use right row and then a mid sort family.
437       return CellUtil.createCell(right.getRowArray(), right.getRowOffset(), right.getRowLength(),
438         midRow, 0, midRow.length, HConstants.EMPTY_BYTE_ARRAY, 0,
439         HConstants.EMPTY_BYTE_ARRAY.length);
440     }
441     // Families are same. Compare on qualifiers.
442     diff = compareQualifiers(left, right);
443     if (diff > 0) {
444       throw new IllegalArgumentException("Left qualifier sorts after right qualifier; left=" +
445           CellUtil.getCellKeyAsString(left) + ", right=" + CellUtil.getCellKeyAsString(right));
446     }
447     if (diff < 0) {
448       byte [] midRow = getMinimumMidpointArray(left.getQualifierArray(), left.getQualifierOffset(),
449           left.getQualifierLength(),
450         right.getQualifierArray(), right.getQualifierOffset(), right.getQualifierLength());
451       // If midRow is null, just return 'right'.  Can't do optimization.
452       if (midRow == null) return right;
453       // Return new Cell where we use right row and family and then a mid sort qualifier.
454       return CellUtil.createCell(right.getRowArray(), right.getRowOffset(), right.getRowLength(),
455         right.getFamilyArray(), right.getFamilyOffset(), right.getFamilyLength(),
456         midRow, 0, midRow.length);
457     }
458     // No opportunity for optimization. Just return right key.
459     return right;
460   }
461 
462   /**
463    * @param leftArray
464    * @param leftOffset
465    * @param leftLength
466    * @param rightArray
467    * @param rightOffset
468    * @param rightLength
469    * @return Return a new array that is between left and right and minimally sized else just return
470    * null as indicator that we could not create a mid point.
471    */
472   private static byte [] getMinimumMidpointArray(final byte [] leftArray, final int leftOffset,
473         final int leftLength,
474       final byte [] rightArray, final int rightOffset, final int rightLength) {
475     // rows are different
476     int minLength = leftLength < rightLength ? leftLength : rightLength;
477     short diffIdx = 0;
478     while (diffIdx < minLength &&
479         leftArray[leftOffset + diffIdx] == rightArray[rightOffset + diffIdx]) {
480       diffIdx++;
481     }
482     byte [] minimumMidpointArray = null;
483     if (diffIdx >= minLength) {
484       // leftKey's row is prefix of rightKey's.
485       minimumMidpointArray = new byte[diffIdx + 1];
486       System.arraycopy(rightArray, rightOffset, minimumMidpointArray, 0, diffIdx + 1);
487     } else {
488       int diffByte = leftArray[leftOffset + diffIdx];
489       if ((0xff & diffByte) < 0xff && (diffByte + 1) < (rightArray[rightOffset + diffIdx] & 0xff)) {
490         minimumMidpointArray = new byte[diffIdx + 1];
491         System.arraycopy(leftArray, leftOffset, minimumMidpointArray, 0, diffIdx);
492         minimumMidpointArray[diffIdx] = (byte) (diffByte + 1);
493       } else {
494         minimumMidpointArray = new byte[diffIdx + 1];
495         System.arraycopy(rightArray, rightOffset, minimumMidpointArray, 0, diffIdx + 1);
496       }
497     }
498     return minimumMidpointArray;
499   }
500 }