Java Code Examples for org.apache.tinkerpop.gremlin.structure.Vertex#remove()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.Vertex#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRemoveAVertexFromAnIndex() {
    final TinkerGraph g = TinkerGraph.open();
    g.createIndex("name", Vertex.class);

    g.addVertex("name", "marko", "age", 29);
    g.addVertex("name", "stephen", "age", 35);
    final Vertex v = g.addVertex("name", "stephen", "age", 35);

    // a tricky way to evaluate if indices are actually being used is to pass a fake BiPredicate to has()
    // to get into the Pipeline and evaluate what's going through it.  in this case, we know that at index
    // is used because only "stephen" ages should pass through the pipeline due to the inclusion of the
    // key index lookup on "name".  If there's an age of something other than 35 in the pipeline being evaluated
    // then something is wrong.
    assertEquals(new Long(2), g.traversal().V().has("age", P.test((t, u) -> {
        assertEquals(35, t);
        return true;
    }, 35)).has("name", "stephen").count().next());

    v.remove();
    assertEquals(new Long(1), g.traversal().V().has("age", P.test((t, u) -> {
        assertEquals(35, t);
        return true;
    }, 35)).has("name", "stephen").count().next());
}
 
Example 2
Source File: TestDeletedVertex.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testDeletedVertexUserSuppliedPK() {
    this.sqlgGraph.getTopology().getPublicSchema()
            .ensureVertexLabelExist(
                    "Person",
                    new HashMap<String, PropertyType>() {{
                        put("name", PropertyType.varChar(100));
                    }},
                    ListOrderedSet.listOrderedSet(Collections.singletonList("name"))
            );
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "marko");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person", "name", "pieter");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().close();
    v1.remove();
    this.sqlgGraph.tx().commit();
    Assert.assertFalse(this.sqlgGraph.traversal().V(v1.id()).hasNext());
    Assert.assertTrue(this.sqlgGraph.traversal().V(v2.id()).hasNext());
}
 
Example 3
Source File: TestBatch.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testBatchRemoveVertexUsersSuppliedPK() {
    this.sqlgGraph.getTopology().getPublicSchema()
            .ensureVertexLabelExist(
                    "Person",
                    new HashMap<String, PropertyType>() {{
                        put("uid1", PropertyType.varChar(100));
                        put("uid2", PropertyType.varChar(100));
                    }},
                    ListOrderedSet.listOrderedSet(Arrays.asList("uid1", "uid2"))
            );
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "uid1", UUID.randomUUID().toString(), "uid2", UUID.randomUUID().toString());
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person", "uid1", UUID.randomUUID().toString(), "uid2", UUID.randomUUID().toString());
    Vertex v3 = this.sqlgGraph.addVertex(T.label, "Person", "uid1", UUID.randomUUID().toString(), "uid2", UUID.randomUUID().toString());
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    v1.remove();
    v2.remove();
    v3.remove();
    this.sqlgGraph.tx().commit();
    testBatchRemoveVertex_assert();
}
 
Example 4
Source File: SetInPropertiesTest.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testSetWithBlankPrefixHandling() throws Exception
{
    try (GraphContext context = contextFactory.create(true))
    {
        TestSetBlankSubModel frame = prepareFrame(context, TestSetBlankSubModel.class);
        System.out.println("    Frame class: " + frame.getClass());

        Vertex v = new GraphService<>(context, TestSetBlankSubModel.class).getUnique().getElement();
        Assert.assertNotNull(v);

        v.property("still here", "does't matter");
        TestSetBlankSubModel framed = (TestSetBlankSubModel) context.getFramed().frameElement(v, TestSetBlankSubModel.class);
        checkSet(framed.getSet(), 5);
        for (String string : framed.getElement().keys())
        {
            System.out.println("    Key: " + string);
        }
        Assert.assertTrue(framed.getSet().contains("still here"));
        v.remove();
    }
}
 
Example 5
Source File: TestRollback.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void shouldRollbackElementAutoTransactionByDefault() {
    assertVertexEdgeCounts(this.sqlgGraph, 0, 0);
    Vertex v1 = this.sqlgGraph.addVertex();
    @SuppressWarnings("UnusedAssignment")
    Edge e1 = v1.addEdge("l", v1);
    this.sqlgGraph.tx().commit();
    assertVertexEdgeCounts(this.sqlgGraph, 1, 1);
    v1.remove();
    this.sqlgGraph.tx().commit();
    assertVertexEdgeCounts(this.sqlgGraph, 0, 0);

    v1 = this.sqlgGraph.addVertex();
    e1 = v1.addEdge("l", v1);
    assertVertexEdgeCounts(this.sqlgGraph, 1, 1);
    assertEquals(v1.id(), this.sqlgGraph.vertices(v1.id()).next().id());
    assertEquals(e1.id(), this.sqlgGraph.edges(e1.id()).next().id());
    this.sqlgGraph.traversal().tx().rollback();
    assertVertexEdgeCounts(this.sqlgGraph, 0, 0);
}
 
