pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / FilterIndexReader.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.document.FieldSelector;
22 import org.apache.lucene.store.Directory;
23 import org.apache.lucene.util.MapBackedSet;
24
25 import java.io.IOException;
26 import java.util.Collection;
27 import java.util.Map;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 /**  A <code>FilterIndexReader</code> contains another IndexReader, which it
31  * uses as its basic source of data, possibly transforming the data along the
32  * way or providing additional functionality. The class
33  * <code>FilterIndexReader</code> itself simply implements all abstract methods
34  * of <code>IndexReader</code> with versions that pass all requests to the
35  * contained index reader. Subclasses of <code>FilterIndexReader</code> may
36  * further override some of these methods and may also provide additional
37  * methods and fields.
38  * <p><b>Note:</b> The default implementation of {@link FilterIndexReader#doOpenIfChanged}
39  * throws {@link UnsupportedOperationException} (like the base class),
40  * so it's not possible to reopen a <code>FilterIndexReader</code>.
41  * To reopen, you have to first reopen the underlying reader
42  * and wrap it again with the custom filter.
43  */
44 public class FilterIndexReader extends IndexReader {
45
46   /** Base class for filtering {@link TermDocs} implementations. */
47   public static class FilterTermDocs implements TermDocs {
48     protected TermDocs in;
49
50     public FilterTermDocs(TermDocs in) { this.in = in; }
51
52     public void seek(Term term) throws IOException { in.seek(term); }
53     public void seek(TermEnum termEnum) throws IOException { in.seek(termEnum); }
54     public int doc() { return in.doc(); }
55     public int freq() { return in.freq(); }
56     public boolean next() throws IOException { return in.next(); }
57     public int read(int[] docs, int[] freqs) throws IOException {
58       return in.read(docs, freqs);
59     }
60     public boolean skipTo(int i) throws IOException { return in.skipTo(i); }
61     public void close() throws IOException { in.close(); }
62   }
63
64   /** Base class for filtering {@link TermPositions} implementations. */
65   public static class FilterTermPositions
66           extends FilterTermDocs implements TermPositions {
67
68     public FilterTermPositions(TermPositions in) { super(in); }
69
70     public int nextPosition() throws IOException {
71       return ((TermPositions) this.in).nextPosition();
72     }
73     
74     public int getPayloadLength() {
75       return ((TermPositions) this.in).getPayloadLength();
76     }
77
78     public byte[] getPayload(byte[] data, int offset) throws IOException {
79       return ((TermPositions) this.in).getPayload(data, offset);
80     }
81
82
83     // TODO: Remove warning after API has been finalized
84     public boolean isPayloadAvailable() {
85       return ((TermPositions)this.in).isPayloadAvailable();
86     }
87   }
88
89   /** Base class for filtering {@link TermEnum} implementations. */
90   public static class FilterTermEnum extends TermEnum {
91     protected TermEnum in;
92
93     public FilterTermEnum(TermEnum in) { this.in = in; }
94
95     @Override
96     public boolean next() throws IOException { return in.next(); }
97     @Override
98     public Term term() { return in.term(); }
99     @Override
100     public int docFreq() { return in.docFreq(); }
101     @Override
102     public void close() throws IOException { in.close(); }
103   }
104
105   protected IndexReader in;
106
107   /**
108    * <p>Construct a FilterIndexReader based on the specified base reader.
109    * Directory locking for delete, undeleteAll, and setNorm operations is
110    * left to the base reader.</p>
111    * <p>Note that base reader is closed if this FilterIndexReader is closed.</p>
112    * @param in specified base reader.
113    */
114   public FilterIndexReader(IndexReader in) {
115     super();
116     this.in = in;
117     readerFinishedListeners = new MapBackedSet<ReaderFinishedListener>(new ConcurrentHashMap<ReaderFinishedListener,Boolean>());
118   }
119
120   @Override
121   public Directory directory() {
122     ensureOpen();
123     return in.directory();
124   }
125   
126   @Override
127   public TermFreqVector[] getTermFreqVectors(int docNumber)
128           throws IOException {
129     ensureOpen();
130     return in.getTermFreqVectors(docNumber);
131   }
132
133   @Override
134   public TermFreqVector getTermFreqVector(int docNumber, String field)
135           throws IOException {
136     ensureOpen();
137     return in.getTermFreqVector(docNumber, field);
138   }
139
140
141   @Override
142   public void getTermFreqVector(int docNumber, String field, TermVectorMapper mapper) throws IOException {
143     ensureOpen();
144     in.getTermFreqVector(docNumber, field, mapper);
145   }
146
147   @Override
148   public void getTermFreqVector(int docNumber, TermVectorMapper mapper) throws IOException {
149     ensureOpen();
150     in.getTermFreqVector(docNumber, mapper);
151   }
152
153   @Override
154   public int numDocs() {
155     // Don't call ensureOpen() here (it could affect performance)
156     return in.numDocs();
157   }
158
159   @Override
160   public int maxDoc() {
161     // Don't call ensureOpen() here (it could affect performance)
162     return in.maxDoc();
163   }
164
165   @Override
166   public Document document(int n, FieldSelector fieldSelector) throws CorruptIndexException, IOException {
167     ensureOpen();
168     return in.document(n, fieldSelector);
169   }
170
171   @Override
172   public boolean isDeleted(int n) {
173     // Don't call ensureOpen() here (it could affect performance)
174     return in.isDeleted(n);
175   }
176
177   @Override
178   public boolean hasDeletions() {
179     ensureOpen();
180     return in.hasDeletions();
181   }
182
183   @Override
184   protected void doUndeleteAll() throws CorruptIndexException, IOException {in.undeleteAll();}
185
186   @Override
187   public boolean hasNorms(String field) throws IOException {
188     ensureOpen();
189     return in.hasNorms(field);
190   }
191
192   @Override
193   public byte[] norms(String f) throws IOException {
194     ensureOpen();
195     return in.norms(f);
196   }
197
198   @Override
199   public void norms(String f, byte[] bytes, int offset) throws IOException {
200     ensureOpen();
201     in.norms(f, bytes, offset);
202   }
203
204   @Override
205   protected void doSetNorm(int d, String f, byte b) throws CorruptIndexException, IOException {
206     in.setNorm(d, f, b);
207   }
208
209   @Override
210   public TermEnum terms() throws IOException {
211     ensureOpen();
212     return in.terms();
213   }
214
215   @Override
216   public TermEnum terms(Term t) throws IOException {
217     ensureOpen();
218     return in.terms(t);
219   }
220
221   @Override
222   public int docFreq(Term t) throws IOException {
223     ensureOpen();
224     return in.docFreq(t);
225   }
226
227   @Override
228   public TermDocs termDocs() throws IOException {
229     ensureOpen();
230     return in.termDocs();
231   }
232
233   @Override
234   public TermDocs termDocs(Term term) throws IOException {
235     ensureOpen();
236     return in.termDocs(term);
237   }
238
239   @Override
240   public TermPositions termPositions() throws IOException {
241     ensureOpen();
242     return in.termPositions();
243   }
244
245   @Override
246   protected void doDelete(int n) throws  CorruptIndexException, IOException { in.deleteDocument(n); }
247   
248   @Override
249   protected void doCommit(Map<String,String> commitUserData) throws IOException {
250     in.commit(commitUserData);
251   }
252   
253   @Override
254   protected void doClose() throws IOException {
255     in.close();
256   }
257
258   @Override
259   public Collection<String> getFieldNames(IndexReader.FieldOption fieldNames) {
260     ensureOpen();
261     return in.getFieldNames(fieldNames);
262   }
263
264   @Override
265   public long getVersion() {
266     ensureOpen();
267     return in.getVersion();
268   }
269
270   @Override
271   public boolean isCurrent() throws CorruptIndexException, IOException {
272     ensureOpen();
273     return in.isCurrent();
274   }
275
276   @Deprecated
277   @Override
278   public boolean isOptimized() {
279     ensureOpen();
280     return in.isOptimized();
281   }
282
283   @Override
284   public IndexReader[] getSequentialSubReaders() {
285     return in.getSequentialSubReaders();
286   }
287
288   @Override
289   public Map<String, String> getCommitUserData() { 
290     return in.getCommitUserData();
291   }
292   
293   /** If the subclass of FilteredIndexReader modifies the
294    *  contents of the FieldCache, you must override this
295    *  method to provide a different key */
296   @Override
297   public Object getCoreCacheKey() {
298     return in.getCoreCacheKey();
299   }
300
301   /** If the subclass of FilteredIndexReader modifies the
302    *  deleted docs, you must override this method to provide
303    *  a different key */
304   @Override
305   public Object getDeletesCacheKey() {
306     return in.getDeletesCacheKey();
307   }
308
309   /** {@inheritDoc} */
310   @Override
311   public String toString() {
312     final StringBuilder buffer = new StringBuilder("FilterReader(");
313     buffer.append(in);
314     buffer.append(')');
315     return buffer.toString();
316   }
317
318   @Override
319   public void addReaderFinishedListener(ReaderFinishedListener listener) {
320     super.addReaderFinishedListener(listener);
321     in.addReaderFinishedListener(listener);
322   }
323
324   @Override
325   public void removeReaderFinishedListener(ReaderFinishedListener listener) {
326     super.removeReaderFinishedListener(listener);
327     in.removeReaderFinishedListener(listener);
328   }
329 }
330