Index: lucene/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java =================================================================== --- lucene/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java (revision 1079301) +++ lucene/contrib/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java (working copy) @@ -524,7 +524,7 @@ { public FragmentQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/contrib/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java =================================================================== --- lucene/contrib/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java (revision 1079301) +++ lucene/contrib/misc/src/java/org/apache/lucene/misc/HighFreqTerms.java (working copy) @@ -255,7 +255,7 @@ **/ final class TermStatsQueue extends PriorityQueue { TermStatsQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java =================================================================== --- lucene/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java (revision 1079301) +++ lucene/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java (working copy) @@ -335,7 +335,7 @@ private static class ScoreTermQueue extends PriorityQueue { public ScoreTermQueue(int size){ - initialize(size); + super(size); } /* (non-Javadoc) Index: lucene/contrib/queries/src/java/org/apache/lucene/search/similar/MoreLikeThis.java =================================================================== --- lucene/contrib/queries/src/java/org/apache/lucene/search/similar/MoreLikeThis.java (revision 1079301) +++ lucene/contrib/queries/src/java/org/apache/lucene/search/similar/MoreLikeThis.java (working copy) @@ -1006,7 +1006,7 @@ */ private static class FreqQ extends PriorityQueue { FreqQ (int s) { - initialize(s); + super(s); } @Override Index: lucene/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SuggestWordQueue.java =================================================================== --- lucene/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SuggestWordQueue.java (revision 1079301) +++ lucene/contrib/spellchecker/src/java/org/apache/lucene/search/spell/SuggestWordQueue.java (working copy) @@ -41,7 +41,7 @@ * @param size The size of the queue */ public SuggestWordQueue (int size) { - initialize(size); + super(size); comparator = DEFAULT_COMPARATOR; } @@ -51,7 +51,7 @@ * @param comparator The comparator. */ public SuggestWordQueue(int size, Comparator comparator){ - initialize(size); + super(size); this.comparator = comparator; } Index: lucene/src/java/org/apache/lucene/index/MultiFieldsEnum.java =================================================================== --- lucene/src/java/org/apache/lucene/index/MultiFieldsEnum.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/index/MultiFieldsEnum.java (working copy) @@ -129,7 +129,7 @@ private final static class FieldMergeQueue extends PriorityQueue { FieldMergeQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/src/java/org/apache/lucene/index/MultiTermsEnum.java =================================================================== --- lucene/src/java/org/apache/lucene/index/MultiTermsEnum.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/index/MultiTermsEnum.java (working copy) @@ -427,7 +427,7 @@ private final static class TermMergeQueue extends PriorityQueue { Comparator termComp; TermMergeQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/src/java/org/apache/lucene/search/FieldValueHitQueue.java =================================================================== --- lucene/src/java/org/apache/lucene/search/FieldValueHitQueue.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/search/FieldValueHitQueue.java (working copy) @@ -56,15 +56,13 @@ public OneComparatorFieldValueHitQueue(SortField[] fields, int size) throws IOException { - super(fields); + super(fields, size); SortField field = fields[0]; setComparator(0,field.getComparator(size, 0)); oneReverseMul = field.reverse ? -1 : 1; reverseMul[0] = oneReverseMul; - - initialize(size); } /** @@ -98,7 +96,7 @@ public MultiComparatorsFieldValueHitQueue(SortField[] fields, int size) throws IOException { - super(fields); + super(fields, size); int numComparators = comparators.length; for (int i = 0; i < numComparators; ++i) { @@ -107,8 +105,6 @@ reverseMul[i] = field.reverse ? -1 : 1; setComparator(i, field.getComparator(size, i)); } - - initialize(size); } @Override @@ -133,7 +129,8 @@ } // prevent instantiation and extension. - private FieldValueHitQueue(SortField[] fields) { + private FieldValueHitQueue(SortField[] fields, int size) { + super(size); // When we get here, fields.length is guaranteed to be > 0, therefore no // need to check it again. Index: lucene/src/java/org/apache/lucene/search/HitQueue.java =================================================================== --- lucene/src/java/org/apache/lucene/search/HitQueue.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/search/HitQueue.java (working copy) @@ -63,17 +63,15 @@ * @see #getSentinelObject() */ HitQueue(int size, boolean prePopulate) { - this.prePopulate = prePopulate; - initialize(size); + super(size, prePopulate); } - // Returns null if prePopulate is false. @Override protected ScoreDoc getSentinelObject() { // Always set the doc Id to MAX_VALUE so that it won't be favored by // lessThan. This generally should not happen since if score is not NEG_INF, // TopScoreDocCollector will always add the object to the queue. - return !prePopulate ? null : new ScoreDoc(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY); + return new ScoreDoc(Integer.MAX_VALUE, Float.NEGATIVE_INFINITY); } @Override Index: lucene/src/java/org/apache/lucene/search/MultiPhraseQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/MultiPhraseQuery.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/search/MultiPhraseQuery.java (working copy) @@ -432,7 +432,7 @@ private static final class DocsQueue extends PriorityQueue { DocsQueue(List docsEnums) throws IOException { - initialize(docsEnums.size()); + super(docsEnums.size()); Iterator i = docsEnums.iterator(); while (i.hasNext()) { Index: lucene/src/java/org/apache/lucene/search/PhraseQueue.java =================================================================== --- lucene/src/java/org/apache/lucene/search/PhraseQueue.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/search/PhraseQueue.java (working copy) @@ -21,7 +21,7 @@ final class PhraseQueue extends PriorityQueue { PhraseQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java =================================================================== --- lucene/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/search/spans/NearSpansUnordered.java (working copy) @@ -53,7 +53,7 @@ private class CellQueue extends PriorityQueue { public CellQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/src/java/org/apache/lucene/search/spans/SpanOrQuery.java =================================================================== --- lucene/src/java/org/apache/lucene/search/spans/SpanOrQuery.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/search/spans/SpanOrQuery.java (working copy) @@ -145,7 +145,7 @@ private class SpanQueue extends PriorityQueue { public SpanQueue(int size) { - initialize(size); + super(size); } @Override Index: lucene/src/java/org/apache/lucene/util/PriorityQueue.java =================================================================== --- lucene/src/java/org/apache/lucene/util/PriorityQueue.java (revision 1079301) +++ lucene/src/java/org/apache/lucene/util/PriorityQueue.java (working copy) @@ -28,9 +28,53 @@ */ public abstract class PriorityQueue { private int size; - private int maxSize; - protected T[] heap; + private final int maxSize; + private final T[] heap; + public PriorityQueue(int maxSize) { + this(maxSize, true); + } + + @SuppressWarnings("unchecked") + public PriorityQueue(int maxSize, boolean prepopulate) { + size = 0; + int heapSize; + if (0 == maxSize) + // We allocate 1 extra to avoid if statement in top() + heapSize = 2; + 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. We don't +1 + // in this case, but it's very unlikely in practice + // one will actually insert this many objects into + // the PQ: + heapSize = Integer.MAX_VALUE; + } else { + // NOTE: we add +1 because all access to heap is + // 1-based not 0-based. heap[0] is unused. + heapSize = maxSize + 1; + } + } + heap = (T[]) new Object[heapSize]; // T is unbounded type, so this unchecked cast works always + this.maxSize = maxSize; + + if (prepopulate) { + // If sentinel objects are supported, populate the queue with them + T sentinel = getSentinelObject(); + if (sentinel != null) { + heap[1] = sentinel; + for (int i = 2; i < heap.length; i++) { + heap[i] = getSentinelObject(); + } + size = maxSize; + } + } + } + /** Determines the ordering of objects in this priority queue. Subclasses * must define this one method. * @return true iff parameter a is less than parameter b. @@ -80,45 +124,6 @@ return null; } - /** Subclass constructors must call this. */ - @SuppressWarnings("unchecked") - protected final void initialize(int maxSize) { - size = 0; - int heapSize; - if (0 == maxSize) - // We allocate 1 extra to avoid if statement in top() - heapSize = 2; - 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. We don't +1 - // in this case, but it's very unlikely in practice - // one will actually insert this many objects into - // the PQ: - heapSize = Integer.MAX_VALUE; - } else { - // NOTE: we add +1 because all access to heap is - // 1-based not 0-based. heap[0] is unused. - heapSize = maxSize + 1; - } - } - heap = (T[]) new Object[heapSize]; // T is unbounded type, so this unchecked cast works always - this.maxSize = maxSize; - - // If sentinel objects are supported, populate the queue with them - T sentinel = getSentinelObject(); - if (sentinel != null) { - heap[1] = sentinel; - for (int i = 2; i < heap.length; i++) { - heap[i] = getSentinelObject(); - } - size = maxSize; - } - } - /** * Adds an Object to a PriorityQueue in log(size) time. If one tries to add * more objects than maxSize from initialize an @@ -247,4 +252,11 @@ } heap[i] = node; // install saved node } + + /** This method returns the internal heap array as Object[]. + * @lucene.internal + */ + protected final Object[] getHeapArray() { + return (Object[]) heap; + } } Index: lucene/src/test/org/apache/lucene/util/TestPriorityQueue.java =================================================================== --- lucene/src/test/org/apache/lucene/util/TestPriorityQueue.java (revision 1079301) +++ lucene/src/test/org/apache/lucene/util/TestPriorityQueue.java (working copy) @@ -23,8 +23,7 @@ private static class IntegerQueue extends PriorityQueue { public IntegerQueue(int count) { - super(); - initialize(count); + super(count); } @Override Index: modules/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java =================================================================== --- modules/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java (revision 1079301) +++ modules/benchmark/src/java/org/apache/lucene/benchmark/quality/utils/QualityQueriesFinder.java (working copy) @@ -124,7 +124,7 @@ private static class TermsDfQueue extends PriorityQueue { TermsDfQueue (int maxSize) { - initialize(maxSize); + super(maxSize); } @Override protected boolean lessThan(TermDf tf1, TermDf tf2) { Index: solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java =================================================================== --- solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java (revision 1079301) +++ solr/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java (working copy) @@ -354,13 +354,17 @@ private static class PQueue extends PriorityQueue> { int myMaxSize; + final Object[] heap; + PQueue(int maxSz) { - super.initialize(maxSz); + super(maxSz); + heap = getHeapArray(); myMaxSize = maxSz; } + @SuppressWarnings("unchecked") Iterable> getValues() { - return Collections.unmodifiableCollection(Arrays.asList(heap)); + return (Iterable) Collections.unmodifiableCollection(Arrays.asList(heap)); } @Override @@ -370,12 +374,13 @@ } // necessary because maxSize is private in base class + @SuppressWarnings("unchecked") public CacheEntry myInsertWithOverflow(CacheEntry element) { if (size() < myMaxSize) { add(element); return null; - } else if (size() > 0 && !lessThan(element, heap[1])) { - CacheEntry ret = heap[1]; + } else if (size() > 0 && !lessThan(element, (CacheEntry) heap[1])) { + CacheEntry ret = (CacheEntry) heap[1]; heap[1] = element; updateTop(); return ret; Index: solr/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java =================================================================== --- solr/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java (revision 1079301) +++ solr/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java (working copy) @@ -586,7 +586,7 @@ public TermHistogram histogram; TopTermQueue(int size) { - initialize(size); + super(size); histogram = new TermHistogram(); } Index: solr/src/java/org/apache/solr/handler/component/ShardDoc.java =================================================================== --- solr/src/java/org/apache/solr/handler/component/ShardDoc.java (revision 1079301) +++ solr/src/java/org/apache/solr/handler/component/ShardDoc.java (working copy) @@ -82,6 +82,7 @@ protected List fieldNames = new ArrayList(); public ShardFieldSortedHitQueue(SortField[] fields, int size) { + super(size); final int n = fields.length; comparators = new Comparator[n]; this.fields = new SortField[n]; @@ -107,8 +108,6 @@ //System.out.println("%%%%%%%%%%%%%%%%%% got "+fields[i].getType() +" for "+ fieldname +" fields[i].getReverse(): "+fields[i].getReverse()); } - - initialize(size); } @Override Index: solr/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java =================================================================== --- solr/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java (revision 1079301) +++ solr/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java (working copy) @@ -114,10 +114,7 @@ // now merge the per-segment results - PriorityQueue queue = new PriorityQueue() { - { - initialize(leaves.length); - } + PriorityQueue queue = new PriorityQueue(leaves.length) { @Override protected boolean lessThan(SegFacet a, SegFacet b) { return a.tempBR.compareTo(b.tempBR) < 0; Index: solr/src/java/org/apache/solr/spelling/suggest/Lookup.java =================================================================== --- solr/src/java/org/apache/solr/spelling/suggest/Lookup.java (revision 1079301) +++ solr/src/java/org/apache/solr/spelling/suggest/Lookup.java (working copy) @@ -39,7 +39,7 @@ public static final class LookupPriorityQueue extends PriorityQueue { public LookupPriorityQueue(int size) { - initialize(size); + super(size); } @Override