Skip to content

Apache Lucene open-source search software

Extrace leaf-slice calculation path from IndexSearch#slices (#14336)

Armin Braun00e507e9630e
diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
index cfd0042c6db..ff241c3ead7 100644
--- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
+++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
@@ -363,57 +363,7 @@ public class IndexSearcher {
sortedLeaves.sort(Collections.reverseOrder(Comparator.comparingInt(l -> l.reader().maxDoc())));
if (allowSegmentPartitions) {
- final List<List<LeafReaderContextPartition>> groupedLeafPartitions = new ArrayList<>();
- int currentSliceNumDocs = 0;
- List<LeafReaderContextPartition> group = null;
- for (LeafReaderContext ctx : sortedLeaves) {
- if (ctx.reader().maxDoc() > maxDocsPerSlice) {
- assert group == null;
- // if the segment does not fit in a single slice, we split it into maximum 5 partitions of
- // equal size
- int numSlices = Math.min(5, Math.ceilDiv(ctx.reader().maxDoc(), maxDocsPerSlice));
- int numDocs = ctx.reader().maxDoc() / numSlices;
- int maxDocId = numDocs;
- int minDocId = 0;
- for (int i = 0; i < numSlices - 1; i++) {
- groupedLeafPartitions.add(
- Collections.singletonList(
- LeafReaderContextPartition.createFromAndTo(ctx, minDocId, maxDocId)));
- minDocId = maxDocId;
- maxDocId += numDocs;
- }
- // the last slice gets all the remaining docs
- groupedLeafPartitions.add(
- Collections.singletonList(
- LeafReaderContextPartition.createFromAndTo(
- ctx, minDocId, ctx.reader().maxDoc())));
- } else {
- if (group == null) {
- group = new ArrayList<>();
- groupedLeafPartitions.add(group);
- }
- group.add(LeafReaderContextPartition.createForEntireSegment(ctx));
-
- currentSliceNumDocs += ctx.reader().maxDoc();
- // We only split a segment when it does not fit entirely in a slice. We don't partition
- // the
- // segment that makes the current slice (which holds multiple segments) go over
- // maxDocsPerSlice. This means that a slice either contains multiple entire segments, or a
- // single partition of a segment.
- if (group.size() >= maxSegmentsPerSlice || currentSliceNumDocs > maxDocsPerSlice) {
- group = null;
- currentSliceNumDocs = 0;
- }
- }
- }
-
- LeafSlice[] slices = new LeafSlice[groupedLeafPartitions.size()];
- int upto = 0;
- for (List<LeafReaderContextPartition> currentGroup : groupedLeafPartitions) {
- slices[upto] = new LeafSlice(currentGroup);
- ++upto;
- }
- return slices;
+ return slicesWithSegmentPartitions(maxDocsPerSlice, maxSegmentsPerSlice, sortedLeaves);
}
final List<List<LeafReaderContext>> groupedLeaves = new ArrayList<>();
@@ -456,6 +406,60 @@ public class IndexSearcher {
return slices;
}
+ private static LeafSlice[] slicesWithSegmentPartitions(
+ int maxDocsPerSlice, int maxSegmentsPerSlice, List<LeafReaderContext> sortedLeaves) {
+ final List<List<LeafReaderContextPartition>> groupedLeafPartitions = new ArrayList<>();
+ int currentSliceNumDocs = 0;
+ List<LeafReaderContextPartition> group = null;
+ for (LeafReaderContext ctx : sortedLeaves) {
+ if (ctx.reader().maxDoc() > maxDocsPerSlice) {
+ assert group == null;
+ // if the segment does not fit in a single slice, we split it into maximum 5 partitions of
+ // equal size
+ int numSlices = Math.min(5, Math.ceilDiv(ctx.reader().maxDoc(), maxDocsPerSlice));
+ int numDocs = ctx.reader().maxDoc() / numSlices;
+ int maxDocId = numDocs;
+ int minDocId = 0;
+ for (int i = 0; i < numSlices - 1; i++) {
+ groupedLeafPartitions.add(
+ Collections.singletonList(
+ LeafReaderContextPartition.createFromAndTo(ctx, minDocId, maxDocId)));
+ minDocId = maxDocId;
+ maxDocId += numDocs;
+ }
+ // the last slice gets all the remaining docs
+ groupedLeafPartitions.add(
+ Collections.singletonList(
+ LeafReaderContextPartition.createFromAndTo(ctx, minDocId, ctx.reader().maxDoc())));
+ } else {
+ if (group == null) {
+ group = new ArrayList<>();
+ groupedLeafPartitions.add(group);
+ }
+ group.add(LeafReaderContextPartition.createForEntireSegment(ctx));
+
+ currentSliceNumDocs += ctx.reader().maxDoc();
+ // We only split a segment when it does not fit entirely in a slice. We don't partition
+ // the
+ // segment that makes the current slice (which holds multiple segments) go over
+ // maxDocsPerSlice. This means that a slice either contains multiple entire segments, or a
+ // single partition of a segment.
+ if (group.size() >= maxSegmentsPerSlice || currentSliceNumDocs > maxDocsPerSlice) {
+ group = null;
+ currentSliceNumDocs = 0;
+ }
+ }
+ }
+
+ LeafSlice[] slices = new LeafSlice[groupedLeafPartitions.size()];
+ int upto = 0;
+ for (List<LeafReaderContextPartition> currentGroup : groupedLeafPartitions) {
+ slices[upto] = new LeafSlice(currentGroup);
+ ++upto;
+ }
+ return slices;
+ }
+
/** Return the {@link IndexReader} this searches. */
public IndexReader getIndexReader() {
return reader;