Search in sources :

Example 46 with TitanVertex

use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.

the class TitanGraphTest method testVertexTTLImplicitKey.

@Test
public void testVertexTTLImplicitKey() throws Exception {
    Duration d;
    if (!features.hasCellTTL()) {
        return;
    }
    clopen(option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true);
    int ttl1 = 1;
    VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make();
    mgmt.setTTL(label1, Duration.ofSeconds(ttl1));
    assertEquals(Duration.ofSeconds(ttl1), mgmt.getTTL(label1));
    mgmt.commit();
    TitanVertex v1 = tx.addVertex("event");
    TitanVertex v2 = tx.addVertex();
    tx.commit();
    /* TODO: this fails
        d = v1.getProperty("~ttl");
        assertEquals(1, d);
        d = v2.getProperty("~ttl");
        assertEquals(0, d);
        */
    Object v1id = v1.id();
    Object v2id = v2.id();
    v1 = getV(graph, v1id);
    v2 = getV(graph, v2id);
    d = v1.value("~ttl");
    assertEquals(Duration.ofSeconds(1), d);
    d = v2.value("~ttl");
    assertEquals(Duration.ZERO, d);
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) Duration(java.time.Duration) Test(org.junit.Test)

Example 47 with TitanVertex

use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.

the class TitanGraphTest method testMediumCreateRetrieve.

