Search in sources :

Example 1 with PlanExecutionContext

use of org.finos.legend.engine.plan.execution.PlanExecutionContext in project legend-engine by finos.

the class TestPlanExecutionWithGraphFetchCrossKeyCache method testCrossCacheWithNoPropertyAccess.

@Test
public void testCrossCacheWithNoPropertyAccess() throws JavaCompileException {
    String fetchFunction = "###Pure\n" + "function test::fetch(): String[1]\n" + "{\n" + "  test::Person.all()\n" + "    ->graphFetch(#{\n" + "      test::Person {\n" + "        fullName\n" + "      }\n" + "    }#, 1)\n" + "    ->serialize(#{\n" + "      test::Person {\n" + "        fullName\n" + "      }\n" + "    }#)\n" + "}";
    SingleExecutionPlan plan = buildPlanForFetchFunction(fetchFunction);
    GraphFetchCacheByTargetCrossKeys firmCache = getFirmEmptyCache(plan);
    PlanExecutionContext context = new PlanExecutionContext(plan, firmCache);
    String expectedRes = "[" + "{\"fullName\":\"P1\"}," + "{\"fullName\":\"P2\"}," + "{\"fullName\":\"P3\"}," + "{\"fullName\":\"P4\"}," + "{\"fullName\":\"P5\"}" + "]";
    Assert.assertEquals(expectedRes, executePlan(plan, context));
    assertCacheStats(firmCache.getExecutionCache(), 0, 0, 0, 0);
}
Also used : GraphFetchCacheByTargetCrossKeys(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByTargetCrossKeys) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) PlanExecutionContext(org.finos.legend.engine.plan.execution.PlanExecutionContext) Test(org.junit.Test)

Example 2 with PlanExecutionContext

use of org.finos.legend.engine.plan.execution.PlanExecutionContext in project legend-engine by finos.

the class TestPlanExecutionWithGraphFetchCrossKeyCache method testMultiCrossPropertyCaches.

@Test
public void testMultiCrossPropertyCaches() throws JavaCompileException {
    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" + "        },\n" + "        address {\n" + "          name\n" + "        }\n" + "      }\n" + "    }#, 1)\n" + "    ->serialize(#{\n" + "      test::Person {\n" + "        fullName,\n" + "        firm {\n" + "          name\n" + "        },\n" + "        address {\n" + "          name\n" + "        }\n" + "      }\n" + "    }#)\n" + "}";
    SingleExecutionPlan plan = buildPlanForFetchFunction(fetchFunction);
    GraphFetchCacheByTargetCrossKeys firmCache = getFirmEmptyCache(plan);
    GraphFetchCacheByTargetCrossKeys addressCache = getAddressCache(plan);
    PlanExecutionContext context = new PlanExecutionContext(plan, firmCache, addressCache);
    String expectedRes = "[" + "{\"fullName\":\"P1\",\"firm\":{\"name\":\"F1\"},\"address\":{\"name\":\"A1\"}}," + "{\"fullName\":\"P2\",\"firm\":{\"name\":\"F2\"},\"address\":{\"name\":\"A2\"}}," + "{\"fullName\":\"P3\",\"firm\":null,\"address\":null}," + "{\"fullName\":\"P4\",\"firm\":null,\"address\":{\"name\":\"A3\"}}," + "{\"fullName\":\"P5\",\"firm\":{\"name\":\"F1\"},\"address\":{\"name\":\"A1\"}}" + "]";
    Assert.assertEquals(expectedRes, executePlan(plan, context));
    assertCacheStats(firmCache.getExecutionCache(), 3, 5, 2, 3);
    assertCacheStats(addressCache.getExecutionCache(), 4, 5, 1, 4);
    Assert.assertEquals(expectedRes, executePlan(plan, context));
    assertCacheStats(firmCache.getExecutionCache(), 3, 10, 7, 3);
    assertCacheStats(addressCache.getExecutionCache(), 4, 10, 6, 4);
}
Also used : GraphFetchCacheByTargetCrossKeys(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByTargetCrossKeys) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) PlanExecutionContext(org.finos.legend.engine.plan.execution.PlanExecutionContext) Test(org.junit.Test)

Example 3 with PlanExecutionContext

use of org.finos.legend.engine.plan.execution.PlanExecutionContext in project legend-engine by finos.

the class TestPlanExecutionWithGraphFetchCrossKeyCache method testToManyCrossPropertyCache.

