1 package org.apache.lucene.index;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
25 import java.io.IOException;
26 import java.util.Collection;
28 import java.util.concurrent.ConcurrentHashMap;
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
39 public class FilterIndexReader extends IndexReader {
41 /** Base class for filtering {@link TermDocs} implementations. */
42 public static class FilterTermDocs implements TermDocs {
43 protected TermDocs in;
45 public FilterTermDocs(TermDocs in) { this.in = in; }
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);
55 public boolean skipTo(int i) throws IOException { return in.skipTo(i); }
56 public void close() throws IOException { in.close(); }
59 /** Base class for filtering {@link TermPositions} implementations. */
60 public static class FilterTermPositions
61 extends FilterTermDocs implements TermPositions {
63 public FilterTermPositions(TermPositions in) { super(in); }
65 public int nextPosition() throws IOException {
66 return ((TermPositions) this.in).nextPosition();
69 public int getPayloadLength() {
70 return ((TermPositions) this.in).getPayloadLength();
73 public byte[] getPayload(byte[] data, int offset) throws IOException {
74 return ((TermPositions) this.in).getPayload(data, offset);
78 // TODO: Remove warning after API has been finalized
79 public boolean isPayloadAvailable() {
80 return ((TermPositions)this.in).isPayloadAvailable();
84 /** Base class for filtering {@link TermEnum} implementations. */
85 public static class FilterTermEnum extends TermEnum {
86 protected TermEnum in;
88 public FilterTermEnum(TermEnum in) { this.in = in; }
91 public boolean next() throws IOException { return in.next(); }
93 public Term term() { return in.term(); }
95 public int docFreq() { return in.docFreq(); }
97 public void close() throws IOException { in.close(); }
100 protected IndexReader in;
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.
109 public FilterIndexReader(IndexReader in) {
112 readerFinishedListeners = new MapBackedSet<ReaderFinishedListener>(new ConcurrentHashMap<ReaderFinishedListener,Boolean>());
116 public Directory directory() {
117 return in.directory();
121 public TermFreqVector[] getTermFreqVectors(int docNumber)
124 return in.getTermFreqVectors(docNumber);
128 public TermFreqVector getTermFreqVector(int docNumber, String field)
131 return in.getTermFreqVector(docNumber, field);
136 public void getTermFreqVector(int docNumber, String field, TermVectorMapper mapper) throws IOException {
138 in.getTermFreqVector(docNumber, field, mapper);
143 public void getTermFreqVector(int docNumber, TermVectorMapper mapper) throws IOException {
145 in.getTermFreqVector(docNumber, mapper);
149 public int numDocs() {
150 // Don't call ensureOpen() here (it could affect performance)
155 public int maxDoc() {
156 // Don't call ensureOpen() here (it could affect performance)
161 public Document document(int n, FieldSelector fieldSelector) throws CorruptIndexException, IOException {
163 return in.document(n, fieldSelector);
167 public boolean isDeleted(int n) {
168 // Don't call ensureOpen() here (it could affect performance)
169 return in.isDeleted(n);
173 public boolean hasDeletions() {
174 // Don't call ensureOpen() here (it could affect performance)
175 return in.hasDeletions();
179 protected void doUndeleteAll() throws CorruptIndexException, IOException {in.undeleteAll();}
182 public boolean hasNorms(String field) throws IOException {
184 return in.hasNorms(field);
188 public byte[] norms(String f) throws IOException {
194 public void norms(String f, byte[] bytes, int offset) throws IOException {
196 in.norms(f, bytes, offset);
200 protected void doSetNorm(int d, String f, byte b) throws CorruptIndexException, IOException {
205 public TermEnum terms() throws IOException {
211 public TermEnum terms(Term t) throws IOException {
217 public int docFreq(Term t) throws IOException {
219 return in.docFreq(t);
223 public TermDocs termDocs() throws IOException {
225 return in.termDocs();
229 public TermDocs termDocs(Term term) throws IOException {
231 return in.termDocs(term);
235 public TermPositions termPositions() throws IOException {
237 return in.termPositions();
241 protected void doDelete(int n) throws CorruptIndexException, IOException { in.deleteDocument(n); }
244 protected void doCommit(Map<String,String> commitUserData) throws IOException { in.commit(commitUserData); }
247 protected void doClose() throws IOException {
253 public Collection<String> getFieldNames(IndexReader.FieldOption fieldNames) {
255 return in.getFieldNames(fieldNames);
259 public long getVersion() {
261 return in.getVersion();
265 public boolean isCurrent() throws CorruptIndexException, IOException {
267 return in.isCurrent();
271 public boolean isOptimized() {
273 return in.isOptimized();
277 public IndexReader[] getSequentialSubReaders() {
278 return in.getSequentialSubReaders();
281 /** If the subclass of FilteredIndexReader modifies the
282 * contents of the FieldCache, you must override this
283 * method to provide a different key */
285 public Object getCoreCacheKey() {
286 return in.getCoreCacheKey();
289 /** If the subclass of FilteredIndexReader modifies the
290 * deleted docs, you must override this method to provide
293 public Object getDeletesCacheKey() {
294 return in.getDeletesCacheKey();
299 public String toString() {
300 final StringBuilder buffer = new StringBuilder("FilterReader(");
303 return buffer.toString();
307 public void addReaderFinishedListener(ReaderFinishedListener listener) {
308 super.addReaderFinishedListener(listener);
309 in.addReaderFinishedListener(listener);
313 public void removeReaderFinishedListener(ReaderFinishedListener listener) {
314 super.removeReaderFinishedListener(listener);
315 in.removeReaderFinishedListener(listener);