1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import org.apache.lucene.store.IndexInput;
23 final class SegmentTermEnum extends TermEnum implements Cloneable {
24 private IndexInput input;
25 FieldInfos fieldInfos;
28 private boolean first = true;
30 private TermBuffer termBuffer = new TermBuffer();
31 private TermBuffer prevBuffer = new TermBuffer();
32 private TermBuffer scanBuffer = new TermBuffer(); // used for scanning
34 private TermInfo termInfo = new TermInfo();
37 private boolean isIndex = false;
38 long indexPointer = 0;
42 private int formatM1SkipInterval;
44 SegmentTermEnum(IndexInput i, FieldInfos fis, boolean isi)
45 throws CorruptIndexException, IOException {
49 maxSkipLevels = 1; // use single-level skip lists for formats > -3
51 int firstInt = input.readInt();
53 // original-format file, without explicit format version number
57 // back-compatible settings
59 skipInterval = Integer.MAX_VALUE; // switch off skipTo optimization
61 // we have a format version number
64 // check that it is a format we can understand
65 if (format < TermInfosWriter.FORMAT_CURRENT)
66 throw new IndexFormatTooNewException(input, format, -1, TermInfosWriter.FORMAT_CURRENT);
68 size = input.readLong(); // read the size
72 indexInterval = input.readInt();
73 formatM1SkipInterval = input.readInt();
75 // switch off skipTo optimization for file format prior to 1.4rc2 in order to avoid a bug in
76 // skipTo implementation of these versions
77 skipInterval = Integer.MAX_VALUE;
79 indexInterval = input.readInt();
80 skipInterval = input.readInt();
81 if (format <= TermInfosWriter.FORMAT) {
82 // this new format introduces multi-level skipping
83 maxSkipLevels = input.readInt();
86 assert indexInterval > 0: "indexInterval=" + indexInterval + " is negative; must be > 0";
87 assert skipInterval > 0: "skipInterval=" + skipInterval + " is negative; must be > 0";
89 if (format > TermInfosWriter.FORMAT_VERSION_UTF8_LENGTH_IN_BYTES) {
90 termBuffer.setPreUTF8Strings();
91 scanBuffer.setPreUTF8Strings();
92 prevBuffer.setPreUTF8Strings();
97 protected Object clone() {
98 SegmentTermEnum clone = null;
100 clone = (SegmentTermEnum) super.clone();
101 } catch (CloneNotSupportedException e) {}
103 clone.input = (IndexInput) input.clone();
104 clone.termInfo = new TermInfo(termInfo);
106 clone.termBuffer = (TermBuffer)termBuffer.clone();
107 clone.prevBuffer = (TermBuffer)prevBuffer.clone();
108 clone.scanBuffer = new TermBuffer();
113 final void seek(long pointer, long p, Term t, TermInfo ti)
123 /** Increments the enumeration to the next element. True if one exists.*/
125 public final boolean next() throws IOException {
126 if (position++ >= size - 1) {
127 prevBuffer.set(termBuffer);
132 prevBuffer.set(termBuffer);
133 termBuffer.read(input, fieldInfos);
135 termInfo.docFreq = input.readVInt(); // read doc freq
136 termInfo.freqPointer += input.readVLong(); // read freq pointer
137 termInfo.proxPointer += input.readVLong(); // read prox pointer
140 // just read skipOffset in order to increment file pointer;
141 // value is never used since skipTo is switched off
143 if (termInfo.docFreq > formatM1SkipInterval) {
144 termInfo.skipOffset = input.readVInt();
149 if (termInfo.docFreq >= skipInterval)
150 termInfo.skipOffset = input.readVInt();
154 indexPointer += input.readVLong(); // read index pointer
159 /* Optimized scan, without allocating new terms.
160 * Return number of invocations to next().
162 * NOTE: LUCENE-3183: if you pass Term("", "") here then this
163 * will incorrectly return before positioning the enum,
164 * and position will be -1; caller must detect this. */
165 final int scanTo(Term term) throws IOException {
166 scanBuffer.set(term);
169 // Always force initial next() in case term is Term("", "")
174 while (scanBuffer.compareTo(termBuffer) > 0 && next()) {
180 /** Returns the current Term in the enumeration.
181 Initially invalid, valid after next() called for the first time.*/
183 public final Term term() {
184 return termBuffer.toTerm();
187 /** Returns the previous Term enumerated. Initially null.*/
189 return prevBuffer.toTerm();
192 /** Returns the current TermInfo in the enumeration.
193 Initially invalid, valid after next() called for the first time.*/
194 final TermInfo termInfo() {
195 return new TermInfo(termInfo);
198 /** Sets the argument to the current TermInfo in the enumeration.
199 Initially invalid, valid after next() called for the first time.*/
200 final void termInfo(TermInfo ti) {
204 /** Returns the docFreq from the current TermInfo in the enumeration.
205 Initially invalid, valid after next() called for the first time.*/
207 public final int docFreq() {
208 return termInfo.docFreq;
211 /* Returns the freqPointer from the current TermInfo in the enumeration.
212 Initially invalid, valid after next() called for the first time.*/
213 final long freqPointer() {
214 return termInfo.freqPointer;
217 /* Returns the proxPointer from the current TermInfo in the enumeration.
218 Initially invalid, valid after next() called for the first time.*/
219 final long proxPointer() {
220 return termInfo.proxPointer;
223 /** Closes the enumeration to further activity, freeing resources. */
225 public final void close() throws IOException {