Skip to content

Apache Lucene open-source search software

LUCENE-2119: behave better if you pass Integer.MAX_VALUE as nDcos to search methods

Michael McCandlessae72952fa413
diff --git a/CHANGES.txt b/CHANGES.txt
index e81d32057a7..e0cd141e5b4 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -32,6 +32,10 @@ Bug fixes
one of the threads before all changes are actually committed.
(Sanne Grinovero via Mike McCandless)
+* LUCENE-2119: Don't throw NegativeArraySizeException if you pass
+ Integer.MAX_VALUE as nDocs to IndexSearcher search methods. (Paul
+ Taylor via Mike McCandless)
+
New features
* LUCENE-2069: Added Unicode 4 support to CharArraySet. Due to the switch
diff --git a/src/java/org/apache/lucene/search/IndexSearcher.java b/src/java/org/apache/lucene/search/IndexSearcher.java
index 2120f421312..4abda95060b 100644
--- a/src/java/org/apache/lucene/search/IndexSearcher.java
+++ b/src/java/org/apache/lucene/search/IndexSearcher.java
@@ -158,12 +158,14 @@ public class IndexSearcher extends Searcher {
// inherit javadoc
@Override
- public TopDocs search(Weight weight, Filter filter, final int nDocs) throws IOException {
+ public TopDocs search(Weight weight, Filter filter, int nDocs) throws IOException {
if (nDocs <= 0) {
throw new IllegalArgumentException("nDocs must be > 0");
}
+ nDocs = Math.min(nDocs, reader.maxDoc());
+
TopScoreDocCollector collector = TopScoreDocCollector.create(nDocs, !weight.scoresDocsOutOfOrder());
search(weight, filter, collector);
return collector.topDocs();
@@ -186,9 +188,12 @@ public class IndexSearcher extends Searcher {
* then pass that to {@link #search(Weight, Filter,
* Collector)}.</p>
*/
- public TopFieldDocs search(Weight weight, Filter filter, final int nDocs,
+ public TopFieldDocs search(Weight weight, Filter filter, int nDocs,
Sort sort, boolean fillFields)
throws IOException {
+
+ nDocs = Math.min(nDocs, reader.maxDoc());
+
TopFieldCollector collector = TopFieldCollector.create(sort, nDocs,
fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.scoresDocsOutOfOrder());
search(weight, filter, collector);
diff --git a/src/java/org/apache/lucene/util/PriorityQueue.java b/src/java/org/apache/lucene/util/PriorityQueue.java
index d0212776093..dff5db673f0 100644
--- a/src/java/org/apache/lucene/util/PriorityQueue.java
+++ b/src/java/org/apache/lucene/util/PriorityQueue.java
@@ -85,8 +85,18 @@ public abstract class PriorityQueue<T> {
if (0 == maxSize)
// We allocate 1 extra to avoid if statement in top()
heapSize = 2;
- else
- heapSize = maxSize + 1;
+ else {
+ if (maxSize == Integer.MAX_VALUE) {
+ // Don't wrap heapSize to -1, in this case, which
+ // causes a confusing NegativeArraySizeException.
+ // Note that very likely this will simply then hit
+ // an OOME, but at least that's more indicative to
+ // caller that this values is too big:
+ heapSize = Integer.MAX_VALUE;
+ } else {
+ heapSize = maxSize + 1;
+ }
+ }
heap = (T[]) new Object[heapSize]; // T is unbounded type, so this unchecked cast works always
this.maxSize = maxSize;