@Test
public void testToManyCrossPropertyCache() throws JavaCompileException {
    String fetchFunction = "###Pure\n" + "function test::fetch(): String[1]\n" + "{\n" + "  test::Address.all()\n" + "    ->graphFetch(#{\n" + "      test::Address {\n" + "        name,\n" + "        persons {\n" + "          fullName\n" + "        }\n" + "      }\n" + "    }#, 1)\n" + "    ->serialize(#{\n" + "      test::Address {\n" + "        name,\n" + "        persons {\n" + "          fullName\n" + "        }\n" + "      }\n" + "    }#)\n" + "}";
    SingleExecutionPlan plan = buildPlanForFetchFunction(fetchFunction);
    GraphFetchCacheByTargetCrossKeys personCache = getPersonCache(plan);
    PlanExecutionContext context = new PlanExecutionContext(plan, personCache);
    String expectedRes = "[" + "{\"name\":\"A1\",\"persons\":[{\"fullName\":\"P1\"},{\"fullName\":\"P5\"}]}," + "{\"name\":\"A2\",\"persons\":[{\"fullName\":\"P2\"}]}," + "{\"name\":\"A3\",\"persons\":[{\"fullName\":\"P4\"}]}," + "{\"name\":\"A4\",\"persons\":[]}," + "{\"name\":\"A5\",\"persons\":[]}" + "]";
    Assert.assertEquals(expectedRes, executePlan(plan, context));
    assertCacheStats(personCache.getExecutionCache(), 5, 5, 0, 5);
    Assert.assertEquals(expectedRes, executePlan(plan, context));
    assertCacheStats(personCache.getExecutionCache(), 5, 10, 5, 5);
}
Also used : GraphFetchCacheByTargetCrossKeys(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByTargetCrossKeys) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) PlanExecutionContext(org.finos.legend.engine.plan.execution.PlanExecutionContext) Test(org.junit.Test)

Example 4 with PlanExecutionContext

use of org.finos.legend.engine.plan.execution.PlanExecutionContext 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);
    }
}
Also used : EngineJavaCompiler(org.finos.legend.engine.shared.javaCompiler.EngineJavaCompiler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) ObjectOutputStream(java.io.ObjectOutputStream) GraphFetchCrossAssociationKeys(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCrossAssociationKeys) GraphFetchCacheKey(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheKey) GraphFetchCacheByTargetCrossKeys(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByTargetCrossKeys) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) PlanExecutionContext(org.finos.legend.engine.plan.execution.PlanExecutionContext) Map(java.util.Map) JavaHelper(org.finos.legend.engine.plan.execution.nodes.helpers.platform.JavaHelper) Test(org.junit.Test)

Example 5 with PlanExecutionContext

use of org.finos.legend.engine.plan.execution.PlanExecutionContext in project legend-engine by finos.

the class TestPlanExecutionWithGraphFetchEqualityCache method testUnUtilizedCache.

@Test
public void testUnUtilizedCache() throws IOException, JavaCompileException {
    String expectedRes = "{\"builder\":{\"_type\":\"json\"},\"values\":{\"legalName\":\"FirmA\",\"employees\":[{\"firstName\":\"Peter\",\"lastName\":\"Smith\"},{\"firstName\":\"John\",\"lastName\":\"Johnson\"},{\"firstName\":\"John\",\"lastName\":\"Hill\"},{\"firstName\":\"Anthony\",\"lastName\":\"Allen\"}]}}";
    SingleExecutionPlan plan = readPlan("org/finos/legend/engine/plan/execution/stores/relational/test/cache/graphFetch//equalityCachePlanWithToManyComplexProperties.json");
    GraphFetchCacheByEqualityKeys personCache = ExecutionCacheBuilder.buildGraphFetchCacheByEqualityKeysFromGuavaCache(CacheBuilder.newBuilder().recordStats().expireAfterWrite(10, TimeUnit.MINUTES).build(), "meta::relational::tests::simpleRelationalMappingInc", "meta_pure_tests_model_simple_Person");
    PlanExecutionContext planExecutionContext = new PlanExecutionContext(plan, personCache);
    JsonStreamingResult result = (JsonStreamingResult) planExecutor.execute(plan, Collections.emptyMap(), null, planExecutionContext);
    String res = result.flush(new JsonStreamToJsonDefaultSerializer(result));
    Assert.assertEquals(expectedRes, res);
    Assert.assertFalse(personCache.isCacheUtilized());
    Assert.assertEquals(0, personCache.getExecutionCache().estimatedSize());
    Assert.assertEquals(0, personCache.getExecutionCache().stats().requestCount());
    Assert.assertEquals(0, personCache.getExecutionCache().stats().hitCount());
    Assert.assertEquals(0, personCache.getExecutionCache().stats().missCount());
}
Also used : JsonStreamingResult(org.finos.legend.engine.plan.execution.result.json.JsonStreamingResult) GraphFetchCacheByEqualityKeys(org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByEqualityKeys) SingleExecutionPlan(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan) PlanExecutionContext(org.finos.legend.engine.plan.execution.PlanExecutionContext) JsonStreamToJsonDefaultSerializer(org.finos.legend.engine.plan.execution.result.json.JsonStreamToJsonDefaultSerializer) Test(org.junit.Test)

Aggregations

PlanExecutionContext (org.finos.legend.engine.plan.execution.PlanExecutionContext)10 SingleExecutionPlan (org.finos.legend.engine.protocol.pure.v1.model.executionPlan.SingleExecutionPlan)8 Test (org.junit.Test)8 GraphFetchCacheByTargetCrossKeys (org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByTargetCrossKeys)6 JsonStreamToJsonDefaultSerializer (org.finos.legend.engine.plan.execution.result.json.JsonStreamToJsonDefaultSerializer)3 JsonStreamingResult (org.finos.legend.engine.plan.execution.result.json.JsonStreamingResult)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 List (java.util.List)2 Map (java.util.Map)2 GraphFetchCacheByEqualityKeys (org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCacheByEqualityKeys)2 GraphFetchCrossAssociationKeys (org.finos.legend.engine.plan.execution.cache.graphFetch.GraphFetchCrossAssociationKeys)2 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 URL (java.net.URL)1