Apache Lucene open-source search software
Removed PriorityQueue.remove. #15309 (#15493)
Dawid Weissdea98efd5e21
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 10f891391e2..6ce3d131a26 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -7,6 +7,9 @@ http://s.apache.org/luceneversionsAPI Changes---------------------+* GITHUB#15309: PriorityQueue.remove method has been removed entirely. If you need remove(), implement+ it yourself in a subclass (heap access and size, upHeap and downHeap are protected members of PQ). (Dawid Weiss)+* GITHUB#15215: Switch to Java 25 as the minimum required platform. Upgrade to gradle 9.1.0.(Robert Muir, Kaival Parikh, Dawid Weiss)diff --git a/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java b/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.javaindex de4f8e81265..799db0700f4 100644--- a/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java+++ b/lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java@@ -26,8 +26,7 @@ import java.util.function.Supplier;/*** A priority queue maintains a partial ordering of its elements such that the least element can- * always be found in constant time. Put()'s and pop()'s require log(size) time but the remove()- * cost implemented here is linear.+ * always be found in constant time. Put()'s and pop()'s require log(size) time.** <p><b>NOTE</b>: This class pre-allocates an array of length {@code maxSize+1} and pre-fills it* with elements if instantiated via the {@link #PriorityQueue(int,LessThan,Supplier)} constructor.@@ -68,7 +67,7 @@ public class PriorityQueue<T> implements Iterable<T> {maxSize, (a, b) -> comparator.compare(a, b) < 0, sentinelObjectSupplier);}- private int size = 0;+ protected int size = 0;private final int maxSize;private final T[] heap;private final LessThan<? super T> lessThan;@@ -274,28 +273,6 @@ public class PriorityQueue<T> implements Iterable<T> {size = 0;}- /**- * Removes an existing element currently stored in the PriorityQueue. Cost is linear with the size- * of the queue. (A specialization of PriorityQueue which tracks element positions would provide a- * constant remove time but the trade-off would be extra cost to all additions/insertions)- */- public final boolean remove(T element) {- for (int i = 1; i <= size; i++) {- if (heap[i] == element) {- heap[i] = heap[size];- heap[size] = null; // permit GC of objects- size--;- if (i <= size) {- if (!upHeap(i)) {- downHeap(i);- }- }- return true;- }- }- return false;- }-/*** Moves the contents of this queue into a new array created by {@code newArray}, lowest items* first@@ -320,7 +297,7 @@ public class PriorityQueue<T> implements Iterable<T> {return array;}- private boolean upHeap(int origPos) {+ protected boolean upHeap(int origPos) {int i = origPos;T node = heap[i]; // save bottom nodeint j = i >>> 1;@@ -333,7 +310,7 @@ public class PriorityQueue<T> implements Iterable<T> {return i != origPos;}- private void downHeap(int i) {+ protected void downHeap(int i) {T node = heap[i]; // save top nodeint j = i << 1; // find smaller childint k = j + 1;diff --git a/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java b/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.javaindex 091834b0e38..204d7c0e711 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java@@ -208,9 +208,9 @@ public class TestPriorityQueue extends LuceneTestCase {() -> pq.addAll(list));}- /** Randomly add and remove elements, comparing against the reference java.util.PriorityQueue. */- public void testRemovalsAndInsertions() {- int maxElement = RandomNumbers.randomIntBetween(random(), 1, 10_000);+ /** Randomly add some elements, comparing against the reference java.util.PriorityQueue. */+ public void testRandomAdditionsAgainstJavaPq() {+ int maxElement = RandomNumbers.randomIntBetween(random(), 1, 500);int size = maxElement / 2 + 1;var reference = new java.util.PriorityQueue<Integer>();@@ -226,25 +226,18 @@ public class TestPriorityQueue extends LuceneTestCase {for (int i = 0, iters = size * 2; i < iters; i++) {Integer element = ints.computeIfAbsent(localRandom.nextInt(maxElement), k -> k);- var action = localRandom.nextInt(100);- if (action < 25) {- // removals, possibly misses.- assertEquals("remove() difference: " + i, reference.remove(element), pq.remove(element));- } else {- // additions.- var dropped = pq.insertWithOverflow(element);-- reference.add(element);- Integer droppedReference;- if (reference.size() > size) {- droppedReference = reference.remove();- } else {- droppedReference = null;- }+ // additions.+ var dropped = pq.insertWithOverflow(element);- assertEquals("insertWithOverflow() difference.", dropped, droppedReference);+ reference.add(element);+ Integer droppedReference;+ if (reference.size() > size) {+ droppedReference = reference.remove();+ } else {+ droppedReference = null;}+ assertEquals("insertWithOverflow() difference.", dropped, droppedReference);assertEquals("insertWithOverflow() size difference?", reference.size(), pq.size());assertEquals("top() difference?", reference.peek(), pq.top());}diff --git a/lucene/misc/src/java/org/apache/lucene/misc/search/DiversifiedTopDocsCollector.java b/lucene/misc/src/java/org/apache/lucene/misc/search/DiversifiedTopDocsCollector.javaindex ef64378af65..b79f4c01f9d 100644--- a/lucene/misc/src/java/org/apache/lucene/misc/search/DiversifiedTopDocsCollector.java+++ b/lucene/misc/src/java/org/apache/lucene/misc/search/DiversifiedTopDocsCollector.java@@ -68,16 +68,45 @@ import org.apache.lucene.util.PriorityQueue;*/public abstract class DiversifiedTopDocsCollector extends TopDocsCollector<ScoreDocKey> {+ /** An internal PQ with remove(element) operation implemented using a linear scan of the heap. */+ private static final class PriorityQueueWithSlowRemove extends PriorityQueue<ScoreDocKey> {+ public PriorityQueueWithSlowRemove(int maxSize, LessThan<? super ScoreDocKey> lessThan) {+ super(maxSize, lessThan);+ }++ /**+ * Removes an existing element currently stored in the PriorityQueue. Cost is linear with the+ * size of the queue. Elements are compared by reference (not equality).+ */+ boolean remove(ScoreDocKey element) {+ var heap = super.getHeapArray();+ for (int i = 1; i <= size; i++) {+ if (heap[i] == element) {+ heap[i] = heap[size];+ heap[size] = null; // permit GC of objects+ size--;+ if (i <= size) {+ if (!upHeap(i)) {+ downHeap(i);+ }+ }+ return true;+ }+ }+ return false;+ }+ }+ScoreDocKey spare;- private final PriorityQueue<ScoreDocKey> globalQueue;+ private final PriorityQueueWithSlowRemove globalQueue;private final int numHits;private final LongObjectHashMap<PriorityQueue<ScoreDocKey>> perKeyQueues;protected int maxNumPerKey;private final Deque<PriorityQueue<ScoreDocKey>> sparePerKeyQueues = new ArrayDeque<>();public DiversifiedTopDocsCollector(int numHits, int maxHitsPerKey) {- super(createScoreDocQueue(numHits));- this.globalQueue = pq;+ super(new PriorityQueueWithSlowRemove(numHits, (a, b) -> KEY_COMPARATOR.compare(a, b) < 0));+ this.globalQueue = (PriorityQueueWithSlowRemove) pq;this.perKeyQueues = new LongObjectHashMap<>();this.numHits = numHits;this.maxNumPerKey = maxHitsPerKey;@@ -127,7 +156,7 @@ public abstract class DiversifiedTopDocsCollector extends TopDocsCollector<Scoreif (thisKeyQ == null) {if (sparePerKeyQueues.isEmpty()) {- thisKeyQ = createScoreDocQueue(maxNumPerKey);+ thisKeyQ = PriorityQueue.usingComparator(maxNumPerKey, KEY_COMPARATOR);} else {thisKeyQ = sparePerKeyQueues.pop();}@@ -211,10 +240,6 @@ public abstract class DiversifiedTopDocsCollector extends TopDocsCollector<ScoreFloatComparator.<ScoreDocKey>comparing(sd -> sd.score).thenComparing(Comparator.<ScoreDocKey>comparingInt(sd -> sd.doc).reversed());- private static PriorityQueue<ScoreDocKey> createScoreDocQueue(int size) {- return PriorityQueue.usingComparator(size, KEY_COMPARATOR);- }-///** An extension to ScoreDoc that includes a key used for grouping purposes */public static class ScoreDocKey extends ScoreDoc {