Skip to content

Commit 5d82ac8

Browse files
Refactor FirstPassGroupingCollector to improve readability. (#15136) (#15168)
--------- Co-authored-by: Ankit Jain <[email protected]>
1 parent 2d8f0cb commit 5d82ac8

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

lucene/grouping/src/java/org/apache/lucene/search/grouping/FirstPassGroupingCollector.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector {
7171
* must be non-null, ie, if you want to groupSort by relevance use Sort.RELEVANCE.
7272
* @param topNGroups How many top groups to keep.
7373
*/
74-
@SuppressWarnings({"unchecked", "rawtypes"})
7574
public FirstPassGroupingCollector(
7675
GroupSelector<T> groupSelector, Sort groupSort, int topNGroups) {
7776
this.groupSelector = groupSelector;
@@ -85,7 +84,7 @@ public FirstPassGroupingCollector(
8584
this.topNGroups = topNGroups;
8685
this.needsScores = groupSort.needsScores();
8786
final SortField[] sortFields = groupSort.getSort();
88-
comparators = new FieldComparator[sortFields.length];
87+
comparators = new FieldComparator<?>[sortFields.length];
8988
leafComparators = new LeafFieldComparator[sortFields.length];
9089
compIDXEnd = comparators.length - 1;
9190
reversed = new int[sortFields.length];
@@ -207,39 +206,44 @@ public void collect(int doc) throws IOException {
207206
final CollectedSearchGroup<T> group = groupMap.get(groupValue);
208207

209208
if (group == null) {
209+
collectNewGroup(doc);
210+
} else {
211+
collectExistingGroup(doc, group);
212+
}
213+
}
210214

211-
// First time we are seeing this group, or, we've seen
212-
// it before but it fell out of the top N and is now
213-
// coming back
215+
private void collectNewGroup(final int doc) throws IOException {
216+
// First time we are seeing this group, or, we've seen
217+
// it before but it fell out of the top N and is now
218+
// coming back
214219

215-
if (groupMap.size() < topNGroups) {
220+
if (isGroupMapFull() == false) {
216221

217-
// Still in startup transient: we have not
218-
// seen enough unique groups to start pruning them;
219-
// just keep collecting them
222+
// Still in startup transient: we have not
223+
// seen enough unique groups to start pruning them;
224+
// just keep collecting them
220225

221-
// Add a new CollectedSearchGroup:
222-
CollectedSearchGroup<T> sg = new CollectedSearchGroup<>();
223-
sg.groupValue = groupSelector.copyValue();
224-
sg.comparatorSlot = groupMap.size();
225-
sg.topDoc = docBase + doc;
226-
for (LeafFieldComparator fc : leafComparators) {
227-
fc.copy(sg.comparatorSlot, doc);
228-
}
229-
groupMap.put(sg.groupValue, sg);
230-
231-
if (groupMap.size() == topNGroups) {
232-
// End of startup transient: we now have max
233-
// number of groups; from here on we will drop
234-
// bottom group when we insert new one:
235-
buildSortedSet();
236-
}
226+
// Add a new CollectedSearchGroup:
227+
CollectedSearchGroup<T> sg = new CollectedSearchGroup<>();
228+
sg.groupValue = groupSelector.copyValue();
229+
sg.comparatorSlot = groupMap.size();
230+
sg.topDoc = docBase + doc;
231+
for (LeafFieldComparator fc : leafComparators) {
232+
fc.copy(sg.comparatorSlot, doc);
233+
}
234+
groupMap.put(sg.groupValue, sg);
237235

238-
return;
236+
if (isGroupMapFull() == true) {
237+
// End of startup transient: we now have max
238+
// number of groups; from here on we will drop
239+
// bottom group when we insert new one:
240+
buildSortedSet();
239241
}
240242

243+
} else {
241244
// We already tested that the document is competitive, so replace
242245
// the bottom group with this new group.
246+
243247
final CollectedSearchGroup<T> bottomGroup = orderedGroups.pollLast();
244248
assert orderedGroups.size() == topNGroups - 1;
245249

@@ -261,10 +265,11 @@ public void collect(int doc) throws IOException {
261265
for (LeafFieldComparator fc : leafComparators) {
262266
fc.setBottom(lastComparatorSlot);
263267
}
264-
265-
return;
266268
}
269+
}
267270

271+
private void collectExistingGroup(final int doc, final CollectedSearchGroup<T> group)
272+
throws IOException {
268273
// Update existing group:
269274
for (int compIDX = 0; ; compIDX++) {
270275
leafComparators[compIDX].copy(spareSlot, doc);
@@ -363,4 +368,8 @@ protected void doSetNextReader(LeafReaderContext readerContext) throws IOExcepti
363368
public GroupSelector<T> getGroupSelector() {
364369
return groupSelector;
365370
}
371+
372+
private boolean isGroupMapFull() {
373+
return groupMap.size() >= topNGroups;
374+
}
366375
}

0 commit comments

Comments
 (0)