Search in sources :

Example 16 with Edge

use of com.tinkerpop.blueprints.Edge in project titan by thinkaurelius.

the class AllEdgesIterator method findNext.

private Edge findNext() {
    Edge rel = null;
    while (rel == null) {
        if (currentEdges.hasNext()) {
            rel = currentEdges.next();
            if (vertices != null && !vertices.contains(rel.getVertex(Direction.IN)))
                rel = null;
        } else {
            if (vertexIter.hasNext()) {
                Vertex nextVertex = vertexIter.next();
                currentEdges = nextVertex.getEdges(Direction.OUT).iterator();
            } else
                break;
        }
    }
    return rel;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Edge(com.tinkerpop.blueprints.Edge)

Example 17 with Edge

use of com.tinkerpop.blueprints.Edge in project titan by thinkaurelius.

the class AllEdgesIterator method next.

@Override
public Edge next() {
    if (next == null)
        throw new NoSuchElementException();
    Edge current = next;
    next = findNext();
    return current;
}
Also used : Edge(com.tinkerpop.blueprints.Edge) NoSuchElementException(java.util.NoSuchElementException)

Example 18 with Edge

use of com.tinkerpop.blueprints.Edge in project titan by thinkaurelius.

the class TitanKeyVertex method getIndexList.

private List<IndexDefinition> getIndexList(Class<? extends Element> type) {
    Preconditions.checkArgument(type == Vertex.class || type == Edge.class, "Expected Vertex or Edge class as argument");
    List<IndexDefinition> result = type == Vertex.class ? vertexIndexes : edgeIndexes;
    if (result == null) {
        // Build it
        ImmutableList.Builder b = new ImmutableList.Builder();
        for (IndexDefinition it : getIndexes()) if (type.isAssignableFrom(it.getElementType()))
            b.add(it);
        result = b.build();
        if (type == Vertex.class)
            vertexIndexes = result;
        else if (type == Edge.class)
            edgeIndexes = result;
    }
    return result;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) IndexDefinition(com.thinkaurelius.titan.graphdb.types.IndexDefinition) ImmutableList(com.google.common.collect.ImmutableList) Edge(com.tinkerpop.blueprints.Edge)

Example 19 with Edge

use of com.tinkerpop.blueprints.Edge in project titan by thinkaurelius.

the class TitanEventualGraphTest method testTimestampSetting.

@Test
public void testTimestampSetting() {
    // Transaction 1: Init graph with two vertices, having set "name" and "age" properties
    TitanTransaction tx1 = graph.buildTransaction().setTimestamp(100).start();
    String name = "name";
    String age = "age";
    String address = "address";
    Vertex v1 = tx1.addVertex();
    Vertex v2 = tx1.addVertex();
    v1.setProperty(name, "a");
    v2.setProperty(age, "14");
    v2.setProperty(name, "b");
    v2.setProperty(age, "42");
    tx1.commit();
    // Fetch vertex ids
    Object id1 = v1.getId();
    Object id2 = v2.getId();
    // Transaction 2: Remove "name" property from v1, set "address" property; create
    // an edge v2 -> v1
    TitanTransaction tx2 = graph.buildTransaction().setTimestamp(1000).start();
    v1 = tx2.getVertex(id1);
    v2 = tx2.getVertex(id2);
    v1.removeProperty(name);
    v1.setProperty(address, "xyz");
    Edge edge = tx2.addEdge(1, v2, v1, "parent");
    tx2.commit();
    Object edgeId = edge.getId();
    Vertex afterTx2 = graph.getVertex(id1);
    // Verify that "name" property is gone
    assertFalse(afterTx2.getPropertyKeys().contains(name));
    // Verify that "address" property is set
    assertEquals("xyz", afterTx2.getProperty(address));
    // Verify that the edge is properly registered with the endpoint vertex
    assertEquals(1, Iterables.size(afterTx2.getEdges(Direction.IN, "parent")));
    // Verify that edge is registered under the id
    assertNotNull(graph.getEdge(edgeId));
    graph.commit();
    // Transaction 3: Remove "address" property from v1 with earlier timestamp than
    // when the value was set
    TitanTransaction tx3 = graph.buildTransaction().setTimestamp(200).start();
    v1 = tx3.getVertex(id1);
    v1.removeProperty(address);
    tx3.commit();
    Vertex afterTx3 = graph.getVertex(id1);
    graph.commit();
    // Verify that "address" is still set
    assertEquals("xyz", afterTx3.getProperty(address));
    // Transaction 4: Modify "age" property on v2, remove edge between v2 and v1
    TitanTransaction tx4 = graph.buildTransaction().setTimestamp(2000).start();
    v2 = tx4.getVertex(id2);
    v2.setProperty(age, "15");
    tx4.removeEdge(tx4.getEdge(edgeId));
    tx4.commit();
    Vertex afterTx4 = graph.getVertex(id2);
    // Verify that "age" property is modified
    assertEquals("15", afterTx4.getProperty(age));
    // Verify that edge is no longer registered with the endpoint vertex
    assertEquals(0, Iterables.size(afterTx4.getEdges(Direction.OUT, "parent")));
    // Verify that edge entry disappeared from id registry
    assertNull(graph.getEdge(edgeId));
    // Transaction 5: Modify "age" property on v2 with earlier timestamp
    TitanTransaction tx5 = graph.buildTransaction().setTimestamp(1500).start();
    v2 = tx5.getVertex(id2);
    v2.setProperty(age, "16");
    tx5.commit();
    Vertex afterTx5 = graph.getVertex(id2);
    // Verify that the property value is unchanged
    assertEquals("15", afterTx5.getProperty(age));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 20 with Edge

use of com.tinkerpop.blueprints.Edge in project titan by thinkaurelius.

the class TitanGraphTest method testIndexRetrieval.

@Test
public void testIndexRetrieval() {
    TitanKey id = tx.makeKey("uid").single().unique().indexed(Vertex.class).indexed(Edge.class).dataType(Integer.class).make();
    TitanKey name = tx.makeKey("name").single().indexed(Vertex.class).indexed(Edge.class).dataType(String.class).make();
    TitanLabel connect = tx.makeLabel("connect").signature(id, name).make();
    int noNodes = 100;
    int div = 10;
    int mod = noNodes / div;
    for (int i = 0; i < noNodes; i++) {
        TitanVertex n = tx.addVertex();
        n.addProperty(id, i);
        n.addProperty(name, "Name" + (i % mod));
        TitanVertex other = tx.getVertex(id, Math.max(0, i - 1));
        Preconditions.checkNotNull(other);
        TitanEdge e = n.addEdge(connect, other);
        e.setProperty(id, i);
        e.setProperty(name, "Edge" + (i % mod));
    }
    clopen();
    for (int j = 0; j < mod; j++) {
        Iterable<Vertex> nodes = tx.getVertices("name", "Name" + j);
        assertEquals(div, Iterables.size(nodes));
        for (Vertex n : nodes) {
            int nid = ((Number) n.getProperty("uid")).intValue();
            assertEquals(j, nid % mod);
        }
        Iterable<Edge> edges = tx.getEdges("name", "Edge" + j);
        assertEquals(div, Iterables.size(edges));
        for (Edge e : edges) {
            int nid = ((Number) e.getProperty("uid")).intValue();
            assertEquals(j, nid % mod);
        }
    }
    clopen();
    for (int i = 0; i < noNodes; i++) {
        assertEquals(Iterables.getOnlyElement(tx.getVertices("uid", i)).getProperty("name").toString().substring(4), String.valueOf(i % mod));
    }
    for (int i = 0; i < noNodes; i++) {
        assertEquals(Iterables.getOnlyElement(tx.getEdges("uid", i)).getProperty("name").toString().substring(4), String.valueOf(i % mod));
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Aggregations

Edge (com.tinkerpop.blueprints.Edge)214 Vertex (com.tinkerpop.blueprints.Vertex)141 Test (org.junit.Test)53 Graph (com.tinkerpop.blueprints.Graph)49 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)49 HashSet (java.util.HashSet)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)13 ArrayList (java.util.ArrayList)13 Collection (java.util.Collection)11 JSONObject (org.codehaus.jettison.json.JSONObject)11 HashMap (java.util.HashMap)10 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)9 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)8 Map (java.util.Map)8 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)6 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)6 URI (org.openrdf.model.URI)6