Search in sources :

Example 21 with Edge

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

the class TitanGraphTest method testVertexDeletionWithIndex.

@Test
public void testVertexDeletionWithIndex() {
    TitanKey name = makeStringPropertyKey("name");
    Vertex v1 = tx.addVertex(null);
    v1.setProperty("name", "v1");
    Vertex v2 = tx.addVertex(null);
    v2.setProperty("name", "v2");
    Edge e = tx.addEdge(null, v1, v2, "some_edge");
    clopen();
    v1 = tx.getVertex(v1);
    tx.removeVertex(v1);
    // generates IllegalArgumentException
    v2 = tx.getVertices("name", "v2").iterator().next();
    assertNotNull(v2);
    assertEquals(0, Iterables.size(tx.getVertices("name", "v1")));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 22 with Edge

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

the class TitanGraphTest method testEdgesExceedCacheSize.

@Test
public void testEdgesExceedCacheSize() {
    // Add a vertex with as many edges as the tx-cache-size. (20000 by default)
    int numEdges = graph.getConfiguration().getTxCacheSize();
    Vertex parentVertex = graph.addVertex(null);
    for (int i = 0; i < numEdges; i++) {
        Vertex childVertex = graph.addVertex(null);
        parentVertex.addEdge("friend", childVertex);
    }
    graph.commit();
    assertEquals(numEdges, Iterables.size(parentVertex.getEdges(Direction.OUT)));
    // Remove an edge.
    Edge edge = parentVertex.getEdges(Direction.OUT).iterator().next();
    edge.remove();
    // Check that getEdges returns one fewer.
    assertEquals(numEdges - 1, Iterables.size(parentVertex.getEdges(Direction.OUT)));
    // Run the same check one more time.
    // This fails! (Expected: 19999. Actual: 20000.)
    assertEquals(numEdges - 1, Iterables.size(parentVertex.getEdges(Direction.OUT)));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 23 with Edge

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

the class TitanGraphTest method testCreateDelete.

// Add more removal operations, different transaction contexts
@Test
public void testCreateDelete() {
    TitanKey weight = graph.makeKey("weight").single().dataType(Double.class).make();
    TitanKey id = graph.makeKey("uid").single().unique().indexed(Vertex.class).dataType(Integer.class).make();
    TitanLabel knows = graph.makeLabel("knows").sortKey(id).sortOrder(Order.DESC).directed().make();
    TitanLabel father = graph.makeLabel("father").manyToOne().make();
    TitanVertex n1 = graph.addVertex(null), n3 = graph.addVertex(null);
    TitanEdge e = n3.addEdge(knows, n1);
    Edge e2 = n1.addEdge("friend", n3);
    e.setProperty(id, 111);
    n3.addProperty(id, 445);
    assertEquals(111, e.getProperty(id));
    graph.commit();
    n3 = graph.getVertex(n3.getID());
    assertEquals(445, n3.getProperty("uid"));
    e = (TitanEdge) Iterables.getOnlyElement(n3.getEdges(OUT, "knows"));
    assertEquals(111, e.getProperty("uid"));
    assertEquals(e, graph.getEdge(e.getId()));
    assertEquals(e, graph.getEdge(e.getId().toString()));
    TitanProperty p = Iterables.getOnlyElement(n3.getProperties("uid"));
    p.remove();
    n3.addProperty("uid", 353);
    e = (TitanEdge) Iterables.getOnlyElement(n3.getEdges(Direction.OUT, "knows"));
    e.setProperty(id, 222);
    e2 = Iterables.getOnlyElement(n1.getEdges(OUT, "friend"));
    e2.setProperty("uid", 1);
    e2.setProperty("weight", 2.0);
    assertEquals(1, e2.getProperty("uid"));
    assertEquals(2.0, e2.getProperty("weight"));
    clopen();
    n3 = graph.getVertex(n3.getID());
    assertEquals(353, n3.getProperty("uid"));
    e = (TitanEdge) Iterables.getOnlyElement(n3.getEdges(Direction.OUT, "knows"));
    assertEquals(222, e.getProperty(id));
}
Also used : Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 24 with Edge

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

the class TitanGraphTest method testQuery.

@Test
public void testQuery() {
    TitanKey name = tx.makeKey("name").dataType(String.class).single().indexed(Vertex.class).unique().make();
    TitanKey time = tx.makeKey("time").dataType(Integer.class).single().make();
    TitanKey weight = tx.makeKey("weight").dataType(Double.class).single().make();
    TitanLabel author = tx.makeLabel("author").manyToOne().unidirected().make();
    TitanLabel connect = tx.makeLabel("connect").sortKey(time).make();
    TitanLabel connectDesc = tx.makeLabel("connectDesc").sortKey(time).sortOrder(Order.DESC).make();
    TitanLabel friend = tx.makeLabel("friend").sortKey(weight, time).sortOrder(Order.ASC).signature(author).make();
    TitanLabel friendDesc = tx.makeLabel("friendDesc").sortKey(weight, time).sortOrder(Order.DESC).signature(author).make();
    TitanLabel knows = tx.makeLabel("knows").sortKey(author, weight).make();
    TitanLabel follows = tx.makeLabel("follows").make();
    TitanVertex v = tx.addVertex();
    v.addProperty(name, "v");
    TitanVertex u = tx.addVertex();
    u.addProperty(name, "u");
    int noVertices = 10000;
    assertEquals(0, (noVertices - 1) % 3);
    TitanVertex[] vs = new TitanVertex[noVertices];
    for (int i = 1; i < noVertices; i++) {
        vs[i] = tx.addVertex();
        vs[i].addProperty(name, "v" + i);
    }
    TitanLabel[] labelsV = { connect, friend, knows };
    TitanLabel[] labelsU = { connectDesc, friendDesc, knows };
    for (int i = 1; i < noVertices; i++) {
        for (TitanVertex vertex : new TitanVertex[] { v, u }) {
            for (Direction d : new Direction[] { OUT, IN }) {
                TitanLabel label = vertex == v ? labelsV[i % 3] : labelsU[i % 3];
                TitanEdge e = d == OUT ? vertex.addEdge(label, vs[i]) : vs[i].addEdge(label, vertex);
                e.setProperty("time", i);
                e.setProperty("weight", i % 4 + 0.5);
                e.setProperty("name", "e" + i);
                e.setProperty(author, i % 5 == 0 ? v : vs[i % 5]);
            }
        }
    }
    int edgesPerLabel = noVertices / 3;
    VertexList vl;
    Map<TitanVertex, Iterable<TitanEdge>> results;
    Map<TitanVertex, Iterable<TitanProperty>> results2;
    TitanVertex[] qvs;
    int lastTime;
    Iterator<Edge> outer;
    clopen();
    long[] vidsubset = new long[31 - 3];
    for (int i = 0; i < vidsubset.length; i++) vidsubset[i] = vs[i + 3].getID();
    Arrays.sort(vidsubset);
    // ##################################################
    // Queries from Cache
    // ##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = tx.getVertex(vs[i].getID());
    v = tx.getVertex(v.getID());
    u = tx.getVertex(u.getID());
    qvs = new TitanVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    // To trigger queries from cache (don't copy!!!)
    assertEquals(2 * (noVertices - 1), Iterables.size(v.getEdges()));
    assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (Edge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime <= nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime >= nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).count());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).count());
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
    assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 31).count());
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Compare.LESS_THAN_EQUAL, 10).count());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Compare.GREATER_THAN, 10).count());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 4.0).count());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 2.0).count());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 2.1, 4.0).count());
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
    assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).count());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).count());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).has("weight", 3.5).count());
    assertEquals(2, v.query().labels("connect").adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(0, v.query().labels("connect").adjacentVertex(vs[8]).has("time", 8).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).count());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).count());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).count());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).count());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
    for (Direction dir : new Direction[] { IN, OUT }) {
        vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
        vl.sort();
        for (int i = 0; i < vl.size(); i++) assertEquals(vidsubset[i], vl.getID(i));
    }
    assertEquals(2 * (noVertices - 1), Iterables.size(v.getEdges()));
    // Property queries
    assertEquals(1, Iterables.size(v.query().properties()));
    assertEquals(1, Iterables.size(v.query().keys("name").properties()));
    // MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(2, Iterables.size(result));
    results = tx.multiQuery(qvs).labels("knows").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(0, Iterables.size(result));
    results = tx.multiQuery(qvs).titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(4, Iterables.size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    // ##################################################
    // Same queries as above but without memory loading (i.e. omitting the first query)
    // ##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = tx.getVertex(vs[i].getID());
    v = tx.getVertex(v.getID());
    u = tx.getVertex(u.getID());
    qvs = new TitanVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (Edge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime <= nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime >= nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).count());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).count());
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
    assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 31).count());
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Compare.LESS_THAN_EQUAL, 10).count());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Compare.GREATER_THAN, 10).count());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 4.0).count());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 2.0).count());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 2.1, 4.0).count());
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
    assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).count());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).count());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).has("weight", 3.5).count());
    assertEquals(2, v.query().labels("connect").adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(0, v.query().labels("connect").adjacentVertex(vs[8]).has("time", 8).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).count());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).count());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).count());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).count());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
    for (Direction dir : new Direction[] { IN, OUT }) {
        vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
        vl.sort();
        for (int i = 0; i < vl.size(); i++) assertEquals(vidsubset[i], vl.getID(i));
    }
    assertEquals(2 * (noVertices - 1), Iterables.size(v.getEdges()));
    // Property queries
    assertEquals(1, Iterables.size(v.query().properties()));
    assertEquals(1, Iterables.size(v.query().keys("name").properties()));
    // MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(2, Iterables.size(result));
    results = tx.multiQuery(qvs).labels("knows").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(0, Iterables.size(result));
    results = tx.multiQuery(qvs).titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(4, Iterables.size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    // ##################################################
    // End copied queries
    // ##################################################
    newTx();
    v = (TitanVertex) tx.getVertices("name", "v").iterator().next();
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).vertexIds().size());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).vertexIds().size());
    newTx();
    v = (TitanVertex) tx.getVertices("name", "v").iterator().next();
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).count());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).count());
    newTx();
    // Test partially new vertex queries
    TitanVertex[] qvs2 = new TitanVertex[qvs.length + 2];
    qvs2[0] = tx.addVertex();
    for (int i = 0; i < qvs.length; i++) qvs2[i + 1] = tx.getVertex(qvs[i].getID());
    qvs2[qvs2.length - 1] = tx.addVertex();
    qvs2[0].addEdge("connect", qvs2[qvs2.length - 1]);
    qvs2[qvs2.length - 1].addEdge("connect", qvs2[0]);
    results = tx.multiQuery(qvs2).direction(IN).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result));
}
Also used : Direction(com.tinkerpop.blueprints.Direction) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 25 with Edge

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

