001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.document.Document;
021: import org.apache.lucene.document.FieldSelector;
022: import org.apache.lucene.store.Directory;
023:
024: import java.io.IOException;
025: import java.util.Collection;
026:
027: /** A <code>FilterIndexReader</code> contains another IndexReader, which it
028: * uses as its basic source of data, possibly transforming the data along the
029: * way or providing additional functionality. The class
030: * <code>FilterIndexReader</code> itself simply implements all abstract methods
031: * of <code>IndexReader</code> with versions that pass all requests to the
032: * contained index reader. Subclasses of <code>FilterIndexReader</code> may
033: * further override some of these methods and may also provide additional
034: * methods and fields.
035: */
036: public class FilterIndexReader extends IndexReader {
037:
038: /** Base class for filtering {@link TermDocs} implementations. */
039: public static class FilterTermDocs implements TermDocs {
040: protected TermDocs in;
041:
042: public FilterTermDocs(TermDocs in) {
043: this .in = in;
044: }
045:
046: public void seek(Term term) throws IOException {
047: in.seek(term);
048: }
049:
050: public void seek(TermEnum termEnum) throws IOException {
051: in.seek(termEnum);
052: }
053:
054: public int doc() {
055: return in.doc();
056: }
057:
058: public int freq() {
059: return in.freq();
060: }
061:
062: public boolean next() throws IOException {
063: return in.next();
064: }
065:
066: public int read(int[] docs, int[] freqs) throws IOException {
067: return in.read(docs, freqs);
068: }
069:
070: public boolean skipTo(int i) throws IOException {
071: return in.skipTo(i);
072: }
073:
074: public void close() throws IOException {
075: in.close();
076: }
077: }
078:
079: /** Base class for filtering {@link TermPositions} implementations. */
080: public static class FilterTermPositions extends FilterTermDocs
081: implements TermPositions {
082:
083: public FilterTermPositions(TermPositions in) {
084: super (in);
085: }
086:
087: public int nextPosition() throws IOException {
088: return ((TermPositions) this .in).nextPosition();
089: }
090:
091: public int getPayloadLength() {
092: return ((TermPositions) this .in).getPayloadLength();
093: }
094:
095: public byte[] getPayload(byte[] data, int offset)
096: throws IOException {
097: return ((TermPositions) this .in).getPayload(data, offset);
098: }
099:
100: // TODO: Remove warning after API has been finalized
101: public boolean isPayloadAvailable() {
102: return ((TermPositions) this .in).isPayloadAvailable();
103: }
104: }
105:
106: /** Base class for filtering {@link TermEnum} implementations. */
107: public static class FilterTermEnum extends TermEnum {
108: protected TermEnum in;
109:
110: public FilterTermEnum(TermEnum in) {
111: this .in = in;
112: }
113:
114: public boolean next() throws IOException {
115: return in.next();
116: }
117:
118: public Term term() {
119: return in.term();
120: }
121:
122: public int docFreq() {
123: return in.docFreq();
124: }
125:
126: public void close() throws IOException {
127: in.close();
128: }
129: }
130:
131: protected IndexReader in;
132:
133: /**
134: * <p>Construct a FilterIndexReader based on the specified base reader.
135: * Directory locking for delete, undeleteAll, and setNorm operations is
136: * left to the base reader.</p>
137: * <p>Note that base reader is closed if this FilterIndexReader is closed.</p>
138: * @param in specified base reader.
139: */
140: public FilterIndexReader(IndexReader in) {
141: super ();
142: this .in = in;
143: }
144:
145: public Directory directory() {
146: return in.directory();
147: }
148:
149: public TermFreqVector[] getTermFreqVectors(int docNumber)
150: throws IOException {
151: ensureOpen();
152: return in.getTermFreqVectors(docNumber);
153: }
154:
155: public TermFreqVector getTermFreqVector(int docNumber, String field)
156: throws IOException {
157: ensureOpen();
158: return in.getTermFreqVector(docNumber, field);
159: }
160:
161: public void getTermFreqVector(int docNumber, String field,
162: TermVectorMapper mapper) throws IOException {
163: ensureOpen();
164: in.getTermFreqVector(docNumber, field, mapper);
165:
166: }
167:
168: public void getTermFreqVector(int docNumber, TermVectorMapper mapper)
169: throws IOException {
170: ensureOpen();
171: in.getTermFreqVector(docNumber, mapper);
172: }
173:
174: public int numDocs() {
175: // Don't call ensureOpen() here (it could affect performance)
176: return in.numDocs();
177: }
178:
179: public int maxDoc() {
180: // Don't call ensureOpen() here (it could affect performance)
181: return in.maxDoc();
182: }
183:
184: public Document document(int n, FieldSelector fieldSelector)
185: throws CorruptIndexException, IOException {
186: ensureOpen();
187: return in.document(n, fieldSelector);
188: }
189:
190: public boolean isDeleted(int n) {
191: // Don't call ensureOpen() here (it could affect performance)
192: return in.isDeleted(n);
193: }
194:
195: public boolean hasDeletions() {
196: // Don't call ensureOpen() here (it could affect performance)
197: return in.hasDeletions();
198: }
199:
200: protected void doUndeleteAll() throws CorruptIndexException,
201: IOException {
202: in.undeleteAll();
203: }
204:
205: public boolean hasNorms(String field) throws IOException {
206: ensureOpen();
207: return in.hasNorms(field);
208: }
209:
210: public byte[] norms(String f) throws IOException {
211: ensureOpen();
212: return in.norms(f);
213: }
214:
215: public void norms(String f, byte[] bytes, int offset)
216: throws IOException {
217: ensureOpen();
218: in.norms(f, bytes, offset);
219: }
220:
221: protected void doSetNorm(int d, String f, byte b)
222: throws CorruptIndexException, IOException {
223: in.setNorm(d, f, b);
224: }
225:
226: public TermEnum terms() throws IOException {
227: ensureOpen();
228: return in.terms();
229: }
230:
231: public TermEnum terms(Term t) throws IOException {
232: ensureOpen();
233: return in.terms(t);
234: }
235:
236: public int docFreq(Term t) throws IOException {
237: ensureOpen();
238: return in.docFreq(t);
239: }
240:
241: public TermDocs termDocs() throws IOException {
242: ensureOpen();
243: return in.termDocs();
244: }
245:
246: public TermPositions termPositions() throws IOException {
247: ensureOpen();
248: return in.termPositions();
249: }
250:
251: protected void doDelete(int n) throws CorruptIndexException,
252: IOException {
253: in.deleteDocument(n);
254: }
255:
256: protected void doCommit() throws IOException {
257: in.commit();
258: }
259:
260: protected void doClose() throws IOException {
261: in.close();
262: }
263:
264: public Collection getFieldNames(IndexReader.FieldOption fieldNames) {
265: ensureOpen();
266: return in.getFieldNames(fieldNames);
267: }
268:
269: public long getVersion() {
270: ensureOpen();
271: return in.getVersion();
272: }
273:
274: public boolean isCurrent() throws CorruptIndexException,
275: IOException {
276: ensureOpen();
277: return in.isCurrent();
278: }
279:
280: public boolean isOptimized() {
281: ensureOpen();
282: return in.isOptimized();
283: }
284: }
|