1 package org.apache.lucene.search;
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 java.io.IOException;
23 * Abstract decorator class for a DocIdSet implementation
24 * that provides on-demand filtering/validation
25 * mechanism on a given DocIdSet.
29 * Technically, this same functionality could be achieved
30 * with ChainedFilter (under contrib/misc), however the
31 * benefit of this class is it never materializes the full
32 * bitset for the filter. Instead, the {@link #match}
33 * method is invoked on-demand, per docID visited during
34 * searching. If you know few docIDs will be visited, and
35 * the logic behind {@link #match} is relatively costly,
36 * this may be a better way to filter than ChainedFilter.
41 public abstract class FilteredDocIdSet extends DocIdSet {
42 private final DocIdSet _innerSet;
46 * @param innerSet Underlying DocIdSet
48 public FilteredDocIdSet(DocIdSet innerSet) {
52 /** This DocIdSet implementation is cacheable if the inner set is cacheable. */
54 public boolean isCacheable() {
55 return _innerSet.isCacheable();
59 * Validation method to determine whether a docid should be in the result set.
60 * @param docid docid to be tested
61 * @return true if input docid should be in the result set, false otherwise.
63 protected abstract boolean match(int docid) throws IOException;
66 * Implementation of the contract to build a DocIdSetIterator.
67 * @see DocIdSetIterator
68 * @see FilteredDocIdSetIterator
71 public DocIdSetIterator iterator() throws IOException {
72 return new FilteredDocIdSetIterator(_innerSet.iterator()) {
74 protected boolean match(int docid) throws IOException {
75 return FilteredDocIdSet.this.match(docid);