2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Index / SegmentTermEnum.cs
blobdc331a49b8e1d24d19f526ea053bb27204b8eb62
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.
16 using System;
17 using IndexInput = Lucene.Net.Store.IndexInput;
18 namespace Lucene.Net.Index
21 sealed public class SegmentTermEnum : TermEnum, System.ICloneable
23 private IndexInput input;
24 internal FieldInfos fieldInfos;
25 internal long size;
26 internal long position = - 1;
28 private TermBuffer termBuffer = new TermBuffer();
29 private TermBuffer prevBuffer = new TermBuffer();
30 private TermBuffer scratch; // used for scanning
32 private TermInfo termInfo = new TermInfo();
34 private int format;
35 private bool isIndex = false;
36 internal long indexPointer = 0;
37 internal int indexInterval;
38 internal int skipInterval;
39 private int formatM1SkipInterval;
41 internal SegmentTermEnum(IndexInput i, FieldInfos fis, bool isi)
43 input = i;
44 fieldInfos = fis;
45 isIndex = isi;
47 int firstInt = input.ReadInt();
48 if (firstInt >= 0)
50 // original-format file, without explicit format version number
51 format = 0;
52 size = firstInt;
54 // back-compatible settings
55 indexInterval = 128;
56 skipInterval = System.Int32.MaxValue; // switch off skipTo optimization
58 else
60 // we have a format version number
61 format = firstInt;
63 // check that it is a format we can understand
64 if (format < TermInfosWriter.FORMAT)
65 throw new System.IO.IOException("Unknown format version:" + format);
67 size = input.ReadLong(); // read the size
69 if (format == - 1)
71 if (!isIndex)
73 indexInterval = input.ReadInt();
74 formatM1SkipInterval = input.ReadInt();
76 // switch off skipTo optimization for file format prior to 1.4rc2 in order to avoid a bug in
77 // skipTo implementation of these versions
78 skipInterval = System.Int32.MaxValue;
80 else
82 indexInterval = input.ReadInt();
83 skipInterval = input.ReadInt();
88 public System.Object Clone()
90 SegmentTermEnum clone = null;
91 try
93 clone = (SegmentTermEnum) base.MemberwiseClone();
95 catch (System.Exception)
99 clone.input = (IndexInput) input.Clone();
100 clone.termInfo = new TermInfo(termInfo);
102 clone.termBuffer = (TermBuffer) termBuffer.Clone();
103 clone.prevBuffer = (TermBuffer) prevBuffer.Clone();
104 clone.scratch = null;
106 return clone;
109 internal void Seek(long pointer, int p, Term t, TermInfo ti)
111 input.Seek(pointer);
112 position = p;
113 termBuffer.Set(t);
114 prevBuffer.Reset();
115 termInfo.Set(ti);
118 /// <summary>Increments the enumeration to the next element. True if one exists.</summary>
119 public override bool Next()
121 if (position++ >= size - 1)
123 termBuffer.Reset();
124 return false;
127 prevBuffer.Set(termBuffer);
128 termBuffer.Read(input, fieldInfos);
130 termInfo.docFreq = input.ReadVInt(); // read doc freq
131 termInfo.freqPointer += input.ReadVLong(); // read freq pointer
132 termInfo.proxPointer += input.ReadVLong(); // read prox pointer
134 if (format == - 1)
136 // just read skipOffset in order to increment file pointer;
137 // value is never used since skipTo is switched off
138 if (!isIndex)
140 if (termInfo.docFreq > formatM1SkipInterval)
142 termInfo.skipOffset = input.ReadVInt();
146 else
148 if (termInfo.docFreq >= skipInterval)
149 termInfo.skipOffset = input.ReadVInt();
152 if (isIndex)
153 indexPointer += input.ReadVLong(); // read index pointer
155 return true;
158 /// <summary>Optimized scan, without allocating new terms. </summary>
159 internal void ScanTo(Term term)
161 if (scratch == null)
162 scratch = new TermBuffer();
163 scratch.Set(term);
164 while (scratch.CompareTo(termBuffer) > 0 && Next())
169 /// <summary>Returns the current Term in the enumeration.
170 /// Initially invalid, valid after next() called for the first time.
171 /// </summary>
172 public override Term Term()
174 return termBuffer.ToTerm();
177 /// <summary>Returns the previous Term enumerated. Initially null.</summary>
178 internal Term Prev()
180 return prevBuffer.ToTerm();
183 /// <summary>Returns the current TermInfo in the enumeration.
184 /// Initially invalid, valid after next() called for the first time.
185 /// </summary>
186 internal TermInfo TermInfo()
188 return new TermInfo(termInfo);
191 /// <summary>Sets the argument to the current TermInfo in the enumeration.
192 /// Initially invalid, valid after next() called for the first time.
193 /// </summary>
194 internal void TermInfo(TermInfo ti)
196 ti.Set(termInfo);
199 /// <summary>Returns the docFreq from the current TermInfo in the enumeration.
200 /// Initially invalid, valid after next() called for the first time.
201 /// </summary>
202 public override int DocFreq()
204 return termInfo.docFreq;
207 /* Returns the freqPointer from the current TermInfo in the enumeration.
208 Initially invalid, valid after next() called for the first time.*/
209 internal long FreqPointer()
211 return termInfo.freqPointer;
214 /* Returns the proxPointer from the current TermInfo in the enumeration.
215 Initially invalid, valid after next() called for the first time.*/
216 internal long ProxPointer()
218 return termInfo.proxPointer;
221 /// <summary>Closes the enumeration to further activity, freeing resources. </summary>
222 public override void Close()
224 input.Close();