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