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.DataOutputStream;
22  import java.io.IOException;
23  import java.nio.ByteBuffer;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map.Entry;
27  import java.util.NavigableMap;
28  
29  import org.apache.hadoop.classification.InterfaceAudience;
30  import org.apache.hadoop.classification.InterfaceStability;
31  import org.apache.hadoop.hbase.KeyValue.Type;
32  import org.apache.hadoop.hbase.io.HeapSize;
33  import org.apache.hadoop.hbase.util.ByteBufferUtils;
34  import org.apache.hadoop.hbase.util.ByteRange;
35  import org.apache.hadoop.hbase.util.Bytes;
36  
37  /**
38   * Utility methods helpful slinging {@link Cell} instances.
39   */
40  @InterfaceAudience.Public
41  @InterfaceStability.Evolving
42  public final class CellUtil {
43  
44    /******************* ByteRange *******************************/
45  
46    public static ByteRange fillRowRange(Cell cell, ByteRange range) {
47      return range.set(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
48    }
49  
50    public static ByteRange fillFamilyRange(Cell cell, ByteRange range) {
51      return range.set(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
52    }
53  
54    public static ByteRange fillQualifierRange(Cell cell, ByteRange range) {
55      return range.set(cell.getQualifierArray(), cell.getQualifierOffset(),
56        cell.getQualifierLength());
57    }
58  
59    public static ByteRange fillTagRange(Cell cell, ByteRange range) {
60      return range.set(cell.getTagsArray(), cell.getTagsOffset(), cell.getTagsLength());
61    }
62  
63    /***************** get individual arrays for tests ************/
64  
65    public static byte[] cloneRow(Cell cell){
66      byte[] output = new byte[cell.getRowLength()];
67      copyRowTo(cell, output, 0);
68      return output;
69    }
70  
71    public static byte[] cloneFamily(Cell cell){
72      byte[] output = new byte[cell.getFamilyLength()];
73      copyFamilyTo(cell, output, 0);
74      return output;
75    }
76  
77    public static byte[] cloneQualifier(Cell cell){
78      byte[] output = new byte[cell.getQualifierLength()];
79      copyQualifierTo(cell, output, 0);
80      return output;
81    }
82  
83    public static byte[] cloneValue(Cell cell){
84      byte[] output = new byte[cell.getValueLength()];
85      copyValueTo(cell, output, 0);
86      return output;
87    }
88  
89    /**
90     * Returns tag value in a new byte array. If server-side, use
91     * {@link Tag#getBuffer()} with appropriate {@link Tag#getTagOffset()} and
92     * {@link Tag#getTagLength()} instead to save on allocations.
93     * @param cell
94     * @return tag value in a new byte array.
95     */
96    public static byte[] getTagArray(Cell cell){
97      byte[] output = new byte[cell.getTagsLength()];
98      copyTagTo(cell, output, 0);
99      return output;
100   }
101 
102 
103   /******************** copyTo **********************************/
104 
105   public static int copyRowTo(Cell cell, byte[] destination, int destinationOffset) {
106     System.arraycopy(cell.getRowArray(), cell.getRowOffset(), destination, destinationOffset,
107       cell.getRowLength());
108     return destinationOffset + cell.getRowLength();
109   }
110 
111   public static int copyFamilyTo(Cell cell, byte[] destination, int destinationOffset) {
112     System.arraycopy(cell.getFamilyArray(), cell.getFamilyOffset(), destination, destinationOffset,
113       cell.getFamilyLength());
114     return destinationOffset + cell.getFamilyLength();
115   }
116 
117   public static int copyQualifierTo(Cell cell, byte[] destination, int destinationOffset) {
118     System.arraycopy(cell.getQualifierArray(), cell.getQualifierOffset(), destination,
119       destinationOffset, cell.getQualifierLength());
120     return destinationOffset + cell.getQualifierLength();
121   }
122 
123   public static int copyValueTo(Cell cell, byte[] destination, int destinationOffset) {
124     System.arraycopy(cell.getValueArray(), cell.getValueOffset(), destination, destinationOffset,
125         cell.getValueLength());
126     return destinationOffset + cell.getValueLength();
127   }
128 
129   /**
130    * Copies the tags info into the tag portion of the cell
131    * @param cell
132    * @param destination
133    * @param destinationOffset
134    * @return position after tags
135    */
136   public static int copyTagTo(Cell cell, byte[] destination, int destinationOffset) {
137     System.arraycopy(cell.getTagsArray(), cell.getTagsOffset(), destination, destinationOffset,
138         cell.getTagsLength());
139     return destinationOffset + cell.getTagsLength();
140   }
141 
142   /********************* misc *************************************/
143 
144   public static byte getRowByte(Cell cell, int index) {
145     return cell.getRowArray()[cell.getRowOffset() + index];
146   }
147 
148   public static ByteBuffer getValueBufferShallowCopy(Cell cell) {
149     ByteBuffer buffer = ByteBuffer.wrap(cell.getValueArray(), cell.getValueOffset(),
150       cell.getValueLength());
151     return buffer;
152   }
153 
154   public static ByteBuffer getQualifierBufferShallowCopy(Cell cell) {
155     ByteBuffer buffer = ByteBuffer.wrap(cell.getQualifierArray(), cell.getQualifierOffset(),
156         cell.getQualifierLength());
157     return buffer;
158   }
159 
160   public static Cell createCell(final byte [] row, final byte [] family, final byte [] qualifier,
161       final long timestamp, final byte type, final byte [] value) {
162     // I need a Cell Factory here.  Using KeyValue for now. TODO.
163     // TODO: Make a new Cell implementation that just carries these
164     // byte arrays.
165     return new KeyValue(row, family, qualifier, timestamp,
166       KeyValue.Type.codeToType(type), value);
167   }
168 
169   public static Cell createCell(final byte[] row, final byte[] family, final byte[] qualifier,
170       final long timestamp, final byte type, final byte[] value, final long memstoreTS) {
171     KeyValue keyValue = new KeyValue(row, family, qualifier, timestamp,
172         KeyValue.Type.codeToType(type), value);
173     keyValue.setSequenceId(memstoreTS);
174     return keyValue;
175   }
176 
177   public static Cell createCell(final byte[] row, final byte[] family, final byte[] qualifier,
178       final long timestamp, final byte type, final byte[] value, byte[] tags, final long memstoreTS) {
179     KeyValue keyValue = new KeyValue(row, family, qualifier, timestamp,
180         KeyValue.Type.codeToType(type), value, tags);
181     keyValue.setSequenceId(memstoreTS);
182     return keyValue;
183   }
184 
185   public static Cell createCell(final byte[] row, final byte[] family, final byte[] qualifier,
186       final long timestamp, Type type, final byte[] value, byte[] tags) {
187     KeyValue keyValue = new KeyValue(row, family, qualifier, timestamp, type, value, tags);
188     return keyValue;
189   }
190 
191   /**
192    * @param cellScannerables
193    * @return CellScanner interface over <code>cellIterables</code>
194    */
195   public static CellScanner createCellScanner(final List<? extends CellScannable> cellScannerables) {
196     return new CellScanner() {
197       private final Iterator<? extends CellScannable> iterator = cellScannerables.iterator();
198       private CellScanner cellScanner = null;
199 
200       @Override
201       public Cell current() {
202         return this.cellScanner != null? this.cellScanner.current(): null;
203       }
204 
205       @Override
206       public boolean advance() throws IOException {
207         while (true) {
208           if (this.cellScanner == null) {
209             if (!this.iterator.hasNext()) return false;
210             this.cellScanner = this.iterator.next().cellScanner();
211           }
212           if (this.cellScanner.advance()) return true;
213           this.cellScanner = null;
214         }
215       }
216     };
217   }
218 
219   /**
220    * @param cellIterable
221    * @return CellScanner interface over <code>cellIterable</code>
222    */
223   public static CellScanner createCellScanner(final Iterable<Cell> cellIterable) {
224     if (cellIterable == null) return null;
225     return createCellScanner(cellIterable.iterator());
226   }
227 
228   /**
229    * @param cells
230    * @return CellScanner interface over <code>cellIterable</code> or null if <code>cells</code> is
231    * null
232    */
233   public static CellScanner createCellScanner(final Iterator<Cell> cells) {
234     if (cells == null) return null;
235     return new CellScanner() {
236       private final Iterator<Cell> iterator = cells;
237       private Cell current = null;
238 
239       @Override
240       public Cell current() {
241         return this.current;
242       }
243 
244       @Override
245       public boolean advance() {
246         boolean hasNext = this.iterator.hasNext();
247         this.current = hasNext? this.iterator.next(): null;
248         return hasNext;
249       }
250     };
251   }
252 
253   /**
254    * @param cellArray
255    * @return CellScanner interface over <code>cellArray</code>
256    */
257   public static CellScanner createCellScanner(final Cell[] cellArray) {
258     return new CellScanner() {
259       private final Cell [] cells = cellArray;
260       private int index = -1;
261 
262       @Override
263       public Cell current() {
264         if (cells == null) return null;
265         return (index < 0)? null: this.cells[index];
266       }
267 
268       @Override
269       public boolean advance() {
270         if (cells == null) return false;
271         return ++index < this.cells.length;
272       }
273     };
274   }
275 
276   /**
277    * Flatten the map of cells out under the CellScanner
278    * @param map Map of Cell Lists; for example, the map of families to Cells that is used
279    * inside Put, etc., keeping Cells organized by family.
280    * @return CellScanner interface over <code>cellIterable</code>
281    */
282   public static CellScanner createCellScanner(final NavigableMap<byte [], List<Cell>> map) {
283     return new CellScanner() {
284       private final Iterator<Entry<byte[], List<Cell>>> entries = map.entrySet().iterator();
285       private Iterator<Cell> currentIterator = null;
286       private Cell currentCell;
287 
288       @Override
289       public Cell current() {
290         return this.currentCell;
291       }
292 
293       @Override
294       public boolean advance() {
295         while(true) {
296           if (this.currentIterator == null) {
297             if (!this.entries.hasNext()) return false;
298             this.currentIterator = this.entries.next().getValue().iterator();
299           }
300           if (this.currentIterator.hasNext()) {
301             this.currentCell = this.currentIterator.next();
302             return true;
303           }
304           this.currentCell = null;
305           this.currentIterator = null;
306         }
307       }
308     };
309   }
310 
311   /**
312    * @param left
313    * @param right
314    * @return True if the rows in <code>left</code> and <code>right</code> Cells match
315    */
316   public static boolean matchingRow(final Cell left, final Cell right) {
317     return Bytes.equals(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
318         right.getRowArray(), right.getRowOffset(), right.getRowLength());
319   }
320 
321   public static boolean matchingRow(final Cell left, final byte[] buf) {
322     return Bytes.equals(left.getRowArray(), left.getRowOffset(), left.getRowLength(), buf, 0,
323         buf.length);
324   }
325 
326   public static boolean matchingRow(final Cell left, final byte[] buf, final int offset,
327       final int length) {
328     return Bytes.equals(left.getRowArray(), left.getRowOffset(), left.getRowLength(), buf, offset,
329         length);
330   }
331 
332   public static boolean matchingFamily(final Cell left, final Cell right) {
333     return Bytes.equals(left.getFamilyArray(), left.getFamilyOffset(), left.getFamilyLength(),
334         right.getFamilyArray(), right.getFamilyOffset(), right.getFamilyLength());
335   }
336 
337   public static boolean matchingFamily(final Cell left, final byte[] buf) {
338     return Bytes.equals(left.getFamilyArray(), left.getFamilyOffset(), left.getFamilyLength(), buf,
339         0, buf.length);
340   }
341 
342   public static boolean matchingFamily(final Cell left, final byte[] buf, final int offset,
343       final int length) {
344     return Bytes.equals(left.getFamilyArray(), left.getFamilyOffset(), left.getFamilyLength(), buf,
345         offset, length);
346   }
347 
348   public static boolean matchingQualifier(final Cell left, final Cell right) {
349     return Bytes.equals(left.getQualifierArray(), left.getQualifierOffset(),
350         left.getQualifierLength(), right.getQualifierArray(), right.getQualifierOffset(),
351         right.getQualifierLength());
352   }
353 
354   public static boolean matchingQualifier(final Cell left, final byte[] buf) {
355     if (buf == null) {
356       return left.getQualifierLength() == 0;
357     }
358     return Bytes.equals(left.getQualifierArray(), left.getQualifierOffset(),
359         left.getQualifierLength(), buf, 0, buf.length);
360   }
361 
362   public static boolean matchingQualifier(final Cell left, final byte[] buf, final int offset,
363       final int length) {
364     if (buf == null) {
365       return left.getQualifierLength() == 0;
366     }
367     return Bytes.equals(left.getQualifierArray(), left.getQualifierOffset(),
368         left.getQualifierLength(), buf, offset, length);
369   }
370 
371   public static boolean matchingColumn(final Cell left, final byte[] fam, final byte[] qual) {
372     if (!matchingFamily(left, fam))
373       return false;
374     return matchingQualifier(left, qual);
375   }
376 
377   public static boolean matchingColumn(final Cell left, final byte[] fam, final int foffset,
378       final int flength, final byte[] qual, final int qoffset, final int qlength) {
379     if (!matchingFamily(left, fam, foffset, flength))
380       return false;
381     return matchingQualifier(left, qual, qoffset, qlength);
382   }
383 
384   public static boolean matchingColumn(final Cell left, final Cell right) {
385     if (!matchingFamily(left, right))
386       return false;
387     return matchingQualifier(left, right);
388   }
389 
390   public static boolean matchingValue(final Cell left, final Cell right) {
391     return Bytes.equals(left.getValueArray(), left.getValueOffset(), left.getValueLength(),
392         right.getValueArray(), right.getValueOffset(), right.getValueLength());
393   }
394 
395   public static boolean matchingValue(final Cell left, final byte[] buf) {
396     return Bytes.equals(left.getValueArray(), left.getValueOffset(), left.getValueLength(), buf, 0,
397         buf.length);
398   }
399 
400   /**
401    * @return True if a delete type, a {@link KeyValue.Type#Delete} or a
402    *         {KeyValue.Type#DeleteFamily} or a
403    *         {@link KeyValue.Type#DeleteColumn} KeyValue type.
404    */
405   public static boolean isDelete(final Cell cell) {
406     return isDelete(cell.getTypeByte());
407   }
408 
409   /**
410    * @return True if a delete type, a {@link KeyValue.Type#Delete} or a
411    *         {KeyValue.Type#DeleteFamily} or a
412    *         {@link KeyValue.Type#DeleteColumn} KeyValue type.
413    */
414   public static boolean isDelete(final byte type) {
415     return Type.Delete.getCode() <= type
416         && type <= Type.DeleteFamily.getCode();
417   }
418 
419   /**
420    * @return True if this cell is a {@link KeyValue.Type#Delete} type.
421    */
422   public static boolean isDeleteType(Cell cell) {
423     return cell.getTypeByte() == Type.Delete.getCode();
424   }
425 
426   public static boolean isDeleteFamily(final Cell cell) {
427     return cell.getTypeByte() == Type.DeleteFamily.getCode();
428   }
429 
430   public static boolean isDeleteFamilyVersion(final Cell cell) {
431     return cell.getTypeByte() == Type.DeleteFamilyVersion.getCode();
432   }
433 
434   public static boolean isDeleteColumns(final Cell cell) {
435     return cell.getTypeByte() == Type.DeleteColumn.getCode();
436   }
437 
438   public static boolean isDeleteColumnVersion(final Cell cell) {
439     return cell.getTypeByte() == Type.Delete.getCode();
440   }
441 
442   /**
443    *
444    * @return True if this cell is a delete family or column type.
445    */
446   public static boolean isDeleteColumnOrFamily(Cell cell) {
447     int t = cell.getTypeByte();
448     return t == Type.DeleteColumn.getCode() || t == Type.DeleteFamily.getCode();
449   }
450 
451   /**
452    * @param cell
453    * @return Estimate of the <code>cell</code> size in bytes.
454    */
455   public static int estimatedSizeOf(final Cell cell) {
456     // If a KeyValue, we can give a good estimate of size.
457     if (cell instanceof KeyValue) {
458       return ((KeyValue)cell).getLength() + Bytes.SIZEOF_INT;
459     }
460     // TODO: Should we add to Cell a sizeOf?  Would it help? Does it make sense if Cell is
461     // prefix encoded or compressed?
462     return cell.getRowLength() + cell.getFamilyLength() +
463       cell.getQualifierLength() +
464       cell.getValueLength() +
465       // Use the KeyValue's infrastructure size presuming that another implementation would have
466       // same basic cost.
467       KeyValue.KEY_INFRASTRUCTURE_SIZE +
468       // Serialization is probably preceded by a length (it is in the KeyValueCodec at least).
469       Bytes.SIZEOF_INT;
470   }
471 
472   /**
473    * This is an estimate of the heap space occupied by a cell. When the cell is of type
474    * {@link HeapSize} we call {@link HeapSize#heapSize()} so cell can give a correct value. In other
475    * cases we just consider the bytes occupied by the cell components ie. row, CF, qualifier,
476    * timestamp, type, value and tags.
477    * @param cell
478    * @return estimate of the heap space
479    */
480   public static long estimatedHeapSizeOf(final Cell cell) {
481     if (cell instanceof HeapSize) {
482       return ((HeapSize) cell).heapSize();
483     }
484     return cell.getRowLength() + cell.getFamilyLength() + cell.getQualifierLength()
485         + cell.getValueLength() + cell.getTagsLength() + KeyValue.TIMESTAMP_TYPE_SIZE;
486   }
487 
488   /********************* tags *************************************/
489   /**
490    * Util method to iterate through the tags
491    * 
492    * @param tags
493    * @param offset
494    * @param length
495    * @return iterator for the tags
496    */
497   public static Iterator<Tag> tagsIterator(final byte[] tags, final int offset, final int length) {
498     return new Iterator<Tag>() {
499       private int pos = offset;
500       private int endOffset = offset + length - 1;
501 
502       @Override
503       public boolean hasNext() {
504         return this.pos < endOffset;
505       }
506 
507       @Override
508       public Tag next() {
509         if (hasNext()) {
510           int curTagLen = Bytes.readAsInt(tags, this.pos, Tag.TAG_LENGTH_SIZE);
511           Tag tag = new Tag(tags, pos, curTagLen + Tag.TAG_LENGTH_SIZE);
512           this.pos += Bytes.SIZEOF_SHORT + curTagLen;
513           return tag;
514         }
515         return null;
516       }
517 
518       @Override
519       public void remove() {
520         throw new UnsupportedOperationException();
521       }
522     };
523   }
524 
525   /**
526    * Returns true if the first range start1...end1 overlaps with the second range
527    * start2...end2, assuming the byte arrays represent row keys
528    */
529   public static boolean overlappingKeys(final byte[] start1, final byte[] end1,
530       final byte[] start2, final byte[] end2) {
531     return (end2.length == 0 || start1.length == 0 || Bytes.compareTo(start1,
532         end2) < 0)
533         && (end1.length == 0 || start2.length == 0 || Bytes.compareTo(start2,
534             end1) < 0);
535   }
536 
537   /**
538    * Sets the given seqId to the cell.
539    * @param cell
540    * @param seqId
541    * @throws IOException when the passed cell is not of type {@link SettableSequenceId}
542    */
543   public static void setSequenceId(Cell cell, long seqId) throws IOException {
544     if (cell instanceof SettableSequenceId) {
545       ((SettableSequenceId) cell).setSequenceId(seqId);
546     } else {
547       throw new IOException(new UnsupportedOperationException("Cell is not of type "
548           + SettableSequenceId.class.getName()));
549     }
550   }
551 
552   /**
553    * Estimation of total number of bytes used by the cell to store its key, value and tags. When the
554    * cell is a {@link KeyValue} we include the extra infrastructure size used by it.
555    * @param cell
556    * @return estimated length
557    */
558   public static int estimatedLengthOf(final Cell cell) {
559     if (cell instanceof KeyValue) {
560       return ((KeyValue)cell).getLength();
561     }
562     return cell.getRowLength() + cell.getFamilyLength() + cell.getQualifierLength()
563         + cell.getValueLength() + cell.getTagsLength() + KeyValue.TIMESTAMP_TYPE_SIZE;
564   }
565 
566   /**
567    * Writes the Cell's key part as it would have serialized in a KeyValue. The format is &lt;2 bytes
568    * rk len&gt;&lt;rk&gt;&lt;1 byte cf len&gt;&lt;cf&gt;&lt;qualifier&gt;&lt;8 bytes
569    * timestamp&gt;&lt;1 byte type&gt;
570    * @param cell
571    * @param out
572    * @throws IOException
573    */
574   public static void writeFlatKey(Cell cell, DataOutputStream out) throws IOException {
575     short rowLen = cell.getRowLength();
576     out.writeShort(rowLen);
577     out.write(cell.getRowArray(), cell.getRowOffset(), rowLen);
578     byte fLen = cell.getFamilyLength();
579     out.writeByte(fLen);
580     out.write(cell.getFamilyArray(), cell.getFamilyOffset(), fLen);
581     out.write(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
582     out.writeLong(cell.getTimestamp());
583     out.writeByte(cell.getTypeByte());
584   }
585 
586   /**
587    * Write rowkey excluding the common part.
588    * @param cell
589    * @param rLen
590    * @param commonPrefix
591    * @param out
592    * @throws IOException
593    */
594   public static void writeRowKeyExcludingCommon(Cell cell, short rLen, int commonPrefix,
595       DataOutputStream out) throws IOException {
596     if (commonPrefix == 0) {
597       out.writeShort(rLen);
598     } else if (commonPrefix == 1) {
599       out.writeByte((byte) rLen);
600       commonPrefix--;
601     } else {
602       commonPrefix -= KeyValue.ROW_LENGTH_SIZE;
603     }
604     if (rLen > commonPrefix) {
605       out.write(cell.getRowArray(), cell.getRowOffset() + commonPrefix, rLen - commonPrefix);
606     }
607   }
608 
609   /**
610    * Find length of common prefix in keys of the cells, considering key as byte[] if serialized in
611    * {@link KeyValue}. The key format is &lt;2 bytes rk len&gt;&lt;rk&gt;&lt;1 byte cf
612    * len&gt;&lt;cf&gt;&lt;qualifier&gt;&lt;8 bytes timestamp&gt;&lt;1 byte type&gt;
613    * @param c1
614    *          the cell
615    * @param c2
616    *          the cell
617    * @param bypassFamilyCheck
618    *          when true assume the family bytes same in both cells. Pass it as true when dealing
619    *          with Cells in same CF so as to avoid some checks
620    * @param withTsType
621    *          when true check timestamp and type bytes also.
622    * @return length of common prefix
623    */
624   public static int findCommonPrefixInFlatKey(Cell c1, Cell c2, boolean bypassFamilyCheck,
625       boolean withTsType) {
626     // Compare the 2 bytes in RK length part
627     short rLen1 = c1.getRowLength();
628     short rLen2 = c2.getRowLength();
629     int commonPrefix = KeyValue.ROW_LENGTH_SIZE;
630     if (rLen1 != rLen2) {
631       // early out when the RK length itself is not matching
632       return ByteBufferUtils.findCommonPrefix(Bytes.toBytes(rLen1), 0, KeyValue.ROW_LENGTH_SIZE,
633           Bytes.toBytes(rLen2), 0, KeyValue.ROW_LENGTH_SIZE);
634     }
635     // Compare the RKs
636     int rkCommonPrefix = ByteBufferUtils.findCommonPrefix(c1.getRowArray(), c1.getRowOffset(),
637         rLen1, c2.getRowArray(), c2.getRowOffset(), rLen2);
638     commonPrefix += rkCommonPrefix;
639     if (rkCommonPrefix != rLen1) {
640       // Early out when RK is not fully matching.
641       return commonPrefix;
642     }
643     // Compare 1 byte CF length part
644     byte fLen1 = c1.getFamilyLength();
645     if (bypassFamilyCheck) {
646       // This flag will be true when caller is sure that the family will be same for both the cells
647       // Just make commonPrefix to increment by the family part
648       commonPrefix += KeyValue.FAMILY_LENGTH_SIZE + fLen1;
649     } else {
650       byte fLen2 = c2.getFamilyLength();
651       if (fLen1 != fLen2) {
652         // early out when the CF length itself is not matching
653         return commonPrefix;
654       }
655       // CF lengths are same so there is one more byte common in key part
656       commonPrefix += KeyValue.FAMILY_LENGTH_SIZE;
657       // Compare the CF names
658       int fCommonPrefix = ByteBufferUtils.findCommonPrefix(c1.getFamilyArray(),
659           c1.getFamilyOffset(), fLen1, c2.getFamilyArray(), c2.getFamilyOffset(), fLen2);
660       commonPrefix += fCommonPrefix;
661       if (fCommonPrefix != fLen1) {
662         return commonPrefix;
663       }
664     }
665     // Compare the Qualifiers
666     int qLen1 = c1.getQualifierLength();
667     int qLen2 = c2.getQualifierLength();
668     int qCommon = ByteBufferUtils.findCommonPrefix(c1.getQualifierArray(), c1.getQualifierOffset(),
669         qLen1, c2.getQualifierArray(), c2.getQualifierOffset(), qLen2);
670     commonPrefix += qCommon;
671     if (!withTsType || Math.max(qLen1, qLen2) != qCommon) {
672       return commonPrefix;
673     }
674     // Compare the timestamp parts
675     int tsCommonPrefix = ByteBufferUtils.findCommonPrefix(Bytes.toBytes(c1.getTimestamp()), 0,
676         KeyValue.TIMESTAMP_SIZE, Bytes.toBytes(c2.getTimestamp()), 0, KeyValue.TIMESTAMP_SIZE);
677     commonPrefix += tsCommonPrefix;
678     if (tsCommonPrefix != KeyValue.TIMESTAMP_SIZE) {
679       return commonPrefix;
680     }
681     // Compare the type
682     if (c1.getTypeByte() == c2.getTypeByte()) {
683       commonPrefix += KeyValue.TYPE_SIZE;
684     }
685     return commonPrefix;
686   }
687 }