martijnvg commented on code in PR #15436:
URL: https://github.com/apache/lucene/pull/15436#discussion_r2549076928


##########
lucene/core/src/java/org/apache/lucene/search/NumericFieldReaderContextComparator.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.ToLongFunction;
+import org.apache.lucene.index.DocValuesSkipper;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.PointValues;
+
+class NumericFieldReaderContextComparator implements 
Comparator<LeafReaderContext> {
+
+  private final Map<Integer, Long> cachedSortValues = new HashMap<>();
+  private final String field;
+  private final boolean reverse;
+  private final Long missingValue;
+  private final ToLongFunction<byte[]> pointDecoder;
+
+  public NumericFieldReaderContextComparator(
+      String field, Long missingValue, boolean reverse, ToLongFunction<byte[]> 
pointDecoder) {
+    this.field = field;
+    this.missingValue = missingValue;
+    this.reverse = reverse;
+    this.pointDecoder = pointDecoder;
+  }
+
+  @Override
+  public int compare(LeafReaderContext o1, LeafReaderContext o2) {
+    return reverse
+        ? Long.compare(getSortValue(o2), getSortValue(o1))
+        : Long.compare(getSortValue(o1), getSortValue(o2));
+  }
+
+  private long getSortValue(LeafReaderContext ctx) {
+    if (cachedSortValues.containsKey(ctx.ord) == false) {
+      cachedSortValues.put(ctx.ord, loadSortValue(ctx));
+    }
+    return cachedSortValues.get(ctx.ord);
+  }
+
+  private long loadSortValue(LeafReaderContext ctx) {
+    LeafReader reader = ctx.reader();
+    try {
+      DocValuesSkipper skipper = reader.getDocValuesSkipper(field);
+      if (skipper != null) {
+        if (skipper.docCount() == reader.maxDoc() || missingValue == null) {
+          return reverse ? skipper.maxValue() : skipper.minValue();
+        }
+        if (reverse) {
+          return Math.max(skipper.maxValue(), missingValue);
+        } else {
+          return Math.min(skipper.minValue(), missingValue);
+        }
+      }
+      PointValues pointValues = reader.getPointValues(field);
+      if (pointValues != null) {
+        if (pointValues.getDocCount() == reader.maxDoc() || missingValue == 
null) {
+          if (reverse) {
+            return pointDecoder.applyAsLong(pointValues.getMaxPackedValue());
+          } else {
+            return pointDecoder.applyAsLong(pointValues.getMinPackedValue());
+          }
+        }
+        if (reverse) {
+          return 
Math.max(pointDecoder.applyAsLong(pointValues.getMaxPackedValue()), 
missingValue);
+        } else {
+          return 
Math.min(pointDecoder.applyAsLong(pointValues.getMinPackedValue()), 
missingValue);
+        }
+      }
+    } catch (IOException _) {
+      return reverse ? Long.MAX_VALUE : Long.MIN_VALUE;

Review Comment:
   Shouldn't an `IOException` be re-thrown?



##########
lucene/core/src/java/org/apache/lucene/search/NumericFieldReaderContextComparator.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.ToLongFunction;
+import org.apache.lucene.index.DocValuesSkipper;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.PointValues;
+
+class NumericFieldReaderContextComparator implements 
Comparator<LeafReaderContext> {
+
+  private final Map<Integer, Long> cachedSortValues = new HashMap<>();
+  private final String field;
+  private final boolean reverse;
+  private final Long missingValue;
+  private final ToLongFunction<byte[]> pointDecoder;
+
+  public NumericFieldReaderContextComparator(

Review Comment:
   Can this constructor also be package protected?



##########
lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java:
##########
@@ -217,6 +217,11 @@ public void collect(int doc) throws IOException {
 
       return collector;
     }
+
+    @Override
+    public Comparator<LeafReaderContext> getLeafReaderComparator() {

Review Comment:
   In the case that the original ordering of leaves is already optimal (because 
leaf sorter has been configured on IndexWriterConfig), would this be the place 
where subclasses overwrite and return null?
   
   In this case there shouldn't be a need to do the re-ordering of segments?



##########
lucene/core/src/java/org/apache/lucene/search/NumericFieldReaderContextComparator.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.ToLongFunction;
+import org.apache.lucene.index.DocValuesSkipper;
+import org.apache.lucene.index.LeafReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.PointValues;
+
+class NumericFieldReaderContextComparator implements 
Comparator<LeafReaderContext> {

Review Comment:
   Maybe add a short description about the purpose of this class?



##########
lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java:
##########
@@ -864,10 +864,6 @@ public void testRandomLong() throws IOException {
     int iterations = limit + random().nextInt(limit);
     long seqNoGenerator = random().nextInt(1000);
     for (long i = 0; i < iterations; i++) {
-      int copies = random().nextInt(100) <= 5 ? 1 : 1 + random().nextInt(5);

Review Comment:
   I think this test was added to catch a bug: 
https://issues.apache.org/jira/browse/LUCENE-10106
   Would removing this randomization cause not to catch the problem that 
LUCENE-10106 tried to fix?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to