@Test
public void testMediumCreateRetrieve() {
    //Create schema
    makeLabel("connect");
    makeVertexIndexedUniqueKey("name", String.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey id = makeVertexIndexedUniqueKey("uid", Integer.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(id).signature(weight).make();
    finishSchema();
    //Create Nodes
    int noVertices = 500;
    String[] names = new String[noVertices];
    int[] ids = new int[noVertices];
    TitanVertex[] nodes = new TitanVertex[noVertices];
    long[] nodeIds = new long[noVertices];
    List[] nodeEdges = new List[noVertices];
    for (int i = 0; i < noVertices; i++) {
        names[i] = "vertex" + i;
        ids[i] = i;
        nodes[i] = tx.addVertex("name", names[i], "uid", ids[i]);
        if ((i + 1) % 100 == 0)
            log.debug("Added 100 nodes");
    }
    log.debug("Nodes created");
    int[] connectOff = { -100, -34, -4, 10, 20 };
    int[] knowsOff = { -400, -18, 8, 232, 334 };
    for (int i = 0; i < noVertices; i++) {
        TitanVertex n = nodes[i];
        nodeEdges[i] = new ArrayList(10);
        for (int c : connectOff) {
            Edge r = n.addEdge("connect", nodes[wrapAround(i + c, noVertices)]);
            nodeEdges[i].add(r);
        }
        for (int k : knowsOff) {
            TitanVertex n2 = nodes[wrapAround(i + k, noVertices)];
            Edge r = n.addEdge("knows", n2, "uid", ((Number) n.value("uid")).intValue() + ((Number) n2.value("uid")).intValue(), "weight", k * 1.5, "name", i + "-" + k);
            nodeEdges[i].add(r);
        }
        if (i % 100 == 99)
            log.debug(".");
    }
    tx.commit();
    tx = null;
    Set[] nodeEdgeIds = new Set[noVertices];
    for (int i = 0; i < noVertices; i++) {
        nodeIds[i] = (Long) nodes[i].id();
        nodeEdgeIds[i] = new HashSet(10);
        for (Object r : nodeEdges[i]) {
            nodeEdgeIds[i].add(((TitanEdge) r).longId());
        }
    }
    clopen();
    nodes = new TitanVertex[noVertices];
    for (int i = 0; i < noVertices; i++) {
        TitanVertex n = getVertex("uid", ids[i]);
        assertEquals(n, getVertex("name", names[i]));
        assertEquals(names[i], n.value("name"));
        nodes[i] = n;
        assertEquals(nodeIds[i], n.longId());
    }
    for (int i = 0; i < noVertices; i++) {
        TitanVertex n = nodes[i];
        assertCount(connectOff.length + knowsOff.length, n.query().direction(Direction.OUT).edges());
        assertCount(connectOff.length, n.query().direction(Direction.OUT).labels("connect").edges());
        assertCount(connectOff.length * 2, n.query().direction(Direction.BOTH).labels("connect").edges());
        assertCount(knowsOff.length * 2, n.query().direction(Direction.BOTH).labels("knows").edges());
        assertCount(connectOff.length + knowsOff.length, n.query().direction(Direction.OUT).edges());
        assertCount(2, n.properties());
        for (TitanEdge r : n.query().direction(Direction.OUT).labels("knows").edges()) {
            TitanVertex n2 = r.vertex(Direction.IN);
            int idsum = ((Number) n.value("uid")).intValue() + ((Number) n2.value("uid")).intValue();
            assertEquals(idsum, ((Number) r.value("uid")).intValue());
            double k = ((Number) r.value("weight")).doubleValue() / 1.5;
            int ki = (int) k;
            assertEquals(i + "-" + ki, r.value("name"));
        }
        Set edgeIds = new HashSet(10);
        for (TitanEdge r : n.query().direction(Direction.OUT).edges()) {
            edgeIds.add(((TitanEdge) r).longId());
        }
        assertTrue(edgeIds + " vs " + nodeEdgeIds[i], edgeIds.equals(nodeEdgeIds[i]));
    }
    newTx();
    //Bulk vertex retrieval
    long[] vids1 = new long[noVertices / 10];
    for (int i = 0; i < vids1.length; i++) {
        vids1[i] = nodeIds[i];
    }
    //All non-cached
    verifyVerticesRetrieval(vids1, Lists.newArrayList(tx.getVertices(vids1)));
    //All cached
    verifyVerticesRetrieval(vids1, Lists.newArrayList(tx.getVertices(vids1)));
    long[] vids2 = new long[noVertices / 10 * 2];
    for (int i = 0; i < vids2.length; i++) {
        vids2[i] = nodeIds[i];
    }
    //Partially cached
    verifyVerticesRetrieval(vids2, Lists.newArrayList(tx.getVertices(vids2)));
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) ArrayList(java.util.ArrayList) OrderList(com.thinkaurelius.titan.graphdb.internal.OrderList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) VertexList(com.thinkaurelius.titan.core.VertexList) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with TitanVertex

use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.

the class TitanGraphTest method testLimitWithMixedIndexCoverage.

@Test
public void testLimitWithMixedIndexCoverage() {
    final String vt = "vt";
    final String fn = "firstname";
    final String user = "user";
    final String alice = "alice";
    final String bob = "bob";
    PropertyKey vtk = makeVertexIndexedKey(vt, String.class);
    PropertyKey fnk = makeKey(fn, String.class);
    finishSchema();
    TitanVertex a = tx.addVertex(vt, user, fn, "alice");
    TitanVertex b = tx.addVertex(vt, user, fn, "bob");
    TitanVertex v;
    v = getOnlyElement(tx.query().has(vt, user).has(fn, bob).limit(1).vertices());
    assertEquals(bob, v.value(fn));
    assertEquals(user, v.value(vt));
    v = getOnlyElement(tx.query().has(vt, user).has(fn, alice).limit(1).vertices());
    assertEquals(alice, v.value(fn));
    assertEquals(user, v.value(vt));
    tx.commit();
    tx = graph.newTransaction();
    v = getOnlyElement(tx.query().has(vt, user).has(fn, bob).limit(1).vertices());
    assertEquals(bob, v.value(fn));
    assertEquals(user, v.value(vt));
    v = getOnlyElement(tx.query().has(vt, user).has(fn, alice).limit(1).vertices());
    assertEquals(alice, v.value(fn));
    assertEquals(user, v.value(vt));
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 49 with TitanVertex

use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.

the class TitanGraphTest method testVertexCentricQuery.

/* ==================================================================================
                            VERTEX CENTRIC QUERIES
     ==================================================================================*/
@Test
@SuppressWarnings("deprecation")
public void testVertexCentricQuery() {
    makeVertexIndexedUniqueKey("name", String.class);
    PropertyKey time = makeKey("time", Integer.class);
    PropertyKey weight = makeKey("weight", Double.class);
    PropertyKey number = makeKey("number", Long.class);
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("connect")).sortKey(time).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("connectDesc")).sortKey(time).sortOrder(Order.DESC).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friend")).sortKey(weight, time).sortOrder(Order.ASC).signature(number).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friendDesc")).sortKey(weight, time).sortOrder(Order.DESC).signature(number).make();
    ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("knows")).sortKey(number, weight).make();
    mgmt.makeEdgeLabel("follows").make();
    finishSchema();
    TitanVertex v = tx.addVertex("name", "v");
    TitanVertex u = tx.addVertex("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("name", "v" + i);
    }
    EdgeLabel[] labelsV = { tx.getEdgeLabel("connect"), tx.getEdgeLabel("friend"), tx.getEdgeLabel("knows") };
    EdgeLabel[] labelsU = { tx.getEdgeLabel("connectDesc"), tx.getEdgeLabel("friendDesc"), tx.getEdgeLabel("knows") };
    for (int i = 1; i < noVertices; i++) {
        for (TitanVertex vertex : new TitanVertex[] { v, u }) {
            for (Direction d : new Direction[] { OUT, IN }) {
                EdgeLabel label = vertex == v ? labelsV[i % 3] : labelsU[i % 3];
                TitanEdge e = d == OUT ? vertex.addEdge(n(label), vs[i]) : vs[i].addEdge(n(label), vertex);
                e.property("time", i);
                e.property("weight", i % 4 + 0.5);
                e.property("name", "e" + i);
                e.property("number", i % 5);
            }
        }
    }
    int edgesPerLabel = noVertices / 3;
    VertexList vl;
    Map<TitanVertex, Iterable<TitanEdge>> results;
    Map<TitanVertex, Iterable<TitanVertexProperty>> results2;
    TitanVertex[] qvs;
    int lastTime;
    Iterator<? extends Edge> outer;
    clopen();
    long[] vidsubset = new long[31 - 3];
    for (int i = 0; i < vidsubset.length; i++) vidsubset[i] = vs[i + 3].longId();
    Arrays.sort(vidsubset);
    //##################################################
    //Queries from Cache
    //##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = getV(tx, vs[i].longId());
    v = getV(tx, v.longId());
    u = getV(tx, u.longId());
    qvs = new TitanVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    //To trigger queries from cache (don't copy!!!)
    assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
    assertEquals(1, v.query().propertyCount());
    assertEquals(10, size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, size(v.query().labels("connect").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, size(u.query().labels("connectDesc").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (TitanEdge e : (Iterable<TitanEdge>) v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime <= nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (Edge e : (Iterable<TitanEdge>) u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime >= nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, size(v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, size(u.query().labels("connectDesc").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (Edge e : (Iterable<TitanEdge>) v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", 15).has("weight", 3.5), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).edgeCount());
    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", Cmp.GREATER_THAN, 31).count());
    assertEquals(10, size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).limit(3).count());
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", 0.5).limit(3), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", Contain.IN, ImmutableList.of(0.5)), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5, 2.5)).interval("time", 3, 33), EDGE, 7, 3, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5)), EDGE, 1667, 2, new boolean[] { true, true });
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).edgeCount());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Cmp.GREATER_THAN, 10).edgeCount());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 4.0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 2.0).edgeCount());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 2.1, 4.0).edgeCount());
    assertEquals(20, size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, 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).edgeCount());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).edgeCount());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(IN).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().labels("knows").direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).has("weight", 3.5).edgeCount());
    assertEquals(2, v.query().labels("connect").adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(0, v.query().labels("connect").adjacent(vs[8]).has("time", 8).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).edgeCount());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).edgeCount());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).edgeCount());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).edgeCount());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(noVertices - 1, size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, 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));
    }
    assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
    //Property queries
    assertEquals(1, size(v.query().properties()));
    assertEquals(1, size(v.query().keys("name").properties()));
    //MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(2, size(result));
    results = tx.multiQuery(qvs).labels("knows").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(0, size(result));
    results = tx.multiQuery(qvs).edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(4, size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<TitanVertexProperty> result : results2.values()) assertEquals(1, size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<TitanVertexProperty> result : results2.values()) assertEquals(1, 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] = getV(tx, vs[i].longId());
    v = getV(tx, v.longId());
    u = getV(tx, u.longId());
    qvs = new TitanVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    assertEquals(10, size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, size(v.query().labels("connect").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, size(u.query().labels("connectDesc").has("time", Cmp.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (Edge e : (Iterable<TitanEdge>) v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime <= nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (Edge e : (Iterable<TitanEdge>) u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.value("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime >= nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, size(v.query().labels("connect").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, size(u.query().labels("connectDesc").direction(OUT).has("time", Cmp.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (Edge e : (Iterable<TitanEdge>) v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    evaluateQuery(v.query().labels("connect").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("connect").direction(OUT).has("time", 15).has("weight", 3.5), EDGE, 1, 1, new boolean[] { false, true });
    evaluateQuery(u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31), EDGE, 10, 1, new boolean[] { true, true });
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).edgeCount());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).edgeCount());
    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", Cmp.GREATER_THAN, 31).count());
    assertEquals(10, size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).limit(3).count());
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", 0.5).limit(3), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", Contain.IN, ImmutableList.of(0.5)), EDGE, 3, 1, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5, 2.5)).interval("time", 3, 33), EDGE, 7, 3, new boolean[] { true, true });
    evaluateQuery(v.query().labels("friend").direction(OUT).has("weight", Contain.IN, ImmutableList.of(0.5, 1.5)), EDGE, 1667, 2, new boolean[] { true, true });
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).edgeCount());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).edgeCount());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Cmp.LESS_THAN_EQUAL, 10).edgeCount());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Cmp.GREATER_THAN, 10).edgeCount());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 4.0).edgeCount());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 0.0, 2.0).edgeCount());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("number", 0).interval("weight", 2.1, 4.0).edgeCount());
    assertEquals(20, size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, 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).edgeCount());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).edgeCount());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(IN).adjacent(vs[11]).edgeCount());
    assertEquals(2, v.query().labels("knows").direction(BOTH).adjacent(vs[11]).edgeCount());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacent(vs[11]).has("weight", 3.5).edgeCount());
    assertEquals(2, v.query().labels("connect").adjacent(vs[6]).has("time", 6).edgeCount());
    assertEquals(0, v.query().labels("connect").adjacent(vs[8]).has("time", 8).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).edgeCount());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).edgeCount());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).edgeCount());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).edgeCount());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).edgeCount());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Cmp.NOT_EQUAL, 10).edgeCount());
    assertEquals(noVertices - 1, size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, 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));
    }
    assertCount(2 * (noVertices - 1), v.query().direction(Direction.BOTH).edges());
    //Property queries
    assertEquals(1, size(v.query().properties()));
    assertEquals(1, size(v.query().keys("name").properties()));
    //MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(2, size(result));
    results = tx.multiQuery(qvs).labels("knows").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(0, size(result));
    results = tx.multiQuery(qvs).edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(4, size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<TitanVertexProperty> result : results2.values()) assertEquals(1, size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<TitanVertexProperty> result : results2.values()) assertEquals(1, size(result));
    //##################################################
    //End copied queries
    //##################################################
    newTx();
    v = getOnlyElement(tx.query().has("name", "v").vertices());
    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 = getOnlyElement(tx.query().has("name", "v").vertices());
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).edgeCount());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).edgeCount());
    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] = getV(tx, qvs[i].longId());
    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").edges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, size(result));
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) Direction(org.apache.tinkerpop.gremlin.structure.Direction) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) StandardEdgeLabelMaker(com.thinkaurelius.titan.graphdb.types.StandardEdgeLabelMaker) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) VertexList(com.thinkaurelius.titan.core.VertexList) Test(org.junit.Test)

