Skip to content

Commit

Permalink
Prevent concurrent modification in the comparison cache
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Feb 15, 2024
1 parent 63fedf8 commit 4ea68b0
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.javarosa.xpath.expr.XPathExpression;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

/**
Expand All @@ -21,7 +21,7 @@
*/
public class ComparisonExpressionCacheFilterStrategy implements FilterStrategy {

private final Map<String, List<TreeReference>> cachedEvaluations = new HashMap<>();
private final Map<String, List<TreeReference>> cachedEvaluations = new ConcurrentHashMap<>();

@NotNull
@Override
Expand Down Expand Up @@ -56,7 +56,10 @@ public List<TreeReference> filter(@NotNull DataInstance sourceInstance, @NotNull
}
}

private List<TreeReference> getCachedEvaluations(@NotNull Supplier<List<TreeReference>> next, String key) {
/**
* Synchronized to prevent two or more threads from modifying {@link #cachedEvaluations} at once
*/
private synchronized List<TreeReference> getCachedEvaluations(@NotNull Supplier<List<TreeReference>> next, String key) {
if (cachedEvaluations.containsKey(key)) {
return cachedEvaluations.get(key);
} else {
Expand Down

0 comments on commit 4ea68b0

Please sign in to comment.