the class TitanIndexTest method testIndexing.

@Test
public void testIndexing() {
    TitanKey text = tx.makeKey("text").single().indexed(INDEX, Vertex.class).indexed(INDEX, Edge.class).dataType(String.class).make();
    TitanKey location = tx.makeKey("location").single().indexed(INDEX, Vertex.class).indexed(INDEX, Edge.class).dataType(Geoshape.class).make();
    TitanKey time = tx.makeKey("time").single().indexed(INDEX, Vertex.class).indexed(INDEX, Edge.class).dataType(Long.class).make();
    TitanKey category = tx.makeKey("category").single().indexed(Vertex.class).indexed(Edge.class).dataType(Integer.class).make();
    TitanKey group = tx.makeKey("group").single().indexed(INDEX, Vertex.class).indexed(INDEX, Edge.class).dataType(Byte.class).make();
    TitanKey id = tx.makeKey("uid").single().unique().indexed(Vertex.class).dataType(Integer.class).make();
    TitanLabel knows = tx.makeLabel("knows").sortKey(time).signature(location).make();
    clopen();
    String[] words = { "world", "aurelius", "titan", "graph" };
    int numCategories = 5;
    int numGroups = 10;
    double distance, offset;
    int numV = 100;
    final int originalNumV = numV;
    for (int i = 0; i < numV; i++) {
        Vertex v = tx.addVertex();
        v.setProperty("uid", i);
        v.setProperty("category", i % numCategories);
        v.setProperty("group", i % numGroups);
        v.setProperty("text", "Vertex " + words[i % words.length]);
        v.setProperty("time", i);
        offset = (i % 2 == 0 ? 1 : -1) * (i * 50.0 / numV);
        v.setProperty("location", Geoshape.point(0.0 + offset, 0.0 + offset));
        Edge e = v.addEdge("knows", tx.getVertex("uid", Math.max(0, i - 1)));
        e.setProperty("text", "Vertex " + words[i % words.length]);
        e.setProperty("time", i);
        e.setProperty("category", i % numCategories);
        e.setProperty("group", i % numGroups);
        e.setProperty("location", Geoshape.point(0.0 + offset, 0.0 + offset));
    }
    for (int i = 0; i < words.length; i++) {
        int expectedSize = numV / words.length;
        assertEquals(expectedSize, Iterables.size(tx.query().has("text", Text.CONTAINS, words[i]).vertices()));
        assertEquals(expectedSize, Iterables.size(tx.query().has("text", Text.CONTAINS, words[i]).edges()));
        // Test ordering
        for (String orderKey : new String[] { "time", "category" }) {
            for (Order order : Order.values()) {
                for (Iterable<? extends Element> iter : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order).vertices(), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order).edges())) {
                    Element previous = null;
                    int count = 0;
                    for (Element element : iter) {
                        if (previous != null) {
                            int cmp = ((Comparable) element.getProperty(orderKey)).compareTo(previous.getProperty(orderKey));
                            assertTrue(element.getProperty(orderKey) + " <> " + previous.getProperty(orderKey), order == Order.ASC ? cmp >= 0 : cmp <= 0);
                        }
                        previous = element;
                        count++;
                    }
                    assertEquals(expectedSize, count);
                }
            }
        }
    }
    assertEquals(3, Iterables.size(tx.query().has("group", 3).orderBy("time", Order.ASC).limit(3).vertices()));
    assertEquals(3, Iterables.size(tx.query().has("group", 3).orderBy("time", Order.DESC).limit(3).edges()));
    for (int i = 0; i < numV / 2; i += numV / 10) {
        assertEquals(i, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices()));
        assertEquals(i, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges()));
    }
    for (int i = 0; i < numV; i += 10) {
        offset = (i * 50.0 / originalNumV);
        distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
        assertEquals(i + 1, Iterables.size(tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()));
        assertEquals(i + 1, Iterables.size(tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()));
    }
    // Mixed index queries
    assertEquals(4, Iterables.size(tx.query().has("category", 1).interval("time", 10, 28).vertices()));
    assertEquals(4, Iterables.size(tx.query().has("category", 1).interval("time", 10, 28).edges()));
    assertEquals(5, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices()));
    offset = (19 * 50.0 / originalNumV);
    distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
    assertEquals(5, Iterables.size(tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()));
    assertEquals(numV, Iterables.size(tx.getVertices()));
    assertEquals(numV, Iterables.size(tx.getEdges()));
    clopen();
    for (int i = 0; i < words.length; i++) {
        int expectedSize = numV / words.length;
        assertEquals(expectedSize, Iterables.size(tx.query().has("text", Text.CONTAINS, words[i]).vertices()));
        assertEquals(expectedSize, Iterables.size(tx.query().has("text", Text.CONTAINS, words[i]).edges()));
        // Test ordering
        for (String orderKey : new String[] { "time", "category" }) {
            for (Order order : Order.values()) {
                for (Iterable<? extends Element> iter : ImmutableList.of(tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order).vertices(), tx.query().has("text", Text.CONTAINS, words[i]).orderBy(orderKey, order).edges())) {
                    TestUtil.verifyElementOrder(iter, orderKey, order, expectedSize);
                }
            }
        }
    }
    assertEquals(3, Iterables.size(tx.query().has("group", 3).orderBy("time", Order.ASC).limit(3).vertices()));
    assertEquals(3, Iterables.size(tx.query().has("group", 3).orderBy("time", Order.DESC).limit(3).edges()));
    for (int i = 0; i < numV / 2; i += numV / 10) {
        assertEquals(i, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices()));
        assertEquals(i, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges()));
    }
    for (int i = 0; i < numV; i += 10) {
        offset = (i * 50.0 / originalNumV);
        distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
        assertEquals(i + 1, Iterables.size(tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()));
        assertEquals(i + 1, Iterables.size(tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()));
    }
    // Mixed index queries
    assertEquals(4, Iterables.size(tx.query().has("category", 1).interval("time", 10, 28).vertices()));
    assertEquals(4, Iterables.size(tx.query().has("category", 1).interval("time", 10, 28).edges()));
    assertEquals(5, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices()));
    offset = (19 * 50.0 / originalNumV);
    distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
    assertEquals(5, Iterables.size(tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()));
    assertEquals(numV, Iterables.size(tx.getVertices()));
    assertEquals(numV, Iterables.size(tx.getEdges()));
    newTx();
    int numDelete = 12;
    for (int i = numV - numDelete; i < numV; i++) {
        tx.getVertex("uid", i).remove();
    }
    numV = numV - numDelete;
    // Copied from above
    for (int i = 0; i < words.length; i++) {
        assertEquals(numV / words.length, Iterables.size(tx.query().has("text", Text.CONTAINS, words[i]).vertices()));
        assertEquals(numV / words.length, Iterables.size(tx.query().has("text", Text.CONTAINS, words[i]).edges()));
    }
    for (int i = 0; i < numV / 2; i += numV / 10) {
        assertEquals(i, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).vertices()));
        assertEquals(i, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, i).has("time", Cmp.LESS_THAN, i + i).edges()));
    }
    for (int i = 0; i < numV; i += 10) {
        offset = (i * 50.0 / originalNumV);
        distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
        assertEquals(i + 1, Iterables.size(tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).vertices()));
        assertEquals(i + 1, Iterables.size(tx.query().has("location", Geo.WITHIN, Geoshape.circle(0.0, 0.0, distance)).edges()));
    }
    assertEquals(5, Iterables.size(tx.query().has("time", Cmp.GREATER_THAN_EQUAL, 10).has("time", Cmp.LESS_THAN, 30).has("text", Text.CONTAINS, words[0]).vertices()));
    offset = (19 * 50.0 / originalNumV);
    distance = Geoshape.point(0.0, 0.0).getPoint().distance(Geoshape.point(offset, offset).getPoint()) + 20;
    assertEquals(5, Iterables.size(tx.query().has("location", Geo.INTERSECT, Geoshape.circle(0.0, 0.0, distance)).has("text", Text.CONTAINS, words[0]).vertices()));
    assertEquals(numV, Iterables.size(tx.getVertices()));
    assertEquals(numV, Iterables.size(tx.getEdges()));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Element(com.tinkerpop.blueprints.Element) Geoshape(com.thinkaurelius.titan.core.attribute.Geoshape) 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