2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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
;
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();
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
)
47 int firstInt
= input
.ReadInt();
50 // original-format file, without explicit format version number
54 // back-compatible settings
56 skipInterval
= System
.Int32
.MaxValue
; // switch off skipTo optimization
60 // we have a format version number
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
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
;
82 indexInterval
= input
.ReadInt();
83 skipInterval
= input
.ReadInt();
88 public System
.Object
Clone()
90 SegmentTermEnum clone
= null;
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;
108 internal void Seek(long pointer
, int p
, Term t
, TermInfo 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)
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
135 // just read skipOffset in order to increment file pointer;
136 // value is never used since skipTo is switched off
139 if (termInfo
.docFreq
> formatM1SkipInterval
)
141 termInfo
.skipOffset
= input
.ReadVInt();
147 if (termInfo
.docFreq
>= skipInterval
)
148 termInfo
.skipOffset
= input
.ReadVInt();
152 indexPointer
+= input
.ReadVLong(); // read index pointer
157 /** Optimized scan, without allocating new terms. */
158 public void ScanTo(Term term
)
161 scratch
= new TermBuffer();
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.
169 public override Term
Term()
171 return termBuffer
.ToTerm();
174 /// <summary>Returns the previous Term in the enumeration.
178 return prevBuffer
.ToTerm();
181 /// <summary>Returns the current TermInfo in the enumeration.
182 /// Initially invalid, valid after next() called for the first time.
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.
192 internal void TermInfo(TermInfo ti
)
197 /// <summary>Returns the docFreq from the current TermInfo in the enumeration.
198 /// Initially invalid, valid after next() called for the first time.
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()