LUCENE-1504 -- SerialChainFilter should use DocSet API

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@732031 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2009-01-06 18:13:06 +00:00
parent 873fbbc1f1
commit 43609e891d
1 changed files with 209 additions and 199 deletions

View File

@ -1,199 +1,209 @@
/** /**
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.lucene.spatial; package org.apache.lucene.spatial;
import java.io.IOException; import java.io.IOException;
import java.util.BitSet; import java.util.BitSet;
import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
/** import org.apache.lucene.util.DocIdBitSet;
*
* Provide a serial chain filter, passing the bitset in with the /**
* index reader to each of the filters in an ordered fashion. *
* * Provide a serial chain filter, passing the bitset in with the
* Based off chain filter, but will some improvements to allow a narrowed down * index reader to each of the filters in an ordered fashion.
* filtering. Traditional filter required iteration through an IndexReader. *
* * Based off chain filter, but with some improvements to allow a narrowed down
* By implementing the ISerialChainFilter class, you can create a bits(IndexReader reader, BitSet bits) * filtering. Traditional filter required iteration through an IndexReader.
* @see org.apache.lucene.spatial.ISerialChainFilter *
* * By implementing the ISerialChainFilter class, you can create a bits(IndexReader reader, BitSet bits)
*/ * @see org.apache.lucene.search.ISerialChainFilter
public class SerialChainFilter extends Filter { *
*/
/** public class SerialChainFilter extends Filter {
* $Id: SerialChainFilter.java 136 2008-12-17 16:16:38Z ryantxu $
*/ /**
private static final long serialVersionUID = 1L; * $Id: SerialChainFilter.java 136 2008-12-17 16:16:38Z ryantxu $
private Filter chain[]; */
public static final int SERIALAND = 1; private static final long serialVersionUID = 1L;
public static final int SERIALOR = 2; private Filter chain[];
public static final int AND = 3; // regular filters may be used first public static final int SERIALAND = 1;
public static final int OR = 4; // regular filters may be used first public static final int SERIALOR = 2;
public static final int DEFAULT = SERIALOR; public static final int AND = 3; // regular filters may be used first
public static final int OR = 4; // regular filters may be used first
private int actionType[]; public static final int DEFAULT = SERIALOR;
public SerialChainFilter(Filter chain[]){ private int actionType[];
this.chain = chain;
this.actionType = new int[] {DEFAULT}; public SerialChainFilter(Filter chain[]){
} this.chain = chain;
this.actionType = new int[] {DEFAULT};
public SerialChainFilter(Filter chain[], int actionType[]){ }
this.chain= chain;
this.actionType = actionType; public SerialChainFilter(Filter chain[], int actionType[]){
} this.chain= chain;
this.actionType = actionType;
/* (non-Javadoc) }
* @see org.apache.lucene.search.Filter#bits(org.apache.lucene.index.IndexReader)
*/ /* (non-Javadoc)
@Override * @see org.apache.lucene.search.Filter#bits(org.apache.lucene.index.IndexReader)
public BitSet bits(IndexReader reader) throws CorruptIndexException, IOException { */
@Override
BitSet bits = new BitSet(reader.maxDoc()); public BitSet bits(IndexReader reader) throws IOException {
int chainSize = chain.length; return ((DocIdBitSet)getDocIdSet(reader)).getBitSet();
int actionSize = actionType.length; }
int i = 0;
/** /* (non-Javadoc)
* taken from ChainedFilter, first and on an empty bitset results in 0 * @see org.apache.lucene.search.Filter#getDocIdSet(org.apache.lucene.index.IndexReader)
*/ */
if (actionType[i] == AND){ @Override
try { public DocIdSet getDocIdSet(IndexReader reader) throws CorruptIndexException, IOException {
bits = (BitSet) chain[i].bits(reader).clone();
} catch (IOException e) { BitSet bits = new BitSet(reader.maxDoc());
// TODO Auto-generated catch block int chainSize = chain.length;
e.printStackTrace(); int actionSize = actionType.length;
} int i = 0;
++i;
} /**
* taken from ChainedFilter, first and on an empty bitset results in 0
for( ; i < chainSize; i++) { */
if (actionType[i] == AND){
int action = (i < actionSize)? actionType[i]: DEFAULT; try {
bits = (BitSet) ((DocIdBitSet)chain[i].getDocIdSet(reader)).getBitSet().clone();
switch (action){ } catch (IOException e) {
// TODO Auto-generated catch block
case (SERIALAND): e.printStackTrace();
try { }
bits.and(((ISerialChainFilter) chain[i]).bits(reader, bits)); ++i;
} catch (CorruptIndexException e) { }
// TODO Auto-generated catch block
e.printStackTrace(); for( ; i < chainSize; i++) {
} catch (IOException e) {
// TODO Auto-generated catch block int action = (i < actionSize)? actionType[i]: DEFAULT;
e.printStackTrace();
} catch (Exception e) { switch (action){
// TODO Auto-generated catch block
e.printStackTrace(); case (SERIALAND):
} try {
break; bits.and(((ISerialChainFilter) chain[i]).bits(reader, bits));
case (SERIALOR): } catch (CorruptIndexException e) {
try { // TODO Auto-generated catch block
bits.or(((ISerialChainFilter) chain[i]).bits(reader,bits)); e.printStackTrace();
} catch (Exception e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} } catch (Exception e) {
break; // TODO Auto-generated catch block
case (AND): e.printStackTrace();
bits.and(chain[i].bits(reader)); }
break; break;
case (OR): case (SERIALOR):
bits.and(chain[i].bits(reader)); try {
break; bits.or(((ISerialChainFilter) chain[i]).bits(reader,bits));
} } catch (Exception e) {
// TODO Auto-generated catch block
} e.printStackTrace();
return bits; }
} break;
case (AND):
/** bits.and(((DocIdBitSet)chain[i].getDocIdSet(reader)).getBitSet());
* @return the chain break;
*/ case (OR):
Filter[] getChain() { bits.or(((DocIdBitSet)chain[i].getDocIdSet(reader)).getBitSet());
return chain; break;
} }
}
/** return new DocIdBitSet(bits);
* @return the actionType }
*/
int[] getActionType() { /**
return actionType; * @return the chain
} */
Filter[] getChain() {
/** return chain;
* Returns true if <code>o</code> is equal to this. }
*
* @see org.apache.lucene.search.RangeFilter#equals /**
*/ * @return the actionType
@Override */
public boolean equals(Object o) { int[] getActionType() {
if (this == o) return true; return actionType;
if (!(o instanceof SerialChainFilter)) return false; }
SerialChainFilter other = (SerialChainFilter) o;
/**
if (this.chain.length != other.getChain().length || * Returns true if <code>o</code> is equal to this.
this.actionType.length != other.getActionType().length) *
return false; * @see org.apache.lucene.search.RangeFilter#equals
*/
for (int i = 0; i < this.chain.length; i++) { @Override
if (this.actionType[i] != other.getActionType()[i] || public boolean equals(Object o) {
(!this.chain[i].equals(other.getChain()[i]))) if (this == o) return true;
return false; if (!(o instanceof SerialChainFilter)) return false;
} SerialChainFilter other = (SerialChainFilter) o;
return true;
} if (this.chain.length != other.getChain().length ||
this.actionType.length != other.getActionType().length)
/** return false;
* Returns a hash code value for this object.
* for (int i = 0; i < this.chain.length; i++) {
* @see org.apache.lucene.search.RangeFilter#hashCode if (this.actionType[i] != other.getActionType()[i] ||
*/ (!this.chain[i].equals(other.getChain()[i])))
@Override return false;
public int hashCode() { }
if (chain.length == 0) return true;
return 0; }
int h = chain[0].hashCode() ^ new Integer(actionType[0]).hashCode(); /**
for (int i = 1; i < this.chain.length; i++) { * Returns a hash code value for this object.
h ^= chain[i].hashCode(); *
h ^= new Integer(actionType[i]).hashCode(); * @see org.apache.lucene.search.RangeFilter#hashCode
} */
return h; @Override
} public int hashCode() {
if (chain.length == 0)
@Override return 0;
public String toString() {
StringBuffer buf = new StringBuffer(); int h = chain[0].hashCode() ^ new Integer(actionType[0]).hashCode();
buf.append("SerialChainFilter("); for (int i = 1; i < this.chain.length; i++) {
for (int i = 0; i < chain.length; i++) { h ^= chain[i].hashCode();
switch(actionType[i]) { h ^= new Integer(actionType[i]).hashCode();
case (SERIALAND): buf.append("SERIALAND"); break; }
case (SERIALOR): buf.append("SERIALOR"); break; return h;
case (AND): buf.append("AND"); break; }
case (OR): buf.append("OR"); break;
default: buf.append(actionType[i]); @Override
} public String toString() {
buf.append(" " + chain[i].toString() + " "); StringBuffer buf = new StringBuffer();
} buf.append("SerialChainFilter(");
return buf.toString().trim() + ")"; for (int i = 0; i < chain.length; i++) {
} switch(actionType[i]) {
} case (SERIALAND): buf.append("SERIALAND"); break;
case (SERIALOR): buf.append("SERIALOR"); break;
case (AND): buf.append("AND"); break;
case (OR): buf.append("OR"); break;
default: buf.append(actionType[i]);
}
buf.append(" " + chain[i].toString() + " ");
}
return buf.toString().trim() + ")";
}
}