From c8b47e2024ddef066b297b7bd67983ebf458b060 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 5 Sep 2018 15:26:37 +0200 Subject: [PATCH] LUCENE-8484: Only drop fully deleted segments in SoftDeletesDirectoryReaderWrapper This specializes the change in LUCENE-8484 since there are valid usecases where filtered reader should not modify the number of readers if a higher level reader wants to expose the deleted or soft-deleted docs. --- .../lucene/index/FilterDirectoryReader.java | 17 ++++++++++------- .../SoftDeletesDirectoryReaderWrapper.java | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) 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();