LUCENE-7898: Remove hasSegID from SegmentInfos.

This commit is contained in:
Adrien Grand 2017-07-05 09:22:33 +02:00
parent 36bca198f4
commit 708462eded
1 changed files with 16 additions and 38 deletions

View File

@ -65,7 +65,7 @@ import org.apache.lucene.util.Version;
* Files: * Files:
* <ul> * <ul>
* <li><tt>segments_N</tt>: Header, LuceneVersion, Version, NameCounter, SegCount, MinSegmentLuceneVersion, &lt;SegName, * <li><tt>segments_N</tt>: Header, LuceneVersion, Version, NameCounter, SegCount, MinSegmentLuceneVersion, &lt;SegName,
* HasSegID, SegID, SegCodec, DelGen, DeletionCount, FieldInfosGen, DocValuesGen, * SegID, SegCodec, DelGen, DeletionCount, FieldInfosGen, DocValuesGen,
* UpdatesFiles&gt;<sup>SegCount</sup>, CommitUserData, Footer * UpdatesFiles&gt;<sup>SegCount</sup>, CommitUserData, Footer
* </ul> * </ul>
* Data types: * Data types:
@ -78,7 +78,6 @@ import org.apache.lucene.util.Version;
* {@link DataOutput#writeInt Int32}</li> * {@link DataOutput#writeInt Int32}</li>
* <li>Generation, Version, DelGen, Checksum, FieldInfosGen, DocValuesGen --&gt; * <li>Generation, Version, DelGen, Checksum, FieldInfosGen, DocValuesGen --&gt;
* {@link DataOutput#writeLong Int64}</li> * {@link DataOutput#writeLong Int64}</li>
* <li>HasSegID --&gt; {@link DataOutput#writeByte Int8}</li>
* <li>SegID --&gt; {@link DataOutput#writeByte Int8<sup>ID_LENGTH</sup>}</li> * <li>SegID --&gt; {@link DataOutput#writeByte Int8<sup>ID_LENGTH</sup>}</li>
* <li>SegName, SegCodec --&gt; {@link DataOutput#writeString String}</li> * <li>SegName, SegCodec --&gt; {@link DataOutput#writeString String}</li>
* <li>CommitUserData --&gt; {@link DataOutput#writeMapOfStrings * <li>CommitUserData --&gt; {@link DataOutput#writeMapOfStrings
@ -100,9 +99,6 @@ import org.apache.lucene.util.Version;
* <li>DeletionCount records the number of deleted documents in this segment.</li> * <li>DeletionCount records the number of deleted documents in this segment.</li>
* <li>SegCodec is the {@link Codec#getName() name} of the Codec that encoded * <li>SegCodec is the {@link Codec#getName() name} of the Codec that encoded
* this segment.</li> * this segment.</li>
* <li>HasSegID is nonzero if the segment has an identifier. Otherwise, when it is 0
* the identifier is {@code null} and no SegID is written. Null only happens for Lucene
* 4.x segments referenced in commits.</li>
* <li>SegID is the identifier of the Codec that encoded this segment. </li> * <li>SegID is the identifier of the Codec that encoded this segment. </li>
* <li>CommitUserData stores an optional user-supplied opaque * <li>CommitUserData stores an optional user-supplied opaque
* Map&lt;String,String&gt; that was passed to * Map&lt;String,String&gt; that was passed to
@ -345,17 +341,17 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo
long totalDocs = 0; long totalDocs = 0;
for (int seg = 0; seg < numSegments; seg++) { for (int seg = 0; seg < numSegments; seg++) {
String segName = input.readString(); String segName = input.readString();
final byte segmentID[]; if (format < VERSION_70) {
byte hasID = input.readByte(); byte hasID = input.readByte();
if (hasID == 1) { if (hasID == 0) {
segmentID = new byte[StringHelper.ID_LENGTH]; throw new IndexFormatTooOldException(input, "Segment is from Lucene 4.x");
input.readBytes(segmentID, 0, segmentID.length); } else if (hasID != 1) {
} else if (hasID == 0) { throw new CorruptIndexException("invalid hasID byte, got: " + hasID, input);
throw new IndexFormatTooOldException(input, "Segment is from Lucene 4.x"); }
} else {
throw new CorruptIndexException("invalid hasID byte, got: " + hasID, input);
} }
Codec codec = readCodec(input, format < VERSION_53); byte[] segmentID = new byte[StringHelper.ID_LENGTH];
input.readBytes(segmentID, 0, segmentID.length);
Codec codec = readCodec(input);
SegmentInfo info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ); SegmentInfo info = codec.segmentInfoFormat().read(directory, segName, segmentID, IOContext.READ);
info.setCodec(codec); info.setCodec(codec);
totalDocs += info.maxDoc(); totalDocs += info.maxDoc();
@ -409,24 +405,12 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo
return infos; return infos;
} }
private static final List<String> unsupportedCodecs = Arrays.asList( private static Codec readCodec(DataInput input) throws IOException {
"Lucene3x", "Lucene40", "Lucene41", "Lucene42", "Lucene45", "Lucene46", "Lucene49", "Lucene410"
);
private static Codec readCodec(DataInput input, boolean unsupportedAllowed) throws IOException {
final String name = input.readString(); final String name = input.readString();
try { try {
return Codec.forName(name); return Codec.forName(name);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// give better error messages if we can, first check if this is a legacy codec // maybe it's an old default codec that moved
if (unsupportedCodecs.contains(name)) {
// We should only get here on pre-5.3 indices, but we can't test this until 7.0 when 5.x indices become too old:
assert unsupportedAllowed;
IOException newExc = new IndexFormatTooOldException(input, "Codec '" + name + "' is too old");
newExc.initCause(e);
throw newExc;
}
// or maybe it's an old default codec that moved
if (name.startsWith("Lucene")) { if (name.startsWith("Lucene")) {
throw new IllegalArgumentException("Could not load codec '" + name + "'. Did you forget to add lucene-backward-codecs.jar?", e); throw new IllegalArgumentException("Could not load codec '" + name + "'. Did you forget to add lucene-backward-codecs.jar?", e);
} }
@ -523,16 +507,10 @@ public final class SegmentInfos implements Cloneable, Iterable<SegmentCommitInfo
} }
out.writeString(si.name); out.writeString(si.name);
byte segmentID[] = si.getId(); byte segmentID[] = si.getId();
// TODO: remove this in lucene 6, we don't need to include 4.x segments in commits anymore if (segmentID.length != StringHelper.ID_LENGTH) {
if (segmentID == null) { throw new IllegalStateException("cannot write segment: invalid id segment=" + si.name + "id=" + StringHelper.idToString(segmentID));
out.writeByte((byte)0);
} else {
if (segmentID.length != StringHelper.ID_LENGTH) {
throw new IllegalStateException("cannot write segment: invalid id segment=" + si.name + "id=" + StringHelper.idToString(segmentID));
}
out.writeByte((byte)1);
out.writeBytes(segmentID, segmentID.length);
} }
out.writeBytes(segmentID, segmentID.length);
out.writeString(si.getCodec().getName()); out.writeString(si.getCodec().getName());
out.writeLong(siPerCommit.getDelGen()); out.writeLong(siPerCommit.getDelGen());
int delCount = siPerCommit.getDelCount(); int delCount = siPerCommit.getDelCount();