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 package org.apache.hadoop.hbase.client;
19
20 import java.util.Arrays;
21 import org.apache.hadoop.hbase.HConstants;
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
23 import org.apache.hadoop.hbase.util.Bytes;
24
25 @InterfaceAudience.Private
26 public class ClientUtil {
27
28
29 public static boolean areScanStartRowAndStopRowEqual(byte[] startRow, byte[] stopRow) {
30 return startRow != null && startRow.length > 0 && Bytes.equals(startRow, stopRow);
31 }
32
33 public static Cursor createCursor(byte[] row) {
34 return new Cursor(row);
35 }
36
37 /**
38 * <p>When scanning for a prefix the scan should stop immediately after the the last row that
39 * has the specified prefix. This method calculates the closest next rowKey immediately following
40 * the given rowKeyPrefix.</p>
41 * <p><b>IMPORTANT: This converts a rowKey<u>Prefix</u> into a rowKey</b>.</p>
42 * <p>If the prefix is an 'ASCII' string put into a byte[] then this is easy because you can
43 * simply increment the last byte of the array.
44 * But if your application uses real binary rowids you may run into the scenario that your
45 * prefix is something like:</p>
46 * <b>{ 0x12, 0x23, 0xFF, 0xFF }</b><br/>
47 * Then this stopRow needs to be fed into the actual scan<br/>
48 * <b>{ 0x12, 0x24 }</b> (Notice that it is shorter now)<br/>
49 * This method calculates the correct stop row value for this usecase.
50 *
51 * @param rowKeyPrefix the rowKey<u>Prefix</u>.
52 * @return the closest next rowKey immediately following the given rowKeyPrefix.
53 */
54 public static byte[] calculateTheClosestNextRowKeyForPrefix(byte[] rowKeyPrefix) {
55 // Essentially we are treating it like an 'unsigned very very long' and doing +1 manually.
56 // Search for the place where the trailing 0xFFs start
57 int offset = rowKeyPrefix.length;
58 while (offset > 0) {
59 if (rowKeyPrefix[offset - 1] != (byte) 0xFF) {
60 break;
61 }
62 offset--;
63 }
64
65 if (offset == 0) {
66 // We got an 0xFFFF... (only FFs) stopRow value which is
67 // the last possible prefix before the end of the table.
68 // So set it to stop at the 'end of the table'
69 return HConstants.EMPTY_END_ROW;
70 }
71
72 // Copy the right length of the original
73 byte[] newStopRow = Arrays.copyOfRange(rowKeyPrefix, 0, offset);
74 // And increment the last one
75 newStopRow[newStopRow.length - 1]++;
76 return newStopRow;
77 }
78 }