cvsimport
[beagle.git] / beagled / Lucene.Net / Index / SegmentTermEnum.cs
blobbdde2fb4f9099933858e9c68338d718f21fb67c9
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using IndexInput = Lucene.Net.Store.IndexInput;
20 namespace Lucene.Net.Index
23 public sealed class SegmentTermEnum : TermEnum, System.ICloneable
25 private IndexInput input;
26 internal FieldInfos fieldInfos;
27 internal long size;
28 internal long position = - 1;
30 private TermBuffer termBuffer = new TermBuffer();
31 private TermBuffer prevBuffer = new TermBuffer();
32 private TermBuffer scratch; // used for scanning
34 private TermInfo termInfo = new TermInfo();
36 private int format;
37 private bool isIndex = false;
38 internal long indexPointer = 0;
39 internal int indexInterval;
40 internal int skipInterval;
41 private int formatM1SkipInterval;
43 internal SegmentTermEnum(IndexInput i, FieldInfos fis, bool isi)
45 input = i;
46 fieldInfos = fis;
47 isIndex = isi;
49 int firstInt = input.ReadInt();
50 if (firstInt >= 0)
52 // original-format file, without explicit format version number
53 format = 0;
54 size = firstInt;
56 // back-compatible settings
57 indexInterval = 128;
58 skipInterval = System.Int32.MaxValue; // switch off skipTo optimization
60 else
62 // we have a format version number
63 format = firstInt;
65 // check that it is a format we can understand
66 if (format < TermInfosWriter.FORMAT)
67 throw new System.IO.IOException("Unknown format version:" + format);
69 size = input.ReadLong(); // read the size
71 if (format == - 1)
73 if (!isIndex)
75 indexInterval = input.ReadInt();
76 formatM1SkipInterval = input.ReadInt();
78 // switch off skipTo optimization for file format prior to 1.4rc2 in order to avoid a bug in
79 // skipTo implementation of these versions
80 skipInterval = System.Int32.MaxValue;
82 else
84 indexInterval = input.ReadInt();
85 skipInterval = input.ReadInt();
90 public System.Object Clone()
92 SegmentTermEnum clone = null;
93 try
95 clone = (SegmentTermEnum) base.MemberwiseClone();
97 catch (System.Exception)
101 clone.input = (IndexInput) input.Clone();
102 clone.termInfo = new TermInfo(termInfo);
104 clone.termBuffer = (TermBuffer) termBuffer.Clone();
105 clone.prevBuffer = (TermBuffer) prevBuffer.Clone();
106 clone.scratch = null;
108 return clone;
111 internal void Seek(long pointer, int p, Term t, TermInfo ti)
113 input.Seek(pointer);
114 position = p;
115 termBuffer.Set(t);
116 prevBuffer.Reset();
117 termInfo.Set(ti);
120 /// <summary>Increments the enumeration to the next element. True if one exists.</summary>
121 public override bool Next()
123 if (position++ >= size - 1)
125 termBuffer.Reset();
126 return false;
129 prevBuffer.Set(termBuffer);
130 termBuffer.Read(input, fieldInfos);
132 termInfo.docFreq = input.ReadVInt(); // read doc freq
133 termInfo.freqPointer += input.ReadVLong(); // read freq pointer
134 termInfo.proxPointer += input.ReadVLong(); // read prox pointer
136 if (format == - 1)
138 // just read skipOffset in order to increment file pointer;
139 // value is never used since skipTo is switched off
140 if (!isIndex)
142 if (termInfo.docFreq > formatM1SkipInterval)
144 termInfo.skipOffset = input.ReadVInt();
148 else
150 if (termInfo.docFreq >= skipInterval)
151 termInfo.skipOffset = input.ReadVInt();
154 if (isIndex)
155 indexPointer += input.ReadVLong(); // read index pointer
157 return true;
160 /// <summary>Optimized scan, without allocating new terms. </summary>
161 internal void ScanTo(Term term)
163 if (scratch == null)
164 scratch = new TermBuffer();
165 scratch.Set(term);
166 while (scratch.CompareTo(termBuffer) > 0 && Next())
171 /// <summary>Returns the current Term in the enumeration.
172 /// Initially invalid, valid after next() called for the first time.
173 /// </summary>
174 public override Term Term()
176 return termBuffer.ToTerm();
179 /// <summary>Returns the previous Term enumerated. Initially null.</summary>
180 internal Term Prev()
182 return prevBuffer.ToTerm();
185 /// <summary>Returns the current TermInfo in the enumeration.
186 /// Initially invalid, valid after next() called for the first time.
187 /// </summary>
188 internal TermInfo TermInfo()
190 return new TermInfo(termInfo);
193 /// <summary>Sets the argument to the current TermInfo in the enumeration.
194 /// Initially invalid, valid after next() called for the first time.
195 /// </summary>
196 internal void TermInfo(TermInfo ti)
198 ti.Set(termInfo);
201 /// <summary>Returns the docFreq from the current TermInfo in the enumeration.
202 /// Initially invalid, valid after next() called for the first time.
203 /// </summary>
204 public override int DocFreq()
206 return termInfo.docFreq;
209 /* Returns the freqPointer from the current TermInfo in the enumeration.
210 Initially invalid, valid after next() called for the first time.*/
211 internal long FreqPointer()
213 return termInfo.freqPointer;
216 /* Returns the proxPointer from the current TermInfo in the enumeration.
217 Initially invalid, valid after next() called for the first time.*/
218 internal long ProxPointer()
220 return termInfo.proxPointer;
223 /// <summary>Closes the enumeration to further activity, freeing resources. </summary>
224 public override void Close()
226 input.Close();