Apache Lucene open-source search software
ExitableDirectoryReader should not cover-up singletons (#15498)
Houston Putmandc5720480919
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 660decf9538..11d44a0ec30 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -263,6 +263,8 @@ Optimizations* GITHUB#15500: Use bulk scoring for filtered HNSW search and for entry-point scoring in the graph. This shouldprovide speed improvements when using vector scorers that satisfy the bulk scoring interface. (Ben Trent)+* GITHUB#15498: ExitableDirectoryReader keeps singleton SortedSetDocValues or SortedNumericDocValues as singletons. (Houston Putman)+Bug Fixes---------------------* GITHUB#14161: PointInSetQuery's constructor now throws IllegalArgumentExceptiondiff --git a/lucene/core/src/java/org/apache/lucene/index/ExitableDirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/ExitableDirectoryReader.javaindex 9b6843e778d..be4b951a9c2 100644--- a/lucene/core/src/java/org/apache/lucene/index/ExitableDirectoryReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/ExitableDirectoryReader.java@@ -109,7 +109,10 @@ public class ExitableDirectoryReader extends FilterDirectoryReader {@Overridepublic NumericDocValues getNumericDocValues(String field) throws IOException {- final NumericDocValues numericDocValues = super.getNumericDocValues(field);+ return wrapNumericDocValues(super.getNumericDocValues(field));+ }++ private NumericDocValues wrapNumericDocValues(final NumericDocValues numericDocValues) {if (numericDocValues == null) {return null;}@@ -191,7 +194,10 @@ public class ExitableDirectoryReader extends FilterDirectoryReader {@Overridepublic SortedDocValues getSortedDocValues(String field) throws IOException {- final SortedDocValues sortedDocValues = super.getSortedDocValues(field);+ return wrapSortedDocValues(super.getSortedDocValues(field));+ }++ private SortedDocValues wrapSortedDocValues(final SortedDocValues sortedDocValues) {if (sortedDocValues == null) {return null;}@@ -234,6 +240,10 @@ public class ExitableDirectoryReader extends FilterDirectoryReader {@Overridepublic SortedNumericDocValues getSortedNumericDocValues(String field) throws IOException {final SortedNumericDocValues sortedNumericDocValues = super.getSortedNumericDocValues(field);+ final NumericDocValues numericDocValues = DocValues.unwrapSingleton(sortedNumericDocValues);+ if (numericDocValues != null) {+ return DocValues.singleton(wrapNumericDocValues(numericDocValues));+ }if (sortedNumericDocValues == null) {return null;}@@ -279,6 +289,10 @@ public class ExitableDirectoryReader extends FilterDirectoryReader {if (sortedSetDocValues == null) {return null;}+ final SortedDocValues sortedDocValues = DocValues.unwrapSingleton(sortedSetDocValues);+ if (sortedDocValues != null) {+ return DocValues.singleton(wrapSortedDocValues(sortedDocValues));+ }return new FilterSortedSetDocValues(sortedSetDocValues) {private int docToCheck = 0;diff --git a/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.javaindex 14e21bc3069..821f3c8f57b 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestExitableDirectoryReader.java@@ -45,6 +45,7 @@ import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.IOFunction;import org.apache.lucene.util.SuppressForbidden;import org.apache.lucene.util.TestVectorUtil;+import org.hamcrest.Matchers;/*** Test that uses a default/lucene Implementation of {@link QueryTimeout} to exit out long running@@ -338,71 +339,88 @@ public class TestExitableDirectoryReader extends LuceneTestCase {}public void testDocValues() throws IOException {- Directory directory = newDirectory();- IndexWriter writer =- new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random())));-- Document d1 = new Document();- addDVs(d1, 10);- writer.addDocument(d1);-- Document d2 = new Document();- addDVs(d2, 100);- writer.addDocument(d2);-- Document d3 = new Document();- addDVs(d3, 1000);- writer.addDocument(d3);-- writer.forceMerge(1);- writer.commit();- writer.close();-- DirectoryReader directoryReader;- DirectoryReader exitableDirectoryReader;+ try (Directory directory = newDirectory()) {+ IndexWriter writer =+ new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random())));++ Document d1 = new Document();+ addDVs(d1, 10);+ writer.addDocument(d1);++ Document d2 = new Document();+ addDVs(d2, 100);+ writer.addDocument(d2);++ Document d3 = new Document();+ addDVs(d3, 1000);+ writer.addDocument(d3);++ writer.forceMerge(1);+ writer.commit();+ writer.close();++ DirectoryReader directoryReader;+ DirectoryReader exitableDirectoryReader;++ for (IOFunction<LeafReader, DocValuesIterator> dvFactory :+ Arrays.<IOFunction<LeafReader, DocValuesIterator>>asList(+ (r) -> r.getSortedDocValues("sorted"),+ (r) -> r.getSortedSetDocValues("sortedset"),+ (r) -> r.getSortedNumericDocValues("sortednumeric"),+ (r) -> r.getNumericDocValues("numeric"),+ (r) -> r.getBinaryDocValues("binary"))) {+ directoryReader = DirectoryReader.open(directory);+ exitableDirectoryReader =+ new ExitableDirectoryReader(directoryReader, immediateQueryTimeout());++ try (IndexReader reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader))) {+ expectThrows(+ ExitingReaderException.class,+ () -> {+ LeafReader leaf = reader.leaves().get(0).reader();+ DocValuesIterator iter = dvFactory.apply(leaf);+ scan(leaf, iter);+ });+ }- for (IOFunction<LeafReader, DocValuesIterator> dvFactory :- Arrays.<IOFunction<LeafReader, DocValuesIterator>>asList(- (r) -> r.getSortedDocValues("sorted"),- (r) -> r.getSortedSetDocValues("sortedset"),- (r) -> r.getSortedNumericDocValues("sortednumeric"),- (r) -> r.getNumericDocValues("numeric"),- (r) -> r.getBinaryDocValues("binary"))) {- directoryReader = DirectoryReader.open(directory);- exitableDirectoryReader =- new ExitableDirectoryReader(directoryReader, immediateQueryTimeout());-- {- IndexReader reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));-- expectThrows(- ExitingReaderException.class,- () -> {- LeafReader leaf = reader.leaves().get(0).reader();- DocValuesIterator iter = dvFactory.apply(leaf);- scan(leaf, iter);- });- reader.close();+ directoryReader = DirectoryReader.open(directory);+ exitableDirectoryReader =+ new ExitableDirectoryReader(directoryReader, infiniteQueryTimeout());+ try (IndexReader reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader))) {+ final LeafReader leaf = reader.leaves().get(0).reader();+ scan(leaf, dvFactory.apply(leaf));+ assertNull(leaf.getNumericDocValues("absent"));+ assertNull(leaf.getBinaryDocValues("absent"));+ assertNull(leaf.getSortedDocValues("absent"));+ assertNull(leaf.getSortedNumericDocValues("absent"));+ assertNull(leaf.getSortedSetDocValues("absent"));+ }}+ // Test that singleton docValues stay as singleton docValues after filteringdirectoryReader = DirectoryReader.open(directory);+ try (IndexReader reader = new TestReader(getOnlyLeafReader(directoryReader))) {+ LeafReader leafReader = reader.leaves().getFirst().reader();+ assertThat(+ leafReader.getSortedSetDocValues("sortedset"),+ Matchers.instanceOf(SingletonSortedSetDocValues.class));+ assertThat(+ leafReader.getSortedNumericDocValues("sortednumeric"),+ Matchers.instanceOf(SingletonSortedNumericDocValues.class));+ }exitableDirectoryReader =- new ExitableDirectoryReader(directoryReader, infiniteQueryTimeout());- {- IndexReader reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));- final LeafReader leaf = reader.leaves().get(0).reader();- scan(leaf, dvFactory.apply(leaf));- assertNull(leaf.getNumericDocValues("absent"));- assertNull(leaf.getBinaryDocValues("absent"));- assertNull(leaf.getSortedDocValues("absent"));- assertNull(leaf.getSortedNumericDocValues("absent"));- assertNull(leaf.getSortedSetDocValues("absent"));-- reader.close();+ new ExitableDirectoryReader(DirectoryReader.open(directory), infiniteQueryTimeout());+ try (IndexReader extitableReader =+ new TestReader(getOnlyLeafReader(exitableDirectoryReader))) {+ LeafReader exitableLeafReader = extitableReader.leaves().getFirst().reader();+ assertThat(+ exitableLeafReader.getSortedSetDocValues("sortedset"),+ Matchers.instanceOf(SingletonSortedSetDocValues.class));+ assertThat(+ exitableLeafReader.getSortedNumericDocValues("sortednumeric"),+ Matchers.instanceOf(SingletonSortedNumericDocValues.class));}}-- directory.close();}public void testFloatVectorValues() throws IOException {