From 1b9c6a24d4fb11f4b17abf0828e6b644cf7d4a23 Mon Sep 17 00:00:00 2001 From: Shai Erera Date: Sun, 1 Sep 2013 10:35:58 +0000 Subject: [PATCH] LUCENE-5196: add LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1519258 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/index/TestCodecs.java | 30 ++++++++++++++++ .../codecs/lucene40/Lucene40RWCodec.java | 9 ++++- .../lucene40/Lucene40RWDocValuesFormat.java | 14 +++++--- .../lucene40/Lucene40RWNormsFormat.java | 14 +++++--- .../lucene40/Lucene40RWPostingsFormat.java | 35 +++++++++++-------- .../codecs/lucene41/Lucene41RWCodec.java | 8 ++++- .../lucene42/Lucene42RWDocValuesFormat.java | 10 ++++-- .../apache/lucene/util/LuceneTestCase.java | 8 +++-- 8 files changed, 100 insertions(+), 28 deletions(-) diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java index 8e56129c790..a446e4b13be 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestCodecs.java @@ -30,10 +30,14 @@ import org.apache.lucene.codecs.FieldsProducer; import org.apache.lucene.codecs.PostingsConsumer; import org.apache.lucene.codecs.TermStats; import org.apache.lucene.codecs.TermsConsumer; +import org.apache.lucene.codecs.lucene40.Lucene40RWCodec; +import org.apache.lucene.codecs.lucene41.Lucene41RWCodec; +import org.apache.lucene.codecs.lucene42.Lucene42RWCodec; import org.apache.lucene.codecs.mocksep.MockSepPostingsFormat; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.FieldType; +import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.FieldInfo.DocValuesType; @@ -695,4 +699,30 @@ public class TestCodecs extends LuceneTestCase { dir.close(); } + + public void testDisableImpersonation() throws Exception { + Codec[] oldCodecs = new Codec[] { new Lucene40RWCodec(), new Lucene41RWCodec(), new Lucene42RWCodec() }; + Directory dir = newDirectory(); + IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); + conf.setCodec(oldCodecs[random().nextInt(oldCodecs.length)]); + IndexWriter writer = new IndexWriter(dir, conf); + + Document doc = new Document(); + doc.add(new StringField("f", "bar", Store.YES)); + doc.add(new NumericDocValuesField("n", 18L)); + writer.addDocument(doc); + + OLD_FORMAT_IMPERSONATION_IS_ACTIVE = false; + try { + writer.close(); + fail("should not have succeeded to impersonate an old format!"); + } catch (UnsupportedOperationException e) { + writer.rollback(); + } finally { + OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; + } + + dir.close(); + } + } diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java index 8cd59d8b154..b894ff73125 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWCodec.java @@ -6,6 +6,7 @@ import org.apache.lucene.codecs.DocValuesFormat; import org.apache.lucene.codecs.FieldInfosFormat; import org.apache.lucene.codecs.FieldInfosWriter; import org.apache.lucene.codecs.NormsFormat; +import org.apache.lucene.util.LuceneTestCase; /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -25,11 +26,17 @@ import org.apache.lucene.codecs.NormsFormat; */ /** Read-write version of Lucene40Codec for testing */ +@SuppressWarnings("deprecation") public final class Lucene40RWCodec extends Lucene40Codec { + private final FieldInfosFormat fieldInfos = new Lucene40FieldInfosFormat() { @Override public FieldInfosWriter getFieldInfosWriter() throws IOException { - return new Lucene40FieldInfosWriter(); + if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) { + return super.getFieldInfosWriter(); + } else { + return new Lucene40FieldInfosWriter(); + } } }; diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWDocValuesFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWDocValuesFormat.java index 127f4d3d9c1..64b7f02aeee 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWDocValuesFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWDocValuesFormat.java @@ -22,15 +22,21 @@ import java.io.IOException; import org.apache.lucene.codecs.DocValuesConsumer; import org.apache.lucene.index.IndexFileNames; import org.apache.lucene.index.SegmentWriteState; +import org.apache.lucene.util.LuceneTestCase; /** Read-write version of {@link Lucene40DocValuesFormat} for testing */ +@SuppressWarnings("deprecation") public class Lucene40RWDocValuesFormat extends Lucene40DocValuesFormat { @Override public DocValuesConsumer fieldsConsumer(SegmentWriteState state) throws IOException { - String filename = IndexFileNames.segmentFileName(state.segmentInfo.name, - "dv", - IndexFileNames.COMPOUND_FILE_EXTENSION); - return new Lucene40DocValuesWriter(state, filename, Lucene40FieldInfosReader.LEGACY_DV_TYPE_KEY); + if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) { + return super.fieldsConsumer(state); + } else { + String filename = IndexFileNames.segmentFileName(state.segmentInfo.name, + "dv", + IndexFileNames.COMPOUND_FILE_EXTENSION); + return new Lucene40DocValuesWriter(state, filename, Lucene40FieldInfosReader.LEGACY_DV_TYPE_KEY); + } } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWNormsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWNormsFormat.java index af32f43ccf5..4372138224c 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWNormsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWNormsFormat.java @@ -22,15 +22,21 @@ import java.io.IOException; import org.apache.lucene.codecs.DocValuesConsumer; import org.apache.lucene.index.IndexFileNames; import org.apache.lucene.index.SegmentWriteState; +import org.apache.lucene.util.LuceneTestCase; /** Read-write version of {@link Lucene40NormsFormat} for testing */ +@SuppressWarnings("deprecation") public class Lucene40RWNormsFormat extends Lucene40NormsFormat { @Override public DocValuesConsumer normsConsumer(SegmentWriteState state) throws IOException { - String filename = IndexFileNames.segmentFileName(state.segmentInfo.name, - "nrm", - IndexFileNames.COMPOUND_FILE_EXTENSION); - return new Lucene40DocValuesWriter(state, filename, Lucene40FieldInfosReader.LEGACY_NORM_TYPE_KEY); + if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) { + return super.normsConsumer(state); + } else { + String filename = IndexFileNames.segmentFileName(state.segmentInfo.name, + "nrm", + IndexFileNames.COMPOUND_FILE_EXTENSION); + return new Lucene40DocValuesWriter(state, filename, Lucene40FieldInfosReader.LEGACY_NORM_TYPE_KEY); + } } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWPostingsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWPostingsFormat.java index f749216bf38..90b3f0c5017 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWPostingsFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene40/Lucene40RWPostingsFormat.java @@ -6,6 +6,7 @@ import org.apache.lucene.codecs.BlockTreeTermsWriter; import org.apache.lucene.codecs.FieldsConsumer; import org.apache.lucene.codecs.PostingsWriterBase; import org.apache.lucene.index.SegmentWriteState; +import org.apache.lucene.util.LuceneTestCase; /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -27,23 +28,29 @@ import org.apache.lucene.index.SegmentWriteState; /** * Read-write version of {@link Lucene40PostingsFormat} for testing. */ +@SuppressWarnings("deprecation") public class Lucene40RWPostingsFormat extends Lucene40PostingsFormat { + @Override public FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException { - PostingsWriterBase docs = new Lucene40PostingsWriter(state); - - // TODO: should we make the terms index more easily - // pluggable? Ie so that this codec would record which - // index impl was used, and switch on loading? - // Or... you must make a new Codec for this? - boolean success = false; - try { - FieldsConsumer ret = new BlockTreeTermsWriter(state, docs, minBlockSize, maxBlockSize); - success = true; - return ret; - } finally { - if (!success) { - docs.close(); + if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) { + return super.fieldsConsumer(state); + } else { + PostingsWriterBase docs = new Lucene40PostingsWriter(state); + + // TODO: should we make the terms index more easily + // pluggable? Ie so that this codec would record which + // index impl was used, and switch on loading? + // Or... you must make a new Codec for this? + boolean success = false; + try { + FieldsConsumer ret = new BlockTreeTermsWriter(state, docs, minBlockSize, maxBlockSize); + success = true; + return ret; + } finally { + if (!success) { + docs.close(); + } } } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene41/Lucene41RWCodec.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene41/Lucene41RWCodec.java index f03e437ef3c..8bd91d2abf3 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene41/Lucene41RWCodec.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene41/Lucene41RWCodec.java @@ -11,6 +11,7 @@ import org.apache.lucene.codecs.lucene40.Lucene40FieldInfosFormat; import org.apache.lucene.codecs.lucene40.Lucene40FieldInfosWriter; import org.apache.lucene.codecs.lucene40.Lucene40RWDocValuesFormat; import org.apache.lucene.codecs.lucene40.Lucene40RWNormsFormat; +import org.apache.lucene.util.LuceneTestCase; /* * Licensed to the Apache Software Foundation (ASF) under one or more @@ -32,12 +33,17 @@ import org.apache.lucene.codecs.lucene40.Lucene40RWNormsFormat; /** * Read-write version of {@link Lucene41Codec} for testing. */ +@SuppressWarnings("deprecation") public class Lucene41RWCodec extends Lucene41Codec { private final StoredFieldsFormat fieldsFormat = new Lucene41StoredFieldsFormat(); private final FieldInfosFormat fieldInfos = new Lucene40FieldInfosFormat() { @Override public FieldInfosWriter getFieldInfosWriter() throws IOException { - return new Lucene40FieldInfosWriter(); + if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) { + return super.getFieldInfosWriter(); + } else { + return new Lucene40FieldInfosWriter(); + } } }; diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene42/Lucene42RWDocValuesFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene42/Lucene42RWDocValuesFormat.java index 45372246184..569ba9e8881 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene42/Lucene42RWDocValuesFormat.java +++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/lucene42/Lucene42RWDocValuesFormat.java @@ -21,15 +21,21 @@ import java.io.IOException; import org.apache.lucene.codecs.DocValuesConsumer; import org.apache.lucene.index.SegmentWriteState; +import org.apache.lucene.util.LuceneTestCase; /** * Read-write version of {@link Lucene42DocValuesFormat} for testing. */ +@SuppressWarnings("deprecation") public class Lucene42RWDocValuesFormat extends Lucene42DocValuesFormat { @Override public DocValuesConsumer fieldsConsumer(SegmentWriteState state) throws IOException { - // note: we choose DEFAULT here (its reasonably fast, and for small bpv has tiny waste) - return new Lucene42DocValuesConsumer(state, DATA_CODEC, DATA_EXTENSION, METADATA_CODEC, METADATA_EXTENSION, acceptableOverheadRatio); + if (!LuceneTestCase.OLD_FORMAT_IMPERSONATION_IS_ACTIVE) { + return super.fieldsConsumer(state); + } else { + // note: we choose DEFAULT here (its reasonably fast, and for small bpv has tiny waste) + return new Lucene42DocValuesConsumer(state, DATA_CODEC, DATA_EXTENSION, METADATA_CODEC, METADATA_EXTENSION, acceptableOverheadRatio); + } } } 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.java index 1ca13974195..b7ff072117a 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 @@ -332,9 +332,13 @@ public abstract class LuceneTestCase extends Assert { // ----------------------------------------------------------------- /** - * @lucene.internal + * When {@code true}, Codecs for old Lucene version will support writing + * indexes in that format. Defaults to {@code true}, can be disabled by + * spdecific tests on demand. + * + * @lucene.internal */ - public static boolean PREFLEX_IMPERSONATION_IS_ACTIVE; + public static boolean OLD_FORMAT_IMPERSONATION_IS_ACTIVE = true; // -----------------------------------------------------------------