You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2024/08/07 14:28:12 UTC

(lucene) branch main updated: Make CandidateMatcher functions public (#13632)

This is an automated email from the ASF dual-hosted git repository.

romseygeek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 926d8f4ce63 Make CandidateMatcher functions public (#13632)
926d8f4ce63 is described below

commit 926d8f4ce630e7d53f674f0afbbb9294e1f3523c
Author: bjacobowitz <br...@gmail.com>
AuthorDate: Wed Aug 7 10:28:06 2024 -0400

    Make CandidateMatcher functions public (#13632)
    
    A number of functions in CandidateMatcher are protected or package-protected,
    meaning that client code can't use them, which makes it difficult to build custom
    wrapper matchers.  This commit makes these functions public
---
 lucene/CHANGES.txt                                 |  2 +
 .../apache/lucene/monitor/CandidateMatcher.java    |  6 +-
 .../apache/lucene/monitor/CollectingMatcher.java   |  2 +-
 .../org/apache/lucene/monitor/ExplainingMatch.java |  4 +-
 .../org/apache/lucene/monitor/HighlightsMatch.java |  4 +-
 .../org/apache/lucene/monitor/ParallelMatcher.java |  2 +-
 .../apache/lucene/monitor/PartitionMatcher.java    |  2 +-
 .../apache/lucene/monitor/QueryTimeListener.java   |  2 +-
 .../org/apache/lucene/monitor/TestMonitor.java     |  2 +-
 .../TestCandidateMatcherVisibility.java            | 65 ++++++++++++++++++++++
 10 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f9b989d723f..bdc5ce0796e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -110,6 +110,8 @@ API Changes
 
 * GITHUB#13499: Remove deprecated TopScoreDocCollector + TopFieldCollector methods (#create, #createSharedManager) (Jakub Slowinski)
 
+* GITHUB#13632: CandidateMatcher public matching functions (Bryan Jacobowitz)
+
 
 New Features
 ---------------------
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/CandidateMatcher.java b/lucene/monitor/src/java/org/apache/lucene/monitor/CandidateMatcher.java
index 6132e195edb..055f1b07c87 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/CandidateMatcher.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/CandidateMatcher.java
@@ -64,7 +64,7 @@ public abstract class CandidateMatcher<T extends QueryMatch> {
    * @param metadata the query metadata
    * @throws IOException on IO errors
    */
-  protected abstract void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
+  public abstract void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
       throws IOException;
 
   /**
@@ -95,14 +95,14 @@ public abstract class CandidateMatcher<T extends QueryMatch> {
   public abstract T resolve(T match1, T match2);
 
   /** Called by the Monitor if running a query throws an Exception */
-  void reportError(String queryId, Exception e) {
+  public void reportError(String queryId, Exception e) {
     this.errors.put(queryId, e);
   }
 
   /**
    * @return the matches from this matcher
    */
-  final MultiMatchingQueries<T> finish(long buildTime, int queryCount) {
+  public final MultiMatchingQueries<T> finish(long buildTime, int queryCount) {
     doFinish();
     this.searchTime =
         TimeUnit.MILLISECONDS.convert(System.nanoTime() - searchTime, TimeUnit.NANOSECONDS);
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/CollectingMatcher.java b/lucene/monitor/src/java/org/apache/lucene/monitor/CollectingMatcher.java
index 02d8e3b6ba2..b6300dafa75 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/CollectingMatcher.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/CollectingMatcher.java
@@ -35,7 +35,7 @@ abstract class CollectingMatcher<T extends QueryMatch> extends CandidateMatcher<
   }
 
   @Override
-  protected void matchQuery(final String queryId, Query matchQuery, Map<String, String> metadata)
+  public void matchQuery(final String queryId, Query matchQuery, Map<String, String> metadata)
       throws IOException {
     searcher.search(matchQuery, new MatchCollector(queryId, scoreMode));
   }
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/ExplainingMatch.java b/lucene/monitor/src/java/org/apache/lucene/monitor/ExplainingMatch.java
index 1b4aa4044ef..19e124f1a3f 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/ExplainingMatch.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/ExplainingMatch.java
@@ -31,8 +31,8 @@ public class ExplainingMatch extends QueryMatch {
       searcher ->
           new CandidateMatcher<ExplainingMatch>(searcher) {
             @Override
-            protected void matchQuery(
-                String queryId, Query matchQuery, Map<String, String> metadata) throws IOException {
+            public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
+                throws IOException {
               int maxDocs = searcher.getIndexReader().maxDoc();
               for (int i = 0; i < maxDocs; i++) {
                 Explanation explanation = searcher.explain(matchQuery, i);
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/HighlightsMatch.java b/lucene/monitor/src/java/org/apache/lucene/monitor/HighlightsMatch.java
index aadcc75fb6e..0e3aea210a1 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/HighlightsMatch.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/HighlightsMatch.java
@@ -46,8 +46,8 @@ public class HighlightsMatch extends QueryMatch {
           new CandidateMatcher<HighlightsMatch>(searcher) {
 
             @Override
-            protected void matchQuery(
-                String queryId, Query matchQuery, Map<String, String> metadata) throws IOException {
+            public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
+                throws IOException {
               Weight w =
                   searcher.createWeight(
                       searcher.rewrite(matchQuery), ScoreMode.COMPLETE_NO_SCORES, 1);
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/ParallelMatcher.java b/lucene/monitor/src/java/org/apache/lucene/monitor/ParallelMatcher.java
index fd6d7efceb3..027343701fc 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/ParallelMatcher.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/ParallelMatcher.java
@@ -74,7 +74,7 @@ public class ParallelMatcher<T extends QueryMatch> extends CandidateMatcher<T> {
   }
 
   @Override
-  protected void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
+  public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
       throws IOException {
     try {
       queue.put(new MatcherTask(queryId, matchQuery, metadata));
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/PartitionMatcher.java b/lucene/monitor/src/java/org/apache/lucene/monitor/PartitionMatcher.java
index aaf1f576ecb..7a38edd74a1 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/PartitionMatcher.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/PartitionMatcher.java
@@ -79,7 +79,7 @@ public class PartitionMatcher<T extends QueryMatch> extends CandidateMatcher<T>
   }
 
   @Override
-  protected void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata) {
+  public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata) {
     tasks.add(new MatchTask(queryId, matchQuery, metadata));
   }
 
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryTimeListener.java b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryTimeListener.java
index a787724d181..b9e2b80f0d9 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryTimeListener.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryTimeListener.java
@@ -39,7 +39,7 @@ public interface QueryTimeListener {
       CandidateMatcher<T> matcher = factory.createMatcher(searcher);
       return new CandidateMatcher<T>(searcher) {
         @Override
-        protected void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
+        public void matchQuery(String queryId, Query matchQuery, Map<String, String> metadata)
             throws IOException {
           long t = System.nanoTime();
           matcher.matchQuery(queryId, matchQuery, metadata);
diff --git a/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitor.java b/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitor.java
index aa5f3452764..52675c3f9a7 100644
--- a/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitor.java
+++ b/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitor.java
@@ -178,7 +178,7 @@ public class TestMonitor extends MonitorTestBase {
           docs ->
               new CandidateMatcher<QueryMatch>(docs) {
                 @Override
-                protected void matchQuery(
+                public void matchQuery(
                     String queryId, Query matchQuery, Map<String, String> metadata) {
                   assertEquals("value", metadata.get("key"));
                 }
diff --git a/lucene/monitor/src/test/org/apache/lucene/monitor/outsidepackage/TestCandidateMatcherVisibility.java b/lucene/monitor/src/test/org/apache/lucene/monitor/outsidepackage/TestCandidateMatcherVisibility.java
new file mode 100644
index 00000000000..73e84112e38
--- /dev/null
+++ b/lucene/monitor/src/test/org/apache/lucene/monitor/outsidepackage/TestCandidateMatcherVisibility.java
@@ -0,0 +1,65 @@
+/*
+ * 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.monitor.outsidepackage;
+
+import java.io.IOException;
+import java.util.Collections;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.memory.MemoryIndex;
+import org.apache.lucene.monitor.CandidateMatcher;
+import org.apache.lucene.monitor.QueryMatch;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TermQuery;
+import org.junit.Test;
+
+public class TestCandidateMatcherVisibility {
+
+  private CandidateMatcher<QueryMatch> newCandidateMatcher() {
+    // Index and searcher for use in creating a matcher
+    MemoryIndex index = new MemoryIndex();
+    final IndexSearcher searcher = index.createSearcher();
+    return QueryMatch.SIMPLE_MATCHER.createMatcher(searcher);
+  }
+
+  @Test
+  public void testMatchQueryVisibleOutsidePackage() throws IOException {
+    CandidateMatcher<QueryMatch> matcher = newCandidateMatcher();
+    // This should compile from outside org.apache.lucene.monitor package
+    // (subpackage org.apache.lucene.monitor.outsidepackage cannot access package-private content
+    // from org.apache.lucene.monitor)
+    matcher.matchQuery("test", new TermQuery(new Term("test_field")), Collections.emptyMap());
+  }
+
+  @Test
+  public void testReportErrorVisibleOutsidePackage() {
+    CandidateMatcher<QueryMatch> matcher = newCandidateMatcher();
+    // This should compile from outside org.apache.lucene.monitor package
+    // (subpackage org.apache.lucene.monitor.outsidepackage cannot access package-private content
+    // from org.apache.lucene.monitor)
+    matcher.reportError("test", new RuntimeException("test exception"));
+  }
+
+  @Test
+  public void testFinishVisibleOutsidePackage() {
+    CandidateMatcher<QueryMatch> matcher = newCandidateMatcher();
+    // This should compile from outside org.apache.lucene.monitor package
+    // (subpackage org.apache.lucene.monitor.outsidepackage cannot access package-private content
+    // from org.apache.lucene.monitor)
+    matcher.finish(0, 0);
+  }
+}