Skip to content

Commit 409f760

Browse files
committed
Address MinAndMax generics warnings
The MinAndMax encapsulates min and max values for a shard. It uses generics to make sure that the values are of the same type and are also comparable. Though there are warnings whenever this class is currently used, which are addressed with this commit. Relates to elastic#49092
1 parent cb9be14 commit 409f760

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

server/src/main/java/org/elasticsearch/action/search/CanMatchPreFilterSearchPhase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private static List<SearchShardIterator> sortShards(GroupShardsIterator<SearchSh
122122
return IntStream.range(0, shardsIts.size())
123123
.boxed()
124124
.sorted(shardComparator(shardsIts, minAndMaxes, order))
125-
.map(ord -> shardsIts.get(ord))
125+
.map(shardsIts::get)
126126
.collect(Collectors.toList());
127127
}
128128

server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public static FieldSortBuilder getPrimaryFieldSortOrNull(SearchSourceBuilder sou
434434
* {@link SortField}. This is needed for {@link SortField} that converts values from one type to another using
435435
* {@link FieldSortBuilder#setNumericType(String)} )} (e.g.: long to double).
436436
*/
437-
private static Function<byte[], Comparable> numericPointConverter(SortField sortField, NumberFieldType numberFieldType) {
437+
private static Function<byte[], Comparable<?>> numericPointConverter(SortField sortField, NumberFieldType numberFieldType) {
438438
switch (IndexSortConfig.getSortFieldType(sortField)) {
439439
case LONG:
440440
return v -> numberFieldType.parsePoint(v).longValue();
@@ -457,7 +457,7 @@ private static Function<byte[], Comparable> numericPointConverter(SortField sort
457457
* Return a {@link Function} that converts a serialized date point into a {@link Long} according to the provided
458458
* {@link NumericType}.
459459
*/
460-
private static Function<byte[], Comparable> datePointConverter(DateFieldType dateFieldType, String numericTypeStr) {
460+
private static Function<byte[], Comparable<?>> datePointConverter(DateFieldType dateFieldType, String numericTypeStr) {
461461
if (numericTypeStr != null) {
462462
NumericType numericType = resolveNumericType(numericTypeStr);
463463
if (dateFieldType.resolution() == MILLISECONDS && numericType == NumericType.DATE_NANOSECONDS) {
@@ -491,7 +491,7 @@ public static MinAndMax<?> getMinMaxOrNull(QueryShardContext context, FieldSortB
491491
case INT:
492492
case DOUBLE:
493493
case FLOAT:
494-
final Function<byte[], Comparable> converter;
494+
final Function<byte[], Comparable<?>> converter;
495495
if (fieldType instanceof NumberFieldType) {
496496
converter = numericPointConverter(sortField, (NumberFieldType) fieldType);
497497
} else if (fieldType instanceof DateFieldType) {
@@ -502,9 +502,7 @@ public static MinAndMax<?> getMinMaxOrNull(QueryShardContext context, FieldSortB
502502
if (PointValues.size(reader, fieldName) == 0) {
503503
return null;
504504
}
505-
final Comparable min = converter.apply(PointValues.getMinPackedValue(reader, fieldName));
506-
final Comparable max = converter.apply(PointValues.getMaxPackedValue(reader, fieldName));
507-
return MinAndMax.newMinMax(min, max);
505+
return extractMinAndMax(reader, fieldName, converter);
508506

509507
case STRING:
510508
case STRING_VAL:
@@ -520,6 +518,14 @@ public static MinAndMax<?> getMinMaxOrNull(QueryShardContext context, FieldSortB
520518
return null;
521519
}
522520

521+
@SuppressWarnings("unchecked")
522+
private static <T extends Comparable<T>> MinAndMax<T> extractMinAndMax(IndexReader reader, String fieldName,
523+
Function<byte[], Comparable<?>> converter) throws IOException {
524+
final T min = (T)converter.apply(PointValues.getMinPackedValue(reader, fieldName));
525+
final T max = (T)converter.apply(PointValues.getMaxPackedValue(reader, fieldName));
526+
return MinAndMax.newMinMax(min, max);
527+
}
528+
523529
/**
524530
* Throws an exception if max children is not located at top level nested sort.
525531
*/
@@ -601,12 +607,12 @@ public static FieldSortBuilder fromXContent(XContentParser parser, String fieldN
601607
private static final ObjectParser<FieldSortBuilder, Void> PARSER = new ObjectParser<>(NAME);
602608

603609
static {
604-
PARSER.declareField(FieldSortBuilder::missing, p -> p.objectText(), MISSING, ValueType.VALUE);
610+
PARSER.declareField(FieldSortBuilder::missing, XContentParser::objectText, MISSING, ValueType.VALUE);
605611
PARSER.declareString(FieldSortBuilder::unmappedType , UNMAPPED_TYPE);
606612
PARSER.declareString((b, v) -> b.order(SortOrder.fromString(v)) , ORDER_FIELD);
607613
PARSER.declareString((b, v) -> b.sortMode(SortMode.fromString(v)), SORT_MODE);
608614
PARSER.declareObject(FieldSortBuilder::setNestedSort, (p, c) -> NestedSortBuilder.fromXContent(p), NESTED_FIELD);
609-
PARSER.declareString((b, v) -> b.setNumericType(v), NUMERIC_TYPE);
615+
PARSER.declareString(FieldSortBuilder::setNumericType, NUMERIC_TYPE);
610616
}
611617

612618
@Override

server/src/main/java/org/elasticsearch/search/sort/MinAndMax.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
import java.util.Objects;
3030

3131
/**
32-
* A class that encapsulates a minimum and a maximum {@link Comparable}.
32+
* A class that encapsulates a minimum and a maximum, that are of the same type and {@link Comparable}.
3333
*/
34-
public class MinAndMax<T extends Comparable<? super T>> implements Writeable {
34+
public class MinAndMax<T extends Comparable<T>> implements Writeable {
3535
private final T minValue;
3636
private final T maxValue;
3737

@@ -40,9 +40,10 @@ private MinAndMax(T minValue, T maxValue) {
4040
this.maxValue = Objects.requireNonNull(maxValue);
4141
}
4242

43+
@SuppressWarnings("unchecked")
4344
public MinAndMax(StreamInput in) throws IOException {
44-
this.minValue = (T) Lucene.readSortValue(in);
45-
this.maxValue = (T) Lucene.readSortValue(in);
45+
this.minValue = (T)Lucene.readSortValue(in);
46+
this.maxValue = (T)Lucene.readSortValue(in);
4647
}
4748

4849
@Override
@@ -54,27 +55,28 @@ public void writeTo(StreamOutput out) throws IOException {
5455
/**
5556
* Return the minimum value.
5657
*/
57-
public T getMin() {
58+
T getMin() {
5859
return minValue;
5960
}
6061

6162
/**
6263
* Return the maximum value.
6364
*/
64-
public T getMax() {
65+
T getMax() {
6566
return maxValue;
6667
}
6768

68-
public static <T extends Comparable<? super T>> MinAndMax<T> newMinMax(T min, T max) {
69+
public static <T extends Comparable<T>> MinAndMax<T> newMinMax(T min, T max) {
6970
return new MinAndMax<>(min, max);
7071
}
7172

7273
/**
7374
* Return a {@link Comparator} for {@link MinAndMax} values according to the provided {@link SortOrder}.
7475
*/
7576
public static Comparator<MinAndMax<?>> getComparator(SortOrder order) {
76-
Comparator<MinAndMax> cmp = order == SortOrder.ASC ?
77-
Comparator.comparing(v -> (Comparable) v.getMin()) : Comparator.comparing(v -> (Comparable) v.getMax());
77+
Comparator<MinAndMax<?>> cmp = order == SortOrder.ASC ?
78+
Comparator.comparing(MinAndMax::getMin) : Comparator.comparing(MinAndMax::getMax);
79+
7880
if (order == SortOrder.DESC) {
7981
cmp = cmp.reversed();
8082
}

0 commit comments

Comments
 (0)