Browse Source

Minor cache fixes.

pull/149/head
Kalle Stenflo 9 years ago
parent
commit
179a818418
  1. 41
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  2. 1
      json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
  3. 17
      json-path/src/main/java/com/jayway/jsonpath/internal/DefaultsImpl.java
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  5. 1
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java
  6. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/function/FunctionFactory.java
  7. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java
  8. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  9. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java
  10. 23
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/Cache.java
  11. 32
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java
  12. 18
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/LRUCache.java
  13. 14
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/NOOPCache.java

41
json-path/src/main/java/com/jayway/jsonpath/Configuration.java

@ -15,8 +15,6 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.DefaultsImpl;
import com.jayway.jsonpath.spi.cache.CacheProvider;
import com.jayway.jsonpath.spi.cache.DefaultCache;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
@ -57,16 +55,14 @@ public class Configuration {
private final MappingProvider mappingProvider;
private final Set<Option> options;
private final Collection<EvaluationListener> evaluationListeners;
private final CacheProvider cacheProvider;
private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider, CacheProvider cacheProvider, EnumSet<Option> options, Collection<EvaluationListener> evaluationListeners) {
private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider, EnumSet<Option> options, Collection<EvaluationListener> evaluationListeners) {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(mappingProvider, "mappingProvider can not be null");
notNull(options, "setOptions can not be null");
notNull(evaluationListeners, "evaluationListeners can not be null");
this.jsonProvider = jsonProvider;
this.mappingProvider = mappingProvider;
this.cacheProvider = cacheProvider;
this.options = Collections.unmodifiableSet(options);
this.evaluationListeners = Collections.unmodifiableCollection(evaluationListeners);
}
@ -131,23 +127,6 @@ public class Configuration {
return mappingProvider;
}
/**
* Creates a new Configuration based on the given {@link com.jayway.jsonpath.spi.cache.CacheProvider}
* @param newCacheProvider cache provider to use in new configuration
* @return a new configuration
*/
public Configuration CacheProvider(CacheProvider newCacheProvider) {
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).cacheProvider(newCacheProvider).options(options).evaluationListener(evaluationListeners).build();
}
/**
* Returns {@link com.jayway.jsonpath.spi.cache.CacheProvider} used by this configuration
* @return cacheProvider used
*/
public CacheProvider CacheProvider() {
return cacheProvider;
}
/**
* Creates a new configuration by adding the new options to the options used in this configuration.
* @param options options to add
@ -210,7 +189,6 @@ public class Configuration {
private JsonProvider jsonProvider;
private MappingProvider mappingProvider;
private CacheProvider cacheProvider = new DefaultCache(200);
private EnumSet<Option> options = EnumSet.noneOf(Option.class);
private Collection<EvaluationListener> evaluationListener = new ArrayList<EvaluationListener>();
@ -224,11 +202,6 @@ public class Configuration {
return this;
}
public ConfigurationBuilder cacheProvider(CacheProvider provider) {
this.cacheProvider = provider;
return this;
}
public ConfigurationBuilder options(Option... flags) {
if(flags.length > 0) {
this.options.addAll(asList(flags));
@ -260,11 +233,8 @@ public class Configuration {
if (mappingProvider == null){
mappingProvider = defaults.mappingProvider();
}
if (cacheProvider == null){
cacheProvider = defaults.cacheProvider();
}
}
return new Configuration(jsonProvider, mappingProvider, cacheProvider, options, evaluationListener);
return new Configuration(jsonProvider, mappingProvider, options, evaluationListener);
}
}
@ -288,12 +258,5 @@ public class Configuration {
*/
MappingProvider mappingProvider();
/**
* Returns the default {@link com.jayway.jsonpath.spi.cache.CacheProvider}
*
* @return default cache provider
*/
CacheProvider cacheProvider();
}
}

1
json-path/src/main/java/com/jayway/jsonpath/ParseContext.java

