Search in sources :

Example 71 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestSetProperty method testDateTimeProperties.

@Test
public void testDateTimeProperties() throws Exception {
    Vertex v = this.sqlgGraph.addVertex(T.label, "Person", "name", "marko");
    LocalDateTime ldt = LocalDateTime.now();
    v.property("ldt", ldt);
    v.property("ld", ldt.toLocalDate());
    LocalTime lt = ldt.toLocalTime().truncatedTo(ChronoUnit.SECONDS);
    v.property("lt", lt);
    ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("Africa/Johannesburg"));
    ZonedDateTime zdt1Fixed = ZonedDateTime.of(zdt.toLocalDateTime(), ZoneId.of("Africa/Johannesburg"));
    v.property("zdt", zdt);
    ZonedDateTime zdt2 = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("+02:00"));
    ZonedDateTime zdt2Fixed = ZonedDateTime.of(zdt2.toLocalDateTime(), ZoneId.of("GMT+02:00"));
    v.property("zdt2", zdt2);
    Period p = Period.ofDays(3);
    v.property("p", p);
    Duration d = Duration.ofHours(12);
    v.property("d", d);
    Vertex vJ = this.sqlgGraph.addVertex(T.label, "Person", "name", "john");
    vJ.addEdge("knows", v);
    this.sqlgGraph.tx().commit();
    assertProperty(v, "ldt", ldt);
    assertProperty(v, "ld", ldt.toLocalDate());
    assertProperty(v, "lt", lt);
    assertProperty(v, "zdt", zdt1Fixed);
    assertProperty(v, "zdt2", zdt2Fixed);
    assertProperty(v, "p", p);
    assertProperty(v, "d", d);
    Vertex vJ2 = sqlgGraph.vertices(vJ.id()).next();
    Vertex v2 = vJ2.edges(Direction.OUT).next().inVertex();
    assertProperty(v2, "ldt", ldt);
    assertProperty(v2, "ld", ldt.toLocalDate());
    assertProperty(v2, "lt", lt);
    assertProperty(v2, "zdt", zdt);
    assertProperty(v2, "zdt2", zdt2Fixed);
    assertProperty(v2, "p", p);
    assertProperty(v2, "d", d);
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        v2 = sqlgGraph1.vertices(v.id()).next();
        assertProperty(sqlgGraph1, v2, "ldt", ldt);
        assertProperty(sqlgGraph1, v2, "ld", ldt.toLocalDate());
        assertProperty(sqlgGraph1, v2, "lt", lt);
        assertProperty(sqlgGraph1, v2, "zdt", zdt);
        assertProperty(sqlgGraph1, v2, "zdt2", zdt2Fixed);
        assertProperty(sqlgGraph1, v2, "p", p);
        assertProperty(sqlgGraph1, v2, "d", d);
    }
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        v2 = sqlgGraph1.traversal().V().hasLabel("Person").next();
        assertProperty(sqlgGraph1, v2, "ldt", ldt);
        assertProperty(sqlgGraph1, v2, "ld", ldt.toLocalDate());
        assertProperty(sqlgGraph1, v2, "lt", lt);
        assertProperty(sqlgGraph1, v2, "zdt", zdt);
        assertProperty(sqlgGraph1, v2, "zdt2", zdt2Fixed);
        assertProperty(sqlgGraph1, v2, "p", p);
        assertProperty(sqlgGraph1, v2, "d", d);
    }
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        vJ2 = sqlgGraph1.vertices(vJ.id()).next();
        v2 = vJ2.edges(Direction.OUT).next().inVertex();
        assertProperty(sqlgGraph1, v2, "ldt", ldt);
        assertProperty(sqlgGraph1, v2, "ld", ldt.toLocalDate());
        assertProperty(sqlgGraph1, v2, "lt", lt);
        assertProperty(sqlgGraph1, v2, "zdt", zdt);
        assertProperty(sqlgGraph1, v2, "zdt2", zdt2Fixed);
        assertProperty(sqlgGraph1, v2, "p", p);
        assertProperty(sqlgGraph1, v2, "d", d);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Test(org.junit.Test)

Example 72 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestBatch method testBatchUpdateOfLabels.

