HBASE-20928 Rewrite calculation of midpoint in binarySearch functions to prevent overflow
HBASE-20928 Rewrite calculation of midpoint - addendum (Xu Cang) Signed-off-by: tedyu <yuzhihong@gmail.com> Conflicts: hbase-common/src/main/java/org/apache/hadoop/hbase/io/encoding/RowIndexSeekerV1.java hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java Amending-Author: Andrew Purtell <apurtell@apache.org>
This commit is contained in:
parent
2f541f5710
commit
231e6d56b5
|
@ -154,15 +154,13 @@ public class RowIndexSeekerV1 extends AbstractEncodedSeeker {
|
|||
private int binarySearch(Cell seekCell, boolean seekBefore) {
|
||||
int low = 0;
|
||||
int high = rowNumber - 1;
|
||||
int mid = (low + high) >>> 1;
|
||||
int mid = low + ((high - low) >> 1);
|
||||
int comp = 0;
|
||||
SimpleMutableByteRange row = new SimpleMutableByteRange();
|
||||
while (low <= high) {
|
||||
mid = (low + high) >>> 1;
|
||||
mid = low + ((high - low) >> 1);
|
||||
getRow(mid, row);
|
||||
comp = comparator.compareRows(row.getBytes(), row.getOffset(),
|
||||
row.getLength(), seekCell.getRowArray(), seekCell.getRowOffset(),
|
||||
seekCell.getRowLength());
|
||||
comp = compareRows(row, seekCell);
|
||||
if (comp < 0) {
|
||||
low = mid + 1;
|
||||
} else if (comp > 0) {
|
||||
|
@ -184,6 +182,12 @@ public class RowIndexSeekerV1 extends AbstractEncodedSeeker {
|
|||
}
|
||||
}
|
||||
|
||||
private int compareRows(SimpleMutableByteRange row, Cell seekCell) {
|
||||
return comparator.compareRows(row.getBytes(), row.getOffset(),
|
||||
row.getLength(), seekCell.getRowArray(), seekCell.getRowOffset(),
|
||||
seekCell.getRowLength());
|
||||
}
|
||||
|
||||
private void getRow(int index, SimpleMutableByteRange row) {
|
||||
int offset = Bytes.toIntUnsafe(rowOffsets.array(), rowOffsets.arrayOffset()
|
||||
+ (index << 2)); // index * Bytes.SIZEOF_INT
|
||||
|
|
|
@ -2145,7 +2145,7 @@ public class Bytes implements Comparable<Bytes> {
|
|||
int high = arr.length - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low+high) >>> 1;
|
||||
int mid = low + ((high - low) >> 1);
|
||||
// we have to compare in this order, because the comparator order
|
||||
// has special logic when the 'left side' is a special key.
|
||||
int cmp = comparator.compare(key, offset, length,
|
||||
|
@ -2182,7 +2182,7 @@ public class Bytes implements Comparable<Bytes> {
|
|||
int high = arr.length - 1;
|
||||
KeyValue.KeyOnlyKeyValue r = new KeyValue.KeyOnlyKeyValue();
|
||||
while (low <= high) {
|
||||
int mid = (low+high) >>> 1;
|
||||
int mid = low + ((high - low) >> 1);
|
||||
// we have to compare in this order, because the comparator order
|
||||
// has special logic when the 'left side' is a special key.
|
||||
r.setKey(arr[mid], 0, arr[mid].length);
|
||||
|
@ -2357,7 +2357,7 @@ public class Bytes implements Comparable<Bytes> {
|
|||
int high = toIndex - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) >>> 1;
|
||||
int mid = low + ((high - low) >> 1);
|
||||
int midVal = a[mid] & 0xff;
|
||||
|
||||
if (midVal < unsignedKey) {
|
||||
|
|
|
@ -533,7 +533,7 @@ public class HFileBlockIndex {
|
|||
// keys[low - 1] < key < keys[high + 1] while narrowing down the range.
|
||||
KeyValue.KeyOnlyKeyValue nonRootIndexKV = new KeyValue.KeyOnlyKeyValue();
|
||||
while (low <= high) {
|
||||
mid = (low + high) >>> 1;
|
||||
mid = low + ((high - low) >> 1);
|
||||
|
||||
// Midkey's offset relative to the end of secondary index
|
||||
int midKeyRelOffset = nonRootIndex.getInt(
|
||||
|
|
|
@ -119,7 +119,7 @@ public class BoundedPriorityBlockingQueue<E> extends AbstractQueue<E> implements
|
|||
|
||||
private int upperBound(int start, int end, E key) {
|
||||
while (start < end) {
|
||||
int mid = (start + end) >>> 1;
|
||||
int mid = start + ((end - start) >> 1);
|
||||
E mitem = objects[mid];
|
||||
int cmp = comparator.compare(mitem, key);
|
||||
if (cmp > 0) {
|
||||
|
|
Loading…
Reference in New Issue