@ -17,7 +17,6 @@ package com.jayway.jsonpath;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public interface ParseContext {

17
json-path/src/main/java/com/jayway/jsonpath/internal/DefaultsImpl.java

@ -1,25 +1,21 @@
package com.jayway.jsonpath.internal;
import java.util.EnumSet;
import java.util.Set;
import com.jayway.jsonpath.Configuration.Defaults;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.cache.DefaultCache;
import com.jayway.jsonpath.spi.cache.CacheProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import java.util.EnumSet;
import java.util.Set;
public final class DefaultsImpl implements Defaults {
public static final DefaultsImpl INSTANCE = new DefaultsImpl();
private final MappingProvider mappingProvider = new JsonSmartMappingProvider();
private final CacheProvider cacheProvider = new DefaultCache(200);
@Override
public JsonProvider jsonProvider() {
return new JsonSmartJsonProvider();
@ -36,11 +32,6 @@ public final class DefaultsImpl implements Defaults {
}
private DefaultsImpl() {
}
@Override
public CacheProvider cacheProvider() {
return cacheProvider;
};
};
}

4
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -23,8 +23,8 @@ import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.Predicate;
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;
@ -136,7 +136,7 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override
public <T> T read(String path, Predicate... filters) {
notEmpty(path, "path can not be null or empty");
CacheProvider cache = configuration.CacheProvider();
Cache cache = CacheProvider.getCache();
path = path.trim();
LinkedList filterStack = new LinkedList<Predicate>(asList(filters));

1
json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java

@ -5,7 +5,6 @@ import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.token.ArrayIndexOperation;
import com.jayway.jsonpath.internal.token.ArraySliceOperation;
import com.jayway.jsonpath.internal.token.FunctionPathToken;
import com.jayway.jsonpath.internal.token.PathTokenAppender;
import com.jayway.jsonpath.internal.token.PathTokenFactory;
import com.jayway.jsonpath.internal.token.RootPathToken;

6
json-path/src/main/java/com/jayway/jsonpath/internal/function/FunctionFactory.java

@ -2,7 +2,11 @@ package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.internal.function.numeric.*;
import com.jayway.jsonpath.internal.function.numeric.Average;
import com.jayway.jsonpath.internal.function.numeric.Max;
import com.jayway.jsonpath.internal.function.numeric.Min;
import com.jayway.jsonpath.internal.function.numeric.StandardDeviation;
import com.jayway.jsonpath.internal.function.numeric.Sum;
import java.util.Collections;
import java.util.HashMap;

3
json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java

@ -3,9 +3,6 @@ package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import net.minidev.json.JSONArray;
import java.util.*;
/**
* Provides the length of a JSONArray Object

3
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java

@ -3,9 +3,6 @@ package com.jayway.jsonpath.internal.function.numeric;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import net.minidev.json.JSONArray;
import java.util.Iterator;
/**
* Defines the pattern for processing numerical values via an abstract implementation that iterates over the collection

6
json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java

@ -15,16 +15,10 @@
package com.jayway.jsonpath.internal.token;
import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.function.FunctionFactory;
import com.jayway.jsonpath.internal.function.numeric.Average;
import com.jayway.jsonpath.internal.function.Length;
import com.jayway.jsonpath.internal.function.PassthruFunction;
import com.jayway.jsonpath.internal.function.numeric.Sum;
import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.List;

23
json-path/src/main/java/com/jayway/jsonpath/spi/cache/Cache.java vendored

@ -0,0 +1,23 @@
package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;
public interface Cache {
/**
* Get the Cached JsonPath
* @param key cache key to lookup the JsonPath
* @return JsonPath
*/
public JsonPath get(String key);
/**
* Add JsonPath to the cache
* @param key cache key to store the JsonPath
* @param value JsonPath to be cached
* @return void
* @throws InvalidJsonException
*/
public void put(String key, JsonPath value);
}

32
json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java vendored

@ -1,23 +1,21 @@
package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.JsonPathException;
public interface CacheProvider {
public class CacheProvider {
private static Cache cache = new LRUCache(200);
/**
* Get the Cached JsonPath
* @param key cache key to lookup the JsonPath
* @return JsonPath
*/
public JsonPath get(String key);
public static void setCache(Cache cache){
if (cache != null){
CacheProvider.cache = cache;
}
}
/**
* Add JsonPath to the cache
* @param key cache key to store the JsonPath
* @param value JsonPath to be cached
* @return void
* @throws InvalidJsonException
*/
public void put(String key, JsonPath value);
public static Cache getCache() {
try {
return cache;
} catch (Exception e) {
throw new JsonPathException("Failed to get cache", e);
}
}
}

18
json-path/src/main/java/com/jayway/jsonpath/spi/cache/DefaultCache.java → json-path/src/main/java/com/jayway/jsonpath/spi/cache/LRUCache.java vendored

@ -14,20 +14,15 @@
*/
package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.JsonPath;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import com.jayway.jsonpath.JsonPath;
/*
* LRU implementation copied from com.jayway.jsonpath.internal.Cache
* Will have the same bugs
*/
public class DefaultCache implements CacheProvider{
public class LRUCache implements Cache {
private final ReentrantLock lock = new ReentrantLock();
@ -35,7 +30,7 @@ public class DefaultCache implements CacheProvider{
private final Deque<String> queue = new LinkedList<String>();
private final int limit;
public DefaultCache(int limit) {
public LRUCache(int limit) {
this.limit = limit;
}
@ -52,10 +47,11 @@ public class DefaultCache implements CacheProvider{
}
public JsonPath get(String key) {
if(map.containsKey(key)){
JsonPath jsonPath = map.get(key);
if(jsonPath != null){
removeThenAddKey(key);
}
return map.get(key);
return jsonPath;
}
private void addKey(String key) {

14
json-path/src/main/java/com/jayway/jsonpath/spi/cache/NOOPCache.java vendored

@ -0,0 +1,14 @@
package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.JsonPath;
public class NOOPCache implements Cache {
@Override
public JsonPath get(String key) {
return null;
}
@Override
public void put(String key, JsonPath value) {
}
}
Loading…
Cancel
Save