Example 6
Source File: MapInPropertiesTest.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testMapWithBlankPrefixHandling() throws Exception
{
    try (GraphContext context = contextFactory.create(true))
    {
        TestMapBlankSubModel frame = prepareFrame(context, TestMapBlankSubModel.class);
        System.out.println("    Frame class: " + frame.getClass());

        Vertex v = new GraphService<>(context, TestMapBlankSubModel.class).getUnique().getElement();
        Assert.assertNotNull(v);

        v.property("preexistingKey", "still here");
        TestMapBlankSubModel framed = (TestMapBlankSubModel) context.getFramed().frameElement(v, TestMapBlankSubModel.class);
        checkMap(framed.getMap(), 4);
        for (String string : framed.getElement().keys())
        {
            System.out.println("    Key: " + string);
        }
        Assert.assertEquals("still here", framed.getMap().get("preexistingKey"));
        v.remove();
    }
}
 
Example 7
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void shouldNotModifyAVertexThatWasRemoved() {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    v.property("name", "stephen");

    assertEquals("stephen", v.value("name"));
    v.remove();

    v.property("status", 1);
}
 
Example 8
Source File: TinkerGraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void shouldNotReadValueOfPropertyOnVertexThatWasRemoved() {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    v.property("name", "stephen");

    assertEquals("stephen", v.value("name"));
    v.remove();
    v.value("name");
}
 
Example 9
Source File: SetInPropertiesTest.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This doesn't use submodel.
 */
@Test
public void testSetWithBlankPrefixHandling2() throws Exception
{
    try (GraphContext context = contextFactory.create(true))
    {
        TestSetBlankModel frame = context.getFramed().addFramedVertex(TestSetBlankModel.class);
        Set<String> set = prepareSet();
        frame.addAllNaturalSet(set);

        System.out.println("    Frame class: " + frame.getClass());
        for (Class<?> iface : frame.getClass().getInterfaces())
        {
            System.out.println("      Implements: " + iface.getName());
        }

        Vertex v = new GraphService<>(context, TestSetBlankModel.class).getUnique().getElement();

        Assert.assertNotNull(v);
        v.property("still here", "does't matter");
        TestSetBlankSubModel framed = (TestSetBlankSubModel) context.getFramed().frameElement(v, TestSetBlankSubModel.class);
        checkSet(framed.getSet(), 5);
        for (String string : framed.getElement().keys())
        {
            System.out.println("    Key: " + string);
        }
        Assert.assertTrue(framed.getSet().contains("still here"));
        v.remove();
    }
}
 
Example 10
Source File: TestBatch.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testBatchRemoveVertex() {
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person");
    Vertex v3 = this.sqlgGraph.addVertex(T.label, "Person");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    v1.remove();
    v2.remove();
    v3.remove();
    this.sqlgGraph.tx().commit();
    testBatchRemoveVertex_assert();
}
 
Example 11
Source File: TransactionImpl.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private void merge(ConceptId duplicateId, ConceptId targetId) {
    GraphTraversalSource tinkerTraversal = janusTraversalSourceProvider.getTinkerTraversal();
    Vertex duplicate = tinkerTraversal.V(Schema.elementId(duplicateId)).next();
    Vertex mergeTargetV = tinkerTraversal.V(Schema.elementId(targetId)).next();

    duplicate.vertices(Direction.IN).forEachRemaining(connectedVertex -> {
        // merge attribute edge connecting 'duplicate' and 'connectedVertex' to 'mergeTargetV', if exists
        GraphTraversal<Vertex, Edge> attributeEdge =
                tinkerTraversal.V(duplicate).inE(Schema.EdgeLabel.ATTRIBUTE.getLabel()).filter(__.outV().is(connectedVertex));
        if (attributeEdge.hasNext()) {
            mergeAttributeEdge(mergeTargetV, connectedVertex, attributeEdge);
        }

        // merge role-player edge connecting 'duplicate' and 'connectedVertex' to 'mergeTargetV', if exists
        GraphTraversal<Vertex, Edge> rolePlayerEdge =
                tinkerTraversal.V(duplicate).inE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()).filter(__.outV().is(connectedVertex));
        if (rolePlayerEdge.hasNext()) {
            mergeRolePlayerEdge(mergeTargetV, rolePlayerEdge);
        }
        try {
            attributeEdge.close();
            rolePlayerEdge.close();
        } catch (Exception e) {
            LOG.warn("Error closing the merging traversals", e);
        }
    });
    duplicate.remove();
}
 
