Browse Source

#403: Enable path caching withing all the operations

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

47
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,15 +104,16 @@ public class JsonContext implements DocumentContext {
return convert(read(path), type, configuration);
}
@Override
public ReadContext limit(int maxResults) {
return withListeners(new LimitingEvaluationListener(maxResults));
}
@Override
public ReadContext withListeners(EvaluationListener... listener) {
return new JsonContext(json, configuration.setEvaluationListeners(listener));
}
private <T> T convert(Object obj, Class<T> targetType, Configuration configuration) {
return configuration.mappingProvider().map(obj, targetType, configuration);
}
@ -136,7 +124,7 @@ public class JsonContext implements DocumentContext {
@Override
public DocumentContext set(String path, Object newValue, Predicate... filters) {
return set(compile(path, filters), newValue);
return set(pathFromCache(path, filters), newValue);
}
@Override
@ -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,7 +152,7 @@ public class JsonContext implements DocumentContext {
@Override
public DocumentContext delete(String path, Predicate... filters) {
return delete(compile(path, filters));
return delete(pathFromCache(path, filters));
}
@Override
@ -172,7 +160,7 @@ public class JsonContext implements DocumentContext {
List<String> modified = path.delete(json, configuration.addOptions(Option.AS_PATH_LIST));
if (logger.isDebugEnabled()) {
for (String p : modified) {
logger.debug("Delete path {}");
logger.debug("Delete path {}", p);
}
}
return this;
@ -180,7 +168,7 @@ public class JsonContext implements DocumentContext {
@Override
public DocumentContext add(String path, Object value, Predicate... filters) {
return add(compile(path, filters), value);
return add(pathFromCache(path, filters), value);
}
@Override
@ -196,12 +184,12 @@ public class JsonContext implements DocumentContext {
@Override
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
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
@ -215,7 +203,6 @@ public class JsonContext implements DocumentContext {
return this;
}
@Override
public DocumentContext put(JsonPath path, String key, Object value) {
List<String> modified = path.put(json, key, value, configuration.addOptions(Option.AS_PATH_LIST));
@ -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;
@ -243,4 +241,5 @@ public class JsonContext implements DocumentContext {
}
}
}
}

Loading…
Cancel
Save