AMQ-9052: Selectors - improve perfomance of Equals and Not

This commit is contained in:
Enrico Olivelli 2022-08-17 10:42:37 +02:00
parent 2c193ea01d
commit a978eb5115
3 changed files with 81 additions and 41 deletions

View File

@ -214,36 +214,66 @@ public abstract class ComparisonExpression extends BinaryExpression implements B
@SuppressWarnings({ "rawtypes" })
private static BooleanExpression doCreateEqual(Expression left, Expression right) {
return new ComparisonExpression(left, right) {
return new EqualsExpression(left, right);
}
public Object evaluate(MessageEvaluationContext message) throws JMSException {
Object lv = left.evaluate(message);
Object rv = right.evaluate(message);
private static class EqualsExpression extends ComparisonExpression {
EqualsExpression(Expression left, Expression right) {
super(left, right);
}
// If one of the values is null
if (lv == null ^ rv == null) {
if (lv == null) {
return null;
}
return Boolean.FALSE;
}
if (lv == rv || lv.equals(rv)) {
return Boolean.TRUE;
}
if (lv instanceof Comparable && rv instanceof Comparable) {
return compare((Comparable)lv, (Comparable)rv);
public Object evaluate(MessageEvaluationContext message) throws JMSException {
Object lv = left.evaluate(message);
Object rv = right.evaluate(message);
// If one of the values is null
if (lv == null ^ rv == null) {
if (lv == null) {
return null;
}
return Boolean.FALSE;
}
protected boolean asBoolean(int answer) {
return answer == 0;
if (lv == rv || lv.equals(rv)) {
return Boolean.TRUE;
}
public String getExpressionSymbol() {
return "=";
if (lv instanceof Comparable && rv instanceof Comparable) {
return compare((Comparable) lv, (Comparable) rv);
}
};
return Boolean.FALSE;
}
@Override
public boolean matches(MessageEvaluationContext message) throws JMSException {
Object lv = left.evaluate(message);
Object rv = right.evaluate(message);
// If one of the values is null
if (lv == null ^ rv == null) {
return false;
}
if (lv == rv || lv.equals(rv)) {
return true;
}
if (lv.getClass() == rv.getClass()) {
// same class, but 'equals' return false, and they are not the same object
// there is no point in doing 'compare'
// this case happens often while comparing non equals Strings
return false;
}
if (lv instanceof Comparable && rv instanceof Comparable) {
Boolean compareResult = compare((Comparable) lv, (Comparable) rv);
return compareResult != null && compareResult;
}
return false;
}
protected boolean asBoolean(int answer) {
return answer == 0;
}
public String getExpressionSymbol() {
return "=";
}
}
public static BooleanExpression createGreaterThan(final Expression left, final Expression right) {
@ -286,7 +316,6 @@ public abstract class ComparisonExpression extends BinaryExpression implements B
public String getExpressionSymbol() {
return "<";
}
};
}
@ -491,5 +520,4 @@ public abstract class ComparisonExpression extends BinaryExpression implements B
Object object = evaluate(message);
return object != null && object == Boolean.TRUE;
}
}

View File

@ -141,7 +141,6 @@ public abstract class LogicExpression implements BooleanExpression {
public String getExpressionSymbol() {
return "OR";
}
}
private static class ANDExpression extends LogicExpression {

View File

@ -86,7 +86,6 @@ public abstract class UnaryExpression implements Expression {
} else {
return Boolean.FALSE;
}
}
public String toString() {
@ -132,19 +131,7 @@ public abstract class UnaryExpression implements Expression {
};
public static BooleanExpression createNOT(BooleanExpression left) {
return new BooleanUnaryExpression(left) {
public Object evaluate(MessageEvaluationContext message) throws JMSException {
Boolean lvalue = (Boolean)right.evaluate(message);
if (lvalue == null) {
return null;
}
return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}
public String getExpressionSymbol() {
return "NOT";
}
};
return new NotExpression(left);
}
public static BooleanExpression createXPath(final String xpath) {
@ -243,7 +230,6 @@ public abstract class UnaryExpression implements Expression {
return false;
}
return toString().equals(o.toString());
}
/**
@ -254,4 +240,31 @@ public abstract class UnaryExpression implements Expression {
*/
public abstract String getExpressionSymbol();
private static class NotExpression extends BooleanUnaryExpression {
public NotExpression(BooleanExpression right) {
super(right);
}
public Object evaluate(MessageEvaluationContext message) throws JMSException {
Boolean lvalue = (Boolean) right.evaluate(message);
if (lvalue == null) {
return null;
}
return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}
@Override
public boolean matches(MessageEvaluationContext message) throws JMSException {
Boolean lvalue = (Boolean) right.evaluate(message);
if (lvalue == null) {
// NOT NULL returns NULL that eventually fails the selector
return false;
}
return !lvalue;
}
public String getExpressionSymbol() {
return "NOT";
}
}
}