Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,45 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.spi.cache.Cache;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nonnull;


public class JsonPathCache implements Cache {

public static final JsonPathCache INSTANCE = new JsonPathCache();

private static final long DEFAULT_CACHE_MAXIMUM_SIZE = 10000;
private static final Predicate[] NO_PREDICATES = new Predicate[0];

private final com.google.common.cache.Cache<String, JsonPath> _jsonPathCache =
CacheBuilder.newBuilder().maximumSize(DEFAULT_CACHE_MAXIMUM_SIZE).build();
private final LoadingCache<String, JsonPath> _jsonPathCache =
CacheBuilder.newBuilder().maximumSize(DEFAULT_CACHE_MAXIMUM_SIZE)
.build(new CacheLoader<String, JsonPath>() {
@Override
public JsonPath load(@Nonnull String jsonPath) {
return JsonPath.compile(jsonPath, NO_PREDICATES);
}
});

@Override
public JsonPath get(String key) {
return _jsonPathCache.getIfPresent(key);
}

public JsonPath getOrCompute(String key) {
try {
return _jsonPathCache.get(key);
} catch (ExecutionException e) {
// the cast is safe because JsonPath.compile only throws RuntimeExceptions
throw (RuntimeException) e.getCause();
}
}

@Override
public void put(String key, JsonPath value) {
_jsonPathCache.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import java.util.List;
import java.util.Map;
import org.apache.pinot.common.function.JsonPathCache;
import org.apache.pinot.core.operator.blocks.ProjectionBlock;
import org.apache.pinot.core.operator.transform.TransformResultMetadata;
import org.apache.pinot.core.plan.DocIdSetPlanNode;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void init(List<TransformFunction> arguments, Map<String, DataSource> data
+ "function");
}
_jsonFieldTransformFunction = firstArgument;
_jsonPath = JsonPath.compile(((LiteralTransformFunction) arguments.get(1)).getLiteral());
_jsonPath = JsonPathCache.INSTANCE.getOrCompute(((LiteralTransformFunction) arguments.get(1)).getLiteral());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import java.util.List;
import java.util.Map;
import org.apache.pinot.common.function.JsonPathCache;
import org.apache.pinot.core.operator.blocks.ProjectionBlock;
import org.apache.pinot.core.operator.transform.TransformResultMetadata;
import org.apache.pinot.core.plan.DocIdSetPlanNode;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void init(List<TransformFunction> arguments, Map<String, DataSource> data
}
_jsonFieldTransformFunction = firstArgument;
_jsonPathString = ((LiteralTransformFunction) arguments.get(1)).getLiteral();
_jsonPath = JsonPath.compile(_jsonPathString);
_jsonPath = JsonPathCache.INSTANCE.getOrCompute(_jsonPathString);
String resultsType = ((LiteralTransformFunction) arguments.get(2)).getLiteral().toUpperCase();
boolean isSingleValue = !resultsType.endsWith("_ARRAY");
try {
Expand Down