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

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@887670 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2009-12-06 11:41:26 +00:00
parent f011f59fe2
commit ae72952fa4
3 changed files with 23 additions and 4 deletions

View File

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

View File

@ -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);

View File

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