dsmiley commented on code in PR #3380:
URL: https://github.com/apache/solr/pull/3380#discussion_r2127833365


##########
solr/core/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java:
##########
@@ -61,32 +67,66 @@ public void setContext(ResultContext context) {
       readerContexts = searcher.getIndexReader().leaves();
       fcontext = ValueSource.newContext(searcher);
       this.valueSource.createWeight(fcontext, searcher);
+      final var docList = context.getDocList();
+      if (docList == null) {
+        return;
+      }
+
+      final int prefetchSize = Math.min(docList.size(), maxPrefetchSize);
+      final int[] ids = new int[prefetchSize];
+      int i = 0;
+      var iter = docList.iterator();
+      while (iter.hasNext() && i < prefetchSize) {
+        ids[i++] = iter.nextDoc();
+      }
+      Arrays.sort(ids);
+      scores = new IntObjectHashMap<>(ids.length);

Review Comment:
   Why is this called "scores"?  I suggest a name like "cachedValuesById".



##########
solr/core/src/java/org/apache/solr/response/transform/ValueSourceAugmenter.java:
##########
@@ -61,32 +67,66 @@ public void setContext(ResultContext context) {
       readerContexts = searcher.getIndexReader().leaves();
       fcontext = ValueSource.newContext(searcher);
       this.valueSource.createWeight(fcontext, searcher);
+      final var docList = context.getDocList();
+      if (docList == null) {
+        return;
+      }
+
+      final int prefetchSize = Math.min(docList.size(), maxPrefetchSize);
+      final int[] ids = new int[prefetchSize];
+      int i = 0;
+      var iter = docList.iterator();
+      while (iter.hasNext() && i < prefetchSize) {
+        ids[i++] = iter.nextDoc();
+      }
+      Arrays.sort(ids);
+      scores = new IntObjectHashMap<>(ids.length);
+
+      FunctionValues values = null;
+      int docBase = -1;
+      int currentIdx = -1;
+      for (int docid : ids) {
+        int idx = ReaderUtil.subIndex(docid, readerContexts);
+        if (currentIdx != idx) {
+          currentIdx = idx;
+          LeafReaderContext rcontext = readerContexts.get(idx);
+          docBase = rcontext.docBase;
+          values = valueSource.getValues(fcontext, rcontext);
+        }
+        int localId = docid - docBase;
+        var value = values.objectVal(localId);
+        scores.put(docid, value != null ? value : NULL_SENTINEL);
+      }
     } catch (IOException e) {
-      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
+      throw new SolrException(
+          SolrException.ErrorCode.SERVER_ERROR, "exception for valuesource " + 
valueSource, e);
     }
   }
 
   Map<Object, Object> fcontext;
   SolrIndexSearcher searcher;
   List<LeafReaderContext> readerContexts;
+  IntObjectHashMap<Object> scores;
 
   @Override
-  public void transform(SolrDocument doc, int docid, DocIterationInfo docInfo) 
{
-    // This is only good for random-access functions
-
-    try {
-
-      // TODO: calculate this stuff just once across diff functions
-      int idx = ReaderUtil.subIndex(docid, readerContexts);
-      LeafReaderContext rcontext = readerContexts.get(idx);
-      FunctionValues values = valueSource.getValues(fcontext, rcontext);
-      int localId = docid - rcontext.docBase;
-      setValue(doc, values.objectVal(localId));
-    } catch (IOException e) {
-      throw new SolrException(
-          SolrException.ErrorCode.SERVER_ERROR,
-          "exception at docid " + docid + " for valuesource " + valueSource,
-          e);
+  public void transform(SolrDocument doc, int docid, DocIterationInfo 
docIterationInfo) {
+    Object scoreValue = (scores != null) ? scores.get(docid) : null;

Review Comment:
   `cacheValue` would be a better name



-- 
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: issues-unsubscr...@solr.apache.org

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


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

Reply via email to