KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > SegmentTermDocs


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import org.apache.lucene.util.BitVector;
21 import org.apache.lucene.store.IndexInput;
22
23 class SegmentTermDocs implements TermDocs {
24   protected SegmentReader parent;
25   protected IndexInput freqStream;
26   protected int count;
27   protected int df;
28   protected BitVector deletedDocs;
29   int doc = 0;
30   int freq;
31
32   private int skipInterval;
33   private int numSkips;
34   private int skipCount;
35   private IndexInput skipStream;
36   private int skipDoc;
37   private long freqPointer;
38   private long proxPointer;
39   private long skipPointer;
40   private boolean haveSkipped;
41
42   protected SegmentTermDocs(SegmentReader parent) {
43     this.parent = parent;
44     this.freqStream = (IndexInput) parent.freqStream.clone();
45     this.deletedDocs = parent.deletedDocs;
46     this.skipInterval = parent.tis.getSkipInterval();
47   }
48
49   public void seek(Term term) throws IOException JavaDoc {
50     TermInfo ti = parent.tis.get(term);
51     seek(ti);
52   }
53
54   public void seek(TermEnum termEnum) throws IOException JavaDoc {
55     TermInfo ti;
56     
57     // use comparison of fieldinfos to verify that termEnum belongs to the same segment as this SegmentTermDocs
58
if (termEnum instanceof SegmentTermEnum && ((SegmentTermEnum) termEnum).fieldInfos == parent.fieldInfos) // optimized case
59
ti = ((SegmentTermEnum) termEnum).termInfo();
60     else // punt case
61
ti = parent.tis.get(termEnum.term());
62       
63     seek(ti);
64   }
65
66   void seek(TermInfo ti) throws IOException JavaDoc {
67     count = 0;
68     if (ti == null) {
69       df = 0;
70     } else {
71       df = ti.docFreq;
72       doc = 0;
73       skipDoc = 0;
74       skipCount = 0;
75       numSkips = df / skipInterval;
76       freqPointer = ti.freqPointer;
77       proxPointer = ti.proxPointer;
78       skipPointer = freqPointer + ti.skipOffset;
79       freqStream.seek(freqPointer);
80       haveSkipped = false;
81     }
82   }
83
84   public void close() throws IOException JavaDoc {
85     freqStream.close();
86     if (skipStream != null)
87       skipStream.close();
88   }
89
90   public final int doc() { return doc; }
91   public final int freq() { return freq; }
92
93   protected void skippingDoc() throws IOException JavaDoc {
94   }
95
96   public boolean next() throws IOException JavaDoc {
97     while (true) {
98       if (count == df)
99         return false;
100
101       int docCode = freqStream.readVInt();
102       doc += docCode >>> 1; // shift off low bit
103
if ((docCode & 1) != 0) // if low bit is set
104
freq = 1; // freq is one
105
else
106         freq = freqStream.readVInt(); // else read freq
107

108       count++;
109
110       if (deletedDocs == null || !deletedDocs.get(doc))
111         break;
112       skippingDoc();
113     }
114     return true;
115   }
116
117   /** Optimized implementation. */
118   public int read(final int[] docs, final int[] freqs)
119           throws IOException JavaDoc {
120     final int length = docs.length;
121     int i = 0;
122     while (i < length && count < df) {
123
124       // manually inlined call to next() for speed
125
final int docCode = freqStream.readVInt();
126       doc += docCode >>> 1; // shift off low bit
127
if ((docCode & 1) != 0) // if low bit is set
128
freq = 1; // freq is one
129
else
130         freq = freqStream.readVInt(); // else read freq
131
count++;
132
133       if (deletedDocs == null || !deletedDocs.get(doc)) {
134         docs[i] = doc;
135         freqs[i] = freq;
136         ++i;
137       }
138     }
139     return i;
140   }
141
142   /** Overridden by SegmentTermPositions to skip in prox stream. */
143   protected void skipProx(long proxPointer) throws IOException JavaDoc {}
144
145   /** Optimized implementation. */
146   public boolean skipTo(int target) throws IOException JavaDoc {
147     if (df >= skipInterval) { // optimized case
148

149       if (skipStream == null)
150         skipStream = (IndexInput) freqStream.clone(); // lazily clone
151

152       if (!haveSkipped) { // lazily seek skip stream
153
skipStream.seek(skipPointer);
154         haveSkipped = true;
155       }
156
157       // scan skip data
158
int lastSkipDoc = skipDoc;
159       long lastFreqPointer = freqStream.getFilePointer();
160       long lastProxPointer = -1;
161       int numSkipped = -1 - (count % skipInterval);
162
163       while (target > skipDoc) {
164         lastSkipDoc = skipDoc;
165         lastFreqPointer = freqPointer;
166         lastProxPointer = proxPointer;
167         
168         if (skipDoc != 0 && skipDoc >= doc)
169           numSkipped += skipInterval;
170         
171         if(skipCount >= numSkips)
172           break;
173
174         skipDoc += skipStream.readVInt();
175         freqPointer += skipStream.readVInt();
176         proxPointer += skipStream.readVInt();
177
178         skipCount++;
179       }
180       
181       // if we found something to skip, then skip it
182
if (lastFreqPointer > freqStream.getFilePointer()) {
183         freqStream.seek(lastFreqPointer);
184         skipProx(lastProxPointer);
185
186         doc = lastSkipDoc;
187         count += numSkipped;
188       }
189
190     }
191
192     // done skipping, now just scan
193
do {
194       if (!next())
195         return false;
196     } while (target > doc);
197     return true;
198   }
199
200 }
201
Popular Tags