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.client;
019
020import java.util.Arrays;
021import org.apache.hadoop.hbase.HConstants;
022import org.apache.hadoop.hbase.util.Bytes;
023import org.apache.yetus.audience.InterfaceAudience;
024
025@InterfaceAudience.Private
026public class ClientUtil {
027
028
029  public static boolean areScanStartRowAndStopRowEqual(byte[] startRow, byte[] stopRow) {
030    return startRow != null && startRow.length > 0 && Bytes.equals(startRow, stopRow);
031  }
032
033  public static Cursor createCursor(byte[] row) {
034    return new Cursor(row);
035  }
036
037  /**
038   * <p>When scanning for a prefix the scan should stop immediately after the the last row that
039   * has the specified prefix. This method calculates the closest next rowKey immediately following
040   * the given rowKeyPrefix.</p>
041   * <p><b>IMPORTANT: This converts a rowKey<u>Prefix</u> into a rowKey</b>.</p>
042   * <p>If the prefix is an 'ASCII' string put into a byte[] then this is easy because you can
043   * simply increment the last byte of the array.
044   * But if your application uses real binary rowids you may run into the scenario that your
045   * prefix is something like:</p>
046   * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x23, 0xFF, 0xFF }</b><br/>
047   * Then this stopRow needs to be fed into the actual scan<br/>
048   * &nbsp;&nbsp;&nbsp;<b>{ 0x12, 0x24 }</b> (Notice that it is shorter now)<br/>
049   * This method calculates the correct stop row value for this usecase.
050   *
051   * @param rowKeyPrefix the rowKey<u>Prefix</u>.
052   * @return the closest next rowKey immediately following the given rowKeyPrefix.
053   */
054  public static byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) {
055    // Essentially we are treating it like an 'unsigned very very long' and doing +1 manually.
056    // Search for the place where the trailing 0xFFs start
057    int offset = rowKeyPrefix.length;
058    while (offset > 0) {
059      if (rowKeyPrefix[offset - 1] != (byte) 0xFF) {
060        break;
061      }
062      offset--;
063    }
064
065    if (offset == 0) {
066      // We got an 0xFFFF... (only FFs) stopRow value which is
067      // the last possible prefix before the end of the table.
068      // So set it to stop at the 'end of the table'
069      return HConstants.EMPTY_END_ROW;
070    }
071
072    // Copy the right length of the original
073    byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset);
074    // And increment the last one
075    newStopRow[newStopRow.length - 1]++;
076    return newStopRow;
077  }
078}