|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.lucene.backward_codecs.lucene90; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import org.apache.lucene.codecs.CodecUtil; |
| 24 | +import org.apache.lucene.codecs.SegmentInfoFormat; |
| 25 | +import org.apache.lucene.index.CorruptIndexException; |
| 26 | +import org.apache.lucene.index.IndexFileNames; |
| 27 | +import org.apache.lucene.index.IndexWriter; |
| 28 | +import org.apache.lucene.index.SegmentInfo; |
| 29 | +import org.apache.lucene.index.SegmentInfos; |
| 30 | +import org.apache.lucene.index.SortFieldProvider; |
| 31 | +import org.apache.lucene.search.Sort; |
| 32 | +import org.apache.lucene.search.SortField; |
| 33 | +import org.apache.lucene.store.ChecksumIndexInput; |
| 34 | +import org.apache.lucene.store.DataInput; |
| 35 | +import org.apache.lucene.store.DataOutput; |
| 36 | +import org.apache.lucene.store.Directory; |
| 37 | +import org.apache.lucene.store.IOContext; |
| 38 | +import org.apache.lucene.util.Version; |
| 39 | + |
| 40 | +/** |
| 41 | + * Lucene 9.0 Segment info format. |
| 42 | + * |
| 43 | + * <p>Files: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li><code>.si</code>: Header, SegVersion, SegSize, IsCompoundFile, Diagnostics, Files, |
| 47 | + * Attributes, IndexSort, Footer |
| 48 | + * </ul> |
| 49 | + * |
| 50 | + * Data types: |
| 51 | + * |
| 52 | + * <ul> |
| 53 | + * <li>Header --> {@link CodecUtil#writeIndexHeader IndexHeader} |
| 54 | + * <li>SegSize --> {@link DataOutput#writeInt Int32} |
| 55 | + * <li>SegVersion --> {@link DataOutput#writeString String} |
| 56 | + * <li>SegMinVersion --> {@link DataOutput#writeString String} |
| 57 | + * <li>Files --> {@link DataOutput#writeSetOfStrings Set<String>} |
| 58 | + * <li>Diagnostics,Attributes --> {@link DataOutput#writeMapOfStrings Map<String,String>} |
| 59 | + * <li>IsCompoundFile --> {@link DataOutput#writeByte Int8} |
| 60 | + * <li>IndexSort --> {@link DataOutput#writeVInt Int32} count, followed by {@code count} |
| 61 | + * SortField |
| 62 | + * <li>SortField --> {@link DataOutput#writeString String} sort class, followed by a per-sort |
| 63 | + * bytestream (see {@link SortFieldProvider#readSortField(DataInput)}) |
| 64 | + * <li>Footer --> {@link CodecUtil#writeFooter CodecFooter} |
| 65 | + * </ul> |
| 66 | + * |
| 67 | + * Field Descriptions: |
| 68 | + * |
| 69 | + * <ul> |
| 70 | + * <li>SegVersion is the code version that created the segment. |
| 71 | + * <li>SegMinVersion is the minimum code version that contributed documents to the segment. |
| 72 | + * <li>SegSize is the number of documents contained in the segment index. |
| 73 | + * <li>IsCompoundFile records whether the segment is written as a compound file or not. If this is |
| 74 | + * -1, the segment is not a compound file. If it is 1, the segment is a compound file. |
| 75 | + * <li>The Diagnostics Map is privately written by {@link IndexWriter}, as a debugging aid, for |
| 76 | + * each segment it creates. It includes metadata like the current Lucene version, OS, Java |
| 77 | + * version, why the segment was created (merge, flush, addIndexes), etc. |
| 78 | + * <li>Files is a list of files referred to by this segment. |
| 79 | + * </ul> |
| 80 | + * |
| 81 | + * @see SegmentInfos |
| 82 | + * @lucene.experimental |
| 83 | + */ |
| 84 | +public class Lucene90SegmentInfoFormat extends SegmentInfoFormat { |
| 85 | + |
| 86 | + /** File extension used to store {@link SegmentInfo}. */ |
| 87 | + public static final String SI_EXTENSION = "si"; |
| 88 | + |
| 89 | + static final String CODEC_NAME = "Lucene90SegmentInfo"; |
| 90 | + static final int VERSION_START = 0; |
| 91 | + static final int VERSION_CURRENT = VERSION_START; |
| 92 | + |
| 93 | + /** Sole constructor. */ |
| 94 | + public Lucene90SegmentInfoFormat() {} |
| 95 | + |
| 96 | + @Override |
| 97 | + public SegmentInfo read(Directory dir, String segment, byte[] segmentID, IOContext context) |
| 98 | + throws IOException { |
| 99 | + final String fileName = IndexFileNames.segmentFileName(segment, "", SI_EXTENSION); |
| 100 | + try (ChecksumIndexInput input = dir.openChecksumInput(fileName)) { |
| 101 | + Throwable priorE = null; |
| 102 | + SegmentInfo si = null; |
| 103 | + try { |
| 104 | + CodecUtil.checkIndexHeader( |
| 105 | + input, CODEC_NAME, VERSION_START, VERSION_CURRENT, segmentID, ""); |
| 106 | + si = parseSegmentInfo(dir, input, segment, segmentID); |
| 107 | + } catch (Throwable exception) { |
| 108 | + priorE = exception; |
| 109 | + } finally { |
| 110 | + CodecUtil.checkFooter(input, priorE); |
| 111 | + } |
| 112 | + return si; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private SegmentInfo parseSegmentInfo( |
| 117 | + Directory dir, DataInput input, String segment, byte[] segmentID) throws IOException { |
| 118 | + final Version version = Version.fromBits(input.readInt(), input.readInt(), input.readInt()); |
| 119 | + byte hasMinVersion = input.readByte(); |
| 120 | + final Version minVersion; |
| 121 | + switch (hasMinVersion) { |
| 122 | + case 0: |
| 123 | + minVersion = null; |
| 124 | + break; |
| 125 | + case 1: |
| 126 | + minVersion = Version.fromBits(input.readInt(), input.readInt(), input.readInt()); |
| 127 | + break; |
| 128 | + default: |
| 129 | + throw new CorruptIndexException("Illegal boolean value " + hasMinVersion, input); |
| 130 | + } |
| 131 | + |
| 132 | + final int docCount = input.readInt(); |
| 133 | + if (docCount < 0) { |
| 134 | + throw new CorruptIndexException("invalid docCount: " + docCount, input); |
| 135 | + } |
| 136 | + final boolean isCompoundFile = input.readByte() == SegmentInfo.YES; |
| 137 | + |
| 138 | + final Map<String, String> diagnostics = input.readMapOfStrings(); |
| 139 | + final Set<String> files = input.readSetOfStrings(); |
| 140 | + final Map<String, String> attributes = input.readMapOfStrings(); |
| 141 | + |
| 142 | + int numSortFields = input.readVInt(); |
| 143 | + Sort indexSort; |
| 144 | + if (numSortFields > 0) { |
| 145 | + SortField[] sortFields = new SortField[numSortFields]; |
| 146 | + for (int i = 0; i < numSortFields; i++) { |
| 147 | + String name = input.readString(); |
| 148 | + sortFields[i] = SortFieldProvider.forName(name).readSortField(input); |
| 149 | + } |
| 150 | + indexSort = new Sort(sortFields); |
| 151 | + } else if (numSortFields < 0) { |
| 152 | + throw new CorruptIndexException("invalid index sort field count: " + numSortFields, input); |
| 153 | + } else { |
| 154 | + indexSort = null; |
| 155 | + } |
| 156 | + |
| 157 | + SegmentInfo si = |
| 158 | + new SegmentInfo( |
| 159 | + dir, |
| 160 | + version, |
| 161 | + minVersion, |
| 162 | + segment, |
| 163 | + docCount, |
| 164 | + isCompoundFile, |
| 165 | + false, |
| 166 | + null, |
| 167 | + diagnostics, |
| 168 | + segmentID, |
| 169 | + attributes, |
| 170 | + indexSort); |
| 171 | + si.setFiles(files); |
| 172 | + return si; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void write(Directory dir, SegmentInfo si, IOContext ioContext) throws IOException { |
| 177 | + throw new UnsupportedOperationException("Old formats can't be used for writing"); |
| 178 | + } |
| 179 | +} |
0 commit comments