You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by th...@apache.org on 2015/08/06 03:03:09 UTC

svn commit: r1694387 - in /lucene/dev/branches/lucene_solr_5_3: ./ lucene/ lucene/CHANGES.txt lucene/core/ lucene/core/src/java/org/apache/lucene/util/NumericUtils.java lucene/core/src/test/org/apache/lucene/index/TestTerms.java

Author: thelabdude
Date: Thu Aug  6 01:03:09 2015
New Revision: 1694387

URL: http://svn.apache.org/r1694387
Log:
LUCENE-6719: Fix NumericUtils getMin/Max methods to return null if there are no terms for the specified field

Modified:
    lucene/dev/branches/lucene_solr_5_3/   (props changed)
    lucene/dev/branches/lucene_solr_5_3/lucene/   (props changed)
    lucene/dev/branches/lucene_solr_5_3/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/lucene_solr_5_3/lucene/core/   (props changed)
    lucene/dev/branches/lucene_solr_5_3/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
    lucene/dev/branches/lucene_solr_5_3/lucene/core/src/test/org/apache/lucene/index/TestTerms.java

Modified: lucene/dev/branches/lucene_solr_5_3/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_5_3/lucene/CHANGES.txt?rev=1694387&r1=1694386&r2=1694387&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_5_3/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/lucene_solr_5_3/lucene/CHANGES.txt Thu Aug  6 01:03:09 2015
@@ -206,6 +206,11 @@ API Changes
   add dedicated method to consider all context values at query time.
   (Areek Zillur, Mike McCandless)
 
+* LUCENE-6719: NumericUtils getMinInt, getMaxInt, getMinLong, getMaxLong now
+  return null if there are no terms for the specified field, previously these
+  methods returned primitive values and raised an undocumented NullPointerException
+  if there were no terms for the field. (hossman, Timothy Potter)
+
 Bug fixes
 
 * LUCENE-6500: ParallelCompositeReader did not always call

Modified: lucene/dev/branches/lucene_solr_5_3/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_5_3/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java?rev=1694387&r1=1694386&r2=1694387&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_5_3/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java (original)
+++ lucene/dev/branches/lucene_solr_5_3/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java Thu Aug  6 01:03:09 2015
@@ -552,34 +552,46 @@ public final class NumericUtils {
       };
   }
     
