diff --git a/README.md b/README.md index cb92eb5..3981d88 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,15 @@ double result = (double) jsonLogic.apply(expression, data); assert result == 20.0; ``` +When building from source, you can supply a dedicated `ConcurrentMap` to control the lifetime of parsed rules. For example, an application using Caffeine can set a size limit at startup: + +```java +Cache cache = Caffeine.newBuilder().maximumSize(1_000).build(); +JsonLogic jsonLogic = JsonLogic.withParseCache(cache.asMap()); +``` + +The default constructor uses an unbounded cache. The supplied cache stores parsed rules, and each call to `apply` evaluates the rule against the current data. Concurrent misses can parse the same rule more than once. With Caffeine 2.x, map lookups bypass the cache's hit and miss counters. + You can add your own operations like so: ```java diff --git a/src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java b/src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java index fa25f2f..d070370 100644 --- a/src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java +++ b/src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java @@ -9,14 +9,32 @@ import java.lang.reflect.Array; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.function.Function; public final class JsonLogic { - private final Map parseCache = new ConcurrentHashMap<>(); + private final ConcurrentMap parseCache; private final Map expressions = new ConcurrentHashMap<>(); private JsonLogicEvaluator evaluator; public JsonLogic() { + this(new ConcurrentHashMap<>()); + } + + /** + * Creates an evaluator using a caller-owned cache keyed by the original rule text. + * The caller may evict entries at any time. + * + * @param parseCache a concurrent map dedicated to parsed JsonLogic rules + * @return an evaluator backed by the supplied map + */ + public static JsonLogic withParseCache(ConcurrentMap parseCache) { + return new JsonLogic(Objects.requireNonNull(parseCache, "parseCache")); + } + + private JsonLogic(ConcurrentMap parseCache) { + this.parseCache = parseCache; + // Add default operations addOperation(MathExpression.ADD); addOperation(MathExpression.SUBTRACT); @@ -76,15 +94,18 @@ public JsonLogic addOperation(JsonLogicExpression expression) { } public Object apply(String json, Object data) throws JsonLogicException { - if (!parseCache.containsKey(json)) { - parseCache.put(json, JsonLogicParser.parse(json)); + JsonLogicNode rule = parseCache.get(json); + if (rule == null) { + JsonLogicNode parsed = JsonLogicParser.parse(json); + JsonLogicNode existing = parseCache.putIfAbsent(json, parsed); + rule = existing == null ? parsed : existing; } if (evaluator == null) { evaluator = new JsonLogicEvaluator(expressions); } - return evaluator.evaluate(parseCache.get(json), data, "$"); + return evaluator.evaluate(rule, data, "$"); } public static boolean truthy(Object value) { diff --git a/src/test/java/io/github/jamsesso/jsonlogic/JsonLogicCacheTests.java b/src/test/java/io/github/jamsesso/jsonlogic/JsonLogicCacheTests.java new file mode 100644 index 0000000..d3cff3a --- /dev/null +++ b/src/test/java/io/github/jamsesso/jsonlogic/JsonLogicCacheTests.java @@ -0,0 +1,48 @@ +package io.github.jamsesso.jsonlogic; + +import io.github.jamsesso.jsonlogic.ast.JsonLogicNode; +import org.junit.Test; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import static org.junit.Assert.*; + +public class JsonLogicCacheTests { + @Test + public void reusesParsedRulesUntilEvictedByCaller() throws JsonLogicException { + ConcurrentMap cache = new ConcurrentHashMap<>(); + JsonLogic logic = JsonLogic.withParseCache(cache); + String rule = "{\"var\":\"\"}"; + + assertEquals("first", logic.apply(rule, "first")); + JsonLogicNode parsed = cache.get(rule); + assertNotNull(parsed); + assertEquals("second", logic.apply(rule, "second")); + assertSame(parsed, cache.remove(rule)); + + assertEquals("third", logic.apply(rule, "third")); + assertNotNull(cache.get(rule)); + assertNotSame(parsed, cache.get(rule)); + } + + @Test + public void evaluatesEvenIfCacheEvictsImmediately() throws JsonLogicException { + ConcurrentMap cache = new ConcurrentHashMap() { + @Override + public JsonLogicNode putIfAbsent(String key, JsonLogicNode rule) { + JsonLogicNode existing = super.putIfAbsent(key, rule); + remove(key); + return existing; + } + }; + + assertEquals(true, JsonLogic.withParseCache(cache).apply("true", null)); + assertTrue(cache.isEmpty()); + } + + @Test(expected = NullPointerException.class) + public void rejectsNullCache() { + JsonLogic.withParseCache(null); + } +}