Example 12
Source File: TestGlobalUniqueIndex.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testVertexUniqueConstraintDelete() {
    VertexLabel vertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", new HashMap<String, PropertyType>() {{
        put("namea", PropertyType.STRING);
        put("nameb", PropertyType.STRING);
        put("namec", PropertyType.STRING);
    }});
    Set<PropertyColumn> properties = new HashSet<>(vertexLabel.getProperties().values());
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(properties);
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
    this.sqlgGraph.addVertex(T.label, "A", "namea", "a2");
    this.sqlgGraph.tx().commit();

    Assert.assertEquals(6, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());

    try {
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
        fail("GlobalUniqueIndex should prevent this form happening");
    } catch (Exception e) {
        //swallow
        this.sqlgGraph.tx().rollback();
    }
    a1.remove();
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(3, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());


    this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
    //this time it passes.
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(6, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());
}
 
Example 13
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void shouldNotReadValueOfPropertyOnVertexThatWasRemoved() {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    v.property("name", "stephen");

    assertEquals("stephen", v.value("name"));
    v.remove();
    v.value("name");
}
 
Example 14
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void shouldNotAddEdgeToAVertexThatWasRemoved() {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    v.property("name", "stephen");

    assertEquals("stephen", v.value("name"));
    v.remove();
    v.addEdge("self", v);
}
 
Example 15
Source File: TinkerGraphTest.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void shouldNotModifyAVertexThatWasRemoved() {
    final TinkerGraph graph = TinkerGraph.open();
    final Vertex v = graph.addVertex();
    v.property("name", "stephen");

    assertEquals("stephen", v.value("name"));
    v.remove();

    v.property("status", 1);
}
 
Example 16
Source File: TestDeletedVertex.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testRemoveOutVertexRemovesEdgesUserSuppliedPK() {
    VertexLabel personVertexLabel = this.sqlgGraph.getTopology().getPublicSchema()
            .ensureVertexLabelExist(
                    "Person",
                    new HashMap<String, PropertyType>() {{
                        put("uid1", PropertyType.varChar(100));
                        put("uid2", PropertyType.varChar(100));
                        put("name", PropertyType.STRING);
                    }},
                    ListOrderedSet.listOrderedSet(Arrays.asList("uid1", "uid2"))
            );
    VertexLabel dogVertexLabel = this.sqlgGraph.getTopology().getPublicSchema()
            .ensureVertexLabelExist(
                    "Dog",
                    new HashMap<String, PropertyType>() {{
                        put("uid1", PropertyType.varChar(100));
                        put("uid2", PropertyType.varChar(100));
                        put("name", PropertyType.STRING);
                    }},
                    ListOrderedSet.listOrderedSet(Arrays.asList("uid1", "uid2"))
            );
    personVertexLabel.ensureEdgeLabelExist(
            "friend",
            dogVertexLabel,
            new HashMap<String, PropertyType>() {{
                put("uid", PropertyType.varChar(100));
            }},
            ListOrderedSet.listOrderedSet(Collections.singletonList("uid"))
    );

    Vertex person = this.sqlgGraph.addVertex(T.label, "Person", "uid1", UUID.randomUUID().toString(), "uid2", UUID.randomUUID().toString(), "name", "marko");
    Vertex dog = this.sqlgGraph.addVertex(T.label, "Dog", "uid1", UUID.randomUUID().toString(), "uid2", UUID.randomUUID().toString(), "name", "snowy");
    person.addEdge("friend", dog, "uid", UUID.randomUUID().toString());
    this.sqlgGraph.tx().commit();
    person.remove();
    Assert.assertEquals(0, this.sqlgGraph.traversal().E().toList().size());
}
 
Example 17
Source File: TestDeletedVertex.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testDeletedVertex() {
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "marko");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person", "name", "pieter");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().close();
    v1.remove();
    this.sqlgGraph.tx().commit();
    Assert.assertFalse(this.sqlgGraph.traversal().V(v1.id()).hasNext());
    Assert.assertTrue(this.sqlgGraph.traversal().V(v2.id()).hasNext());
}
 
