rmuir commented on code in PR #12183:
URL: https://github.com/apache/lucene/pull/12183#discussion_r1124967116


##########
lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java:
##########
@@ -269,28 +273,55 @@ public String toString(String field) {
   @Override
   public final Query rewrite(IndexSearcher indexSearcher) throws IOException {
     final TermStates[] contexts = ArrayUtil.copyOfSubArray(this.contexts, 0, 
this.contexts.length);
-    for (int i = 0; i < contexts.length; ++i) {
-      if (contexts[i] == null
-          || contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext()) == 
false) {
-        contexts[i] = TermStates.build(indexSearcher.getTopReaderContext(), 
terms[i], true);
-      }
-    }
-
-    // Compute aggregated doc freq and total term freq
-    // df will be the max of all doc freqs
-    // ttf will be the sum of all total term freqs
-    int df = 0;
-    long ttf = 0;
-    for (TermStates ctx : contexts) {
-      df = Math.max(df, ctx.docFreq());
-      ttf += ctx.totalTermFreq();
-    }
-
-    for (int i = 0; i < contexts.length; ++i) {
-      contexts[i] = adjustFrequencies(indexSearcher.getTopReaderContext(), 
contexts[i], df, ttf);
-    }
-
     Query[] termQueries = new Query[terms.length];
+    AtomicInteger index = new AtomicInteger(0);
+    AtomicInteger df = new AtomicInteger(0);
+    AtomicInteger ttf = new AtomicInteger(0);
+    Executor executor = 
Objects.requireNonNullElse(indexSearcher.getExecutor(), Runnable::run);
+    SliceExecutor sliceExecutor = new SliceExecutor(executor);
+    List<FutureTask<Object>> tasks =
+        Arrays.stream(contexts)
+            .map(
+                task ->
+                    new FutureTask<>(
+                        () -> {
+                          int i = index.getAndIncrement();
+                          if (contexts[i] == null
+                              || 
contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext())
+                                  == false) {
+                            contexts[i] =
+                                TermStates.build(
+                                    indexSearcher.getTopReaderContext(), 
terms[i], true);
+                          }
+                          // Compute aggregated doc freq and total term freq
+                          // df will be the max of all doc freqs
+                          // ttf will be the sum of all total term freqs
+                          df.set(Math.max(df.get(), contexts[i].docFreq()));
+                          ttf.set((int) (ttf.get() + 
contexts[i].totalTermFreq()));

Review Comment:
   this is wrong, as it is still possible for threads to easily race here. 
either add synchronization or make the logic truly atomic (e.g. 
ttf.incrementAndGet vs set + get)



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