Apache Lucene open-source search software
LUCENE-4055: use a codec header for the upgraded 3.x SI
Robert Muirf7210b248189
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoFormat.javaindex 82d7e17e809..0d531688420 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoFormat.java@@ -42,13 +42,12 @@ public class Lucene3xSegmentInfoFormat extends SegmentInfoFormat {/** Each segment records the Lucene version that created it. */public static final int FORMAT_3_1 = -11;- /** Each segment records whether its postings are written- * in the new flex format */- public static final int FORMAT_4X_UPGRADE = -12;-/** Extension used for saving each SegmentInfo, once a 3.x* index is first committed to with 4.0. */- public static final String SI_EXTENSION = "si";+ public static final String UPGRADED_SI_EXTENSION = "si";+ public static final String UPGRADED_SI_CODEC_NAME = "Lucene3xSegmentInfo";+ public static final int UPGRADED_SI_VERSION_START = 0;+ public static final int UPGRADED_SI_VERSION_CURRENT = UPGRADED_SI_VERSION_START;@Overridepublic SegmentInfoReader getSegmentInfosReader() {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoReader.javaindex 27f5d3d27ed..c8442a555d6 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene3x/Lucene3xSegmentInfoReader.java@@ -35,6 +35,7 @@ import org.apache.lucene.store.CompoundFileDirectory;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;+import org.apache.lucene.util.CodecUtil;import org.apache.lucene.util.IOUtils;/**@@ -95,14 +96,13 @@ public class Lucene3xSegmentInfoReader extends SegmentInfoReader {@Overridepublic SegmentInfo read(Directory directory, String segmentName, IOContext context) throws IOException {// NOTE: this is NOT how 3.x is really written...- String fileName = IndexFileNames.segmentFileName(segmentName, "", Lucene3xSegmentInfoFormat.SI_EXTENSION);+ String fileName = IndexFileNames.segmentFileName(segmentName, "", Lucene3xSegmentInfoFormat.UPGRADED_SI_EXTENSION);boolean success = false;IndexInput input = directory.openInput(fileName, context);try {- // nocommit: we need a version headerSegmentInfo si = readUpgradedSegmentInfo(segmentName, directory, input);success = true;return si;@@ -124,14 +124,13 @@ public class Lucene3xSegmentInfoReader extends SegmentInfoReader {/** reads from legacy 3.x segments_N */private SegmentInfoPerCommit readLegacySegmentInfo(Directory dir, int format, IndexInput input) throws IOException {// check that it is a format we can understand- assert format != Lucene3xSegmentInfoFormat.FORMAT_4X_UPGRADE;if (format > Lucene3xSegmentInfoFormat.FORMAT_DIAGNOSTICS) {throw new IndexFormatTooOldException(input, format,- Lucene3xSegmentInfoFormat.FORMAT_DIAGNOSTICS, Lucene3xSegmentInfoFormat.FORMAT_4X_UPGRADE);+ Lucene3xSegmentInfoFormat.FORMAT_DIAGNOSTICS, Lucene3xSegmentInfoFormat.FORMAT_3_1);}if (format < Lucene3xSegmentInfoFormat.FORMAT_3_1) {throw new IndexFormatTooNewException(input, format,- Lucene3xSegmentInfoFormat.FORMAT_DIAGNOSTICS, Lucene3xSegmentInfoFormat.FORMAT_4X_UPGRADE);+ Lucene3xSegmentInfoFormat.FORMAT_DIAGNOSTICS, Lucene3xSegmentInfoFormat.FORMAT_3_1);}final String version;if (format <= Lucene3xSegmentInfoFormat.FORMAT_3_1) {@@ -248,7 +247,9 @@ public class Lucene3xSegmentInfoReader extends SegmentInfoReader {}private SegmentInfo readUpgradedSegmentInfo(String name, Directory dir, IndexInput input) throws IOException {-+ CodecUtil.checkHeader(input, Lucene3xSegmentInfoFormat.UPGRADED_SI_CODEC_NAME,+ Lucene3xSegmentInfoFormat.UPGRADED_SI_VERSION_START,+ Lucene3xSegmentInfoFormat.UPGRADED_SI_VERSION_CURRENT);final String version = input.readString();final int docCount = input.readInt();diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 2a7a3079434..cfd6863d170 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -2371,9 +2371,6 @@ public class IndexWriter implements Closeable, TwoPhaseCommit {Set<String> codecDocStoreFiles = new HashSet<String>();final boolean hasSharedDocStore = Lucene3xSegmentInfoFormat.getDocStoreOffset(info.info) != -1;- final String segmentInfoFileName3X = IndexFileNames.segmentFileName(info.info.name,- "",- Lucene3xSegmentInfoFormat.SI_EXTENSION);// copy the attributes map, we modify it for the preflex casefinal Map<String,String> attributes;diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.javaindex 77a82fe372a..bb6ee7b3ad2 100644--- a/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java+++ b/lucene/core/src/java/org/apache/lucene/index/SegmentInfos.java@@ -373,7 +373,7 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentInfoPerCom// "ugprade" to write the .si file for it:String version = si.getVersion();if (version == null || StringHelper.getVersionComparator().compare(version, "4.0") < 0) {- String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene3xSegmentInfoFormat.SI_EXTENSION);+ String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene3xSegmentInfoFormat.UPGRADED_SI_EXTENSION);if (!directory.fileExists(fileName)) {upgradedSIFiles.add(write3xInfo(directory, si, IOContext.DEFAULT));}@@ -411,7 +411,7 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentInfoPerCompublic static String write3xInfo(Directory dir, SegmentInfo si, IOContext context) throws IOException {// NOTE: this is NOT how 3.x is really written...- String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene3xSegmentInfoFormat.SI_EXTENSION);+ String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene3xSegmentInfoFormat.UPGRADED_SI_EXTENSION);si.addFile(fileName);//System.out.println("UPGRADE write " + fileName);@@ -421,6 +421,8 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentInfoPerCom// we are about to write this SI in 3.x format, dropping all codec information, etc.// so it had better be a 3.x segment or you will get very confusing errors later.assert si.getCodec() instanceof Lucene3xCodec : "broken test, trying to mix preflex with other codecs";+ CodecUtil.writeHeader(output, Lucene3xSegmentInfoFormat.UPGRADED_SI_CODEC_NAME,+ Lucene3xSegmentInfoFormat.UPGRADED_SI_VERSION_CURRENT);// Write the Lucene version that created this segment, since 3.1output.writeString(si.getVersion());output.writeInt(si.getDocCount());