Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Index / SegmentTermEnum.cs
blob8cd139dc37383da4d4c15c58ad4205e417cb7187
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 InputStream = Lucene.Net.Store.InputStream;
18 namespace Lucene.Net.Index
21 sealed public class SegmentTermEnum:TermEnum, System.ICloneable
23 private InputStream 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(InputStream 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 = (InputStream) input.Clone();
100 clone.termInfo = new TermInfo(termInfo);
101 clone.termBuffer = (TermBuffer) termBuffer.Clone();
102 clone.prevBuffer = (TermBuffer) prevBuffer.Clone();
103 clone.scratch = null;
105 return clone;
108 internal void Seek(long pointer, int p, Term t, TermInfo ti)
110 input.Seek(pointer);
111 position = p;
112 termBuffer.Set(t);
113 prevBuffer.Reset();
114 termInfo.Set(ti);
117 /// <summary>Increments the enumeration to the next element. True if one exists.</summary>
118 public override bool Next()
120 if (position++ >= size - 1)
122 termBuffer.Reset();
123 return false;
126 prevBuffer.Set(termBuffer);
127 termBuffer.Read(input, fieldInfos);
129 termInfo.docFreq = input.ReadVInt(); // read doc freq
130 termInfo.freqPointer += input.ReadVLong(); // read freq pointer
131 termInfo.proxPointer += input.ReadVLong(); // read prox pointer
133 if (format == - 1)
135 // just read skipOffset in order to increment file pointer;
136 // value is never used since skipTo is switched off
137 if (!isIndex)
139 if (termInfo.docFreq > formatM1SkipInterval)
141 termInfo.skipOffset = input.ReadVInt();
145 else
147 if (termInfo.docFreq >= skipInterval)
148 termInfo.skipOffset = input.ReadVInt();
151 if (isIndex)
152 indexPointer += input.ReadVLong(); // read index pointer
154 return true;
157 /** Optimized scan, without allocating new terms. */
158 public void ScanTo(Term term)
160 if (scratch == null)
161 scratch = new TermBuffer();
162 scratch.Set(term);
163 while (scratch.CompareTo(termBuffer) > 0 && Next()) {}
166 /// <summary>Returns the current Term in the enumeration.
167 /// Initially invalid, valid after next() called for the first time.
168 /// </summary>
169 public override Term Term()
171 return termBuffer.ToTerm();
174 /// <summary>Returns the previous Term in the enumeration.
175 /// Initially null.
176 /// </summary>
177 public Term Prev() {
178 return prevBuffer.ToTerm();
181 /// <summary>Returns the current TermInfo in the enumeration.
182 /// Initially invalid, valid after next() called for the first time.
183 /// </summary>
184 public /*internal*/ TermInfo TermInfo()
186 return new TermInfo(termInfo);
189 /// <summary>Sets the argument to the current TermInfo in the enumeration.
190 /// Initially invalid, valid after next() called for the first time.
191 /// </summary>
192 internal void TermInfo(TermInfo ti)
194 ti.Set(termInfo);
197 /// <summary>Returns the docFreq from the current TermInfo in the enumeration.
198 /// Initially invalid, valid after next() called for the first time.
199 /// </summary>
200 public override int DocFreq()
202 return termInfo.docFreq;
205 /* Returns the freqPointer from the current TermInfo in the enumeration.
206 Initially invalid, valid after next() called for the first time.*/
207 internal long FreqPointer()
209 return termInfo.freqPointer;
212 /* Returns the proxPointer from the current TermInfo in the enumeration.
213 Initially invalid, valid after next() called for the first time.*/
214 internal long ProxPointer()
216 return termInfo.proxPointer;
219 /// <summary>Closes the enumeration to further activity, freeing resources. </summary>
220 public override void Close()
222 input.Close();