From 63fedf8cb6c25365f759c1c7e6e5870913944b21 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Thu, 15 Feb 2024 13:37:43 +0000 Subject: [PATCH] Prevent concurrent modification of equality index --- ...EqualityExpressionIndexFilterStrategy.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/javarosa/core/model/EqualityExpressionIndexFilterStrategy.java b/src/main/java/org/javarosa/core/model/EqualityExpressionIndexFilterStrategy.java index 57ab68c54..4ceeed482 100644 --- a/src/main/java/org/javarosa/core/model/EqualityExpressionIndexFilterStrategy.java +++ b/src/main/java/org/javarosa/core/model/EqualityExpressionIndexFilterStrategy.java @@ -38,9 +38,7 @@ public List filter(@NotNull DataInstance sourceInstance, @NotNull XPathEqExpr original = (XPathEqExpr) candidate.getOriginal(); if (original.isEqual()) { String section = nodeSet + candidate.getNodeSide().toString(); - if (!index.contains(section)) { - buildIndex(sourceInstance, candidate, children, evaluationContext, section); - } + buildIndexIfNeeded(sourceInstance, candidate, children, evaluationContext, section); Object absoluteValue = candidate.evalContextSide(sourceInstance, evaluationContext); return index.lookup(section, absoluteValue.toString()); @@ -52,13 +50,18 @@ public List filter(@NotNull DataInstance sourceInstance, @NotNull } } - private void buildIndex(DataInstance sourceInstance, CompareToNodeExpression predicate, List children, EvaluationContext evaluationContext, String section) { - for (int i = 0; i < children.size(); i++) { - TreeReference child = children.get(i); - - Measure.log("IndexEvaluation"); - String relativeValue = predicate.evalNodeSide(sourceInstance, evaluationContext, child, i).toString(); - index.add(section, relativeValue, child); + /** + * Synchronized to prevent two or more threads from modifying the index at once + */ + private synchronized void buildIndexIfNeeded(DataInstance sourceInstance, CompareToNodeExpression predicate, List children, EvaluationContext evaluationContext, String section) { + if (!index.contains(section)) { + for (int i = 0; i < children.size(); i++) { + TreeReference child = children.get(i); + + Measure.log("IndexEvaluation"); + String relativeValue = predicate.evalNodeSide(sourceInstance, evaluationContext, child, i).toString(); + index.add(section, relativeValue, child); + } } }