Remove unnecessary Prolog-to-Java conversion from submit filters

Instead of converting intermediate filtering results from Prolog to Java
(and back) in each filtering iteration, perform the conversion only
after the last submit filter is done.

Also change the return type of the SubmitRuleEvaluator.evaluate to
return ListTerm and let the caller decide how to convert it to a Java
list. This is necessary because the ListTerm.toJava method can produce
either List<Term> or List<String> as its result depending on what is
contained in the ListTerm instance.

Change-Id: I70ca979785c766653281994dd3097fc8795e1a87
Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com>
This commit is contained in:
Sasa Zivkov
2012-10-11 15:47:17 +02:00
committed by Gerrit Code Review
parent 3531ca4b8f
commit 162c801f4b
2 changed files with 7 additions and 10 deletions

View File

@@ -297,6 +297,7 @@ public class ChangeControl {
return canSubmit(db, patchSet, null, false, false); return canSubmit(db, patchSet, null, false, false);
} }
@SuppressWarnings("unchecked")
public List<SubmitRecord> canSubmit(ReviewDb db, PatchSet patchSet, public List<SubmitRecord> canSubmit(ReviewDb db, PatchSet patchSet,
@Nullable ChangeData cd, boolean fastEvalLabels, boolean allowClosed) { @Nullable ChangeData cd, boolean fastEvalLabels, boolean allowClosed) {
if (!allowClosed && change.getStatus().isClosed()) { if (!allowClosed && change.getStatus().isClosed()) {
@@ -337,7 +338,7 @@ public class ChangeControl {
fastEvalLabels, fastEvalLabels,
"locate_submit_rule", "can_submit", "locate_submit_rule", "can_submit",
"locate_submit_filter", "filter_submit_results"); "locate_submit_filter", "filter_submit_results");
results = evaluator.evaluate(); results = evaluator.evaluate().toJava();
} catch (RuleEvalException e) { } catch (RuleEvalException e) {
return logRuleError(e.getMessage(), e); return logRuleError(e.getMessage(), e);
} }

View File

@@ -96,7 +96,7 @@ public class SubmitRuleEvaluator {
* @return List of {@link Term} objects returned from the evaluated rules. * @return List of {@link Term} objects returned from the evaluated rules.
* @throws RuleEvalException * @throws RuleEvalException
*/ */
List<Term> evaluate() throws RuleEvalException { ListTerm evaluate() throws RuleEvalException {
List<Term> results = new ArrayList<Term>(); List<Term> results = new ArrayList<Term>();
ProjectState projectState = projectControl.getProjectState(); ProjectState projectState = projectControl.getProjectState();
PrologEnvironment env; PrologEnvironment env;
@@ -140,6 +140,7 @@ public class SubmitRuleEvaluator {
Set<Project.NameKey> projectsSeen = new HashSet<Project.NameKey>(); Set<Project.NameKey> projectsSeen = new HashSet<Project.NameKey>();
projectsSeen.add(projectState.getProject().getNameKey()); projectsSeen.add(projectState.getProject().getNameKey());
Term resultsTerm = toListTerm(results);
while (parentState != null) { while (parentState != null) {
if (!projectsSeen.add(parentState.getProject().getNameKey())) { if (!projectsSeen.add(parentState.getProject().getNameKey())) {
// parent has been seen before, stop walk up inheritance tree // parent has been seen before, stop walk up inheritance tree
@@ -161,15 +162,10 @@ public class SubmitRuleEvaluator {
env.once("gerrit", "assume_range_from_label"); env.once("gerrit", "assume_range_from_label");
} }
Term resultsTerm = toListTerm(results);
results.clear();
Term[] template = Term[] template =
parentEnv.once("gerrit", filterRuleWrapperName, filterRule, parentEnv.once("gerrit", filterRuleWrapperName, filterRule,
resultsTerm, new VariableTerm()); resultsTerm, new VariableTerm());
@SuppressWarnings("unchecked") resultsTerm = template[2];
final List<? extends Term> termList =
((ListTerm) template[2]).toJava();
results.addAll(termList);
} catch (PrologException err) { } catch (PrologException err) {
throw new RuleEvalException("Exception calling " + filterRule throw new RuleEvalException("Exception calling " + filterRule
+ " on change " + change.getId() + " of " + " on change " + change.getId() + " of "
@@ -183,11 +179,11 @@ public class SubmitRuleEvaluator {
parentState = parentState.getParentState(); parentState = parentState.getParentState();
childEnv = parentEnv; childEnv = parentEnv;
} }
return (ListTerm) resultsTerm;
} finally { } finally {
env.close(); env.close();
} }
return results;
} }
private static Term toListTerm(List<Term> terms) { private static Term toListTerm(List<Term> terms) {