Search in sources :

Example 31 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty 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)

Example 32 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project timbuctoo by HuygensING.

the class PropertyTypesTest method localPropertyReturnsLocalPropertyWithCorrectPropNameAndCorrectConverter.

@Test
public void localPropertyReturnsLocalPropertyWithCorrectPropNameAndCorrectConverter() throws IOException {
    Converter converter = mock(Converter.class);
    JsonNode value = jsn("value");
    given(converter.jsonToTinkerpop(value)).willReturn("converted-value");
    LocalProperty instance = localProperty("propName", converter);
    Vertex vertex = mock(Vertex.class);
    VertexProperty vertexProperty = mock(VertexProperty.class);
    given(vertex.property("propName")).willReturn(vertexProperty);
    instance.setJson(vertex, null);
    instance.setJson(vertex, value);
    verify(vertex, times(1)).property("propName", "converted-value");
    verify(vertexProperty, times(1)).remove();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Converter(nl.knaw.huygens.timbuctoo.model.properties.converters.Converter) JsonNode(com.fasterxml.jackson.databind.JsonNode) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Test(org.junit.Test)

Example 33 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project timbuctoo by HuygensING.

the class PropertyTypesTest method localPropertyReturnsLocalPropertyWithCorrectPropNameAndStringConverter.

@Test
public void localPropertyReturnsLocalPropertyWithCorrectPropNameAndStringConverter() throws IOException {
    LocalProperty instance = localProperty("propName");
    Vertex vertex = mock(Vertex.class);
    VertexProperty vertexProperty = mock(VertexProperty.class);
    given(vertex.property("propName")).willReturn(vertexProperty);
    instance.setJson(vertex, null);
    instance.setJson(vertex, jsn("value"));
    verify(vertex, times(1)).property("propName", "value");
    verify(vertexProperty, times(1)).remove();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Test(org.junit.Test)

Example 34 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project timbuctoo by HuygensING.

the class D3GraphTest method mockVertex.

private Vertex mockVertex(String timId, String type, String labelPropertyKey, String label) {
    Vertex mockDocumentVertex = mock(Vertex.class);
    VertexProperty timIdProperty = mock(VertexProperty.class);
    VertexProperty typesProperty = mock(VertexProperty.class);
    given(timIdProperty.value()).willReturn(timId);
    given(typesProperty.value()).willReturn("[\"" + type + "\"]");
    given(mockDocumentVertex.property("tim_id")).willReturn(timIdProperty);
    given(mockDocumentVertex.property("types")).willReturn(typesProperty);
    given(mockDocumentVertex.value(labelPropertyKey)).willReturn(label);
    given(mockDocumentVertex.keys()).willReturn(Sets.newHashSet(labelPropertyKey));
    return mockDocumentVertex;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Example 35 with VertexProperty

use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project timbuctoo by HuygensING.

the class VertexDuplicator method duplicateVertex.

public static Vertex duplicateVertex(GraphTraversalSource traversal, Vertex vertex, IndexHandler indexHandler) {
    Vertex duplicate = traversal.addV().next();
    for (Iterator<VertexProperty<Object>> properties = vertex.properties(); properties.hasNext(); ) {
        VertexProperty<Object> property = properties.next();
        duplicate.property(property.key(), property.value());
    }
    for (String label : ((Neo4jVertex) vertex).labels()) {
        ((Neo4jVertex) duplicate).addLabel(label);
    }
    moveIncomingEdges(vertex, duplicate, indexHandler);
    moveOutgoingEdges(vertex, duplicate, indexHandler);
    vertex.property(IS_LATEST, false);
    duplicate.property(IS_LATEST, true);
    vertex.addEdge(VERSION_OF, duplicate);
    return duplicate;
}
Also used : Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Neo4jVertex(org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jVertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty)

Aggregations

VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)37 Edge (org.apache.tinkerpop.gremlin.structure.Edge)18 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)14 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)14 Test (org.junit.jupiter.api.Test)13 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)12 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Test (org.junit.Test)9 PropertyKey (org.janusgraph.core.PropertyKey)7 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)5 EdgeLabel (org.janusgraph.core.EdgeLabel)5 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)4 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)4 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)4 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)4 Instant (java.time.Instant)4 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)4 VertexLabel (org.janusgraph.core.VertexLabel)4 AbstractEdge (org.janusgraph.graphdb.relations.AbstractEdge)4