Search in sources :

Example 1 with SqlgGraph

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

the class SqlgHasStepStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    List<HasStep> hasSteps = TraversalHelper.getStepsOfAssignableClass(HasStep.class, traversal);
    for (HasStep<?> hasStep : hasSteps) {
        SqlgHasStep sqlgHasStep = new SqlgHasStep(hasStep.getTraversal(), hasStep.getHasContainers().toArray(new HasContainer[] {}));
        for (String label : hasStep.getLabels()) {
            sqlgHasStep.addLabel(label);
        }
        TraversalHelper.replaceStep(hasStep, sqlgHasStep, hasStep.getTraversal());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasStep) SqlgHasStep(org.umlg.sqlg.step.SqlgHasStep) SqlgHasStep(org.umlg.sqlg.step.SqlgHasStep) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 2 with SqlgGraph

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

the class SqlgWhereStrategy method apply.

@SuppressWarnings("resource")
@Override
public void apply(Admin<?, ?> traversal) {
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    SqlgGraph sqlgGraph = (SqlgGraph) traversal.getGraph().get();
    // The query will read from the cache if this is for a cached vertex
    if (sqlgGraph.features().supportsBatchMode() && sqlgGraph.tx().isInNormalBatchMode()) {
        sqlgGraph.tx().flush();
    }
    List<Step<?, ?>> steps = new ArrayList(traversal.asAdmin().getSteps());
    ListIterator<Step<?, ?>> stepIterator = steps.listIterator();
    // get all steps per label
    Map<String, Object> stepsByLabel = new HashMap<>();
    stepIterator = steps.listIterator();
    Step<?, ?> previous = null;
    int idx = 0;
    while (stepIterator.hasNext()) {
        Step<?, ?> step = stepIterator.next();
        captureLabels(step, stepsByLabel);
        if (step instanceof WherePredicateStep<?>) {
            WherePredicateStep<?> wps = (WherePredicateStep<?>) step;
            if (wps.getPredicate().isPresent() && wps.getPredicate().get().getBiPredicate() instanceof FullText) {
                Object referTo = previous;
                if (wps.getStartKey().isPresent()) {
                    referTo = stepsByLabel.get(wps.getStartKey().get());
                }
                if (referTo instanceof SqlgGraphStep<?, ?>) {
                    SqlgGraphStep<?, ?> sgs = (SqlgGraphStep<?, ?>) referTo;
                    if (sgs.getReplacedSteps().size() > 0) {
                        referTo = sgs.getReplacedSteps().get(sgs.getReplacedSteps().size() - 1);
                    }
                }
                if (referTo instanceof ReplacedStep<?, ?>) {
                    ReplacedStep<?, ?> rs = (ReplacedStep<?, ?>) referTo;
                    rs.addHasContainer(new HasContainer("__dummy__", wps.getPredicate().get()));
                    traversal.removeStep(idx);
                }
            }
        }
        previous = step;
        idx++;
    }
}
Also used : WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) Step(org.apache.tinkerpop.gremlin.process.traversal.Step) WherePredicateStep(org.apache.tinkerpop.gremlin.process.traversal.step.filter.WherePredicateStep) ReplacedStep(org.umlg.sqlg.sql.parse.ReplacedStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) SqlgGraphStep(org.umlg.sqlg.step.SqlgGraphStep) FullText(org.umlg.sqlg.predicate.FullText)

Example 3 with SqlgGraph

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

the class SqlgOptionalStepStrategy method apply.

@Override
public void apply(final Traversal.Admin<?, ?> traversal) {
    // noinspection OptionalGetWithoutIsPresent
    if (!(traversal.getGraph().get() instanceof SqlgGraph)) {
        return;
    }
    List<OptionalStep> optionalSteps = TraversalHelper.getStepsOfAssignableClass(OptionalStep.class, traversal);
    for (OptionalStep<S> optionalStep : optionalSteps) {
        // The predicate branch step is a local traversal.
        // As such if it contains a ReducingBarrierStep the SqlgBranchStepBarrier will not work.
        Traversal.Admin<S, S> optionalTraversal = optionalStep.getLocalChildren().get(0);
        List<ReducingBarrierStep> reducingBarrierSteps = TraversalHelper.getStepsOfAssignableClass(ReducingBarrierStep.class, optionalTraversal);
        if (!reducingBarrierSteps.isEmpty()) {
            continue;
        }
        SqlgOptionalStepBarrier sqlgOptionalStepBarrier = new SqlgOptionalStepBarrier<>(traversal, optionalTraversal);
        for (String label : optionalStep.getLabels()) {
            sqlgOptionalStepBarrier.addLabel(label);
        }
        TraversalHelper.replaceStep(optionalStep, sqlgOptionalStepBarrier, optionalStep.getTraversal());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) OptionalStep(org.apache.tinkerpop.gremlin.process.traversal.step.branch.OptionalStep) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) SqlgOptionalStepBarrier(org.umlg.sqlg.step.barrier.SqlgOptionalStepBarrier) ReducingBarrierStep(org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)

Example 4 with SqlgGraph

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

the class SqlgTraversalFilterStepStrategy method apply.

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

Example 5 with SqlgGraph

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

the class TestMultiThread method testMultipleGraphs.

/**
 * test when each graph is created in its own thread, in distributed mode
 *
 * @throws Exception
 */
@Test
public void testMultipleGraphs() throws Exception {
    URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
    try {
        configuration = new PropertiesConfiguration(sqlProperties);
        Assume.assumeTrue(isPostgres());
        configuration.addProperty("distributed", true);
        if (!configuration.containsKey("jdbc.url"))
            throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
    ExecutorService executorService = newFixedThreadPool(50);
    int loop = 400;
    for (int i = 0; i < loop; i++) {
        String n = "person" + i;
        executorService.submit(() -> {
            try {
                try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
                    final Random random = new Random();
                    if (random.nextBoolean()) {
                        Vertex person = sqlgGraph1.addVertex(T.label, "Person_True", "name", n);
                        Vertex address = sqlgGraph1.addVertex(T.label, "Address_True", "name", n);
                        person.addEdge("address_True", address, "name", n);
                    } else {
                        Vertex person = sqlgGraph1.addVertex(T.label, "Person", "name", n);
                        Vertex address = sqlgGraph1.addVertex(T.label, "Address", "name", n);
                        person.addEdge("address", address, "name", n);
                    }
                    sqlgGraph1.tx().commit();
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                fail(e.getMessage());
            }
        });
    }
    executorService.shutdown();
    executorService.awaitTermination(100, TimeUnit.SECONDS);
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        Assert.assertEquals(400, sqlgGraph1.traversal().V().hasLabel("Person_True").count().next().longValue() + sqlgGraph1.traversal().V().hasLabel("Person").count().next().longValue());
        Assert.assertEquals(400, sqlgGraph1.traversal().V().hasLabel("Address_True").count().next().longValue() + sqlgGraph1.traversal().V().hasLabel("Address").count().next().longValue());
        Assert.assertEquals(400, sqlgGraph1.traversal().E().hasLabel("address_True").count().next().longValue() + sqlgGraph1.traversal().E().hasLabel("address").count().next().longValue());
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Random(java.util.Random) ConfigurationException(org.apache.commons.configuration.ConfigurationException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) URL(java.net.URL) ConfigurationException(org.apache.commons.configuration.ConfigurationException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

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