Search in sources :

Example 6 with EventStrategy

use of org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy 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 7 with EventStrategy

use of org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy in project sqlg by pietermartin.

the class TestDropStepTruncate method before.

@Before
public void before() throws Exception {
    super.before();
    configuration.setProperty("implement.foreign.keys", this.fkOn);
    this.removedVertices.clear();
    if (this.mutatingCallback) {
        final MutationListener listener = new AbstractMutationListener() {

            @Override
            public void vertexRemoved(final Vertex vertex) {
                removedVertices.add(vertex);
            }

            @Override
            public void edgeRemoved(final Edge edge) {
                removedEdges.add(edge);
            }
        };
        final EventStrategy.Builder builder = EventStrategy.build().addListener(listener);
        eventStrategy = builder.create();
        this.dropTraversal = this.sqlgGraph.traversal();
        if (this.mutatingCallback) {
            this.dropTraversal = this.dropTraversal.withStrategies(this.eventStrategy);
        }
    } else {
        this.dropTraversal = this.sqlgGraph.traversal();
    }
}
Also used : MutationListener(org.apache.tinkerpop.gremlin.process.traversal.step.util.event.MutationListener) EventStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy) Before(org.junit.Before)

Aggregations

EventStrategy (org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy)7 MutationListener (org.apache.tinkerpop.gremlin.process.traversal.step.util.event.MutationListener)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Before (org.junit.Before)3 Test (org.junit.Test)3 BaseTest (org.umlg.sqlg.test.BaseTest)3 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 ReferenceVertex (org.apache.tinkerpop.gremlin.structure.util.reference.ReferenceVertex)2 Preconditions (com.google.common.base.Preconditions)1 java.util (java.util)1 MultiValuedMap (org.apache.commons.collections4.MultiValuedMap)1 HashSetValuedHashMap (org.apache.commons.collections4.multimap.HashSetValuedHashMap)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AbstractGremlinTest (org.apache.tinkerpop.gremlin.AbstractGremlinTest)1 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)1 Traverser (org.apache.tinkerpop.gremlin.process.traversal.Traverser)1 Mutating (org.apache.tinkerpop.gremlin.process.traversal.step.Mutating)1 CallbackRegistry (org.apache.tinkerpop.gremlin.process.traversal.step.util.event.CallbackRegistry)1 Event (org.apache.tinkerpop.gremlin.process.traversal.step.util.event.Event)1 ListCallbackRegistry (org.apache.tinkerpop.gremlin.process.traversal.step.util.event.ListCallbackRegistry)1