cpoerschke commented on code in PR #3418:
URL: https://github.com/apache/solr/pull/3418#discussion_r2318888403


##########
solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java:
##########
@@ -905,7 +906,84 @@ protected boolean addFL(StringBuilder fl, String field, 
boolean additionalAdded)
     return true;
   }
 
+  protected abstract static class ShardDocQueue {
+    public abstract boolean push(ShardDoc shardDoc);
+
+    public abstract Map<Object, ShardDoc> resultIds(int offset);
+  }
+  ;
+
+  protected static class ShardDocQueueFactory
+      implements BiFunction<SortField[], Integer, ShardDocQueue> {
+
+    private final SolrIndexSearcher searcher;
+
+    public ShardDocQueueFactory(SolrIndexSearcher searcher) {
+      this.searcher = searcher;
+    }
+
+    @Override
+    public ShardDocQueue apply(SortField[] sortFields, Integer size) {
+      return new ShardDocQueue() {
+
+        // id to shard mapping, to eliminate any accidental dups
+        private final HashMap<Object, String> uniqueDoc = new HashMap<>();
+
+        private final ShardFieldSortedHitQueue queue =
+            new ShardFieldSortedHitQueue(sortFields, size, searcher);
+
+        @Override
+        public boolean push(ShardDoc shardDoc) {
+          final String prevShard = uniqueDoc.put(shardDoc.id, shardDoc.shard);
+          if (prevShard != null) {
+            // duplicate detected
+
+            // For now, just always use the first encountered since we can't 
currently
+            // remove the previous one added to the priority queue.  If we 
switched
+            // to the Java5 PriorityQueue, this would be easier.
+            return false;
+            // make which duplicate is used deterministic based on shard
+            // if (prevShard.compareTo(shardDoc.shard) >= 0) {
+            //  TODO: remove previous from priority queue
+            //  return false;
+            // }
+          }
+
+          queue.insertWithOverflow(shardDoc);
+          return true;
+        }
+
+        @Override
+        public Map<Object, ShardDoc> resultIds(int offset) {
+          final Map<Object, ShardDoc> resultIds = new HashMap<>();
+
+          // The queue now has 0 -> queuesize docs, where queuesize <= start + 
rows
+          // So we want to pop the last documents off the queue to get
+          // the docs offset -> queuesize
+          int resultSize = queue.size() - offset;
+          resultSize = Math.max(0, resultSize); // there may not be any docs 
in range
+
+          for (int i = resultSize - 1; i >= 0; i--) {
+            ShardDoc shardDoc = queue.pop();
+            shardDoc.positionInResponse = i;
+            // Need the toString() for correlation with other lists that must
+            // be strings (like keys in highlighting, explain, etc)
+            resultIds.put(shardDoc.id.toString(), shardDoc);
+          }
+
+          return resultIds;
+        }
+      };
+    }
+  }
+  ;
+
   protected void mergeIds(ResponseBuilder rb, ShardRequest sreq) {
+    implementMergeIds(rb, sreq, new 
ShardDocQueueFactory(rb.req.getSearcher()));
+  }
+
+  private void implementMergeIds(

Review Comment:
   > > though in that case no need for a factory perhaps:
   > 
   > Simple!
   
   
https://github.com/apache/solr/pull/3418/commits/a52dd22072b98a91d6e7d065c4df843e340549cd
 for that.



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