Browse Source

Externalized the cache using CacheProvider spi.

Set the CacheProvider in the Configuration:
Configuration
            .builder()
            .mappingProvider(new <<Implementation of mapping provider>>)
            .jsonProvider(new <<Implementation of JsonProvider>>)
            .cacheProvider(new <<implementation of CacheProvider>>)
            .build();
pull/137/head
gauravgupta 9 years ago
parent
commit
bf33697891
  1. 41
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  2. 11
      json-path/src/main/java/com/jayway/jsonpath/internal/DefaultsImpl.java
  3. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  4. 12
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java
  5. 23
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java
  6. 116
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/DefaultCache.java

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

@ -15,6 +15,8 @@
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;
@ -55,14 +57,16 @@ 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, EnumSet<Option> options, Collection<EvaluationListener> evaluationListeners) {
private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider, CacheProvider cacheProvider, 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);
}
@ -126,6 +130,23 @@ public class Configuration {
public MappingProvider 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.
@ -189,6 +210,7 @@ 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>();
@ -201,6 +223,11 @@ public class Configuration {
this.mappingProvider = provider;
return this;
}
public ConfigurationBuilder cacheProvider(CacheProvider provider) {
this.cacheProvider = provider;
return this;
}
public ConfigurationBuilder options(Option... flags) {
if(flags.length > 0) {
@ -233,8 +260,11 @@ public class Configuration {
if (mappingProvider == null){
mappingProvider = defaults.mappingProvider();
}
if (cacheProvider == null){
cacheProvider = defaults.cacheProvider();
}
}
return new Configuration(jsonProvider, mappingProvider, options, evaluationListener);
return new Configuration(jsonProvider, mappingProvider, cacheProvider, options, evaluationListener);
}
}
@ -257,6 +287,13 @@ public class Configuration {
* @return default mapping provider
*/
MappingProvider mappingProvider();
/**
* Returns the default {@link com.jayway.jsonpath.spi.cache.CacheProvider}
*
* @return default cache provider
*/
CacheProvider cacheProvider();
}
}

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

@ -5,6 +5,8 @@ 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;
@ -15,6 +17,8 @@ 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() {
@ -32,6 +36,11 @@ public final class DefaultsImpl implements Defaults {
}
private DefaultsImpl() {
};
}
@Override
public CacheProvider cacheProvider() {
return cacheProvider;
};
}

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

@ -132,7 +132,13 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override
public <T> T read(String path, Predicate... filters) {
notEmpty(path, "path can not be null or empty");
return read(compile(path, filters));
JsonPath cached = configuration.CacheProvider().get(path);
if(cached != null){
return read(cached);
}else {
return read(compile(path, filters));
}
}
@Override

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

@ -66,12 +66,12 @@ public class PathCompiler {
fail("Path must not end wid a scan operation '..'");
}
LinkedList filterStack = new LinkedList<Predicate>(asList(filters));
String cacheKey = Utils.concat(path, filterStack.toString());
Path p = cache.get(cacheKey);
if (p == null) {
p = new PathCompiler(path.trim(), filterStack).compile();
cache.put(cacheKey, p);
}
// String cacheKey = Utils.concat(path, filterStack.toString());
// Path p = cache.get(cacheKey);
// if (p == null) {
Path p = new PathCompiler(path.trim(), filterStack).compile();
// cache.put(cacheKey, p);
// }
return p;
} catch (Exception e) {
InvalidPathException ipe;

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

@ -0,0 +1,23 @@
package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;
public interface CacheProvider {
/**
* 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);
}

116
json-path/src/main/java/com/jayway/jsonpath/spi/cache/DefaultCache.java vendored

@ -0,0 +1,116 @@
/*
* Copyright 2011 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.jsonpath.spi.cache;
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{
private final ReentrantLock lock = new ReentrantLock();
private final Map<String, JsonPath> map = new ConcurrentHashMap<String, JsonPath>();
private final Deque<String> queue = new LinkedList<String>();
private final int limit;
public DefaultCache(int limit) {
this.limit = limit;
}
public void put(String key, JsonPath value) {
JsonPath oldValue = map.put(key, value);
if (oldValue != null) {
removeThenAddKey(key);
} else {
addKey(key);
}
if (map.size() > limit) {
map.remove(removeLast());
}
}
public JsonPath get(String key) {
if(map.containsKey(key)){
removeThenAddKey(key);
}
return map.get(key);
}
private void addKey(String key) {
lock.lock();
try {
queue.addFirst(key);
} finally {
lock.unlock();
}
}
private String removeLast() {
lock.lock();
try {
final String removedKey = queue.removeLast();
return removedKey;
} finally {
lock.unlock();
}
}
private void removeThenAddKey(String key) {
lock.lock();
try {
queue.removeFirstOccurrence(key);
queue.addFirst(key);
} finally {
lock.unlock();
}
}
private void removeFirstOccurrence(String key) {
lock.lock();
try {
queue.removeFirstOccurrence(key);
} finally {
lock.unlock();
}
}
public JsonPath getSilent(String key) {
return map.get(key);
}
public void remove(String key) {
removeFirstOccurrence(key);
map.remove(key);
}
public int size() {
return map.size();
}
public String toString() {
return map.toString();
}
}
Loading…
Cancel
Save