KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > SegmentTermEnum


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import org.apache.lucene.store.IndexInput;
21
22 final class SegmentTermEnum extends TermEnum implements Cloneable JavaDoc {
23   private IndexInput input;
24   FieldInfos fieldInfos;
25   long size;
26   long position = -1;
27
28   private TermBuffer termBuffer = new TermBuffer();
29   private TermBuffer prevBuffer = new TermBuffer();
30   private TermBuffer scratch; // used for scanning
31

32   private TermInfo termInfo = new TermInfo();
33
34   private int format;
35   private boolean isIndex = false;
36   long indexPointer = 0;
37   int indexInterval;
38   int skipInterval;
39   private int formatM1SkipInterval;
40
41   SegmentTermEnum(IndexInput i, FieldInfos fis, boolean isi)
42           throws IOException JavaDoc {
43     input = i;
44     fieldInfos = fis;
45     isIndex = isi;
46
47     int firstInt = input.readInt();
48     if (firstInt >= 0) {
49       // original-format file, without explicit format version number
50
format = 0;
51       size = firstInt;
52
53       // back-compatible settings
54
indexInterval = 128;
55       skipInterval = Integer.MAX_VALUE; // switch off skipTo optimization
56

57     } else {
58       // we have a format version number
59
format = firstInt;
60
61       // check that it is a format we can understand
62
if (format < TermInfosWriter.FORMAT)
63         throw new IOException JavaDoc("Unknown format version:" + format);
64
65       size = input.readLong(); // read the size
66

67       if(format == -1){
68         if (!isIndex) {
69           indexInterval = input.readInt();
70           formatM1SkipInterval = input.readInt();
71         }
72         // switch off skipTo optimization for file format prior to 1.4rc2 in order to avoid a bug in
73
// skipTo implementation of these versions
74
skipInterval = Integer.MAX_VALUE;
75       }
76       else{
77         indexInterval = input.readInt();
78         skipInterval = input.readInt();
79       }
80     }
81
82   }
83
84   protected Object JavaDoc clone() {
85     SegmentTermEnum clone = null;
86     try {
87       clone = (SegmentTermEnum) super.clone();
88     } catch (CloneNotSupportedException JavaDoc e) {}
89
90     clone.input = (IndexInput) input.clone();
91     clone.termInfo = new TermInfo(termInfo);
92
93     clone.termBuffer = (TermBuffer)termBuffer.clone();
94     clone.prevBuffer = (TermBuffer)prevBuffer.clone();
95     clone.scratch = null;
96
97     return clone;
98   }
99
100   final void seek(long pointer, int p, Term t, TermInfo ti)
101           throws IOException JavaDoc {
102     input.seek(pointer);
103     position = p;
104     termBuffer.set(t);
105     prevBuffer.reset();
106     termInfo.set(ti);
107   }
108
109   /** Increments the enumeration to the next element. True if one exists.*/
110   public final boolean next() throws IOException JavaDoc {
111     if (position++ >= size - 1) {
112       termBuffer.reset();
113       return false;
114     }
115
116     prevBuffer.set(termBuffer);
117     termBuffer.read(input, fieldInfos);
118
119     termInfo.docFreq = input.readVInt(); // read doc freq
120
termInfo.freqPointer += input.readVLong(); // read freq pointer
121
termInfo.proxPointer += input.readVLong(); // read prox pointer
122

123     if(format == -1){
124     // just read skipOffset in order to increment file pointer;
125
// value is never used since skipTo is switched off
126
if (!isIndex) {
127         if (termInfo.docFreq > formatM1SkipInterval) {
128           termInfo.skipOffset = input.readVInt();
129         }
130       }
131     }
132     else{
133       if (termInfo.docFreq >= skipInterval)
134         termInfo.skipOffset = input.readVInt();
135     }
136     
137     if (isIndex)
138       indexPointer += input.readVLong(); // read index pointer
139

140     return true;
141   }
142
143   /** Optimized scan, without allocating new terms. */
144   final void scanTo(Term term) throws IOException JavaDoc {
145     if (scratch == null)
146       scratch = new TermBuffer();
147     scratch.set(term);
148     while (scratch.compareTo(termBuffer) > 0 && next()) {}
149   }
150
151   /** Returns the current Term in the enumeration.
152    Initially invalid, valid after next() called for the first time.*/

153   public final Term term() {
154     return termBuffer.toTerm();
155   }
156
157   /** Returns the previous Term enumerated. Initially null.*/
158   final Term prev() {
159     return prevBuffer.toTerm();
160   }
161
162   /** Returns the current TermInfo in the enumeration.
163    Initially invalid, valid after next() called for the first time.*/

164   final TermInfo termInfo() {
165     return new TermInfo(termInfo);
166   }
167
168   /** Sets the argument to the current TermInfo in the enumeration.
169    Initially invalid, valid after next() called for the first time.*/

170   final void termInfo(TermInfo ti) {
171     ti.set(termInfo);
172   }
173
174   /** Returns the docFreq from the current TermInfo in the enumeration.
175    Initially invalid, valid after next() called for the first time.*/

176   public final int docFreq() {
177     return termInfo.docFreq;
178   }
179
180   /* Returns the freqPointer from the current TermInfo in the enumeration.
181     Initially invalid, valid after next() called for the first time.*/

182   final long freqPointer() {
183     return termInfo.freqPointer;
184   }
185
186   /* Returns the proxPointer from the current TermInfo in the enumeration.
187     Initially invalid, valid after next() called for the first time.*/

188   final long proxPointer() {
189     return termInfo.proxPointer;
190   }
191
192   /** Closes the enumeration to further activity, freeing resources. */
193   public final void close() throws IOException JavaDoc {
194     input.close();
195   }
196 }
197
Popular Tags