Merge "Reduce BooleanCondition predicate tree before evaluating permissions"

This commit is contained in:
Patrick Hiesel
2018-01-26 08:27:23 +00:00
committed by Gerrit Code Review
5 changed files with 281 additions and 3 deletions

View File

@@ -59,6 +59,40 @@ public abstract class BooleanCondition {
*/
public abstract <T> Iterable<T> children(Class<T> type);
/**
* Reduce evaluation tree by cutting off branches that evaluate trivially and replacing them with
* a leave note corresponding to the value the branch evaluated to.
*
* <p><code>
* Example 1 (T=True, F=False, C=non-trivial check):
* OR
* / \ => T
* C T
* Example 2 (cuts off a not-trivial check):
* AND
* / \ => F
* C F
* Example 3:
* AND
* / \ => F
* T F
* </code>
*
* <p>There is no guarantee that the resulting tree is minimal. The only guarantee made is that
* branches that evaluate trivially will be cut off and replaced by primitive values.
*/
public abstract BooleanCondition reduce();
/**
* Check if the condition evaluates to either {@code true} or {@code false} without providing
* additional information to the evaluation tree, e.g. through checks to a remote service such as
* {@code PermissionBackend}.
*
* <p>In this case, the tree can be reduced to skip all non-trivial checks resulting in a
* performance gain.
*/
protected abstract boolean evaluatesTrivially();
private static final class And extends BooleanCondition {
private final BooleanCondition a;
private final BooleanCondition b;
@@ -70,6 +104,10 @@ public abstract class BooleanCondition {
@Override
public boolean value() {
if (evaluatesTriviallyToExpectedValue(a, false)
|| evaluatesTriviallyToExpectedValue(b, false)) {
return false;
}
return a.value() && b.value();
}
@@ -78,6 +116,14 @@ public abstract class BooleanCondition {
return Iterables.concat(a.children(type), b.children(type));
}
@Override
public BooleanCondition reduce() {
if (evaluatesTrivially()) {
return Value.valueOf(value());
}
return new And(a.reduce(), b.reduce());
}
@Override
public int hashCode() {
return a.hashCode() * 31 + b.hashCode();
@@ -96,6 +142,13 @@ public abstract class BooleanCondition {
public String toString() {
return "(" + maybeTrim(a, getClass()) + " && " + maybeTrim(a, getClass()) + ")";
}
@Override
protected boolean evaluatesTrivially() {
return evaluatesTriviallyToExpectedValue(a, false)
|| evaluatesTriviallyToExpectedValue(b, false)
|| (a.evaluatesTrivially() && b.evaluatesTrivially());
}
}
private static final class Or extends BooleanCondition {
@@ -109,6 +162,10 @@ public abstract class BooleanCondition {
@Override
public boolean value() {
if (evaluatesTriviallyToExpectedValue(a, true)
|| evaluatesTriviallyToExpectedValue(b, true)) {
return true;
}
return a.value() || b.value();
}
@@ -117,6 +174,14 @@ public abstract class BooleanCondition {
return Iterables.concat(a.children(type), b.children(type));
}
@Override
public BooleanCondition reduce() {
if (evaluatesTrivially()) {
return Value.valueOf(value());
}
return new Or(a.reduce(), b.reduce());
}
@Override
public int hashCode() {
return a.hashCode() * 31 + b.hashCode();
@@ -135,6 +200,13 @@ public abstract class BooleanCondition {
public String toString() {
return "(" + maybeTrim(a, getClass()) + " || " + maybeTrim(a, getClass()) + ")";
}
@Override
protected boolean evaluatesTrivially() {
return evaluatesTriviallyToExpectedValue(a, true)
|| evaluatesTriviallyToExpectedValue(b, true)
|| (a.evaluatesTrivially() && b.evaluatesTrivially());
}
}
private static final class Not extends BooleanCondition {
@@ -154,6 +226,14 @@ public abstract class BooleanCondition {
return cond.children(type);
}
@Override
public BooleanCondition reduce() {
if (evaluatesTrivially()) {
return Value.valueOf(value());
}
return this;
}
@Override
public int hashCode() {
return cond.hashCode() * 31;
@@ -168,6 +248,11 @@ public abstract class BooleanCondition {
public String toString() {
return "!" + cond;
}
@Override
protected boolean evaluatesTrivially() {
return cond.evaluatesTrivially();
}
}
private static final class Value extends BooleanCondition {
@@ -187,6 +272,11 @@ public abstract class BooleanCondition {
return Collections.emptyList();
}
@Override
public BooleanCondition reduce() {
return this;
}
@Override
public int hashCode() {
return value ? 1 : 0;
@@ -201,9 +291,17 @@ public abstract class BooleanCondition {
public String toString() {
return Boolean.toString(value);
}
@Override
protected boolean evaluatesTrivially() {
return true;
}
}
/** Remove leading '(' and trailing ')' if the type is the same as the parent. */
/**
* Helper for use in toString methods. Remove leading '(' and trailing ')' if the type is the same
* as the parent.
*/
static String maybeTrim(BooleanCondition cond, Class<? extends BooleanCondition> type) {
String s = cond.toString();
if (cond.getClass() == type
@@ -214,4 +312,9 @@ public abstract class BooleanCondition {
}
return s;
}
private static boolean evaluatesTriviallyToExpectedValue(
BooleanCondition cond, boolean expectedValue) {
return cond.evaluatesTrivially() && (cond.value() == expectedValue);
}
}