Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-26635 Optimize decodeNumeric in OrderedBytes #3986

Merged
merged 5 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ private static BigDecimal decodeSignificand(PositionedByteRange src, int e, bool
byte[] a = src.getBytes();
final int start = src.getPosition(), offset = src.getOffset(), remaining = src.getRemaining();
Order ord = comp ? DESCENDING : ASCENDING;
BigDecimal m = BigDecimal.ZERO;
e--;
BigDecimal m;
StringBuilder sb = new StringBuilder();
for (int i = 0;; i++) {
if (i > remaining) {
// we've exceeded this range's window
Expand All @@ -566,18 +566,23 @@ private static BigDecimal decodeSignificand(PositionedByteRange src, int e, bool
"Read exceeds range before termination byte found. offset: " + offset + " position: "
+ (start + i));
}
// one byte -> 2 digits
// base-100 digits are encoded as val * 2 + 1 except for the termination digit.
m = m.add( // m +=
new BigDecimal(BigInteger.ONE, e * -2).multiply( // 100 ^ p * [decoded digit]
BigDecimal.valueOf((ord.apply(a[offset + start + i]) & 0xff) / 2)));
e--;
int twoDigits = (ord.apply(a[offset + start + i]) & 0xff) / 2;
sb.append(String.format("%02d", twoDigits));
// detect termination digit
if ((ord.apply(a[offset + start + i]) & 1) == 0) {
// Besides, as we will normalise the return value at last,
// we only need to decode at most MAX_PRECISION + 2 digits here.
if ((ord.apply(a[offset + start + i]) & 1) == 0 || sb.length() > MAX_PRECISION + 1) {
src.setPosition(start + i + 1);
break;
}
}
return normalize(m);
m = new BigDecimal(sb.toString());
int stepsMoveLeft = sb.charAt(0) != '0' ? m.precision() : m.precision() + 1;
stepsMoveLeft -= e * 2;

return normalize(m.movePointLeft(stepsMoveLeft));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
Expand Down Expand Up @@ -1319,4 +1320,20 @@ public void testEncodedValueCheck() {
OrderedBytes.skip(buff);
}
}

@Test
public void testEncodeDecodeMatch() {
BigDecimal[] randomData = new BigDecimal[200];
Random rand = new Random(System.currentTimeMillis());
Apache9 marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < randomData.length; i++) {
randomData[i] = BigDecimal.valueOf(rand.nextDouble() + rand.nextLong());
PositionedByteRange tmp = new SimplePositionedMutableByteRange(100);
Order ord = rand.nextBoolean() ? Order.DESCENDING : Order.ASCENDING;
OrderedBytes.encodeNumeric(tmp, randomData[i], ord);
tmp.setPosition(0);
BigDecimal left = OrderedBytes.normalize(randomData[i]);
BigDecimal right = OrderedBytes.decodeNumericAsBigDecimal(tmp);
assertEquals(0, left.compareTo(right));
}
}
}