From 9862f8e56ed24498f778b9a96f6f27763bc04d34 Mon Sep 17 00:00:00 2001 From: Ihor Herasymenko Date: Thu, 26 Oct 2017 00:43:59 +0300 Subject: [PATCH] #403: Enable path caching withing all the operations --- .../jayway/jsonpath/internal/JsonContext.java | 81 +++++++++---------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java index e16e33d3..73147334 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java @@ -25,6 +25,7 @@ import com.jayway.jsonpath.ReadContext; import com.jayway.jsonpath.TypeRef; import com.jayway.jsonpath.spi.cache.Cache; import com.jayway.jsonpath.spi.cache.CacheProvider; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,21 +75,7 @@ public class JsonContext implements DocumentContext { @Override public T read(String path, Predicate... filters) { notEmpty(path, "path can not be null or empty"); - Cache cache = CacheProvider.getCache(); - - path = path.trim(); - LinkedList filterStack = new LinkedList(asList(filters)); - String cacheKey = Utils.concat(path, filterStack.toString()); - - JsonPath jsonPath = cache.get(cacheKey); - if(jsonPath != null){ - return read(jsonPath); - } else { - jsonPath = compile(path, filters); - cache.put(cacheKey, jsonPath); - return read(jsonPath); - } - + return read(pathFromCache(path, filters)); } @Override @@ -117,32 +104,33 @@ public class JsonContext implements DocumentContext { return convert(read(path), type, configuration); } - public ReadContext limit(int maxResults){ + @Override + public ReadContext limit(int maxResults) { return withListeners(new LimitingEvaluationListener(maxResults)); } - public ReadContext withListeners(EvaluationListener... listener){ + @Override + public ReadContext withListeners(EvaluationListener... listener) { return new JsonContext(json, configuration.setEvaluationListeners(listener)); } - - private T convert(Object obj, Class targetType, Configuration configuration){ + private T convert(Object obj, Class targetType, Configuration configuration) { return configuration.mappingProvider().map(obj, targetType, configuration); } - private T convert(Object obj, TypeRef targetType, Configuration configuration){ + private T convert(Object obj, TypeRef targetType, Configuration configuration) { return configuration.mappingProvider().map(obj, targetType, configuration); } @Override public DocumentContext set(String path, Object newValue, Predicate... filters) { - return set(compile(path, filters), newValue); + return set(pathFromCache(path, filters), newValue); } @Override - public DocumentContext set(JsonPath path, Object newValue){ + public DocumentContext set(JsonPath path, Object newValue) { List modified = path.set(json, newValue, configuration.addOptions(Option.AS_PATH_LIST)); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Set path {} new value {}", p, newValue); } @@ -152,7 +140,7 @@ public class JsonContext implements DocumentContext { @Override public DocumentContext map(String path, MapFunction mapFunction, Predicate... filters) { - map(compile(path, filters), mapFunction); + map(pathFromCache(path, filters), mapFunction); return this; } @@ -164,29 +152,29 @@ public class JsonContext implements DocumentContext { @Override public DocumentContext delete(String path, Predicate... filters) { - return delete(compile(path, filters)); + return delete(pathFromCache(path, filters)); } @Override public DocumentContext delete(JsonPath path) { List modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST)); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { for (String p : modified) { - logger.debug("Delete path {}"); + logger.debug("Delete path {}", p); } } return this; } @Override - public DocumentContext add(String path, Object value, Predicate... filters){ - return add(compile(path, filters), value); + public DocumentContext add(String path, Object value, Predicate... filters) { + return add(pathFromCache(path, filters), value); } @Override - public DocumentContext add(JsonPath path, Object value){ - List modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST)); - if(logger.isDebugEnabled()){ + public DocumentContext add(JsonPath path, Object value) { + List modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST)); + if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Add path {} new value {}", p, value); } @@ -195,19 +183,19 @@ public class JsonContext implements DocumentContext { } @Override - public DocumentContext put(String path, String key, Object value, Predicate... filters){ - return put(compile(path, filters), key, value); + public DocumentContext put(String path, String key, Object value, Predicate... filters) { + return put(pathFromCache(path, filters), key, value); } @Override public DocumentContext renameKey(String path, String oldKeyName, String newKeyName, Predicate... filters) { - return renameKey(compile(path, filters), oldKeyName, newKeyName); + return renameKey(pathFromCache(path, filters), oldKeyName, newKeyName); } @Override public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) { - List modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST)); - if(logger.isDebugEnabled()){ + List modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST)); + if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Rename path {} new value {}", p, newKeyName); } @@ -215,11 +203,10 @@ public class JsonContext implements DocumentContext { return this; } - @Override - public DocumentContext put(JsonPath path, String key, Object value){ + public DocumentContext put(JsonPath path, String key, Object value) { List modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST)); - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { for (String p : modified) { logger.debug("Put path {} key {} value {}", p, key, value); } @@ -227,6 +214,17 @@ public class JsonContext implements DocumentContext { return this; } + private JsonPath pathFromCache(String path, Predicate[] filters) { + Cache cache = CacheProvider.getCache(); + String cacheKey = Utils.concat(path, new LinkedList(asList(filters)).toString()); + JsonPath jsonPath = cache.get(cacheKey); + if (jsonPath == null) { + jsonPath = compile(path, filters); + cache.put(cacheKey, jsonPath); + } + return jsonPath; + } + private final static class LimitingEvaluationListener implements EvaluationListener { final int limit; @@ -236,11 +234,12 @@ public class JsonContext implements DocumentContext { @Override public EvaluationContinuation resultFound(FoundResult found) { - if(found.index() == limit - 1){ + if (found.index() == limit - 1) { return EvaluationContinuation.ABORT; } else { return EvaluationContinuation.CONTINUE; } } } + }