Skip to content

Commit

Permalink
revert Rounding logic
Browse files Browse the repository at this point in the history
Signed-off-by: bowenlan-amzn <[email protected]>
  • Loading branch information
bowenlan-amzn committed Feb 7, 2025
1 parent 99eabbf commit 5475b5a
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions server/src/main/java/org/opensearch/common/Rounding.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.time.temporal.TemporalQueries;
import java.time.zone.ZoneOffsetTransition;
import java.time.zone.ZoneRules;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
Expand Down Expand Up @@ -452,7 +453,7 @@ protected Prepared maybeUseArray(long minUtcMillis, long maxUtcMillis, int max)
values = ArrayUtil.grow(values, i + 1);
values[i++] = rounded;
}
return new ArrayRounding(RoundableFactory.create(values, i), this);
return new ArrayRounding(values, i, this);
}
}

Expand All @@ -461,17 +462,26 @@ protected Prepared maybeUseArray(long minUtcMillis, long maxUtcMillis, int max)
* pre-calculated round-down points to speed up lookups.
*/
private static class ArrayRounding implements Prepared {
private final Roundable roundable;
private final long[] values;
private final int max;
private final Prepared delegate;

public ArrayRounding(Roundable roundable, Prepared delegate) {
this.roundable = roundable;
private ArrayRounding(long[] values, int max, Prepared delegate) {
this.values = values;
this.max = max;
this.delegate = delegate;
}

@Override
public long round(long utcMillis) {
return roundable.floor(utcMillis);
assert values[0] <= utcMillis : utcMillis + " must be after " + values[0];
int idx = Arrays.binarySearch(values, 0, max, utcMillis);
assert idx != -1 : "The insertion point is before the array! This should have tripped the assertion above.";
assert -1 - idx <= values.length : "This insertion point is after the end of the array.";
if (idx < 0) {
idx = -2 - idx;
}
return values[idx];
}

@Override
Expand Down

0 comments on commit 5475b5a

Please sign in to comment.