/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* 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 obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: IndexQuery.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/
package org.apache.xindice.core.indexer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.data.Value;
import java.util.Arrays;
/**
* IndexQuery represents the most primitive form of index querying.
* Instances of this object should be created by QueryResolvers and
* cached in Query instances.
*
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class IndexQuery {
/**
* Logger used by this class
*/
private static final Log log = LogFactory.getLog(IndexQuery.class);
// No Operator
public static final int ANY = 0; // Any And All Matches
// Singleton Operators
public static final int EQ = 1; // Equal To
public static final int NEQ = -1; // Not Equal To
public static final int GT = 2; // Greater Than
public static final int LEQ = -2; // Less Than Or Equal To
public static final int LT = 3; // Less Than
public static final int GEQ = -3; // Greater Than Or Equal To
// Range Operators
public static final int BW = 4; // Between (Inclusive)
public static final int NBW = -4; // Not Between (Inclusive)
public static final int BWX = 5; // Between (Exclusive)
public static final int NBWX = -5; // Not Between (Exclusive)
// Set Operators
public static final int IN = 6; // In The Set
public static final int NIN = -6; // Not In The Set
// Other operators
public static final int SW = 7; // Starts-with
public static final int NSW = -7; // Not Starts-with
protected final IndexPattern pattern;
protected final int op;
protected final Value[] vals;
/**
* ANY operator index query
*/
public IndexQuery(IndexPattern pattern) {
this.pattern = pattern;
this.op = ANY;
this.vals = null;
}
/**
* IN operator index query
*/
public IndexQuery(IndexPattern pattern, Value[] vals) {
this(pattern, IN, vals);
}
/**
* Binary operator index query
*/
public IndexQuery(IndexPattern pattern, int op, Value[] vals) {
this.pattern = pattern;
this.op = op;
this.vals = vals;
}
/**
* Unary operator index query
*/
public IndexQuery(IndexPattern pattern, int op, Value val) {
this.pattern = pattern;
this.op = op;
if (op == SW || op == NSW) {
// SW and NSW operators are treated as "between" and "not between"
// where second value is same as first value plus max byte
byte[] b = new byte[val.getLength() + 1];
val.copyTo(b, 0);
b[b.length - 1] = Byte.MAX_VALUE;
Value val2 = new Value(b);
this.vals = new Value[]{ val, val2 };
} else {
this.vals = new Value[]{ val };
}
}
/**
* EQ operator index query
*/
public IndexQuery(IndexPattern pattern, Value val1) {
this(pattern, EQ, val1);
}
/**
* Binary operator index query
*/
public IndexQuery(IndexPattern pattern, int op, Value val1, Value val2) {
this.pattern = pattern;
this.op = op;
this.vals = new Value[]{val1, val2};
}
/**
* IN operator index query
*/
public IndexQuery(IndexPattern pattern, Value val1, Value val2) {
this(pattern, IN, val1, val2);
}
/**
* Unary operator index query
*/
public IndexQuery(IndexPattern pattern, int op, String val1) {
this(pattern, op, new Value(val1));
}
/**
* EQ operator index query
*/
public IndexQuery(IndexPattern pattern, String val1) {
this(pattern, new Value(val1));
}
/**
* Binary operator index query
*/
public IndexQuery(IndexPattern pattern, int op, String val1, String val2) {
this(pattern, op, new Value(val1), new Value(val2));
}
/**
* IN operator index query
*/
public IndexQuery(IndexPattern pattern, String val1, String val2) {
this(pattern, new Value(val1), new Value(val2));
}
/**
* getPattern returns the IndexPattern associated with this query.
*
* @return the IndexPattern
*/
public IndexPattern getPattern() {
return this.pattern;
}
/**
* getOperator returns the operator associated with this query.
*
* @return The operator
*/
public int getOperator() {
return this.op;
}
/**
* getValue returns one of the Values associated with this query.
*
* @param index The Value index
* @return The request Value
*/
public final Value getValue(int index) {
return this.vals[index];
}
/**
* getValues returns the Values associated with this query.
*
* @return The Value set
*/
public Value[] getValues() {
return this.vals;
}
/**
* getLength returns the length of the Value set associated with
* this query.
*
* @return The Value set length
*/
public final int getLength() {
return this.vals.length;
}
/**
* testValue tests the specified value for validity against this
* IndexQuery. The helper classes in org.apache.xindice.core.indexer.helpers
* should be used for optimized performance.
*
* @param value The Value to compare
* @return Whether or not the value matches
*/
public boolean testValue(Value value) {
switch (this.op) {
// No Comparison (Any)
case ANY:
return true;
// Singleton Comparisons
case EQ:
return value.equals(vals[0]);
case NEQ:
return !value.equals(vals[0]);
case GT:
return value.compareTo(vals[0]) > 0;
case LEQ:
return value.compareTo(vals[0]) <= 0;
case LT:
return value.compareTo(vals[0]) < 0;
case GEQ:
return value.compareTo(vals[0]) >= 0;
// Range Comparisons
case BW:
return value.compareTo(vals[0]) >= 0 && value.compareTo(vals[1]) <= 0;
case NBW:
return value.compareTo(vals[0]) <= 0 || value.compareTo(vals[1]) >= 0;
case BWX:
return value.compareTo(vals[0]) > 0 && value.compareTo(vals[1]) < 0;
case NBWX:
return value.compareTo(vals[0]) < 0 || value.compareTo(vals[1]) > 0;
// Set Comparisons
case IN:
case NIN:
return Arrays.binarySearch(vals, value) >= 0 ? op == IN
: op == NIN;
// Other comparisons
case SW:
case NSW:
return value.startsWith(vals[0]) ? op == SW : op == NSW;
default:
if (log.isWarnEnabled()) {
log.warn("invalid operation : " + op);
}
}
return false;
}
/**
* testValue tests the specified value for validity against this
* IndexQuery. The helper classes in org.apache.xindice.core.indexer.helpers
* should be used for optimized performance.
*
* @param value The Value to compare
* @return Whether or not the value matches
*/
public final boolean testValue(String value) {
return testValue(new Value(value));
}
}