-  /** Returns the minimum int value indexed into this
-   *  numeric field. */
-  public static int getMinInt(Terms terms) throws IOException {
+  /**
+   * Returns the minimum int value indexed into this
+   * numeric field or null if no terms exist.
+   */
+  public static Integer getMinInt(Terms terms) throws IOException {
     // All shift=0 terms are sorted first, so we don't need
     // to filter the incoming terms; we can just get the
-    // min: 
-    return NumericUtils.prefixCodedToInt(terms.getMin());
+    // min:
+    BytesRef min = terms.getMin();
+    return (min != null) ? NumericUtils.prefixCodedToInt(min) : null;
   }
 
-  /** Returns the maximum int value indexed into this
-   *  numeric field. */
-  public static int getMaxInt(Terms terms) throws IOException {
-    return NumericUtils.prefixCodedToInt(intTerms(terms).getMax());
+  /**
+   * Returns the maximum int value indexed into this
+   * numeric field or null if no terms exist.
+   */
+  public static Integer getMaxInt(Terms terms) throws IOException {
+    BytesRef max = intTerms(terms).getMax();
+    return (max != null) ? NumericUtils.prefixCodedToInt(max) : null;
   }
 
-  /** Returns the minimum long value indexed into this
-   *  numeric field. */
-  public static long getMinLong(Terms terms) throws IOException {
+  /**
+   * Returns the minimum long value indexed into this
+   * numeric field or null if no terms exist.
+   */
+  public static Long getMinLong(Terms terms) throws IOException {
     // All shift=0 terms are sorted first, so we don't need
     // to filter the incoming terms; we can just get the
-    // min: 
-    return NumericUtils.prefixCodedToLong(terms.getMin());
+    // min:
+    BytesRef min = terms.getMin();
+    return (min != null) ? NumericUtils.prefixCodedToLong(min) : null;
   }
 
-  /** Returns the maximum long value indexed into this
-   *  numeric field. */
-  public static long getMaxLong(Terms terms) throws IOException {
-    return NumericUtils.prefixCodedToLong(longTerms(terms).getMax());
+  /**
+   * Returns the maximum long value indexed into this
+   * numeric field or null if no terms exist.
+   */
+  public static Long getMaxLong(Terms terms) throws IOException {
+    BytesRef max = longTerms(terms).getMax();
+    return (max != null) ? NumericUtils.prefixCodedToLong(max) : null;
   }
   
 }

Modified: lucene/dev/branches/lucene_solr_5_3/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene_solr_5_3/lucene/core/src/test/org/apache/lucene/index/TestTerms.java?rev=1694387&r1=1694386&r2=1694387&view=diff
==============================================================================
--- lucene/dev/branches/lucene_solr_5_3/lucene/core/src/test/org/apache/lucene/index/TestTerms.java (original)
+++ lucene/dev/branches/lucene_solr_5_3/lucene/core/src/test/org/apache/lucene/index/TestTerms.java Thu Aug  6 01:03:09 2015
@@ -91,6 +91,11 @@ public class TestTerms extends LuceneTes
     dir.close();
   }
 
+  public void testEmptyIntFieldMinMax() throws Exception {
+    assertNull(NumericUtils.getMinInt(EMPTY_TERMS));
+    assertNull(NumericUtils.getMaxInt(EMPTY_TERMS));
+  }
+  
   public void testIntFieldMinMax() throws Exception {
     Directory dir = newDirectory();
     RandomIndexWriter w = new RandomIndexWriter(random(), dir);
@@ -108,14 +113,19 @@ public class TestTerms extends LuceneTes
     
     IndexReader r = w.getReader();
     Terms terms = MultiFields.getTerms(r, "field");
-    assertEquals(minValue, NumericUtils.getMinInt(terms));
-    assertEquals(maxValue, NumericUtils.getMaxInt(terms));
+    assertEquals(new Integer(minValue), NumericUtils.getMinInt(terms));
+    assertEquals(new Integer(maxValue), NumericUtils.getMaxInt(terms));
 
     r.close();
     w.close();
     dir.close();
   }
 
+  public void testEmptyLongFieldMinMax() throws Exception {
+    assertNull(NumericUtils.getMinLong(EMPTY_TERMS));
+    assertNull(NumericUtils.getMaxLong(EMPTY_TERMS));
+  }
+  
   public void testLongFieldMinMax() throws Exception {
     Directory dir = newDirectory();
     RandomIndexWriter w = new RandomIndexWriter(random(), dir);
@@ -134,8 +144,8 @@ public class TestTerms extends LuceneTes
     IndexReader r = w.getReader();
 
     Terms terms = MultiFields.getTerms(r, "field");
-    assertEquals(minValue, NumericUtils.getMinLong(terms));
-    assertEquals(maxValue, NumericUtils.getMaxLong(terms));
+    assertEquals(new Long(minValue), NumericUtils.getMinLong(terms));
+    assertEquals(new Long(maxValue), NumericUtils.getMaxLong(terms));
 
     r.close();
     w.close();
@@ -193,4 +203,19 @@ public class TestTerms extends LuceneTes
     w.close();
     dir.close();
   }
+
+  /**
+   * A complete empty Terms instance that has no terms in it and supports no optional statistics
+   */
+  private static Terms EMPTY_TERMS = new Terms() {
+    public TermsEnum iterator() { return TermsEnum.EMPTY; }
+    public long size() { return -1; }
+    public long getSumTotalTermFreq() { return -1; }
+    public long getSumDocFreq() { return -1; }
+    public int getDocCount() { return -1; }
+    public boolean hasFreqs() { return false; }
+    public boolean hasOffsets() { return false; }
+    public boolean hasPositions() { return false; }
+    public boolean hasPayloads() { return false; }
+  };
 }