Browse Source

Stab at fixing #94 cache problem.

pull/133/head
Kalle Stenflo 9 years ago
parent
commit
ba08ae3018
  1. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/Cache.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/CompiledPath.java
  3. 100
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

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

@ -24,12 +24,10 @@ public class Cache {
private final ReentrantLock lock = new ReentrantLock();
private final Map<String, Path> map = new ConcurrentHashMap<String, Path>();
private final Deque<String> queue = new LinkedList<String>();
private final int limit;
public Cache(int limit) {
this.limit = limit;
}
@ -47,7 +45,9 @@ public class Cache {
}
public Path get(String key) {
if(map.containsKey(key)){
removeThenAddKey(key);
}
return map.get(key);
}
@ -58,8 +58,6 @@ public class Cache {
} finally {
lock.unlock();
}
}
private String removeLast() {
@ -90,10 +88,8 @@ public class Cache {
} finally {
lock.unlock();
}
}
public Path getSilent(String key) {
return map.get(key);
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/CompiledPath.java

@ -29,8 +29,6 @@ public class CompiledPath implements Path {
private final boolean isRootPath;
public CompiledPath(PathToken root, boolean isRootPath) {
this.root = root;
this.isRootPath = isRootPath;

100
json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

@ -8,34 +8,28 @@ import com.jayway.jsonpath.Filter;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Cache;
import com.jayway.jsonpath.internal.CompiledPath;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import net.minidev.json.JSONAware;
import net.minidev.json.parser.JSONParser;
import org.assertj.core.api.Assertions;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.jayway.jsonpath.JsonPath.read;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.assertj.core.api.Assertions.filter;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@ -606,6 +600,69 @@ public class IssuesTest extends BaseTest {
Assertions.assertThat(res).containsExactly("url5");
}
@Test
public void issue_94_1() throws Exception {
Cache cache = new Cache(200);
Path dummy = new CompiledPath(null, false);
for (int i = 0; i < 10000; ++i) {
String key = String.valueOf(i);
cache.get(key);
cache.put(key, dummy);
}
Thread.sleep(2000);
Assertions.assertThat(cache.size()).isEqualTo(200);
}
@Test
public void issue_94_2() throws Exception {
Cache cache = new Cache(5);
Path dummy = new CompiledPath(null, false);
cache.put("1", dummy);
cache.put("2", dummy);
cache.put("3", dummy);
cache.put("4", dummy);
cache.put("5", dummy);
cache.put("6", dummy);
cache.get("1");
cache.get("2");
cache.get("3");
cache.get("4");
cache.get("5");
cache.get("6");
cache.get("2");
cache.get("3");
cache.get("4");
cache.get("5");
cache.get("6");
cache.get("3");
cache.get("4");
cache.get("5");
cache.get("6");
cache.get("4");
cache.get("5");
cache.get("6");
cache.get("5");
cache.get("6");
cache.get("6");
Assertions.assertThat(cache.getSilent("6")).isNotNull();
Assertions.assertThat(cache.getSilent("5")).isNotNull();
Assertions.assertThat(cache.getSilent("4")).isNotNull();
Assertions.assertThat(cache.getSilent("3")).isNotNull();
Assertions.assertThat(cache.getSilent("2")).isNotNull();
Assertions.assertThat(cache.getSilent("1")).isNull();
}
@Test
public void issue_97() throws Exception {
String json = "{ \"books\": [ " +
@ -633,4 +690,29 @@ public class IssuesTest extends BaseTest {
Assertions.assertThat(categories).containsOnly("fiction");
}
@Test
public void issue_99() throws Exception {
String json = "{\n" +
" \"array1\": [\n" +
" {\n" +
" \"array2\": []\n" +
" },\n" +
" {\n" +
" \"array2\": [\n" +
" {\n" +
" \"key\": \"test_key\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
Configuration configuration = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
List<String> keys = JsonPath.using(configuration).parse(json).read("$.array1[*].array2[0].key");
System.out.println(keys);
}
}

Loading…
Cancel
Save