diff --git a/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java index 1efd482d9ed..4a9cd68f410 100644 --- a/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/FilterDirectoryReader.java @@ -18,7 +18,6 @@ package org.apache.lucene.index; import java.io.IOException; -import java.util.ArrayList; import java.util.List; /** @@ -51,16 +50,20 @@ public abstract class FilterDirectoryReader extends DirectoryReader { */ public static abstract class SubReaderWrapper { - private LeafReader[] wrap(List readers) { - List wrapped = new ArrayList<>(readers.size()); + /** + * Wraps a list of LeafReaders + * @return an array of wrapped LeafReaders. The returned array might contain less elements compared to the given + * reader list if an entire reader is filtered out. + */ + protected LeafReader[] wrap(List readers) { + LeafReader[] wrapped = new LeafReader[readers.size()]; + int i = 0; for (LeafReader reader : readers) { LeafReader wrap = wrap(reader); assert wrap != null; - if (wrap.numDocs() > 0) { - wrapped.add(wrap); - } + wrapped[i++] = wrap; } - return wrapped.toArray(new LeafReader[0]); + return wrapped; } /** Constructor */ diff --git a/lucene/core/src/java/org/apache/lucene/index/SoftDeletesDirectoryReaderWrapper.java b/lucene/core/src/java/org/apache/lucene/index/SoftDeletesDirectoryReaderWrapper.java index cf3e437ab9f..21aac962fc9 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SoftDeletesDirectoryReaderWrapper.java +++ b/lucene/core/src/java/org/apache/lucene/index/SoftDeletesDirectoryReaderWrapper.java @@ -19,8 +19,10 @@ package org.apache.lucene.index; import java.io.IOException; import java.io.UncheckedIOException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; @@ -86,6 +88,18 @@ public final class SoftDeletesDirectoryReaderWrapper extends FilterDirectoryRead this.field = field; } + protected LeafReader[] wrap(List readers) { + List wrapped = new ArrayList<>(readers.size()); + for (LeafReader reader : readers) { + LeafReader wrap = wrap(reader); + assert wrap != null; + if (wrap.numDocs() != 0) { + wrapped.add(wrap); + } + } + return wrapped.toArray(new LeafReader[0]); + } + @Override public LeafReader wrap(LeafReader reader) { CacheHelper readerCacheHelper = reader.getReaderCacheHelper();