Skip to content

Commit

Permalink
Simplified unsigned to signed long conversion
Browse files Browse the repository at this point in the history
Signed-off-by: expani <[email protected]>
  • Loading branch information
expani committed Dec 27, 2024
1 parent 7bc5cbf commit 54ac23e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
4 changes: 2 additions & 2 deletions libs/common/src/main/java/org/opensearch/common/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public final class Numbers {
public static final long MIN_UNSIGNED_LONG_VALUE_AS_LONG = MIN_UNSIGNED_LONG_VALUE.longValue();
public static final long MAX_UNSIGNED_LONG_VALUE_AS_LONG = MAX_UNSIGNED_LONG_VALUE.longValue();

public static final BigInteger MAX_LONG_VALUE = BigInteger.valueOf(Long.MAX_VALUE);
public static final BigInteger MIN_LONG_VALUE = BigInteger.valueOf(Long.MIN_VALUE);
private static final BigInteger MAX_LONG_VALUE = BigInteger.valueOf(Long.MAX_VALUE);
private static final BigInteger MIN_LONG_VALUE = BigInteger.valueOf(Long.MIN_VALUE);

private Numbers() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws I

@Override
protected long missingValueAsComparableLong() {
return unsignedLongToSignedLong(missingValue);
return unSignedToSignedLong(missingValue);
}

@Override
protected long sortableBytesToLong(byte[] bytes) {
return unsignedLongToSignedLong(NumericUtils.sortableBytesToBigInt(bytes, 0, BigIntegerPoint.BYTES));
return unSignedToSignedLong(NumericUtils.sortableBytesToBigInt(bytes, 0, BigIntegerPoint.BYTES));
}

/** Leaf comparator for {@link UnsignedLongComparator} that provides skipping functionality */
Expand Down Expand Up @@ -100,27 +100,19 @@ public void copy(int slot, int doc) throws IOException {

@Override
protected long bottomAsComparableLong() {
return unsignedLongToSignedLong(bottom);
return unSignedToSignedLong(bottom);
}

@Override
protected long topAsComparableLong() {
return unsignedLongToSignedLong(topValue);
return unSignedToSignedLong(topValue);
}
}

/**
* Handles overflow in conversion.
* @param value : Unsigned long
* @return : Signed long representing value's position in number line as much as possible within bounds.
* Converts unsigned long to signed the same as java.lang.{@link Long#compareUnsigned(long, long)}
*/
private static long unsignedLongToSignedLong(BigInteger value) {
if (value.compareTo(Numbers.MIN_LONG_VALUE) < 0) {
return Long.MIN_VALUE;
} else if (value.compareTo(Numbers.MAX_LONG_VALUE) > 0) {
return Long.MAX_VALUE;
} else {
return value.longValue();
}
private static long unSignedToSignedLong(BigInteger value) {
return value.longValue() + Long.MIN_VALUE;
}
}

0 comments on commit 54ac23e

Please sign in to comment.