// this test a 'contains' bug in the update of labels batch logic
@Test
public void testBatchUpdateOfLabels() throws Exception {
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "mike");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Car", "name", "bmw");
    Vertex v3 = this.sqlgGraph.addVertex(T.label, "Car", "name", "bmw");
    Vertex v4 = this.sqlgGraph.addVertex(T.label, "Bike", "name", "ktm");
    v1.addEdge("bts_aaaaaa", v2);
    v1.addEdge("bts_btsalmtos", v4);
    v1.addEdge("bts_btsalm", v3);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph = SqlgGraph.open(configuration)) {
        v1 = sqlgGraph.traversal().V(v1.id()).next();
        Assert.assertEquals(1, sqlgGraph.traversal().V(v1.id()).out("bts_btsalm").count().next().intValue());
        Assert.assertEquals(1, sqlgGraph.traversal().V(v1.id()).out("bts_btsalmtos").count().next().intValue());
    }
}
Also used : SqlgVertex(org.umlg.sqlg.structure.SqlgVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) BaseTest(org.umlg.sqlg.test.BaseTest)

Example 73 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestGis method testUpgradePostGisTypes.

@Test
public void testUpgradePostGisTypes() throws Exception {
    Point johannesburgPoint = new Point(26.2044, 28.0456);
    Vertex johannesburg = this.sqlgGraph.addVertex(T.label, "Gis", "point", johannesburgPoint);
    Point pretoriaPoint = new Point(25.7461, 28.1881);
    LineString lineString = new LineString(new Point[] { johannesburgPoint, pretoriaPoint });
    Vertex pretoria = this.sqlgGraph.addVertex(T.label, "Gis", "lineString", lineString);
    GeographyPoint geographyPointJohannesburg = new GeographyPoint(26.2044, 28.0456);
    Vertex johannesburgGeographyPoint = this.sqlgGraph.addVertex(T.label, "Gis", "geographyPoint", geographyPointJohannesburg);
    LinearRing linearRing = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
    Polygon polygon1 = new Polygon(new LinearRing[] { linearRing });
    Vertex johannesburgPolygon = this.sqlgGraph.addVertex(T.label, "Gis", "polygon", polygon1);
    linearRing = new LinearRing("0 0, 1 1, 1 2, 1 1, 0 0");
    GeographyPolygon geographyPolygon = new GeographyPolygon(new LinearRing[] { linearRing });
    Vertex johannesburgGeographyPolygon = this.sqlgGraph.addVertex(T.label, "Gis", "geographyPolygon", geographyPolygon);
    this.sqlgGraph.tx().commit();
    // Delete the topology
    Connection conn = this.sqlgGraph.tx().getConnection();
    try (Statement statement = conn.createStatement()) {
        statement.execute("DROP SCHEMA " + this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("sqlg_schema") + " CASCADE");
    }
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        List<Vertex> schemaVertices = sqlgGraph1.topology().V().hasLabel("sqlg_schema.schema").has("name", sqlgGraph1.getSqlDialect().getPublicSchema()).toList();
        Assert.assertEquals(1, schemaVertices.size());
        List<Vertex> propertyVertices = sqlgGraph1.topology().V().hasLabel("sqlg_schema.schema").has("name", sqlgGraph1.getSqlDialect().getPublicSchema()).out("schema_vertex").has("name", "Gis").out("vertex_property").has("name", "point").toList();
        Assert.assertEquals(1, propertyVertices.size());
        Assert.assertEquals("POINT", propertyVertices.get(0).value("type"));
        Assert.assertEquals(johannesburgPoint, sqlgGraph1.traversal().V(johannesburg.id()).next().value("point"));
        Assert.assertEquals(lineString, sqlgGraph1.traversal().V(pretoria.id()).next().value("lineString"));
        Assert.assertEquals(geographyPointJohannesburg, sqlgGraph1.traversal().V(johannesburgGeographyPoint.id()).next().value("geographyPoint"));
        Assert.assertEquals(polygon1, sqlgGraph1.traversal().V(johannesburgPolygon.id()).next().value("polygon"));
        Assert.assertEquals(geographyPolygon, sqlgGraph1.traversal().V(johannesburgGeographyPolygon.id()).next().value("geographyPolygon"));
    }
}
Also used : GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) LineString(org.postgis.LineString) Statement(java.sql.Statement) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) Connection(java.sql.Connection) Point(org.postgis.Point) GeographyPoint(org.umlg.sqlg.gis.GeographyPoint) LinearRing(org.postgis.LinearRing) GeographyPolygon(org.umlg.sqlg.gis.GeographyPolygon) Polygon(org.postgis.Polygon) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 74 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class SqlgChooseStepStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    List<ChooseStep> chooseSteps = TraversalHelper.getStepsOfAssignableClass(ChooseStep.class, traversal);
    for (ChooseStep<S, E, M> chooseStep : chooseSteps) {
        Traversal.Admin<S, M> predicateTraversal = chooseStep.getLocalChildren().get(0);
        // The predicate branch step is a local traversal.
        // As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, predicateTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }
        if (predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1) instanceof HasNextStep) {
            predicateTraversal.removeStep(predicateTraversal.getSteps().get(predicateTraversal.getSteps().size() - 1));
        }
        SqlgBranchStepBarrier sqlgBranchStepBarrier = new SqlgChooseStepBarrier<>(traversal, predicateTraversal);
        for (String label : chooseStep.getLabels()) {
            sqlgBranchStepBarrier.addLabel(label);
        }
        try {
            Field traversalOptionsField = chooseStep.getClass().getSuperclass().getDeclaredField("traversalOptions");
            traversalOptionsField.setAccessible(true);
            Map<M, List<Traversal.Admin<S, E>>> traversalOptions = (Map<M, List<Traversal.Admin<S, E>>>) traversalOptionsField.get(chooseStep);
            for (Map.Entry<M, List<Traversal.Admin<S, E>>> entry : traversalOptions.entrySet()) {
                for (Traversal.Admin<S, E> admin : entry.getValue()) {
                    sqlgBranchStepBarrier.addGlobalChildOption(entry.getKey(), admin);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        TraversalHelper.replaceStep(chooseStep, sqlgBranchStepBarrier, chooseStep.getTraversal());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) ChooseStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.ChooseStep) Field(java.lang.reflect.Field) List(java.util.List) ReducingBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep) HasNextStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.HasNextStep) SqlgChooseStepBarrier(org.umlg.sqlg.step.barrier.SqlgChooseStepBarrier) Map(java.util.Map) SqlgBranchStepBarrier(org.umlg.sqlg.step.barrier.SqlgBranchStepBarrier)

