add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / instantiated / src / java / org / apache / lucene / store / instantiated / InstantiatedTermDocs.java
1 package org.apache.lucene.store.instantiated;
2
3 /**
4  * Copyright 2006 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 org.apache.lucene.index.Term;
20 import org.apache.lucene.index.TermDocs;
21
22 /**
23  * A {@link org.apache.lucene.index.TermDocs} navigating an {@link InstantiatedIndexReader}.
24  */
25 public class InstantiatedTermDocs
26     implements TermDocs {
27
28   private final InstantiatedIndexReader reader;
29
30   public InstantiatedTermDocs(InstantiatedIndexReader reader) {
31     this.reader = reader;
32   }
33
34   private int currentDocumentIndex;
35   protected InstantiatedTermDocumentInformation currentDocumentInformation;
36   protected InstantiatedTerm currentTerm;
37
38
39   public void seek(Term term) {
40     currentTerm = reader.getIndex().findTerm(term);
41     currentDocumentIndex = -1;
42   }
43
44   public void seek(org.apache.lucene.index.TermEnum termEnum) {
45     seek(termEnum.term());
46   }
47
48
49   public int doc() {
50     return currentDocumentInformation.getDocument().getDocumentNumber();
51   }
52
53   public int freq() {
54     return currentDocumentInformation.getTermPositions().length;
55   }
56
57
58   public boolean next() {
59     if (currentTerm != null) {
60       currentDocumentIndex++;
61       if (currentDocumentIndex < currentTerm.getAssociatedDocuments().length) {
62         currentDocumentInformation = currentTerm.getAssociatedDocuments()[currentDocumentIndex];
63         if (reader.isDeleted(currentDocumentInformation.getDocument().getDocumentNumber())) {
64           return next();
65         } else {
66           return true;
67         }
68       } else {
69         // mimic SegmentTermDocs
70         currentDocumentIndex = currentTerm.getAssociatedDocuments().length -1;
71       }
72     }
73     return false;
74   }
75
76
77   public int read(int[] docs, int[] freqs) {
78     int i;
79     for (i = 0; i < docs.length; i++) {
80       if (!next()) {
81         break;
82       }
83       docs[i] = doc();
84       freqs[i] = freq();
85     }
86     return i;
87   }
88
89   /**
90    * Skips entries to the first beyond the current whose document number is
91    * greater than or equal to <i>target</i>. <p>Returns true if there is such
92    * an entry.  <p>Behaves as if written: <pre>
93    *   boolean skipTo(int target) {
94    *     do {
95    *       if (!next())
96    *         return false;
97    *     } while (target > doc());
98    *     return true;
99    *   }
100    * </pre>
101    * This implementation is considerably more efficient than that.
102    *
103    */
104   public boolean skipTo(int target) {
105     if (currentTerm == null) {
106       return false;
107     }
108     
109     if (currentDocumentIndex >= target) {
110       return next();
111     }
112
113     int startOffset = currentDocumentIndex >= 0 ? currentDocumentIndex : 0;
114     int pos = currentTerm.seekCeilingDocumentInformationIndex(target, startOffset);
115
116     if (pos == -1) {
117       // mimic SegmentTermDocs that positions at the last index
118       currentDocumentIndex = currentTerm.getAssociatedDocuments().length -1;
119       return false;
120     }
121
122     currentDocumentInformation = currentTerm.getAssociatedDocuments()[pos];
123     currentDocumentIndex = pos;
124     if (reader.hasDeletions() && reader.isDeleted(currentDocumentInformation.getDocument().getDocumentNumber())) {
125       return next();
126     } else {
127       return true;
128     }
129   }
130
131   /**
132    * Does nothing
133    */
134   public void close() {
135   }
136 }