Example 18
Source File: TestOptionalWithOrder.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testOrder() {

    Vertex a1 = sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = sqlgGraph.addVertex(T.label, "B", "name", "b2");

    a1.addEdge("e_a", b1);
    a2.addEdge("e_a", b2);

    GraphTraversal<Vertex, Path> t = sqlgGraph.traversal().V(a1, a2).order().by("name").optional(__.out()).path();
    Assert.assertTrue(t.hasNext());
    Path p = t.next();
    Assert.assertEquals(2, p.size());
    Vertex v1 = p.get(0);
    Assert.assertEquals("a1", v1.property("name").value());
    Vertex v2 = p.get(1);
    Assert.assertEquals("b1", v2.property("name").value());
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(2, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a2", v1.property("name").value());
    v2 = p.get(1);
    Assert.assertEquals("b2", v2.property("name").value());

    b1.remove();
    this.sqlgGraph.tx().commit();

    List<Path> paths  = sqlgGraph.traversal().V(a2, a1).order().by("name").optional(__.out()).path().toList();
    Assert.assertEquals(2, paths.size());
    Assert.assertEquals(1, paths.get(0).size());

    t = sqlgGraph.traversal().V(a2, a1).order().by("name").optional(__.out()).path();
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(1, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a1", v1.property("name").value());
    Assert.assertTrue(t.hasNext());
    p = t.next();
    Assert.assertEquals(2, p.size());
    v1 = p.get(0);
    Assert.assertEquals("a2", v1.property("name").value());
    v2 = p.get(1);
    Assert.assertEquals("b2", v2.property("name").value());
}
 
Example 19
Source File: GraphOMRSMetadataStore.java    From egeria with Apache License 2.0 4 votes vote down vote up
synchronized void removeEntityProxyFromStore(String entityGUID)
{
    final String methodName = "removeEntityProxyFromStore";
    // TODO - could capture existing entity and move it to 'history'

    // Look in the graph

    GraphTraversalSource g = instanceGraph.traversal();


    GraphTraversal<Vertex, Vertex> gt = g.V().hasLabel("Entity").has(PROPERTY_KEY_ENTITY_GUID, entityGUID);

    // Only looking for proxy entities:
    gt = gt.has(PROPERTY_KEY_ENTITY_IS_PROXY, true);

    if (gt.hasNext()) {
        Vertex vertex = gt.next();

        Boolean isProxy = entityMapper.isProxy(vertex);
        if (isProxy) {

            log.debug("{} found entity proxy vertex {} to be removed", methodName, vertex);

            // Look for associated classifications.
            Iterator<Edge> classifierEdges = vertex.edges(Direction.OUT, "Classifier");
            while (classifierEdges.hasNext()) {
                Edge classifierEdge = classifierEdges.next();
                Vertex classificationVertex = classifierEdge.inVertex();
                // Get the classification's name for debug/info only
                Classification existingClassification = new Classification();
                try {
                    classificationMapper.mapVertexToClassification(classificationVertex, existingClassification);
                } catch (Exception e) {
                    log.error("{} caught exception from classification mapper for classification {}", methodName, existingClassification.getName());
                    // Nothing you can do - just keep going
                }
                log.debug("{} removing classification {} from entity proxy", methodName, existingClassification.getName());
                classifierEdge.remove();
                classificationVertex.remove();
            }

            // Finally remove the entity vertex...
            vertex.remove();

            log.debug("{} removed entity proxy vertex with guid {}", methodName, entityGUID);
        }
    }
    g.tx().commit();

}
 
Example 20
Source File: TestBatchGlobalUniqueIndexes.java    From sqlg with MIT License 4 votes vote down vote up
@Test
public void testVertexUniqueConstraintDeleteBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());

    VertexLabel vertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", new HashMap<String, PropertyType>() {{
        put("namea", PropertyType.STRING);
        put("nameb", PropertyType.STRING);
        put("namec", PropertyType.STRING);
    }});
    Set<PropertyColumn> properties = new HashSet<>(vertexLabel.getProperties().values());
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(properties);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    for (int i = 1; i < 1001; i++) {
        Vertex a = this.sqlgGraph.addVertex(T.label, "A", "namea", "a" + i);
    }
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(3000, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());

    try {
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
        Assert.fail("GlobalUniqueIndex should prevent this form happening");
    } catch (Exception e) {
        //swallow
        this.sqlgGraph.tx().rollback();
    }
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.traversal().V().hasLabel("A").has("namea", "a1").next();
    a1.remove();
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(2997, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());

    this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
    //this time it passes.
    this.sqlgGraph.tx().commit();
    testVertexUniqueConstraintDeleteBatchMode_assert(this.sqlgGraph);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testVertexUniqueConstraintDeleteBatchMode_assert(this.sqlgGraph1);
    }
}