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 2023/10/31 15:11:15 UTC

(lucene) branch branch_9x updated: Fix NullPointerException in Monitor.getQuery when query is not present (#12736)

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

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


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 5694e30bccd Fix NullPointerException in Monitor.getQuery when query is not present (#12736)
5694e30bccd is described below

commit 5694e30bccd13d180cd1a813f532aa12c6ab36dc
Author: Davis Cook <da...@gmail.com>
AuthorDate: Tue Oct 31 11:05:31 2023 -0400

    Fix NullPointerException in Monitor.getQuery when query is not present (#12736)
---
 lucene/CHANGES.txt                                 |  2 ++
 .../java/org/apache/lucene/monitor/QueryIndex.java |  2 +-
 .../lucene/monitor/TestMonitorPersistence.java     | 34 +++++++++++++++++-----
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 60672b6c968..b3170032e21 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -138,6 +138,8 @@ Bug Fixes
 
 * GITHUB#12727: Ensure negative scores are not returned by vector similarity functions (Ben Trent)
 
+* GITHUB#12736: Fix NullPointerException when Monitor.getQuery cannot find the requested queryId (Davis Cook)
+
 Build
 ---------------------
 
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java
index a8ccd44218b..887ee51d451 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java
@@ -68,7 +68,7 @@ abstract class QueryIndex implements Closeable {
     search(
         new TermQuery(new Term(FIELDS.query_id, queryId)),
         (id, query, dataValues) -> bytesHolder[0] = dataValues.mq.binaryValue());
-    return serializer.deserialize(bytesHolder[0]);
+    return bytesHolder[0] != null ? serializer.deserialize(bytesHolder[0]) : null;
   }
 
   public void scan(QueryCollector matcher) throws IOException {
diff --git a/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java b/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java
index 945abcd3293..1a60c29618e 100644
--- a/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java
+++ b/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java
@@ -28,16 +28,21 @@ public class TestMonitorPersistence extends MonitorTestBase {
 
   private Path indexDirectory = createTempDir();
 
-  public void testCacheIsRepopulated() throws IOException {
-
-    Document doc = new Document();
-    doc.add(newTextField(FIELD, "test", Field.Store.NO));
+  protected Monitor newMonitorWithPersistence() throws IOException {
     MonitorConfiguration config =
         new MonitorConfiguration()
             .setIndexPath(
                 indexDirectory, MonitorQuerySerializer.fromParser(MonitorTestBase::parse));
 
-    try (Monitor monitor = new Monitor(ANALYZER, config)) {
+    return new Monitor(ANALYZER, config);
+  }
+
+  public void testCacheIsRepopulated() throws IOException {
+
+    Document doc = new Document();
+    doc.add(newTextField(FIELD, "test", Field.Store.NO));
+
+    try (Monitor monitor = newMonitorWithPersistence()) {
       monitor.register(
           mq("1", "test"),
           mq("2", "test"),
@@ -58,7 +63,7 @@ public class TestMonitorPersistence extends MonitorTestBase {
           e.getMessage());
     }
 
-    try (Monitor monitor2 = new Monitor(ANALYZER, config)) {
+    try (Monitor monitor2 = newMonitorWithPersistence()) {
       assertEquals(4, monitor2.getQueryCount());
       assertEquals(4, monitor2.match(doc, QueryMatch.SIMPLE_MATCHER).getMatchCount());
 
@@ -67,9 +72,24 @@ public class TestMonitorPersistence extends MonitorTestBase {
     }
   }
 
+  public void testGetQueryPresent() throws IOException {
+    try (Monitor monitor = newMonitorWithPersistence()) {
+      MonitorQuery monitorQuery = mq("1", "test");
+      monitor.register(monitorQuery);
+
+      assertEquals(monitorQuery, monitor.getQuery("1"));
+    }
+  }
+
+  public void testGetQueryNotPresent() throws IOException {
+    try (Monitor monitor = newMonitorWithPersistence()) {
+      assertNull(monitor.getQuery("1"));
+    }
+  }
+
   public void testEphemeralMonitorDoesNotStoreQueries() throws IOException {
 
-    try (Monitor monitor2 = new Monitor(ANALYZER)) {
+    try (Monitor monitor2 = newMonitor(ANALYZER)) {
       IllegalStateException e =
           expectThrows(IllegalStateException.class, () -> monitor2.getQuery("query"));
       assertEquals(