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. 36
      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; package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.DefaultsImpl; 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.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider; import com.jayway.jsonpath.spi.mapper.MappingProvider;
@ -57,16 +55,14 @@ public class Configuration {
private final MappingProvider mappingProvider; private final MappingProvider mappingProvider;
private final Set<Option> options; private final Set<Option> options;
private final Collection<EvaluationListener> evaluationListeners; 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(jsonProvider, "jsonProvider can not be null");
notNull(mappingProvider, "mappingProvider can not be null"); notNull(mappingProvider, "mappingProvider can not be null");
notNull(options, "setOptions can not be null"); notNull(options, "setOptions can not be null");
notNull(evaluationListeners, "evaluationListeners can not be null"); notNull(evaluationListeners, "evaluationListeners can not be null");
this.jsonProvider = jsonProvider; this.jsonProvider = jsonProvider;
this.mappingProvider = mappingProvider; this.mappingProvider = mappingProvider;
this.cacheProvider = cacheProvider;
this.options = Collections.unmodifiableSet(options); this.options = Collections.unmodifiableSet(options);
this.evaluationListeners = Collections.unmodifiableCollection(evaluationListeners); this.evaluationListeners = Collections.unmodifiableCollection(evaluationListeners);
} }
@ -130,23 +126,6 @@ public class Configuration {
public MappingProvider mappingProvider() { public MappingProvider mappingProvider() {
return mappingProvider; 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. * Creates a new configuration by adding the new options to the options used in this configuration.
@ -210,7 +189,6 @@ public class Configuration {
private JsonProvider jsonProvider; private JsonProvider jsonProvider;
private MappingProvider mappingProvider; private MappingProvider mappingProvider;
private CacheProvider cacheProvider = new DefaultCache(200);
private EnumSet<Option> options = EnumSet.noneOf(Option.class); private EnumSet<Option> options = EnumSet.noneOf(Option.class);
private Collection<EvaluationListener> evaluationListener = new ArrayList<EvaluationListener>(); private Collection<EvaluationListener> evaluationListener = new ArrayList<EvaluationListener>();
@ -223,11 +201,6 @@ public class Configuration {
this.mappingProvider = provider; this.mappingProvider = provider;
return this; return this;
} }
public ConfigurationBuilder cacheProvider(CacheProvider provider) {
this.cacheProvider = provider;
return this;
}
public ConfigurationBuilder options(Option... flags) { public ConfigurationBuilder options(Option... flags) {
if(flags.length > 0) { if(flags.length > 0) {
@ -260,11 +233,8 @@ public class Configuration {
if (mappingProvider == null){ if (mappingProvider == null){
mappingProvider = defaults.mappingProvider(); mappingProvider = defaults.mappingProvider();
} }
if (cacheProvider == null){
cacheProvider = defaults.cacheProvider();
}
} }
return new Configuration(jsonProvider, mappingProvider, cacheProvider, options, evaluationListener); return new Configuration(jsonProvider, mappingProvider, options, evaluationListener);
} }
} }
@ -287,13 +257,6 @@ public class Configuration {
* @return default mapping provider * @return default mapping provider
*/ */
MappingProvider mappingProvider(); 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.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
public interface ParseContext { public interface ParseContext {

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

@ -1,24 +1,20 @@
package com.jayway.jsonpath.internal; package com.jayway.jsonpath.internal;
import java.util.EnumSet;
import java.util.Set;
import com.jayway.jsonpath.Configuration.Defaults; import com.jayway.jsonpath.Configuration.Defaults;
import com.jayway.jsonpath.Option; 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.JsonProvider;
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider; import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider; import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider; import com.jayway.jsonpath.spi.mapper.MappingProvider;
import java.util.EnumSet;
import java.util.Set;
public final class DefaultsImpl implements Defaults { public final class DefaultsImpl implements Defaults {
public static final DefaultsImpl INSTANCE = new DefaultsImpl(); public static final DefaultsImpl INSTANCE = new DefaultsImpl();
private final MappingProvider mappingProvider = new JsonSmartMappingProvider(); private final MappingProvider mappingProvider = new JsonSmartMappingProvider();
private final CacheProvider cacheProvider = new DefaultCache(200);
@Override @Override
public JsonProvider jsonProvider() { public JsonProvider jsonProvider() {
@ -36,11 +32,6 @@ public final class DefaultsImpl implements Defaults {
} }
private DefaultsImpl() { 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.Predicate;
import com.jayway.jsonpath.ReadContext; 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.CacheProvider; import com.jayway.jsonpath.spi.cache.CacheProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -136,7 +136,7 @@ public class JsonReader implements ParseContext, 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");
CacheProvider cache = configuration.CacheProvider(); Cache cache = CacheProvider.getCache();
path = path.trim(); path = path.trim();
LinkedList filterStack = new LinkedList<Predicate>(asList(filters)); 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.Predicate;
import com.jayway.jsonpath.internal.token.ArrayIndexOperation; import com.jayway.jsonpath.internal.token.ArrayIndexOperation;
import com.jayway.jsonpath.internal.token.ArraySliceOperation; 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.PathTokenAppender;
import com.jayway.jsonpath.internal.token.PathTokenFactory; import com.jayway.jsonpath.internal.token.PathTokenFactory;
import com.jayway.jsonpath.internal.token.RootPathToken; 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.Function;
import com.jayway.jsonpath.InvalidPathException; 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.Collections;
import java.util.HashMap; 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.Function;
import com.jayway.jsonpath.internal.EvaluationContext; import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef; import com.jayway.jsonpath.internal.PathRef;
import net.minidev.json.JSONArray;
import java.util.*;
/** /**
* Provides the length of a JSONArray Object * 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.Function;
import com.jayway.jsonpath.internal.EvaluationContext; import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef; 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 * 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; package com.jayway.jsonpath.internal.token;
import com.jayway.jsonpath.Function; import com.jayway.jsonpath.Function;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.Option; import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException; import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.PathRef; import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.Utils; 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 com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.List; 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);
}

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

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

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