Browse Source

#403: Enable path caching withing all the operations

pull/404/head
Ihor Herasymenko 7 years ago
parent
commit
9862f8e56e
  1. 81
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java

81
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.TypeRef;
import com.jayway.jsonpath.spi.cache.Cache; import com.jayway.jsonpath.spi.cache.Cache;
import com.jayway.jsonpath.spi.cache.CacheProvider; import com.jayway.jsonpath.spi.cache.CacheProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -74,21 +75,7 @@ public class JsonContext implements DocumentContext {
@Override @Override
public <T> T read(String path, Predicate... filters) { public <T> T read(String path, Predicate... filters) {
notEmpty(path, "path can not be null or empty"); notEmpty(path, "path can not be null or empty");
Cache cache = CacheProvider.getCache(); return read(pathFromCache(path, filters));
path = path.trim();
LinkedList filterStack = new LinkedList<Predicate>(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);
}
} }
@Override @Override
@ -117,32 +104,33 @@ public class JsonContext implements DocumentContext {
return convert(read(path), type, configuration); return convert(read(path), type, configuration);
} }
public ReadContext limit(int maxResults){ @Override
public ReadContext limit(int maxResults) {
return withListeners(new LimitingEvaluationListener(maxResults)); return withListeners(new LimitingEvaluationListener(maxResults));
} }
public ReadContext withListeners(EvaluationListener... listener){ @Override
public ReadContext withListeners(EvaluationListener... listener) {
return new JsonContext(json, configuration.setEvaluationListeners(listener)); return new JsonContext(json, configuration.setEvaluationListeners(listener));
} }
private <T> T convert(Object obj, Class<T> targetType, Configuration configuration) {
private <T> T convert(Object obj, Class<T> targetType, Configuration configuration){
return configuration.mappingProvider().map(obj, targetType, configuration); return configuration.mappingProvider().map(obj, targetType, configuration);
} }
private <T> T convert(Object obj, TypeRef<T> targetType, Configuration configuration){ private <T> T convert(Object obj, TypeRef<T> targetType, Configuration configuration) {
return configuration.mappingProvider().map(obj, targetType, configuration); return configuration.mappingProvider().map(obj, targetType, configuration);
} }
@Override @Override
public DocumentContext set(String path, Object newValue, Predicate... filters) { public DocumentContext set(String path, Object newValue, Predicate... filters) {
return set(compile(path, filters), newValue); return set(pathFromCache(path, filters), newValue);
} }
@Override @Override
public DocumentContext set(JsonPath path, Object newValue){ public DocumentContext set(JsonPath path, Object newValue) {
List<String> modified = path.set(json, newValue, configuration.addOptions(Option.AS_PATH_LIST)); List<String> modified = path.set(json, newValue, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){ if (logger.isDebugEnabled()) {
for (String p : modified) { for (String p : modified) {
logger.debug("Set path {} new value {}", p, newValue); logger.debug("Set path {} new value {}", p, newValue);
} }
@ -152,7 +140,7 @@ public class JsonContext implements DocumentContext {
@Override @Override
public DocumentContext map(String path, MapFunction mapFunction, Predicate... filters) { public DocumentContext map(String path, MapFunction mapFunction, Predicate... filters) {
map(compile(path, filters), mapFunction); map(pathFromCache(path, filters), mapFunction);
return this; return this;
} }
@ -164,29 +152,29 @@ public class JsonContext implements DocumentContext {
@Override @Override
public DocumentContext delete(String path, Predicate... filters) { public DocumentContext delete(String path, Predicate... filters) {
return delete(compile(path, filters)); return delete(pathFromCache(path, filters));
} }
@Override @Override
public DocumentContext delete(JsonPath path) { public DocumentContext delete(JsonPath path) {
List<String> modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST)); List<String> modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){ if (logger.isDebugEnabled()) {
for (String p : modified) { for (String p : modified) {
logger.debug("Delete path {}"); logger.debug("Delete path {}", p);
} }
} }
return this; return this;
} }
@Override @Override
public DocumentContext add(String path, Object value, Predicate... filters){ public DocumentContext add(String path, Object value, Predicate... filters) {
return add(compile(path, filters), value); return add(pathFromCache(path, filters), value);
} }
@Override @Override
public DocumentContext add(JsonPath path, Object value){ public DocumentContext add(JsonPath path, Object value) {
List<String> modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST)); List<String> modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){ if (logger.isDebugEnabled()) {
for (String p : modified) { for (String p : modified) {
logger.debug("Add path {} new value {}", p, value); logger.debug("Add path {} new value {}", p, value);
} }
@ -195,19 +183,19 @@ public class JsonContext implements DocumentContext {
} }
@Override @Override
public DocumentContext put(String path, String key, Object value, Predicate... filters){ public DocumentContext put(String path, String key, Object value, Predicate... filters) {
return put(compile(path, filters), key, value); return put(pathFromCache(path, filters), key, value);
} }
@Override @Override
public DocumentContext renameKey(String path, String oldKeyName, String newKeyName, Predicate... filters) { 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 @Override
public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) { public DocumentContext renameKey(JsonPath path, String oldKeyName, String newKeyName) {
List<String> modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST)); List<String> modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){ if (logger.isDebugEnabled()) {
for (String p : modified) { for (String p : modified) {
logger.debug("Rename path {} new value {}", p, newKeyName); logger.debug("Rename path {} new value {}", p, newKeyName);
} }
@ -215,11 +203,10 @@ public class JsonContext implements DocumentContext {
return this; return this;
} }
@Override @Override
public DocumentContext put(JsonPath path, String key, Object value){ public DocumentContext put(JsonPath path, String key, Object value) {
List<String> modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST)); List<String> modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){ if (logger.isDebugEnabled()) {
for (String p : modified) { for (String p : modified) {
logger.debug("Put path {} key {} value {}", p, key, value); logger.debug("Put path {} key {} value {}", p, key, value);
} }
@ -227,6 +214,17 @@ public class JsonContext implements DocumentContext {
return this; return this;
} }
private JsonPath pathFromCache(String path, Predicate[] filters) {
Cache cache = CacheProvider.getCache();
String cacheKey = Utils.concat(path, new LinkedList<Predicate>(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 { private final static class LimitingEvaluationListener implements EvaluationListener {
final int limit; final int limit;
@ -236,11 +234,12 @@ public class JsonContext implements DocumentContext {
@Override @Override
public EvaluationContinuation resultFound(FoundResult found) { public EvaluationContinuation resultFound(FoundResult found) {
if(found.index() == limit - 1){ if (found.index() == limit - 1) {
return EvaluationContinuation.ABORT; return EvaluationContinuation.ABORT;
} else { } else {
return EvaluationContinuation.CONTINUE; return EvaluationContinuation.CONTINUE;
} }
} }
} }
} }

Loading…
Cancel
Save