use of org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCrossAssociationKeys in project legend-engine by finos.
the class TestPlanExecutionWithGraphFetchCrossKeyCache method testCrossPropertyCachingWithSerializableCache.
@Test
public void testCrossPropertyCachingWithSerializableCache() throws JavaCompileException, IOException, ClassNotFoundException {
String fetchFunction = "###Pure\n" + "function test::fetch(): String[1]\n" + "{\n" + " test::Person.all()\n" + " ->graphFetch(#{\n" + " test::Person {\n" + " fullName,\n" + " firm {\n" + " name,\n" + " address {\n" + " name\n" + " }\n" + " }\n" + " }\n" + " }#, 1)\n" + " ->serialize(#{\n" + " test::Person {\n" + " fullName,\n" + " firm {\n" + " name,\n" + " address {\n" + " name\n" + " }\n" + " }\n" + " }\n" + " }#)\n" + "}";
String expectedRes = "[" + "{\"fullName\":\"P1\",\"firm\":{\"name\":\"F1\",\"address\":{\"name\":\"A4\"}}}," + "{\"fullName\":\"P2\",\"firm\":{\"name\":\"F2\",\"address\":{\"name\":\"A3\"}}}," + "{\"fullName\":\"P3\",\"firm\":null}," + "{\"fullName\":\"P4\",\"firm\":null}," + "{\"fullName\":\"P5\",\"firm\":{\"name\":\"F1\",\"address\":{\"name\":\"A4\"}}}" + "]";
SingleExecutionPlan plan = buildPlanForFetchFunction(fetchFunction);
EngineJavaCompiler compiler = JavaHelper.compilePlan(plan, null);
GraphFetchCrossAssociationKeys graphFetchCrossAssociationKeys = GraphFetchCrossAssociationKeys.graphFetchCrossAssociationKeysForPlan(plan).stream().filter(x -> x.getName().equals("<default, root.firm>")).findFirst().orElse(null);
try (JavaHelper.ThreadContextClassLoaderScope ignored = JavaHelper.withCurrentThreadContextClassLoader(compiler.getClassLoader())) {
Map<GraphFetchCacheKey, List<Object>> firmMap1 = Maps.mutable.empty();
GraphFetchCacheByTargetCrossKeys firmCache1 = ExecutionCacheBuilder.buildGraphFetchCacheByTargetCrossKeysFromExecutionCache(buildExecutionCacheFromMap(firmMap1), graphFetchCrossAssociationKeys);
Assert.assertEquals(expectedRes, executePlan(plan, new PlanExecutionContext(firmCache1)));
assertCacheStats(firmCache1.getExecutionCache(), 3, 5, 2, 3);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(firmMap1);
objectOutputStream.flush();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ClassLoaderObjectInputStream objectInputStream = new ClassLoaderObjectInputStream(compiler.getClassLoader(), byteArrayInputStream);
Object o = objectInputStream.readObject();
@SuppressWarnings("unchecked") Map<GraphFetchCacheKey, List<Object>> firmMap2 = (Map<GraphFetchCacheKey, List<Object>>) o;
GraphFetchCacheByTargetCrossKeys firmCache2 = ExecutionCacheBuilder.buildGraphFetchCacheByTargetCrossKeysFromExecutionCache(buildExecutionCacheFromMap(firmMap2), graphFetchCrossAssociationKeys);
Assert.assertEquals(expectedRes, executePlan(plan, new PlanExecutionContext(firmCache2)));
assertCacheStats(firmCache2.getExecutionCache(), 3, 5, 5, 0);
}
}
use of org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCrossAssociationKeys in project legend-engine by finos.
the class ExecutionNodeExecutor method findGraphFetchCacheByTargetCrossKeys.
private ExecutionCache<GraphFetchCacheKey, List<Object>> findGraphFetchCacheByTargetCrossKeys(GlobalGraphFetchExecutionNode globalGraphFetchExecutionNode) {
List<GraphFetchCache> graphFetchCaches = this.executionState.graphFetchCaches;
XStorePropertyFetchDetails fetchDetails = globalGraphFetchExecutionNode.xStorePropertyFetchDetails;
return graphFetchCaches.stream().filter(GraphFetchCacheByTargetCrossKeys.class::isInstance).map(GraphFetchCacheByTargetCrossKeys.class::cast).filter(cache -> cache.getGraphFetchCrossAssociationKeys() != null).filter(cache -> {
GraphFetchCrossAssociationKeys c = cache.getGraphFetchCrossAssociationKeys();
return c.getPropertyPath().equals(fetchDetails.propertyPath) && c.getSourceMappingId().equals(fetchDetails.sourceMappingId) && c.getSourceSetId().equals(fetchDetails.sourceSetId) && c.getTargetMappingId().equals(fetchDetails.targetMappingId) && c.getTargetSetId().equals(fetchDetails.targetSetId) && c.getTargetPropertiesOrdered().equals(fetchDetails.targetPropertiesOrdered) && c.getSubTree().equals(fetchDetails.subTree);
}).map(GraphFetchCacheByTargetCrossKeys::getExecutionCache).findFirst().orElse(null);
}
use of org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCrossAssociationKeys in project legend-engine by finos.
the class OperationalContext method assertSharedCachesValidity.
private static void assertSharedCachesValidity(Map<GraphFetchCrossAssociationKeys, ExecutionCache<GraphFetchCacheKey, List<Object>>> cacheConfig) {
Map<ExecutionCache<GraphFetchCacheKey, List<Object>>, List<GraphFetchCrossAssociationKeys>> reverseCacheMap = cacheConfig.entrySet().stream().collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
reverseCacheMap.forEach((c, keys) -> {
GraphFetchCrossAssociationKeys first = keys.get(0);
for (GraphFetchCrossAssociationKeys key : keys) {
if (!first.isCompatible(key)) {
throw new IllegalArgumentException("Cannot use shared cache for incompatible cross association keys '" + first.getName() + "' and '" + key.getName() + "'");
}
}
});
}
Aggregations