Skip to content

Commit

Permalink
Prevent concurrent modification of equality index
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Feb 15, 2024
1 parent 65c723a commit 63fedf8
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public List<TreeReference> 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());
Expand All @@ -52,13 +50,18 @@ public List<TreeReference> filter(@NotNull DataInstance sourceInstance, @NotNull
}
}

private void buildIndex(DataInstance sourceInstance, CompareToNodeExpression predicate, List<TreeReference> 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<TreeReference> 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);
}
}
}

Expand Down

0 comments on commit 63fedf8

Please sign in to comment.