Browse Source

Merge pull request #404 from iherasymenko/issue403-caching

#403: Enable path caching within all the operations
pull/422/head
kallestenflo 7 years ago committed by GitHub
parent
commit
6c738c9aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 77
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java

77
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> 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<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);
}
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> 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);
}
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);
}
@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<String> 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<String> 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){
public DocumentContext add(JsonPath path, Object value) {
List<String> modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){
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<String> modified = path.renameKey(json, oldKeyName, newKeyName, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){
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<String> 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<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 {
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;
}
}
}
}

Loading…
Cancel
Save