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 using BitVector
= Lucene
.Net
.Util
.BitVector
;
19 namespace Lucene
.Net
.Index
22 public class SegmentTermDocs
: TermDocs
24 protected internal SegmentReader parent
;
25 private InputStream freqStream
;
28 private BitVector deletedDocs
;
32 private int skipInterval
;
34 private int skipCount
;
35 private InputStream skipStream
;
37 private long freqPointer
;
38 private long proxPointer
;
39 private long skipPointer
;
40 private bool haveSkipped
;
42 public /*internal*/ SegmentTermDocs(SegmentReader parent
)
45 this.freqStream
= (InputStream
) parent
.freqStream
.Clone();
46 this.deletedDocs
= parent
.deletedDocs
;
47 this.skipInterval
= parent
.tis
.GetSkipInterval();
50 public virtual void Seek(Term term
)
52 TermInfo ti
= parent
.tis
.Get(term
);
56 public virtual void Seek(TermEnum termEnum
)
60 // use comparison of fieldinfos to verify that termEnum belongs to the same segment as this SegmentTermDocs
61 if (termEnum
is SegmentTermEnum
&& ((SegmentTermEnum
) termEnum
).fieldInfos
== parent
.fieldInfos
)
63 ti
= ((SegmentTermEnum
) termEnum
).TermInfo();
66 ti
= parent
.tis
.Get(termEnum
.Term());
71 internal virtual void Seek(TermInfo ti
)
84 numSkips
= df
/ skipInterval
;
85 freqPointer
= ti
.freqPointer
;
86 proxPointer
= ti
.proxPointer
;
87 skipPointer
= freqPointer
+ ti
.skipOffset
;
88 freqStream
.Seek(freqPointer
);
93 public virtual void Close()
96 if (skipStream
!= null)
109 protected internal virtual void SkippingDoc()
113 public virtual bool Next()
120 int docCode
= freqStream
.ReadVInt();
121 doc
+= (int) (((uint) docCode
) >> 1); // shift off low bit
122 if ((docCode
& 1) != 0)
127 freq
= freqStream
.ReadVInt(); // else read freq
131 if (deletedDocs
== null || !deletedDocs
.Get(doc
))
138 /// <summary>Optimized implementation. </summary>
139 public virtual int Read(int[] docs
, int[] freqs
)
141 int length
= docs
.Length
;
143 while (i
< length
&& count
< df
)
146 // manually inlined call to next() for speed
147 int docCode
= freqStream
.ReadVInt();
148 doc
+= (int) (((uint) docCode
) >> 1); // shift off low bit
149 if ((docCode
& 1) != 0)
154 freq
= freqStream
.ReadVInt(); // else read freq
157 if (deletedDocs
== null || !deletedDocs
.Get(doc
))
167 /// <summary>Overridden by SegmentTermPositions to skip in prox stream. </summary>
168 protected internal virtual void SkipProx(long proxPointer
)
172 /// <summary>Optimized implementation. </summary>
173 public virtual bool SkipTo(int target
)
175 if (df
>= skipInterval
)
179 if (skipStream
== null)
180 skipStream
= (InputStream
) freqStream
.Clone(); // lazily clone
184 // lazily seek skip stream
185 skipStream
.Seek(skipPointer
);
190 int lastSkipDoc
= skipDoc
;
191 long lastFreqPointer
= freqStream
.GetFilePointer();
192 long lastProxPointer
= - 1;
193 int numSkipped
= - 1 - (count
% skipInterval
);
195 while (target
> skipDoc
)
197 lastSkipDoc
= skipDoc
;
198 lastFreqPointer
= freqPointer
;
199 lastProxPointer
= proxPointer
;
201 if (skipDoc
!= 0 && skipDoc
>= doc
)
202 numSkipped
+= skipInterval
;
204 if (skipCount
>= numSkips
)
207 skipDoc
+= skipStream
.ReadVInt();
208 freqPointer
+= skipStream
.ReadVInt();
209 proxPointer
+= skipStream
.ReadVInt();
214 // if we found something to skip, then skip it
215 if (lastFreqPointer
> freqStream
.GetFilePointer())
217 freqStream
.Seek(lastFreqPointer
);
218 SkipProx(lastProxPointer
);
225 // done skipping, now just scan
231 while (target
> doc
);