diff --git a/libs/common/src/main/java/org/opensearch/common/Numbers.java b/libs/common/src/main/java/org/opensearch/common/Numbers.java index 8fb1922086c68..d5a364a4a934e 100644 --- a/libs/common/src/main/java/org/opensearch/common/Numbers.java +++ b/libs/common/src/main/java/org/opensearch/common/Numbers.java @@ -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() {} diff --git a/server/src/main/java/org/opensearch/index/search/comparators/UnsignedLongComparator.java b/server/src/main/java/org/opensearch/index/search/comparators/UnsignedLongComparator.java index e3817b2210665..22a818ab52547 100644 --- a/server/src/main/java/org/opensearch/index/search/comparators/UnsignedLongComparator.java +++ b/server/src/main/java/org/opensearch/index/search/comparators/UnsignedLongComparator.java @@ -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 */ @@ -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; } }