Search in sources :

Example 16 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.

the class TestTraversalPerformance method testSpeedWithLargeSchemaFastQuery1.

@Test
public void testSpeedWithLargeSchemaFastQuery1() {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    Map<String, PropertyType> columns = new HashMap<>();
    for (int i = 0; i < 100; i++) {
        columns.put("property_" + i, PropertyType.STRING);
    }
    // Create a large schema, it slows the maps  down
    int NUMBER_OF_SCHEMA_ELEMENTS = 1_000;
    for (int i = 0; i < NUMBER_OF_SCHEMA_ELEMENTS; i++) {
        VertexLabel person = this.sqlgGraph.getTopology().ensureVertexLabelExist("Person_" + i, columns);
        VertexLabel dog = this.sqlgGraph.getTopology().ensureVertexLabelExist("Dog_" + i, columns);
        person.ensureEdgeLabelExist("pet_" + i, dog, columns);
        if (i % 100 == 0) {
            this.sqlgGraph.tx().commit();
        }
    }
    this.sqlgGraph.tx().commit();
    stopWatch.stop();
    System.out.println("done creating schema time taken " + stopWatch.toString());
    stopWatch.reset();
    stopWatch.start();
    Map<String, Object> columnValues = new HashMap<>();
    for (int i = 0; i < 100; i++) {
        columnValues.put("property_" + i, "asdasdasd");
    }
    for (int i = 0; i < NUMBER_OF_SCHEMA_ELEMENTS; i++) {
        SqlgVertex person = (SqlgVertex) this.sqlgGraph.addVertex("Person_" + i, columnValues);
        SqlgVertex dog = (SqlgVertex) this.sqlgGraph.addVertex("Dog_" + i, columnValues);
        person.addEdgeWithMap("pet_" + i, dog, columnValues);
    }
    this.sqlgGraph.tx().commit();
    stopWatch.stop();
    System.out.println("done inserting data time taken " + stopWatch.toString());
    stopWatch.reset();
    stopWatch.start();
    for (int i = 0; i < 10_000; i++) {
        Assert.assertEquals(1, this.sqlgGraph.traversal().V().hasLabel("Person_0").out("pet_0").toList().size());
    }
    stopWatch.stop();
    System.out.println("total query time " + stopWatch.toString());
}
Also used : HashMap(java.util.HashMap) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) SqlgVertex(org.umlg.sqlg.structure.SqlgVertex) PropertyType(org.umlg.sqlg.structure.PropertyType) StopWatch(org.apache.commons.lang3.time.StopWatch) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 17 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.

the class TestGremlinCompileFullTextPredicate method testPerfOneColumn.

@Test
public // @Ignore("check manually index is used")
void testPerfOneColumn() throws SQLException {
    assumeTrue(isPostgres());
    Vertex v0 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "a fat cat sat on a mat and ate a fat rat");
    int LOOPS = 10000;
    for (int a = 0; a < LOOPS; a++) {
        this.sqlgGraph.addVertex(T.label, "Sentence", "name", "loop" + a);
    }
    VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
    vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Collections.singletonList(vl.getProperty("name").get()));
    this.sqlgGraph.tx().commit();
    long t0 = System.currentTimeMillis();
    List<Vertex> vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", "fat & rat")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v0));
    long t1 = System.currentTimeMillis();
    long delta = t1 - t0;
    System.out.println("query time:" + delta + "ms");
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 18 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.

the class TestGremlinCompileFullTextPredicate method testConcat.

@Test
public void testConcat() throws SQLException {
    assumeTrue(isPostgres());
    Vertex v0 = this.sqlgGraph.addVertex(T.label, "Sentence", "name1", "a fat cat sat on a", "name2", "mat and ate a fat rat");
    this.sqlgGraph.addVertex(T.label, "Sentence", "name1", "fatal error");
    this.sqlgGraph.addVertex(T.label, "Sentence", "name1", "error is not fatal");
    VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
    vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Arrays.asList(vl.getProperty("name1").get(), vl.getProperty("name2").get()));
    this.sqlgGraph.tx().commit();
    List<Vertex> vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, Arrays.asList("name1", "name2"), "fat & rat")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v0));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 19 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.

