Skip to content

#403: Enable path caching within all the operations #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -74,21 +75,7 @@ public String jsonString() {
@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
Expand Down Expand Up @@ -117,32 +104,33 @@ public <T> T read(String path, TypeRef<T> type) {
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);
}
Expand All @@ -152,7 +140,7 @@ public DocumentContext set(JsonPath path, Object newValue){

@Override
public DocumentContext map(String path, MapFunction mapFunction, Predicate... filters) {
map(compile(path, filters), mapFunction);
map(pathFromCache(path, filters), mapFunction);
return this;
}

Expand All @@ -164,29 +152,29 @@ public DocumentContext map(JsonPath path, MapFunction mapFunction) {

@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){
List<String> modified = path.add(json, value, configuration.addOptions(Option.AS_PATH_LIST));
if(logger.isDebugEnabled()){
public DocumentContext add(JsonPath path, Object value) {
List<String> 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);
}
Expand All @@ -195,38 +183,48 @@ public DocumentContext add(JsonPath path, Object value){
}

@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()){
List<String> 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);
}
}
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);
}
}
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;

Expand All @@ -236,11 +234,12 @@ private LimitingEvaluationListener(int limit) {

@Override
public EvaluationContinuation resultFound(FoundResult found) {
if(found.index() == limit - 1){
if (found.index() == limit - 1) {
return EvaluationContinuation.ABORT;
} else {
return EvaluationContinuation.CONTINUE;
}
}
}

}