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;
021
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * This class is a server side extension to the {@link Cell} interface. It is used when the
026 * Cell is backed by a {@link ByteBuffer}: i.e. <code>cell instanceof ByteBufferedCell</code>.
027 *
028 * <p>This class has getters for the row, column family, column qualifier, value and tags hosting
029 * ByteBuffers. It also has getters of the *position* within a ByteBuffer where these
030 * field bytes begin. These are needed because a single ByteBuffer may back one or many Cell
031 * instances -- it depends on the implementation -- so the ByteBuffer position as returned by
032 * {@link ByteBuffer#arrayOffset()} cannot be relied upon. Also, do not confuse these position
033 * methods with the getXXXOffset methods from the super Interface, {@link Cell}; dependent up on
034 * implementation, the Cell getXXXOffset methods can return the same value as a call to its
035 * equivalent position method from below BUT they can also stray; if a ByteBufferedCell, use the
036 * below position methods to find where a field begins.
037 *
038 * <p>Use the getXXXLength methods from Cell to find a fields length.
039 *
040 * <p>A Cell object can be of this type only on the server side.
041 *
042 * <p>WARNING: If a Cell is backed by an offheap ByteBuffer, any call to getXXXArray() will result
043 * in a temporary byte array creation and a bytes copy. Avoid these allocations by using the
044 * appropriate Cell access server-side: i.e. ByteBufferedCell when backed by a ByteBuffer and Cell
045 * when it is not.
046 */
047/*
048 * Even though all the methods are abstract, ByteBufferExtendedCell is not made to be an interface
049 * with intent. In CellComparator compare method, we have instance of check to decide whether to
050 * use getXXXArray() or getXXXByteBuffer(). This is a very hot method in read and write paths.
051 * if (left instanceof ByteBufferExtendedCell && right instanceof ByteBufferExtendedCell) {
052      ....
053    }
054    if (left instanceof ByteBufferExtendedCell) {
055      ....
056    }
057    if (right instanceof ByteBufferExtendedCell) {
058      ....
059    }
060    return Bytes.compareTo(left.getRowArray(), left.getRowOffset(), left.getRowLength(),
061        right.getRowArray(), right.getRowOffset(), right.getRowLength());
062 * We did JMH micro benchmark tests with both left and right cells as ByteBufferExtendedCell, one
063 * only ByteBufferExtendedCell and both as Cells. This is compared against JMH results on compare
064 * logic with out any instance of checks. We noticed that if ByteBufferExtendedCell is an
065 * interface, the benchmark result seems to be very bad for case of both right and left are Cell
066 * only (Not ByteBufferExtendedCell). When ByteBufferExtendedCell is an abstract class all 4
067 * possible cases giving almost similar performance number compared with compare logic with no
068 * instance of checks.
069 */
070@InterfaceAudience.Private
071public abstract class ByteBufferExtendedCell implements ExtendedCell {
072  /**
073   * @return The {@link ByteBuffer} containing the row bytes.
074   */
075  public abstract ByteBuffer getRowByteBuffer();
076
077  /**
078   * @return Position in the {@link ByteBuffer} where row bytes start
079   */
080  public abstract int getRowPosition();
081
082  /**
083   * @return The {@link ByteBuffer} containing the column family bytes.
084   */
085  public abstract ByteBuffer getFamilyByteBuffer();
086
087  /**
088   * @return Position in the {@link ByteBuffer} where column family bytes start
089   */
090  public abstract int getFamilyPosition();
091
092  /**
093   * @return The {@link ByteBuffer} containing the column qualifier bytes.
094   */
095  public abstract ByteBuffer getQualifierByteBuffer();
096
097  /**
098   * @return Position in the {@link ByteBuffer} where column qualifier bytes start
099   */
100  public abstract int getQualifierPosition();
101
102  /**
103   * @return The {@link ByteBuffer} containing the value bytes.
104   */
105  public abstract ByteBuffer getValueByteBuffer();
106
107  /**
108   * @return Position in the {@link ByteBuffer} where value bytes start
109   */
110  public abstract int getValuePosition();
111
112  /**
113   * @return The {@link ByteBuffer} containing the tag bytes.
114   */
115  public abstract ByteBuffer getTagsByteBuffer();
116
117  /**
118   * @return Position in the {@link ByteBuffer} where tag bytes start
119   */
120  public abstract int getTagsPosition();
121}