Example 50 with TitanVertex

use of com.thinkaurelius.titan.core.TitanVertex in project titan by thinkaurelius.

the class TitanGraphTest method testGlobalIteration.

/**
     * Iterating over all vertices and edges in a graph
     */
@Test
public void testGlobalIteration() {
    int numV = 50;
    int deleteV = 5;
    TitanVertex previous = tx.addVertex("count", 0);
    for (int i = 1; i < numV; i++) {
        TitanVertex next = tx.addVertex("count", i);
        previous.addEdge("next", next);
        previous = next;
    }
    int numE = numV - 1;
    assertCount(numV, tx.query().vertices());
    assertCount(numV, tx.query().vertices());
    assertCount(numE, tx.query().edges());
    assertCount(numE, tx.query().edges());
    clopen();
    assertCount(numV, tx.query().vertices());
    assertCount(numV, tx.query().vertices());
    assertCount(numE, tx.query().edges());
    assertCount(numE, tx.query().edges());
    //tx.V().range(0,deleteV).remove();
    for (TitanVertex v : tx.query().limit(deleteV).vertices()) {
        v.remove();
    }
    for (int i = 0; i < 10; i++) {
        //Repeated vertex counts
        assertCount(numV - deleteV, tx.query().vertices());
        assertCount(numV - deleteV, tx.query().has("count", Cmp.GREATER_THAN_EQUAL, 0).vertices());
    }
    clopen();
    for (int i = 0; i < 10; i++) {
        //Repeated vertex counts
        assertCount(numV - deleteV, tx.query().vertices());
        assertCount(numV - deleteV, tx.query().has("count", Cmp.GREATER_THAN_EQUAL, 0).vertices());
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Test(org.junit.Test)

Aggregations

TitanVertex (com.thinkaurelius.titan.core.TitanVertex)77 Test (org.junit.Test)63 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)46 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)21 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)17 Edge (org.apache.tinkerpop.gremlin.structure.Edge)17 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)14 VertexLabel (com.thinkaurelius.titan.core.VertexLabel)12 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)12 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)10 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)8 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)7 ElementCategory (com.thinkaurelius.titan.graphdb.internal.ElementCategory)4 Category (org.junit.experimental.categories.Category)4 SchemaViolationException (com.thinkaurelius.titan.core.SchemaViolationException)3 VertexList (com.thinkaurelius.titan.core.VertexList)3 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)3 TitanGraphBaseTest (com.thinkaurelius.titan.graphdb.TitanGraphBaseTest)3