jimczi commented on a change in pull request #1725:
URL: https://github.com/apache/lucene-solr/pull/1725#discussion_r480039931



##########
File path: 
lucene/core/src/java/org/apache/lucene/search/comparators/DocComparator.java
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.comparators;
+
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.FieldComparator;
+import org.apache.lucene.search.FilteringLeafFieldComparator;
+import org.apache.lucene.search.LeafFieldComparator;
+import org.apache.lucene.search.Scorable;
+
+import java.io.IOException;
+
+/**
+ * Comparator that sorts by asc _doc
+ */
+public class DocComparator extends FieldComparator<Integer> {
+    private final int[] docIDs;
+    private int topValue;
+    private boolean topValueSet;
+    private boolean reverse = false; // only used to check if skipping 
functionality should be enabled
+
+    /** Creates a new comparator based on document ids for {@code numHits} */
+    public DocComparator(int numHits) {
+        docIDs = new int[numHits];
+    }
+
+    @Override
+    public int compare(int slot1, int slot2) {
+        // No overflow risk because docIDs are non-negative
+        return docIDs[slot1] - docIDs[slot2];
+    }
+
+
+    @Override
+    public LeafFieldComparator getLeafComparator(LeafReaderContext context) {
+        // TODO: can we "map" our docIDs to the current
+        // reader? saves having to then subtract on every
+        // compare call
+        return new DocLeafComparator(context);
+    }
+
+    @Override
+    public void setTopValue(Integer value) {
+        topValue = value;
+        topValueSet = true;
+    }
+
+    @Override
+    public Integer value(int slot) {
+        return Integer.valueOf(docIDs[slot]);
+    }
+
+    @Override
+    public void setReverse() {
+        reverse = true;
+    }
+
+
+    /**
+     * DocLeafComparator with skipping functionality.
+     * When sort by _doc asc and "after" document is set,
+     * the comparator provides an iterator that can quickly skip to the 
desired "after" document.
+     */
+    private class DocLeafComparator implements FilteringLeafFieldComparator {
+        private final int docBase;
+        private int bottom;
+
+        private final boolean enableSkipping;
+        private final int minDoc;
+        private final int maxDoc;
+        private DocIdSetIterator topValueIterator; // iterator that starts 
from topValue
+
+        private boolean iteratorUpdated = false;
+
+        public DocLeafComparator(LeafReaderContext context) {
+            this.docBase = context.docBase;
+            // skipping functionality is enabled if topValue is set and sort 
is asc
+            this.enableSkipping = topValueSet && reverse == false ? true: 
false;
+            if (enableSkipping) {
+                this.minDoc = topValue + 1;
+                this.maxDoc = context.reader().maxDoc();
+                this.topValueIterator = DocIdSetIterator.all(maxDoc);
+            } else {
+                this.minDoc = -1;
+                this.maxDoc = -1;
+                this.topValueIterator = null;
+            }
+        }
+
+        @Override
+        public void setBottom(int slot) {
+            bottom = docIDs[slot];

Review comment:
       > What's the way to terminate after N matches here? Is it to update an 
iterator to an empty iterator?
   
   I think so, yes. Updating to an empty iterator is what we do for constant 
score queries for instance. 
   
   > Isn't this termination already handled in TopFieldCollector with the code 
around canEarlyTerminate?
   
   The code is only for sorted index and while I think that we should move this 
code in the sort comparator, I agree that it's out of the scope of this PR. 
Early termination should be handled in the field comparator so that we don't 
need to add new logic in the main collector. 
   
   
   




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to