Browse Source

Merge branch 'gauravgupta2-master'

pull/149/head
Kalle Stenflo 9 years ago
parent
commit
a96499b9b2
  1. 1
      json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
  2. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/DefaultsImpl.java
  3. 20
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  4. 13
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java
  5. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/function/FunctionFactory.java
  6. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java
  7. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  8. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/token/PathToken.java
  9. 23
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/Cache.java
  10. 21
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java
  11. 112
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/LRUCache.java
  12. 14
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/NOOPCache.java
  13. 33
      json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java

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 {

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

@ -1,8 +1,5 @@
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.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
@ -10,6 +7,9 @@ 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();

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

@ -23,6 +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 org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -30,11 +32,13 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import static com.jayway.jsonpath.JsonPath.compile; import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.internal.Utils.notEmpty; import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull; import static com.jayway.jsonpath.internal.Utils.notNull;
import static java.util.Arrays.asList;
public class JsonReader implements ParseContext, DocumentContext { public class JsonReader implements ParseContext, DocumentContext {
@ -132,7 +136,21 @@ 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");
return read(compile(path, filters)); 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);
}
} }
@Override @Override

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

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);
}

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

@ -0,0 +1,21 @@
package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.JsonPathException;
public class CacheProvider {
private static Cache cache = new LRUCache(200);
public static void setCache(Cache cache){
if (cache != null){
CacheProvider.cache = cache;
}
}
public static Cache getCache() {
try {
return cache;
} catch (Exception e) {
throw new JsonPathException("Failed to get cache", e);
}
}
}

112
json-path/src/main/java/com/jayway/jsonpath/spi/cache/LRUCache.java vendored

@ -0,0 +1,112 @@
/*
* 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 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;
public class LRUCache implements Cache {
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 LRUCache(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) {
JsonPath jsonPath = map.get(key);
if(jsonPath != null){
removeThenAddKey(key);
}
return jsonPath;
}
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();
}
}

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) {
}
}

33
json-path/src/test/java/com/jayway/jsonpath/internal/JsonReaderTest.java

@ -0,0 +1,33 @@
package com.jayway.jsonpath.internal;
import static org.junit.Assert.*;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Criteria;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
public class JsonReaderTest extends BaseTest {
@Test
public void cached_path_with_predicates() {
Filter feq = Filter.filter(Criteria.where("category").eq("reference"));
Filter fne = Filter.filter(Criteria.where("category").ne("reference"));
DocumentContext JsonDoc = JsonPath.parse(JSON_DOCUMENT);
List<String> eq = JsonDoc.read("$.store.book[?].category", feq);
List<String> ne = JsonDoc.read("$.store.book[?].category", fne);
Assertions.assertThat(eq).contains("reference");
Assertions.assertThat(ne).doesNotContain("reference");
}
}
Loading…
Cancel
Save