Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SegmentTermEnum.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using IndexInput = Lucene.Net.Store.IndexInput;
19 
20 namespace Lucene.Net.Index
21 {
22  internal sealed class SegmentTermEnum : TermEnum, System.ICloneable
23  {
24  private IndexInput input;
25  internal FieldInfos fieldInfos;
26  internal long size;
27  internal long position = - 1;
28 
29  private TermBuffer termBuffer = new TermBuffer();
30  private TermBuffer prevBuffer = new TermBuffer();
31  private TermBuffer scanBuffer = new TermBuffer(); // used for scanning
32 
33  private TermInfo termInfo = new TermInfo();
34 
35  private readonly int format;
36  private readonly bool isIndex = false;
37  internal long indexPointer = 0;
38  internal int indexInterval;
39  internal int skipInterval;
40  internal int maxSkipLevels;
41  private readonly int formatM1SkipInterval;
42 
43  internal SegmentTermEnum(IndexInput i, FieldInfos fis, bool isi)
44  {
45  input = i;
46  fieldInfos = fis;
47  isIndex = isi;
48  maxSkipLevels = 1; // use single-level skip lists for formats > -3
49 
50  int firstInt = input.ReadInt();
51  if (firstInt >= 0)
52  {
53  // original-format file, without explicit format version number
54  format = 0;
55  size = firstInt;
56 
57  // back-compatible settings
58  indexInterval = 128;
59  skipInterval = System.Int32.MaxValue; // switch off skipTo optimization
60  }
61  else
62  {
63  // we have a format version number
64  format = firstInt;
65 
66  // check that it is a format we can understand
67  if (format < TermInfosWriter.FORMAT_CURRENT)
68  throw new CorruptIndexException("Unknown format version:" + format + " expected " + TermInfosWriter.FORMAT_CURRENT + " or higher");
69 
70  size = input.ReadLong(); // read the size
71 
72  if (format == - 1)
73  {
74  if (!isIndex)
75  {
76  indexInterval = input.ReadInt();
77  formatM1SkipInterval = input.ReadInt();
78  }
79  // switch off skipTo optimization for file format prior to 1.4rc2 in order to avoid a bug in
80  // skipTo implementation of these versions
81  skipInterval = System.Int32.MaxValue;
82  }
83  else
84  {
85  indexInterval = input.ReadInt();
86  skipInterval = input.ReadInt();
87  if (format <= TermInfosWriter.FORMAT)
88  {
89  // this new format introduces multi-level skipping
90  maxSkipLevels = input.ReadInt();
91  }
92  }
93  System.Diagnostics.Debug.Assert(indexInterval > 0, "indexInterval=" + indexInterval + " is negative; must be > 0");
94  System.Diagnostics.Debug.Assert(skipInterval > 0, "skipInterval=" + skipInterval + " is negative; must be > 0");
95  }
96  if (format > TermInfosWriter.FORMAT_VERSION_UTF8_LENGTH_IN_BYTES)
97  {
98  termBuffer.SetPreUTF8Strings();
99  scanBuffer.SetPreUTF8Strings();
100  prevBuffer.SetPreUTF8Strings();
101  }
102  }
103 
104  public System.Object Clone()
105  {
106  SegmentTermEnum clone = null;
107  try
108  {
109  clone = (SegmentTermEnum) base.MemberwiseClone();
110  }
111  catch (System.Exception)
112  {
113  }
114 
115  clone.input = (IndexInput) input.Clone();
116  clone.termInfo = new TermInfo(termInfo);
117 
118  clone.termBuffer = (TermBuffer) termBuffer.Clone();
119  clone.prevBuffer = (TermBuffer) prevBuffer.Clone();
120  clone.scanBuffer = new TermBuffer();
121 
122  return clone;
123  }
124 
125  internal void Seek(long pointer, long p, Term t, TermInfo ti)
126  {
127  input.Seek(pointer);
128  position = p;
129  termBuffer.Set(t);
130  prevBuffer.Reset();
131  termInfo.Set(ti);
132  }
133 
134  /// <summary>Increments the enumeration to the next element. True if one exists.</summary>
135  public override bool Next()
136  {
137  if (position++ >= size - 1)
138  {
139  prevBuffer.Set(termBuffer);
140  termBuffer.Reset();
141  return false;
142  }
143 
144  prevBuffer.Set(termBuffer);
145  termBuffer.Read(input, fieldInfos);
146 
147  termInfo.docFreq = input.ReadVInt(); // read doc freq
148  termInfo.freqPointer += input.ReadVLong(); // read freq pointer
149  termInfo.proxPointer += input.ReadVLong(); // read prox pointer
150 
151  if (format == - 1)
152  {
153  // just read skipOffset in order to increment file pointer;
154  // value is never used since skipTo is switched off
155  if (!isIndex)
156  {
157  if (termInfo.docFreq > formatM1SkipInterval)
158  {
159  termInfo.skipOffset = input.ReadVInt();
160  }
161  }
162  }
163  else
164  {
165  if (termInfo.docFreq >= skipInterval)
166  termInfo.skipOffset = input.ReadVInt();
167  }
168 
169  if (isIndex)
170  indexPointer += input.ReadVLong(); // read index pointer
171 
172  return true;
173  }
174 
175  /// <summary>Optimized scan, without allocating new terms.
176  /// Return number of invocations to next().
177  /// </summary>
178  internal int ScanTo(Term term)
179  {
180  scanBuffer.Set(term);
181  int count = 0;
182  while (scanBuffer.CompareTo(termBuffer) > 0 && Next())
183  {
184  count++;
185  }
186  return count;
187  }
188 
189  /// <summary>Returns the current Term in the enumeration.
190  /// Initially invalid, valid after next() called for the first time.
191  /// </summary>
192  public override Term Term
193  {
194  get { return termBuffer.ToTerm(); }
195  }
196 
197  /// <summary>Returns the previous Term enumerated. Initially null.</summary>
198  public /*internal*/ Term Prev()
199  {
200  return prevBuffer.ToTerm();
201  }
202 
203  /// <summary>Returns the current TermInfo in the enumeration.
204  /// Initially invalid, valid after next() called for the first time.
205  /// </summary>
206  internal TermInfo TermInfo()
207  {
208  return new TermInfo(termInfo);
209  }
210 
211  /// <summary>Sets the argument to the current TermInfo in the enumeration.
212  /// Initially invalid, valid after next() called for the first time.
213  /// </summary>
214  internal void TermInfo(TermInfo ti)
215  {
216  ti.Set(termInfo);
217  }
218 
219  /// <summary>Returns the docFreq from the current TermInfo in the enumeration.
220  /// Initially invalid, valid after next() called for the first time.
221  /// </summary>
222  public override int DocFreq()
223  {
224  return termInfo.docFreq;
225  }
226 
227  /* Returns the freqPointer from the current TermInfo in the enumeration.
228  Initially invalid, valid after next() called for the first time.*/
229  internal long FreqPointer()
230  {
231  return termInfo.freqPointer;
232  }
233 
234  /* Returns the proxPointer from the current TermInfo in the enumeration.
235  Initially invalid, valid after next() called for the first time.*/
236  internal long ProxPointer()
237  {
238  return termInfo.proxPointer;
239  }
240 
241  /// <summary>Closes the enumeration to further activity, freeing resources. </summary>
242  protected override void Dispose(bool disposing)
243  {
244  input.Dispose();
245  }
246  }
247 }