diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java index 417655e4443..3948d166c9a 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java @@ -488,22 +488,42 @@ public class HTable implements HTableInterface { */ public List getRegionsInRange(final byte [] startKey, final byte [] endKey) throws IOException { - final boolean endKeyIsEndOfTable = Bytes.equals(endKey, - HConstants.EMPTY_END_ROW); + return getKeysAndRegionsInRange(startKey, endKey, false).getSecond(); + } + + /** + * Get the corresponding start keys and regions for an arbitrary range of + * keys. + *

+ * @param startKey Starting row in range, inclusive + * @param endKey Ending row in range + * @param includeEndKey true if endRow is inclusive, false if exclusive + * @return A pair of list of start keys and list of HRegionLocations that + * contain the specified range + * @throws IOException if a remote or network exception occurs + */ + private Pair, List> getKeysAndRegionsInRange( + final byte[] startKey, final byte[] endKey, final boolean includeEndKey) + throws IOException { + final boolean endKeyIsEndOfTable = Bytes.equals(endKey,HConstants.EMPTY_END_ROW); if ((Bytes.compareTo(startKey, endKey) > 0) && !endKeyIsEndOfTable) { throw new IllegalArgumentException( "Invalid range: " + Bytes.toStringBinary(startKey) + " > " + Bytes.toStringBinary(endKey)); } - final List regionList = new ArrayList(); - byte [] currentKey = startKey; + List keysInRange = new ArrayList(); + List regionsInRange = new ArrayList(); + byte[] currentKey = startKey; do { HRegionLocation regionLocation = getRegionLocation(currentKey, false); - regionList.add(regionLocation); + keysInRange.add(currentKey); + regionsInRange.add(regionLocation); currentKey = regionLocation.getRegionInfo().getEndKey(); - } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW) && - (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0)); - return regionList; + } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW) + && (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0 + || (includeEndKey && Bytes.compareTo(currentKey, endKey) == 0))); + return new Pair, List>(keysInRange, + regionsInRange); } /** @@ -1369,33 +1389,13 @@ public class HTable implements HTableInterface { private List getStartKeysInRange(byte[] start, byte[] end) throws IOException { - Pair startEndKeys = getStartEndKeys(); - byte[][] startKeys = startEndKeys.getFirst(); - byte[][] endKeys = startEndKeys.getSecond(); - if (start == null) { start = HConstants.EMPTY_START_ROW; } if (end == null) { end = HConstants.EMPTY_END_ROW; } - - List rangeKeys = new ArrayList(); - for (int i=0; i= 0 ) { - if (Bytes.equals(endKeys[i], HConstants.EMPTY_END_ROW) || - Bytes.compareTo(start, endKeys[i]) < 0) { - rangeKeys.add(start); - } - } else if (Bytes.equals(end, HConstants.EMPTY_END_ROW) || - Bytes.compareTo(startKeys[i], end) <= 0) { - rangeKeys.add(startKeys[i]); - } else { - break; // past stop - } - } - - return rangeKeys; + return getKeysAndRegionsInRange(start, end, true).getFirst(); } public void setOperationTimeout(int operationTimeout) {