Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsonLogicNode>` to control the lifetime of parsed rules. For example, an application using Caffeine can set a size limit at startup:

```java
Cache<String, JsonLogicNode> 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
Expand Down
29 changes: 25 additions & 4 deletions src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JsonLogicNode> parseCache = new ConcurrentHashMap<>();
private final ConcurrentMap<String, JsonLogicNode> parseCache;
private final Map<String, JsonLogicExpression> 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<String, JsonLogicNode> parseCache) {
return new JsonLogic(Objects.requireNonNull(parseCache, "parseCache"));
}

private JsonLogic(ConcurrentMap<String, JsonLogicNode> parseCache) {
this.parseCache = parseCache;

// Add default operations
addOperation(MathExpression.ADD);
addOperation(MathExpression.SUBTRACT);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, JsonLogicNode> 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<String, JsonLogicNode> cache = new ConcurrentHashMap<String, JsonLogicNode>() {
@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);
}
}