diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnDiskFull.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnDiskFull.java index 63d3b7af0ae..ff33ff7e717 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnDiskFull.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnDiskFull.java @@ -490,7 +490,8 @@ public class TestIndexWriterOnDiskFull extends LuceneTestCase { setReaderPooling(true). setMergePolicy(newLogMergePolicy(2)) ); - _TestUtil.keepFullyDeletedSegments(w); + // we can do this because we add/delete/add (and dont merge to "nothing") + w.setKeepFullyDeletedSegments(true); Document doc = new Document(); diff --git a/lucene/core/src/test/org/apache/lucene/index/TestMultiFields.java b/lucene/core/src/test/org/apache/lucene/index/TestMultiFields.java index a9b14822683..1f51ecb605a 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestMultiFields.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestMultiFields.java @@ -37,7 +37,8 @@ public class TestMultiFields extends LuceneTestCase { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random())).setMergePolicy(NoMergePolicy.COMPOUND_FILES)); - _TestUtil.keepFullyDeletedSegments(w); + // we can do this because we use NoMergePolicy (and dont merge to "nothing") + w.setKeepFullyDeletedSegments(true); Map> docs = new HashMap>(); Set deleted = new HashSet(); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperFilter.java b/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperFilter.java index 80d4e3d6f42..2313b07bd6f 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperFilter.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperFilter.java @@ -173,7 +173,6 @@ public class TestCachingWrapperFilter extends LuceneTestCase { // asserts below requires no unexpected merges: setMergePolicy(newLogMergePolicy(10)) ); - _TestUtil.keepFullyDeletedSegments(writer.w); // NOTE: cannot use writer.getReader because RIW (on // flipping a coin) may give us a newly opened reader, diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AllDeletedFilterReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AllDeletedFilterReader.java new file mode 100644 index 00000000000..6667607ac5b --- /dev/null +++ b/lucene/test-framework/src/java/org/apache/lucene/index/AllDeletedFilterReader.java @@ -0,0 +1,42 @@ +package org.apache.lucene.index; + +import org.apache.lucene.util.Bits; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Filters the incoming reader and makes all documents appear deleted. + */ +public class AllDeletedFilterReader extends FilterAtomicReader { + final Bits liveDocs; + + public AllDeletedFilterReader(AtomicReader in) { + super(in); + liveDocs = new Bits.MatchNoBits(in.maxDoc()); + } + + @Override + public Bits getLiveDocs() { + return liveDocs; + } + + @Override + public int numDocs() { + return 0; + } +} diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java b/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java index ff03f6e62a5..2ac8d62149c 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java +++ b/lucene/test-framework/src/java/org/apache/lucene/search/QueryUtils.java @@ -25,6 +25,7 @@ import junit.framework.Assert; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.index.AllDeletedFilterReader; import org.apache.lucene.index.AtomicReader; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.IndexReader; @@ -38,7 +39,6 @@ import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Bits; import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.util._TestUtil; import static org.apache.lucene.util.LuceneTestCase.TEST_VERSION_CURRENT; @@ -193,7 +193,7 @@ public class QueryUtils { static final IndexReader[] emptyReaders = new IndexReader[8]; static { try { - emptyReaders[0] = makeEmptyIndex(new Random(0), 0); + emptyReaders[0] = new MultiReader(); emptyReaders[4] = makeEmptyIndex(new Random(0), 4); emptyReaders[5] = makeEmptyIndex(new Random(0), 5); emptyReaders[7] = makeEmptyIndex(new Random(0), 7); @@ -202,31 +202,18 @@ public class QueryUtils { } } - private static DirectoryReader makeEmptyIndex(Random random, final int numDeletedDocs) - throws IOException { + private static IndexReader makeEmptyIndex(Random random, final int numDocs) throws IOException { + assert numDocs > 0; Directory d = new MockDirectoryWrapper(random, new RAMDirectory()); - IndexWriter w = new IndexWriter(d, new IndexWriterConfig( - TEST_VERSION_CURRENT, new MockAnalyzer(random))); - for (int i = 0; i < numDeletedDocs; i++) { - w.addDocument(new Document()); - } - w.commit(); - w.deleteDocuments( new MatchAllDocsQuery() ); - _TestUtil.keepFullyDeletedSegments(w); - w.commit(); - - if (0 < numDeletedDocs) - Assert.assertTrue("writer has no deletions", w.hasDeletions()); - - Assert.assertEquals("writer is missing some deleted docs", - numDeletedDocs, w.maxDoc()); - Assert.assertEquals("writer has non-deleted docs", - 0, w.numDocs()); - w.close(); - DirectoryReader r = DirectoryReader.open(d); - Assert.assertEquals("reader has wrong number of deleted docs", - numDeletedDocs, r.numDeletedDocs()); - return r; + IndexWriter w = new IndexWriter(d, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))); + for (int i = 0; i < numDocs; i++) { + w.addDocument(new Document()); + } + w.forceMerge(1); + w.commit(); + w.close(); + DirectoryReader reader = DirectoryReader.open(d); + return new AllDeletedFilterReader(LuceneTestCase.getOnlySegmentReader(reader)); } /** alternate scorer skipTo(),skipTo(),next(),next(),skipTo(),skipTo(), etc diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java index c5dea7fae9c..e68d42c2bf0 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; -import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.CharBuffer; @@ -788,19 +787,6 @@ public class _TestUtil { }); Assert.assertEquals("Reflection does not produce same map", reflectedValues, map); } - - public static void keepFullyDeletedSegments(IndexWriter w) { - try { - // Carefully invoke what is a package-private (test - // only, internal) method on IndexWriter: - Method m = IndexWriter.class.getDeclaredMethod("setKeepFullyDeletedSegments", boolean.class); - m.setAccessible(true); - m.invoke(w, Boolean.TRUE); - } catch (Exception e) { - // Should not happen? - throw new RuntimeException(e); - } - } /** * insecure, fast version of File.createTempFile