the class SchemaTableTree method hasNoEdgeLabels.

private boolean hasNoEdgeLabels(SchemaTable schemaTable) {
    Optional<Schema> schemaOptional = sqlgGraph.getTopology().getSchema(schemaTable.getSchema());
    Preconditions.checkState(schemaOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.getSchema());
    Schema schema = schemaOptional.get();
    boolean result = true;
    if (schemaTable.isVertexTable()) {
        // Need to delete any in/out edges.
        Optional<VertexLabel> vertexLabelOptional = schema.getVertexLabel(schemaTable.withOutPrefix().getTable());
        Preconditions.checkState(vertexLabelOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.withOutPrefix().getTable());
        VertexLabel vertexLabel = vertexLabelOptional.get();
        Collection<EdgeLabel> outEdgeLabels = vertexLabel.getOutEdgeLabels().values();
        Collection<EdgeLabel> inEdgeLabels = vertexLabel.getInEdgeLabels().values();
        result = outEdgeLabels.isEmpty() && inEdgeLabels.isEmpty();
    }
    return result;
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) Schema(org.umlg.sqlg.structure.topology.Schema) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel)

Example 20 with VertexLabel

use of org.umlg.sqlg.structure.topology.VertexLabel in project sqlg by pietermartin.

the class SqlgDropStepBarrier method processNextStart.

