add --shared
[pylucene.git] / lucene-java-3.4.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  */
39 public class FilterIndexReader extends IndexReader {
40
41   /** Base class for filtering {@link TermDocs} implementations. */
42   public static class FilterTermDocs implements TermDocs {
43     protected TermDocs in;
44
45     public FilterTermDocs(TermDocs in) { this.in = in; }
46
47     public void seek(Term term) throws IOException { in.seek(term); }
48     public void seek(TermEnum termEnum) throws IOException { in.seek(termEnum); }
49     public int doc() { return in.doc(); }
50     public int freq() { return in.freq(); }
51     public boolean next() throws IOException { return in.next(); }
52     public int read(int[] docs, int[] freqs) throws IOException {
53       return in.read(docs, freqs);
54     }
55     public boolean skipTo(int i) throws IOException { return in.skipTo(i); }
56     public void close() throws IOException { in.close(); }
57   }
58
59   /** Base class for filtering {@link TermPositions} implementations. */
60   public static class FilterTermPositions
61           extends FilterTermDocs implements TermPositions {
62
63     public FilterTermPositions(TermPositions in) { super(in); }
64
65     public int nextPosition() throws IOException {
66       return ((TermPositions) this.in).nextPosition();
67     }
68     
69     public int getPayloadLength() {
70       return ((TermPositions) this.in).getPayloadLength();
71     }
72
73     public byte[] getPayload(byte[] data, int offset) throws IOException {
74       return ((TermPositions) this.in).getPayload(data, offset);
75     }
76
77
78     // TODO: Remove warning after API has been finalized
79     public boolean isPayloadAvailable() {
80       return ((TermPositions)this.in).isPayloadAvailable();
81     }
82   }
83
84   /** Base class for filtering {@link TermEnum} implementations. */
85   public static class FilterTermEnum extends TermEnum {
86     protected TermEnum in;
87
88     public FilterTermEnum(TermEnum in) { this.in = in; }
89
90     @Override
91     public boolean next() throws IOException { return in.next(); }
92     @Override
93     public Term term() { return in.term(); }
94     @Override
95     public int docFreq() { return in.docFreq(); }
96     @Override
97     public void close() throws IOException { in.close(); }
98   }
99
100   protected IndexReader in;
101
102   /**
103    * <p>Construct a FilterIndexReader based on the specified base reader.
104    * Directory locking for delete, undeleteAll, and setNorm operations is
105    * left to the base reader.</p>
106    * <p>Note that base reader is closed if this FilterIndexReader is closed.</p>
107    * @param in specified base reader.
108    */
109   public FilterIndexReader(IndexReader in) {
110     super();
111     this.in = in;
112     readerFinishedListeners = new MapBackedSet<ReaderFinishedListener>(new ConcurrentHashMap<ReaderFinishedListener,Boolean>());
113   }
114
115   @Override
116   public Directory directory() {
117     return in.directory();
118   }
119   
120   @Override
121   public TermFreqVector[] getTermFreqVectors(int docNumber)
122           throws IOException {
123     ensureOpen();
124     return in.getTermFreqVectors(docNumber);
125   }
126
127   @Override
128   public TermFreqVector getTermFreqVector(int docNumber, String field)
129           throws IOException {
130     ensureOpen();
131     return in.getTermFreqVector(docNumber, field);
132   }
133
134
135   @Override
136   public void getTermFreqVector(int docNumber, String field, TermVectorMapper mapper) throws IOException {
137     ensureOpen();
138     in.getTermFreqVector(docNumber, field, mapper);
139
140   }
141
142   @Override
143   public void getTermFreqVector(int docNumber, TermVectorMapper mapper) throws IOException {
144     ensureOpen();
145     in.getTermFreqVector(docNumber, mapper);
146   }
147
148   @Override
149   public int numDocs() {
150     // Don't call ensureOpen() here (it could affect performance)
151     return in.numDocs();
152   }
153
154   @Override
155   public int maxDoc() {
156     // Don't call ensureOpen() here (it could affect performance)
157     return in.maxDoc();
158   }
159
160   @Override
161   public Document document(int n, FieldSelector fieldSelector) throws CorruptIndexException, IOException {
162     ensureOpen();
163     return in.document(n, fieldSelector);
164   }
165
166   @Override
167   public boolean isDeleted(int n) {
168     // Don't call ensureOpen() here (it could affect performance)
169     return in.isDeleted(n);
170   }
171
172   @Override
173   public boolean hasDeletions() {
174     // Don't call ensureOpen() here (it could affect performance)
175     return in.hasDeletions();
176   }
177
178   @Override
179   protected void doUndeleteAll() throws CorruptIndexException, IOException {in.undeleteAll();}
180
181   @Override
182   public boolean hasNorms(String field) throws IOException {
183     ensureOpen();
184     return in.hasNorms(field);
185   }
186
187   @Override
188   public byte[] norms(String f) throws IOException {
189     ensureOpen();
190     return in.norms(f);
191   }
192
193   @Override
194   public void norms(String f, byte[] bytes, int offset) throws IOException {
195     ensureOpen();
196     in.norms(f, bytes, offset);
197   }
198
199   @Override
200   protected void doSetNorm(int d, String f, byte b) throws CorruptIndexException, IOException {
201     in.setNorm(d, f, b);
202   }
203
204   @Override
205   public TermEnum terms() throws IOException {
206     ensureOpen();
207     return in.terms();
208   }
209
210   @Override
211   public TermEnum terms(Term t) throws IOException {
212     ensureOpen();
213     return in.terms(t);
214   }
215
216   @Override
217   public int docFreq(Term t) throws IOException {
218     ensureOpen();
219     return in.docFreq(t);
220   }
221
222   @Override
223   public TermDocs termDocs() throws IOException {
224     ensureOpen();
225     return in.termDocs();
226   }
227
228   @Override
229   public TermDocs termDocs(Term term) throws IOException {
230     ensureOpen();
231     return in.termDocs(term);
232   }
233
234   @Override
235   public TermPositions termPositions() throws IOException {
236     ensureOpen();
237     return in.termPositions();
238   }
239
240   @Override
241   protected void doDelete(int n) throws  CorruptIndexException, IOException { in.deleteDocument(n); }
242   
243   @Override
244   protected void doCommit(Map<String,String> commitUserData) throws IOException { in.commit(commitUserData); }
245   
246   @Override
247   protected void doClose() throws IOException {
248     in.close();
249   }
250
251
252   @Override
253   public Collection<String> getFieldNames(IndexReader.FieldOption fieldNames) {
254     ensureOpen();
255     return in.getFieldNames(fieldNames);
256   }
257
258   @Override
259   public long getVersion() {
260     ensureOpen();
261     return in.getVersion();
262   }
263
264   @Override
265   public boolean isCurrent() throws CorruptIndexException, IOException {
266     ensureOpen();
267     return in.isCurrent();
268   }
269   
270   @Override
271   public boolean isOptimized() {
272     ensureOpen();
273     return in.isOptimized();
274   }
275   
276   @Override
277   public IndexReader[] getSequentialSubReaders() {
278     return in.getSequentialSubReaders();
279   }
280
281   /** If the subclass of FilteredIndexReader modifies the
282    *  contents of the FieldCache, you must override this
283    *  method to provide a different key */
284   @Override
285   public Object getCoreCacheKey() {
286     return in.getCoreCacheKey();
287   }
288
289   /** If the subclass of FilteredIndexReader modifies the
290    *  deleted docs, you must override this method to provide
291    *  a different key */
292   @Override
293   public Object getDeletesCacheKey() {
294     return in.getDeletesCacheKey();
295   }
296
297   /** {@inheritDoc} */
298   @Override
299   public String toString() {
300     final StringBuilder buffer = new StringBuilder("FilterReader(");
301     buffer.append(in);
302     buffer.append(')');
303     return buffer.toString();
304   }
305
306   @Override
307   public void addReaderFinishedListener(ReaderFinishedListener listener) {
308     super.addReaderFinishedListener(listener);
309     in.addReaderFinishedListener(listener);
310   }
311
312   @Override
313   public void removeReaderFinishedListener(ReaderFinishedListener listener) {
314     super.removeReaderFinishedListener(listener);
315     in.removeReaderFinishedListener(listener);
316   }
317 }
318