Example 75 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class SqlgWhereTraversalStepStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    List<WhereTraversalStep> whereTraversalSteps = TraversalHelper.getStepsOfAssignableClass(WhereTraversalStep.class, traversal);
    for (WhereTraversalStep<S> whereTraversalStep : whereTraversalSteps) {
        List<Traversal.Admin<?, ?>> whereTraversals = whereTraversalStep.getLocalChildren();
        Preconditions.checkState(whereTraversals.size() == 1);
        Traversal.Admin<?, ?> whereTraversal = whereTraversals.get(0);
        // reducing barrier steps like count does not work with Sqlg's barrier optimizations
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClassRecursively(ReducingBarrierStep.class, whereTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }
        SqlgWhereTraversalStepBarrier sqlgTraversalFilterStepBarrier = new SqlgWhereTraversalStepBarrier<>(traversal, whereTraversalStep);
        for (String label : whereTraversalStep.getLabels()) {
            sqlgTraversalFilterStepBarrier.addLabel(label);
        }
        TraversalHelper.replaceStep(whereTraversalStep, sqlgTraversalFilterStepBarrier, whereTraversalStep.getTraversal());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) WhereTraversalStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WhereTraversalStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) SqlgWhereTraversalStepBarrier(org.umlg.sqlg.step.barrier.SqlgWhereTraversalStepBarrier) ReducingBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)

Aggregations

SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)75 Test (org.junit.Test)68 BaseTest (org.umlg.sqlg.test.BaseTest)64 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)44 ConfigurationException (org.apache.commons.configuration.ConfigurationException)10 PropertyVetoException (java.beans.PropertyVetoException)9 IOException (java.io.IOException)9 Connection (java.sql.Connection)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)6 ResultSet (java.sql.ResultSet)4 Configuration (org.apache.commons.configuration.Configuration)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 ReducingBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)4 Statement (java.sql.Statement)3 PropertyType (org.umlg.sqlg.structure.PropertyType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 URL (java.net.URL)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 PreparedStatement (java.sql.PreparedStatement)2