Skip to content

Commit a31367e

Browse files
committed
Convert simple two-value queues to using Comparators
1 parent 822f746 commit a31367e

File tree

18 files changed

+100
-251
lines changed

18 files changed

+100
-251
lines changed

lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/lucene60/bkd/BKDWriter60.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.Comparator;
2324
import java.util.List;
2425
import java.util.function.IntFunction;
2526
import org.apache.lucene.codecs.CodecUtil;
@@ -453,29 +454,14 @@ public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
453454
}
454455
}
455456

456-
private static class BKDMergeQueue extends PriorityQueue<MergeReader> {
457-
private final int bytesPerDim;
458-
459-
public BKDMergeQueue(int bytesPerDim, int maxSize) {
460-
super(maxSize);
461-
this.bytesPerDim = bytesPerDim;
462-
}
463-
464-
@Override
465-
public boolean lessThan(MergeReader a, MergeReader b) {
466-
assert a != b;
467-
468-
int cmp =
469-
Arrays.compareUnsigned(a.packedValue, 0, bytesPerDim, b.packedValue, 0, bytesPerDim);
470-
if (cmp < 0) {
471-
return true;
472-
} else if (cmp > 0) {
473-
return false;
474-
}
475-
476-
// Tie break by sorting smaller docIDs earlier:
477-
return a.docID < b.docID;
478-
}
457+
private static Comparator<MergeReader> mergeComparator(int bytesPerDim) {
458+
return ((Comparator<MergeReader>)
459+
(a, b) -> {
460+
assert a != b;
461+
return Arrays.compareUnsigned(
462+
a.packedValue, 0, bytesPerDim, b.packedValue, 0, bytesPerDim);
463+
})
464+
.thenComparingInt(mr -> mr.docID);
479465
}
480466

481467
/**
@@ -642,7 +628,8 @@ public long merge(IndexOutput out, List<MergeState.DocMap> docMaps, List<PointVa
642628
throws IOException {
643629
assert docMaps == null || readers.size() == docMaps.size();
644630

645-
BKDMergeQueue queue = new BKDMergeQueue(config.bytesPerDim(), readers.size());
631+
PriorityQueue<MergeReader> queue =
632+
PriorityQueue.usingComparator(readers.size(), mergeComparator(config.bytesPerDim()));
646633

647634
for (int i = 0; i < readers.size(); i++) {
648635
PointValues pointValues = readers.get(i);

lucene/core/src/java/org/apache/lucene/index/DocValuesFieldUpdates.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
2020

21+
import java.util.Comparator;
2122
import org.apache.lucene.search.DocIdSetIterator;
2223
import org.apache.lucene.util.Accountable;
2324
import org.apache.lucene.util.BytesRef;
@@ -159,24 +160,12 @@ public static Iterator mergedIterator(Iterator[] subs) {
159160
return subs[0];
160161
}
161162

163+
// sort by smaller docID, then larger delGen
162164
PriorityQueue<Iterator> queue =
163-
new PriorityQueue<Iterator>(subs.length) {
164-
@Override
165-
protected boolean lessThan(Iterator a, Iterator b) {
166-
// sort by smaller docID
167-
int cmp = Integer.compare(a.docID(), b.docID());
168-
if (cmp == 0) {
169-
// then by larger delGen
170-
cmp = Long.compare(b.delGen(), a.delGen());
171-
172-
// delGens are unique across our subs:
173-
assert cmp != 0;
174-
}
175-
176-
return cmp < 0;
177-
}
178-
};
179-
165+
PriorityQueue.usingComparator(
166+
subs.length,
167+
Comparator.comparingInt(Iterator::docID)
168+
.thenComparing(Comparator.comparingLong(Iterator::delGen).reversed()));
180169
for (Iterator sub : subs) {
181170
if (sub.nextDoc() != NO_MORE_DOCS) {
182171
queue.add(sub);

lucene/core/src/java/org/apache/lucene/search/PhraseQueue.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

lucene/core/src/java/org/apache/lucene/search/SloppyPhraseMatcher.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.lucene.internal.hppc.IntHashSet;
3333
import org.apache.lucene.search.similarities.Similarity.SimScorer;
3434
import org.apache.lucene.util.FixedBitSet;
35+
import org.apache.lucene.util.PriorityQueue;
3536

3637
/**
3738
* Find all slop-valid position-combinations (matches) encountered while traversing/hopping the
@@ -56,7 +57,7 @@ public final class SloppyPhraseMatcher extends PhraseMatcher {
5657

5758
private final int slop;
5859
private final int numPostings;
59-
private final PhraseQueue pq; // for advancing min position
60+
private final PriorityQueue<PhrasePositions> pq; // for advancing min position
6061
private final boolean captureLeadMatch;
6162

6263
private final DocIdSetIterator approximation;
@@ -92,7 +93,14 @@ public SloppyPhraseMatcher(
9293
this.slop = slop;
9394
this.numPostings = postings.length;
9495
this.captureLeadMatch = captureLeadMatch;
95-
pq = new PhraseQueue(postings.length);
96+
pq =
97+
PriorityQueue.usingComparator(
98+
postings.length,
99+
Comparator.<PhrasePositions>comparingInt(pp -> pp.position)
100+
// same doc and pp.position, so decide by actual term positions.
101+
// rely on: pp.position == tp.position - offset.
102+
.thenComparingInt(pp -> pp.offset)
103+
.thenComparingInt(pp -> pp.ord));
96104
phrasePositions = new PhrasePositions[postings.length];
97105
for (int i = 0; i < postings.length; ++i) {
98106
phrasePositions[i] =

lucene/core/src/java/org/apache/lucene/util/MergedIterator.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.lucene.util;
1818

19+
import java.util.Comparator;
1920
import java.util.Iterator;
2021
import java.util.NoSuchElementException;
2122

@@ -44,7 +45,7 @@
4445
*/
4546
public final class MergedIterator<T extends Comparable<T>> implements Iterator<T> {
4647
private T current;
47-
private final TermMergeQueue<T> queue;
48+
private final PriorityQueue<SubIterator<T>> queue;
4849
private final SubIterator<T>[] top;
4950
private final boolean removeDuplicates;
5051
private int numTop;
@@ -57,7 +58,11 @@ public MergedIterator(Iterator<T>... iterators) {
5758
@SuppressWarnings({"unchecked", "rawtypes"})
5859
public MergedIterator(boolean removeDuplicates, Iterator<T>... iterators) {
5960
this.removeDuplicates = removeDuplicates;
60-
queue = new TermMergeQueue<>(iterators.length);
61+
queue =
62+
PriorityQueue.usingComparator(
63+
iterators.length,
64+
Comparator.<SubIterator<T>, T>comparing(it -> it.current)
65+
.thenComparingInt(it -> it.index));
6166
top = new SubIterator[iterators.length];
6267
int index = 0;
6368
for (Iterator<T> iterator : iterators) {
@@ -138,21 +143,4 @@ private static class SubIterator<I extends Comparable<I>> {
138143
I current;
139144
int index;
140145
}
141-
142-
private static class TermMergeQueue<C extends Comparable<C>>
143-
extends PriorityQueue<SubIterator<C>> {
144-
TermMergeQueue(int size) {
145-
super(size);
146-
}
147-
148-
@Override
149-
protected boolean lessThan(SubIterator<C> a, SubIterator<C> b) {
150-
final int cmp = a.current.compareTo(b.current);
151-
if (cmp != 0) {
152-
return cmp < 0;
153-
} else {
154-
return a.index < b.index;
155-
}
156-
}
157-
}
158146
}

lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.Comparator;
2324
import java.util.List;
2425
import java.util.function.IntFunction;
2526
import org.apache.lucene.codecs.CodecUtil;
@@ -396,29 +397,14 @@ public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
396397
}
397398
}
398399

399-
private static class BKDMergeQueue extends PriorityQueue<MergeReader> {
400-
private final ArrayUtil.ByteArrayComparator comparator;
401-
402-
public BKDMergeQueue(int bytesPerDim, int maxSize) {
403-
super(maxSize);
404-
this.comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
405-
}
406-
407-
@Override
408-
public boolean lessThan(MergeReader a, MergeReader b) {
409-
assert a != b;
410-
411-
int cmp = comparator.compare(a.packedValue, 0, b.packedValue, 0);
412-
413-
if (cmp < 0) {
414-
return true;
415-
} else if (cmp > 0) {
416-
return false;
417-
}
418-
419-
// Tie break by sorting smaller docIDs earlier:
420-
return a.docID < b.docID;
421-
}
400+
private static Comparator<MergeReader> mergeComparator(int bytesPerDim) {
401+
ByteArrayComparator comparator = ArrayUtil.getUnsignedComparator(bytesPerDim);
402+
return ((Comparator<MergeReader>)
403+
(a, b) -> {
404+
assert a != b;
405+
return comparator.compare(a.packedValue, 0, b.packedValue, 0);
406+
})
407+
.thenComparingInt(mr -> mr.docID); // Tie break by sorting smaller docIDs earlier
422408
}
423409

424410
/** flat representation of a kd-tree */
@@ -651,7 +637,8 @@ public IORunnable merge(
651637
throws IOException {
652638
assert docMaps == null || readers.size() == docMaps.size();
653639

654-
BKDMergeQueue queue = new BKDMergeQueue(config.bytesPerDim(), readers.size());
640+
PriorityQueue<MergeReader> queue =
641+
PriorityQueue.usingComparator(readers.size(), mergeComparator(config.bytesPerDim()));
655642

656643
for (int i = 0; i < readers.size(); i++) {
657644
PointValues pointValues = readers.get(i);

lucene/facet/src/java/org/apache/lucene/facet/LongValueFacetCounts.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
2323
import java.util.Collections;
24+
import java.util.Comparator;
2425
import java.util.List;
2526
import org.apache.lucene.facet.FacetsCollector.MatchingDocs;
2627
import org.apache.lucene.index.DocValues;
@@ -419,14 +420,12 @@ public FacetResult getTopChildren(int topN, String dim, String... path) {
419420
return new FacetResult(field, new String[0], totCount, new LabelAndValue[0], 0);
420421
}
421422

423+
// sort by count descending, breaking ties by value ascending:
422424
PriorityQueue<Entry> pq =
423-
new PriorityQueue<>(Math.min(topN, counts.length + hashCounts.size())) {
424-
@Override
425-
protected boolean lessThan(Entry a, Entry b) {
426-
// sort by count descending, breaking ties by value ascending:
427-
return a.count < b.count || (a.count == b.count && a.value > b.value);
428-
}
429-
};
425+
PriorityQueue.usingComparator(
426+
Math.min(topN, counts.length + hashCounts.size()),
427+
Comparator.<Entry>comparingInt(e -> e.count)
428+
.thenComparing(Comparator.<Entry>comparingLong(dv -> dv.value).reversed()));
430429

431430
int childCount = 0;
432431
Entry e = null;

lucene/facet/src/java/org/apache/lucene/facet/range/RangeFacetCounts.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.util.Collections;
21+
import java.util.Comparator;
2122
import java.util.List;
2223
import org.apache.lucene.facet.FacetCountsWithFilterQuery;
2324
import org.apache.lucene.facet.FacetResult;
@@ -223,16 +224,10 @@ public FacetResult getTopChildren(int topN, String dim, String... path) throws I
223224
}
224225

225226
PriorityQueue<Entry> pq =
226-
new PriorityQueue<>(Math.min(topN, counts.length)) {
227-
@Override
228-
protected boolean lessThan(Entry a, Entry b) {
229-
int cmp = Integer.compare(a.count, b.count);
230-
if (cmp == 0) {
231-
cmp = b.label.compareTo(a.label);
232-
}
233-
return cmp < 0;
234-
}
235-
};
227+
PriorityQueue.usingComparator(
228+
Math.min(topN, counts.length),
229+
Comparator.<Entry>comparingInt(e -> e.count)
230+
.thenComparing(e -> e.label, Comparator.reverseOrder()));
236231

237232
int childCount = 0;
238233
Entry e = null;

lucene/facet/src/java/org/apache/lucene/facet/rangeonrange/RangeOnRangeFacetCounts.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.util.Collections;
21+
import java.util.Comparator;
2122
import java.util.List;
2223
import org.apache.lucene.document.BinaryRangeDocValues;
2324
import org.apache.lucene.document.RangeFieldQuery;
@@ -144,16 +145,10 @@ public FacetResult getTopChildren(int topN, String dim, String... path) throws I
144145
validateDimAndPathForGetChildren(dim, path);
145146

146147
PriorityQueue<Entry> pq =
147-
new PriorityQueue<>(Math.min(topN, counts.length)) {
148-
@Override
149-
protected boolean lessThan(Entry a, Entry b) {
150-
int cmp = Integer.compare(a.count, b.count);
151-
if (cmp == 0) {
152-
cmp = b.label.compareTo(a.label);
153-
}
154-
return cmp < 0;
155-
}
156-
};
148+
PriorityQueue.usingComparator(
149+
Math.min(topN, counts.length),
150+
Comparator.<Entry>comparingInt(e -> e.count)
151+
.thenComparing(e -> e.label, Comparator.reverseOrder()));
157152

158153
int childCount = 0;
159154
Entry e = null;

lucene/facet/src/java/org/apache/lucene/facet/sortedset/AbstractSortedSetDocValueFacetCounts.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Collections;
23+
import java.util.Comparator;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
@@ -150,18 +151,10 @@ public List<FacetResult> getTopDims(int topNDims, int topNChildren) throws IOExc
150151
// Creates priority queue to store top dimensions and sort by their aggregated values/hits and
151152
// string values.
152153
PriorityQueue<DimValue> pq =
153-
new PriorityQueue<>(topNDims) {
154-
@Override
155-
protected boolean lessThan(DimValue a, DimValue b) {
156-
if (a.value > b.value) {
157-
return false;
158-
} else if (a.value < b.value) {
159-
return true;
160-
} else {
161-
return a.dim.compareTo(b.dim) > 0;
162-
}
163-
}
164-
};
154+
PriorityQueue.usingComparator(
155+
topNDims,
156+
Comparator.<DimValue>comparingInt(dv -> dv.value)
157+
.thenComparing(dv -> dv.dim, Comparator.reverseOrder()));
165158

166159
// Keep track of intermediate results, if we compute them, so we can reuse them later:
167160
Map<String, TopChildrenForPath> intermediateResults = null;

0 commit comments

Comments
 (0)