Apache Lucene open-source search software
LUCENE-3069: API refactoring on MockRandom, revert supress codec in compatibility test
Han Jiang25143ff1e036
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.javaindex 45e9da54165..3ba647fce98 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java@@ -74,7 +74,7 @@ import org.junit.Ignore;// we won't even be running the actual code, only the impostor// @SuppressCodecs("Lucene4x")// Sep codec cannot yet handle the offsets in our 4.x index!-@SuppressCodecs({"MockFixedIntBlock", "MockVariableIntBlock", "MockSep", "MockRandom", "TempSep", "TempFixedIntBlock", "TempVariableIntBlock"})+@SuppressCodecs({"MockFixedIntBlock", "MockVariableIntBlock", "MockSep", "MockRandom", "Lucene40", "Lucene41", "TempSep", "TempFixedIntBlock", "TempVariableIntBlock", "TempRandom"})public class TestBackwardsCompatibility extends LuceneTestCase {// Uncomment these cases & run them on an older Lucene version,diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPostingsOffsets.java b/lucene/core/src/test/org/apache/lucene/index/TestPostingsOffsets.javaindex f9455f4b02b..9393e09d1c8 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestPostingsOffsets.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestPostingsOffsets.java@@ -49,7 +49,7 @@ import org.apache.lucene.util._TestUtil;// TODO: we really need to test indexingoffsets, but then getting only docs / docs + freqs.// not all codecs store prx separate...// TODO: fix sep codec to index offsets so we can greatly reduce this list!-@SuppressCodecs({"MockFixedIntBlock", "MockVariableIntBlock", "MockSep", "MockRandom", "TempSep", "TempFixedIntBlock", "TempVariableIntBlock"})+@SuppressCodecs({"MockFixedIntBlock", "MockVariableIntBlock", "MockSep", "MockRandom", "TempSep", "TempFixedIntBlock", "TempVariableIntBlock", "TempRandom"})public class TestPostingsOffsets extends LuceneTestCase {IndexWriterConfig iwc;diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/temp/TempRandomPostingsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/temp/TempRandomPostingsFormat.javanew file mode 100644index 00000000000..16afc37c1b3--- /dev/null+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/temp/TempRandomPostingsFormat.java@@ -0,0 +1,459 @@+package org.apache.lucene.codecs.temp;++/*+ * 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.+ */++import java.io.IOException;+import java.util.ArrayList;+import java.util.List;+import java.util.Random;++import org.apache.lucene.codecs.BlockTreeTermsReader;+import org.apache.lucene.codecs.BlockTreeTermsWriter;+import org.apache.lucene.codecs.FieldsConsumer;+import org.apache.lucene.codecs.FieldsProducer;+import org.apache.lucene.codecs.PostingsFormat;+import org.apache.lucene.codecs.TempPostingsReaderBase;+import org.apache.lucene.codecs.TempPostingsWriterBase;+import org.apache.lucene.codecs.TermStats;+import org.apache.lucene.codecs.blockterms.BlockTermsReader;+import org.apache.lucene.codecs.blockterms.BlockTermsWriter;+import org.apache.lucene.codecs.blockterms.FixedGapTermsIndexReader;+import org.apache.lucene.codecs.blockterms.FixedGapTermsIndexWriter;+import org.apache.lucene.codecs.blockterms.TermsIndexReaderBase;+import org.apache.lucene.codecs.blockterms.TermsIndexWriterBase;+import org.apache.lucene.codecs.blockterms.VariableGapTermsIndexReader;+import org.apache.lucene.codecs.blockterms.VariableGapTermsIndexWriter;+import org.apache.lucene.codecs.lucene41.Lucene41PostingsReader;+import org.apache.lucene.codecs.lucene41.Lucene41PostingsWriter;+import org.apache.lucene.codecs.mockintblock.MockFixedIntBlockPostingsFormat;+import org.apache.lucene.codecs.mockintblock.MockVariableIntBlockPostingsFormat;+import org.apache.lucene.codecs.mocksep.MockSingleIntFactory;+import org.apache.lucene.codecs.pulsing.PulsingPostingsReader;+import org.apache.lucene.codecs.pulsing.PulsingPostingsWriter;+import org.apache.lucene.codecs.sep.IntIndexInput;+import org.apache.lucene.codecs.sep.IntIndexOutput;+import org.apache.lucene.codecs.sep.IntStreamFactory;+import org.apache.lucene.codecs.sep.TempSepPostingsReader;+import org.apache.lucene.codecs.sep.TempSepPostingsWriter;+import org.apache.lucene.index.FieldInfo;+import org.apache.lucene.index.IndexFileNames;+import org.apache.lucene.index.SegmentReadState;+import org.apache.lucene.index.SegmentWriteState;+import org.apache.lucene.store.Directory;+import org.apache.lucene.store.IOContext;+import org.apache.lucene.store.IndexInput;+import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util._TestUtil;++/**+ * Randomly combines terms index impl w/ postings impls.+ */++public final class TempRandomPostingsFormat extends PostingsFormat {+ private final Random seedRandom;+ private final String SEED_EXT = "sd";++ public TempRandomPostingsFormat() {+ // This ctor should *only* be used at read-time: get NPE if you use it!+ this(null);+ }++ public TempRandomPostingsFormat(Random random) {+ super("TempRandom");+ if (random == null) {+ this.seedRandom = new Random(0L) {+ @Override+ protected int next(int arg0) {+ throw new IllegalStateException("Please use MockRandomPostingsFormat(Random)");+ }+ };+ } else {+ this.seedRandom = new Random(random.nextLong());+ }+ }++ // Chooses random IntStreamFactory depending on file's extension+ private static class MockIntStreamFactory extends IntStreamFactory {+ private final int salt;+ private final List<IntStreamFactory> delegates = new ArrayList<IntStreamFactory>();++ public MockIntStreamFactory(Random random) {+ salt = random.nextInt();+ delegates.add(new MockSingleIntFactory());+ final int blockSize = _TestUtil.nextInt(random, 1, 2000);+ delegates.add(new MockFixedIntBlockPostingsFormat.MockIntFactory(blockSize));+ final int baseBlockSize = _TestUtil.nextInt(random, 1, 127);+ delegates.add(new MockVariableIntBlockPostingsFormat.MockIntFactory(baseBlockSize));+ // TODO: others+ }++ private static String getExtension(String fileName) {+ final int idx = fileName.indexOf('.');+ assert idx != -1;+ return fileName.substring(idx);+ }++ @Override+ public IntIndexInput openInput(Directory dir, String fileName, IOContext context) throws IOException {+ // Must only use extension, because IW.addIndexes can+ // rename segment!+ final IntStreamFactory f = delegates.get((Math.abs(salt ^ getExtension(fileName).hashCode())) % delegates.size());+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: read using int factory " + f + " from fileName=" + fileName);+ }+ return f.openInput(dir, fileName, context);+ }++ @Override+ public IntIndexOutput createOutput(Directory dir, String fileName, IOContext context) throws IOException {+ final IntStreamFactory f = delegates.get((Math.abs(salt ^ getExtension(fileName).hashCode())) % delegates.size());+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: write using int factory " + f + " to fileName=" + fileName);+ }+ return f.createOutput(dir, fileName, context);+ }+ }++ @Override+ public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException {+ int minSkipInterval;+ if (state.segmentInfo.getDocCount() > 1000000) {+ // Test2BPostings can OOME otherwise:+ minSkipInterval = 3;+ } else {+ minSkipInterval = 2;+ }++ // we pull this before the seed intentionally: because its not consumed at runtime+ // (the skipInterval is written into postings header)+ int skipInterval = _TestUtil.nextInt(seedRandom, minSkipInterval, 10);++ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: skipInterval=" + skipInterval);+ }++ final long seed = seedRandom.nextLong();++ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: writing to seg=" + state.segmentInfo.name + " formatID=" + state.segmentSuffix + " seed=" + seed);+ }++ final String seedFileName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, SEED_EXT);+ final IndexOutput out = state.directory.createOutput(seedFileName, state.context);+ try {+ out.writeLong(seed);+ } finally {+ out.close();+ }++ final Random random = new Random(seed);++ random.nextInt(); // consume a random for buffersize++ TempPostingsWriterBase postingsWriter;+ if (random.nextBoolean()) {+ postingsWriter = new TempSepPostingsWriter(state, new MockIntStreamFactory(random), skipInterval);+ } else {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: writing Standard postings");+ }+ // TODO: randomize variables like acceptibleOverHead?!+ postingsWriter = new TempPostingsWriter(state, skipInterval);+ }++ if (random.nextBoolean()) {+ final int totTFCutoff = _TestUtil.nextInt(random, 1, 20);+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: writing pulsing postings with totTFCutoff=" + totTFCutoff);+ }+ postingsWriter = new TempPulsingPostingsWriter(state, totTFCutoff, postingsWriter);+ }++ final FieldsConsumer fields;+ final int t1 = random.nextInt(4);++ if (t1 == 0) {+ boolean success = false;+ try {+ fields = new TempFSTTermsWriter(state, postingsWriter);+ success = true;+ } finally {+ if (!success) {+ postingsWriter.close();+ }+ }+ } else if (t1 == 1) {+ boolean success = false;+ try {+ fields = new TempFSTOrdTermsWriter(state, postingsWriter);+ success = true;+ } finally {+ if (!success) {+ postingsWriter.close();+ }+ }+ } else if (t1 == 2) {+ // Use BlockTree terms dict++ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: writing BlockTree terms dict");+ }++ // TODO: would be nice to allow 1 but this is very+ // slow to write+ final int minTermsInBlock = _TestUtil.nextInt(random, 2, 100);+ final int maxTermsInBlock = Math.max(2, (minTermsInBlock-1)*2 + random.nextInt(100));++ boolean success = false;+ try {+ fields = new TempBlockTreeTermsWriter(state, postingsWriter, minTermsInBlock, maxTermsInBlock);+ success = true;+ } finally {+ if (!success) {+ postingsWriter.close();+ }+ }+ } else {++ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: writing Block terms dict");+ }++ boolean success = false;++ final TermsIndexWriterBase indexWriter;+ try {+ if (random.nextBoolean()) {+ int termIndexInterval = _TestUtil.nextInt(random, 1, 100);+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: fixed-gap terms index (tii=" + termIndexInterval + ")");+ }+ indexWriter = new FixedGapTermsIndexWriter(state, termIndexInterval);+ } else {+ final VariableGapTermsIndexWriter.IndexTermSelector selector;+ final int n2 = random.nextInt(3);+ if (n2 == 0) {+ final int tii = _TestUtil.nextInt(random, 1, 100);+ selector = new VariableGapTermsIndexWriter.EveryNTermSelector(tii);+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: variable-gap terms index (tii=" + tii + ")");+ }+ } else if (n2 == 1) {+ final int docFreqThresh = _TestUtil.nextInt(random, 2, 100);+ final int tii = _TestUtil.nextInt(random, 1, 100);+ selector = new VariableGapTermsIndexWriter.EveryNOrDocFreqTermSelector(docFreqThresh, tii);+ } else {+ final long seed2 = random.nextLong();+ final int gap = _TestUtil.nextInt(random, 2, 40);+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: random-gap terms index (max gap=" + gap + ")");+ }+ selector = new VariableGapTermsIndexWriter.IndexTermSelector() {+ final Random rand = new Random(seed2);++ @Override+ public boolean isIndexTerm(BytesRef term, TermStats stats) {+ return rand.nextInt(gap) == gap/2;+ }++ @Override+ public void newField(FieldInfo fieldInfo) {+ }+ };+ }+ indexWriter = new VariableGapTermsIndexWriter(state, selector);+ }+ success = true;+ } finally {+ if (!success) {+ postingsWriter.close();+ }+ }++ success = false;+ try {+ fields = new TempBlockTermsWriter(indexWriter, state, postingsWriter);+ success = true;+ } finally {+ if (!success) {+ try {+ postingsWriter.close();+ } finally {+ indexWriter.close();+ }+ }+ }+ }++ return fields;+ }++ @Override+ public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException {++ final String seedFileName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, SEED_EXT);+ final IndexInput in = state.directory.openInput(seedFileName, state.context);+ final long seed = in.readLong();+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: reading from seg=" + state.segmentInfo.name + " formatID=" + state.segmentSuffix + " seed=" + seed);+ }+ in.close();++ final Random random = new Random(seed);++ int readBufferSize = _TestUtil.nextInt(random, 1, 4096);+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: readBufferSize=" + readBufferSize);+ }++ TempPostingsReaderBase postingsReader;++ if (random.nextBoolean()) {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: reading Sep postings");+ }+ postingsReader = new TempSepPostingsReader(state.directory, state.fieldInfos, state.segmentInfo,+ state.context, new MockIntStreamFactory(random), state.segmentSuffix);+ } else {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: reading Standard postings");+ }+ postingsReader = new TempPostingsReader(state.directory, state.fieldInfos, state.segmentInfo, state.context, state.segmentSuffix);+ }++ if (random.nextBoolean()) {+ final int totTFCutoff = _TestUtil.nextInt(random, 1, 20);+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: reading pulsing postings with totTFCutoff=" + totTFCutoff);+ }+ postingsReader = new TempPulsingPostingsReader(state, postingsReader);+ }++ final FieldsProducer fields;+ final int t1 = random.nextInt(4);+ if (t1 == 0) {+ boolean success = false;+ try {+ fields = new TempFSTTermsReader(state, postingsReader);+ success = true;+ } finally {+ if (!success) {+ postingsReader.close();+ }+ }+ } else if (t1 == 1) {+ boolean success = false;+ try {+ fields = new TempFSTOrdTermsReader(state, postingsReader);+ success = true;+ } finally {+ if (!success) {+ postingsReader.close();+ }+ }+ } else if (t1 == 2) {+ // Use BlockTree terms dict+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: reading BlockTree terms dict");+ }++ boolean success = false;+ try {+ fields = new TempBlockTreeTermsReader(state.directory,+ state.fieldInfos,+ state.segmentInfo,+ postingsReader,+ state.context,+ state.segmentSuffix);+ success = true;+ } finally {+ if (!success) {+ postingsReader.close();+ }+ }+ } else {++ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: reading Block terms dict");+ }+ final TermsIndexReaderBase indexReader;+ boolean success = false;+ try {+ final boolean doFixedGap = random.nextBoolean();++ // randomness diverges from writer, here:++ if (doFixedGap) {+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: fixed-gap terms index");+ }+ indexReader = new FixedGapTermsIndexReader(state.directory,+ state.fieldInfos,+ state.segmentInfo.name,+ BytesRef.getUTF8SortedAsUnicodeComparator(),+ state.segmentSuffix, state.context);+ } else {+ final int n2 = random.nextInt(3);+ if (n2 == 1) {+ random.nextInt();+ } else if (n2 == 2) {+ random.nextLong();+ }+ if (LuceneTestCase.VERBOSE) {+ System.out.println("MockRandomCodec: variable-gap terms index");+ }+ indexReader = new VariableGapTermsIndexReader(state.directory,+ state.fieldInfos,+ state.segmentInfo.name,+ state.segmentSuffix, state.context);++ }++ success = true;+ } finally {+ if (!success) {+ postingsReader.close();+ }+ }++ success = false;+ try {+ fields = new TempBlockTermsReader(indexReader,+ state.directory,+ state.fieldInfos,+ state.segmentInfo,+ postingsReader,+ state.context,+ state.segmentSuffix);+ success = true;+ } finally {+ if (!success) {+ try {+ postingsReader.close();+ } finally {+ indexReader.close();+ }+ }+ }+ }++ return fields;+ }+}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/RandomCodec.java b/lucene/test-framework/src/java/org/apache/lucene/index/RandomCodec.javaindex 9ecc5122f33..b13532a9db5 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/RandomCodec.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/RandomCodec.java@@ -50,6 +50,7 @@ import org.apache.lucene.codecs.nestedpulsing.NestedPulsingPostingsFormat;import org.apache.lucene.codecs.pulsing.Pulsing41PostingsFormat;import org.apache.lucene.codecs.simpletext.SimpleTextPostingsFormat;import org.apache.lucene.codecs.simpletext.SimpleTextDocValuesFormat;+import org.apache.lucene.codecs.temp.*;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util._TestUtil;@@ -123,6 +124,8 @@ public class RandomCodec extends Lucene42Codec {int maxItemsPerBlock = 2*(Math.max(2, minItemsPerBlock-1)) + random.nextInt(100);int lowFreqCutoff = _TestUtil.nextInt(random, 2, 100);+ // nocommit: temporary disable other format tests+ /*add(avoidCodecs,new Lucene41PostingsFormat(minItemsPerBlock, maxItemsPerBlock),new DirectPostingsFormat(LuceneTestCase.rarely(random) ? 1 : (LuceneTestCase.rarely(random) ? Integer.MAX_VALUE : maxItemsPerBlock),@@ -146,7 +149,14 @@ public class RandomCodec extends Lucene42Codec {new AssertingPostingsFormat(),new MemoryPostingsFormat(true, random.nextFloat()),new MemoryPostingsFormat(false, random.nextFloat()));-+ */+ add(avoidCodecs,+ new TempBlockPostingsFormat(_TestUtil.nextInt(random, 1, 1000)),+ new TempBlockTreePostingsFormat(minItemsPerBlock, maxItemsPerBlock),+ new TempRandomPostingsFormat(random),+ new TempNestedPulsingPostingsFormat(),+ new TempPulsing41PostingsFormat(1 + random.nextInt(20), minItemsPerBlock, maxItemsPerBlock));+addDocValues(avoidCodecs,new Lucene42DocValuesFormat(),new DiskDocValuesFormat(),diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex 8bdd0ba21c7..d9fa478789e 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -327,7 +327,8 @@ public abstract class LuceneTestCase extends Assert {"MockRandom","TempSep","TempFixedIntBlock",- "TempVariableIntBlock"+ "TempVariableIntBlock",+ "TempRandom"));// -----------------------------------------------------------------diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.javaindex ebe0426a18c..db34ac868a1 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java@@ -40,6 +40,7 @@ import org.apache.lucene.codecs.lucene40.Lucene40RWCodec;import org.apache.lucene.codecs.lucene40.Lucene40RWPostingsFormat;import org.apache.lucene.codecs.lucene41.Lucene41RWCodec;import org.apache.lucene.codecs.lucene42.Lucene42Codec;+import org.apache.lucene.codecs.mockrandom.MockRandomPostingsFormat;import org.apache.lucene.codecs.simpletext.SimpleTextCodec;import org.apache.lucene.index.RandomCodec;import org.apache.lucene.search.RandomSimilarityProvider;@@ -168,6 +169,8 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {final PostingsFormat format;if ("random".equals(TEST_POSTINGSFORMAT)) {format = PostingsFormat.forName("Lucene41");+ } else if ("TempRandom".equals(TEST_POSTINGSFORMAT)) {+ format = new MockRandomPostingsFormat(new Random(random.nextLong()));} else {format = PostingsFormat.forName(TEST_POSTINGSFORMAT);}diff --git a/lucene/test-framework/src/resources/META-INF/services/org.apache.lucene.codecs.PostingsFormat b/lucene/test-framework/src/resources/META-INF/services/org.apache.lucene.codecs.PostingsFormatindex 1674c9b79ac..5e55cef461a 100644--- a/lucene/test-framework/src/resources/META-INF/services/org.apache.lucene.codecs.PostingsFormat+++ b/lucene/test-framework/src/resources/META-INF/services/org.apache.lucene.codecs.PostingsFormat@@ -28,3 +28,4 @@ org.apache.lucene.codecs.lucene40.Lucene40RWPostingsFormatorg.apache.lucene.codecs.temp.TempSepPostingsFormatorg.apache.lucene.codecs.temp.TempFixedIntBlockPostingsFormatorg.apache.lucene.codecs.temp.TempVariableIntBlockPostingsFormat+org.apache.lucene.codecs.temp.TempRandomPostingsFormat