@Override
protected Traverser.Admin<S> processNextStart() {
    if (this.first) {
        this.first = false;
        EventStrategy eventStrategy = null;
        if (!this.callbackRegistry.getCallbacks().isEmpty()) {
            eventStrategy = getTraversal().getStrategies().getStrategy(EventStrategy.class).get();
        }
        while (this.starts.hasNext()) {
            Traverser.Admin<S> start = this.starts.next();
            Object object = start.get();
            if (object instanceof SqlgElement) {
                SqlgElement sqlgElement = (SqlgElement) object;
                RecordId recordId = (RecordId) sqlgElement.id();
                SchemaTable schemaTable = recordId.getSchemaTable();
                Long id = recordId.getId();
                if (sqlgElement instanceof SqlgVertex) {
                    Optional<VertexLabel> vertexLabelOptional = this.sqlgGraph.getTopology().getVertexLabel(schemaTable.getSchema(), schemaTable.getTable());
                    Preconditions.checkState(vertexLabelOptional.isPresent());
                    SqlgVertex sqlgVertex = (SqlgVertex) sqlgElement;
                    boolean added = this.verticesToDelete.put(vertexLabelOptional.get(), id);
                    if (added && eventStrategy != null) {
                        final Event removeEvent = new Event.VertexRemovedEvent(eventStrategy.detach(sqlgVertex));
                        this.callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
                    }
                    for (EdgeLabel outEdgeLabel : vertexLabelOptional.get().getOutEdgeLabels().values()) {
                        // Get all the edges, register to the callBack and delete.
                        if (eventStrategy != null) {
                            Iterator<Edge> edges = sqlgVertex.edges(Direction.OUT);
                            while (edges.hasNext()) {
                                Edge edge = edges.next();
                                SchemaTable schemaTableEdge = ((SqlgEdge) edge).getSchemaTablePrefixed().withOutPrefix();
                                Optional<EdgeLabel> edgeLabelOptional = this.sqlgGraph.getTopology().getEdgeLabel(schemaTableEdge.getSchema(), schemaTableEdge.getTable());
                                Preconditions.checkState(edgeLabelOptional.isPresent());
                                added = this.edgesToDelete.put(edgeLabelOptional.get(), ((RecordId) edge.id()).getId());
                                if (added) {
                                    final Event removeEvent = new Event.EdgeRemovedEvent(eventStrategy.detach(edge));
                                    this.callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
                                }
                            }
                        } else {
                            this.foreignKeyOutEdgesToDelete.put(Pair.of(outEdgeLabel, vertexLabelOptional.get()), id);
                        }
                    }
                    for (EdgeLabel inEdgeLabel : vertexLabelOptional.get().getInEdgeLabels().values()) {
                        // Get all the edges, register to the callBack and delete.
                        if (!this.callbackRegistry.getCallbacks().isEmpty()) {
                            Iterator<Edge> edges = sqlgVertex.edges(Direction.IN);
                            while (edges.hasNext()) {
                                Edge edge = edges.next();
                                SchemaTable schemaTableEdge = ((SqlgEdge) edge).getSchemaTablePrefixed().withOutPrefix();
                                Optional<EdgeLabel> edgeLabelOptional = this.sqlgGraph.getTopology().getEdgeLabel(schemaTableEdge.getSchema(), schemaTableEdge.getTable());
                                Preconditions.checkState(edgeLabelOptional.isPresent());
                                added = this.edgesToDelete.put(edgeLabelOptional.get(), ((RecordId) edge.id()).getId());
                                if (added) {
                                    final Event removeEvent = new Event.EdgeRemovedEvent(eventStrategy.detach(edge));
                                    this.callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
                                }
                            }
                        } else {
                            this.foreignKeyInEdgesToDelete.put(Pair.of(inEdgeLabel, vertexLabelOptional.get()), id);
                        }
                    }
                } else if (sqlgElement instanceof SqlgEdge) {
                    Optional<EdgeLabel> edgeLabelOptional = this.sqlgGraph.getTopology().getEdgeLabel(schemaTable.getSchema(), schemaTable.getTable());
                    Preconditions.checkState(edgeLabelOptional.isPresent());
                    boolean added = this.edgesToDelete.put(edgeLabelOptional.get(), id);
                    if (added && eventStrategy != null) {
                        final Event removeEvent = new Event.EdgeRemovedEvent(eventStrategy.detach((SqlgEdge) sqlgElement));
                        this.callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
                    }
                }
            } else if (object instanceof SqlgProperty) {
                SqlgProperty sqlgProperty = (SqlgProperty) object;
                if (eventStrategy != null) {
                    final Event removeEvent;
                    if (sqlgProperty.element() instanceof Edge) {
                        removeEvent = new Event.EdgePropertyRemovedEvent(eventStrategy.detach((Edge) sqlgProperty.element()), eventStrategy.detach(sqlgProperty));
                    } else if (sqlgProperty instanceof VertexProperty)
                        removeEvent = new Event.VertexPropertyRemovedEvent(eventStrategy.detach((VertexProperty) sqlgProperty));
                    else
                        throw new IllegalStateException("The incoming object is not removable: " + object);
                    this.callbackRegistry.getCallbacks().forEach(c -> c.accept(removeEvent));
                }
                sqlgProperty.remove();
            } else {
                throw new IllegalStateException("Expected SqlgElement or SqlgProperty. Found " + object.getClass().getSimpleName());
            }
        }
    }
    for (Pair<EdgeLabel, VertexLabel> edgeLabelVertexLabelPair : this.foreignKeyOutEdgesToDelete.keySet()) {
        EdgeLabel outEdgeLabel = edgeLabelVertexLabelPair.getKey();
        VertexLabel vertexLabel = edgeLabelVertexLabelPair.getValue();
        Collection<Long> ids = this.foreignKeyOutEdgesToDelete.get(edgeLabelVertexLabelPair);
        String sql = this.sqlgGraph.getSqlDialect().dropWithForeignKey(true, outEdgeLabel, vertexLabel, ids, !this.callbackRegistry.getCallbacks().isEmpty());
        SqlgSqlExecutor.executeDropEdges(this.sqlgGraph, outEdgeLabel, sql, this.callbackRegistry.getCallbacks());
    }
    for (Pair<EdgeLabel, VertexLabel> edgeLabelVertexLabelPair : this.foreignKeyInEdgesToDelete.keySet()) {
        EdgeLabel inEdgeLabel = edgeLabelVertexLabelPair.getKey();
        VertexLabel vertexLabel = edgeLabelVertexLabelPair.getValue();
        Collection<Long> ids = this.foreignKeyInEdgesToDelete.get(edgeLabelVertexLabelPair);
        String sql = this.sqlgGraph.getSqlDialect().dropWithForeignKey(false, inEdgeLabel, vertexLabel, ids, !this.callbackRegistry.getCallbacks().isEmpty());
        SqlgSqlExecutor.executeDropEdges(this.sqlgGraph, inEdgeLabel, sql, this.callbackRegistry.getCallbacks());
    }
    for (EdgeLabel edgeLabel : this.edgesToDelete.keySet()) {
        Collection<Long> ids = this.edgesToDelete.get(edgeLabel);
        String sql = this.sqlgGraph.getSqlDialect().drop(edgeLabel, ids);
        SqlgSqlExecutor.executeDrop(this.sqlgGraph, sql);
    }
    for (VertexLabel vertexLabel : this.verticesToDelete.keySet()) {
        Collection<Long> ids = this.verticesToDelete.get(vertexLabel);
        String sql = this.sqlgGraph.getSqlDialect().drop(vertexLabel, ids);
        SqlgSqlExecutor.executeDrop(this.sqlgGraph, sql);
    }
    // The standard TraversalFilterStep.filter calls TraversalUtil.test which normally resets the traversal for every incoming start.
    reset();
    throw FastNoSuchElementException.instance();
}
Also used : EventStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy) org.umlg.sqlg.structure(org.umlg.sqlg.structure) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) java.util(java.util) Event(org.apache.tinkerpop.gremlin.process.traversal.step.util.event.Event) MultiValuedMap(org.apache.commons.collections4.MultiValuedMap) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) ListCallbackRegistry(org.apache.tinkerpop.gremlin.process.traversal.step.util.event.ListCallbackRegistry) SqlgSqlExecutor(org.umlg.sqlg.strategy.SqlgSqlExecutor) HashSetValuedHashMap(org.apache.commons.collections4.multimap.HashSetValuedHashMap) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Pair(org.apache.commons.lang3.tuple.Pair) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Mutating(org.apache.tinkerpop.gremlin.process.traversal.step.Mutating) Preconditions(com.google.common.base.Preconditions) SqlgFilterStep(org.umlg.sqlg.step.SqlgFilterStep) CallbackRegistry(org.apache.tinkerpop.gremlin.process.traversal.step.util.event.CallbackRegistry) FastNoSuchElementException(org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException) Edge(org.apache.tinkerpop.gremlin.structure.Edge) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) EventStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) Traverser(org.apache.tinkerpop.gremlin.process.traversal.Traverser) Event(org.apache.tinkerpop.gremlin.process.traversal.step.util.event.Event) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Aggregations

VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)31 BaseTest (org.umlg.sqlg.test.BaseTest)23 Test (org.junit.Test)22 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)17 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)11 PropertyColumn (org.umlg.sqlg.structure.topology.PropertyColumn)9 Schema (org.umlg.sqlg.structure.topology.Schema)7 PropertyVetoException (java.beans.PropertyVetoException)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 PropertyType (org.umlg.sqlg.structure.PropertyType)3 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 SqlgSqlExecutor (org.umlg.sqlg.strategy.SqlgSqlExecutor)2 org.umlg.sqlg.structure (org.umlg.sqlg.structure)2 GlobalUniqueIndex (org.umlg.sqlg.structure.topology.GlobalUniqueIndex)2 Preconditions (com.google.common.base.Preconditions)1 java.util (java.util)1 Collections (java.util.Collections)1 Map (java.util.Map)1