Search in sources :

Example 1 with VertexLabel

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

the class TitanGraphTest method testGlobalGraphIndexingAndQueriesForInternalIndexes.

/* ==================================================================================
                            GLOBAL GRAPH QUERIES
     ==================================================================================*/
/**
     * Tests index defintions and their correct application for internal indexes only
     */
@Test
public void testGlobalGraphIndexingAndQueriesForInternalIndexes() {
    PropertyKey weight = makeKey("weight", Float.class);
    PropertyKey time = makeKey("time", Long.class);
    PropertyKey text = makeKey("text", String.class);
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.LIST).make();
    EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(weight).make();
    EdgeLabel related = mgmt.makeEdgeLabel("related").signature(time).make();
    VertexLabel person = mgmt.makeVertexLabel("person").make();
    VertexLabel organization = mgmt.makeVertexLabel("organization").make();
    TitanGraphIndex edge1 = mgmt.buildIndex("edge1", Edge.class).addKey(time).addKey(weight).buildCompositeIndex();
    TitanGraphIndex edge2 = mgmt.buildIndex("edge2", Edge.class).indexOnly(connect).addKey(text).buildCompositeIndex();
    TitanGraphIndex prop1 = mgmt.buildIndex("prop1", TitanVertexProperty.class).addKey(time).buildCompositeIndex();
    TitanGraphIndex prop2 = mgmt.buildIndex("prop2", TitanVertexProperty.class).addKey(weight).addKey(text).buildCompositeIndex();
    TitanGraphIndex vertex1 = mgmt.buildIndex("vertex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
    TitanGraphIndex vertex12 = mgmt.buildIndex("vertex12", Vertex.class).addKey(text).indexOnly(person).buildCompositeIndex();
    TitanGraphIndex vertex2 = mgmt.buildIndex("vertex2", Vertex.class).addKey(time).addKey(name).indexOnly(organization).buildCompositeIndex();
    TitanGraphIndex vertex3 = mgmt.buildIndex("vertex3", Vertex.class).addKey(name).buildCompositeIndex();
    // ########### INSPECTION & FAILURE ##############
    assertTrue(mgmt.containsRelationType("name"));
    assertTrue(mgmt.containsGraphIndex("prop1"));
    assertFalse(mgmt.containsGraphIndex("prop3"));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(Edge.class)));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(TitanVertexProperty.class)));
    assertEquals(4, Iterables.size(mgmt.getGraphIndexes(Vertex.class)));
    assertNull(mgmt.getGraphIndex("balblub"));
    edge1 = mgmt.getGraphIndex("edge1");
    edge2 = mgmt.getGraphIndex("edge2");
    prop1 = mgmt.getGraphIndex("prop1");
    prop2 = mgmt.getGraphIndex("prop2");
    vertex1 = mgmt.getGraphIndex("vertex1");
    vertex12 = mgmt.getGraphIndex("vertex12");
    vertex2 = mgmt.getGraphIndex("vertex2");
    vertex3 = mgmt.getGraphIndex("vertex3");
    assertTrue(vertex1.isUnique());
    assertFalse(edge2.isUnique());
    assertEquals("prop1", prop1.name());
    assertTrue(Vertex.class.isAssignableFrom(vertex3.getIndexedElement()));
    assertTrue(TitanVertexProperty.class.isAssignableFrom(prop1.getIndexedElement()));
    assertTrue(Edge.class.isAssignableFrom(edge2.getIndexedElement()));
    assertEquals(2, vertex2.getFieldKeys().length);
    assertEquals(1, vertex1.getFieldKeys().length);
    try {
        //Parameters not supported
        mgmt.buildIndex("blablub", Vertex.class).addKey(text, Mapping.TEXT.asParameter()).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Name already in use
        mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //ImplicitKeys not allowed
        mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Unique is only allowed for vertex
        mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########### END INSPECTION & FAILURE ##############
    finishSchema();
    clopen();
    text = mgmt.getPropertyKey("text");
    time = mgmt.getPropertyKey("time");
    weight = mgmt.getPropertyKey("weight");
    // ########### INSPECTION & FAILURE (copied from above) ##############
    assertTrue(mgmt.containsRelationType("name"));
    assertTrue(mgmt.containsGraphIndex("prop1"));
    assertFalse(mgmt.containsGraphIndex("prop3"));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(Edge.class)));
    assertEquals(2, Iterables.size(mgmt.getGraphIndexes(TitanVertexProperty.class)));
    assertEquals(4, Iterables.size(mgmt.getGraphIndexes(Vertex.class)));
    assertNull(mgmt.getGraphIndex("balblub"));
    edge1 = mgmt.getGraphIndex("edge1");
    edge2 = mgmt.getGraphIndex("edge2");
    prop1 = mgmt.getGraphIndex("prop1");
    prop2 = mgmt.getGraphIndex("prop2");
    vertex1 = mgmt.getGraphIndex("vertex1");
    vertex12 = mgmt.getGraphIndex("vertex12");
    vertex2 = mgmt.getGraphIndex("vertex2");
    vertex3 = mgmt.getGraphIndex("vertex3");
    assertTrue(vertex1.isUnique());
    assertFalse(edge2.isUnique());
    assertEquals("prop1", prop1.name());
    assertTrue(Vertex.class.isAssignableFrom(vertex3.getIndexedElement()));
    assertTrue(TitanVertexProperty.class.isAssignableFrom(prop1.getIndexedElement()));
    assertTrue(Edge.class.isAssignableFrom(edge2.getIndexedElement()));
    assertEquals(2, vertex2.getFieldKeys().length);
    assertEquals(1, vertex1.getFieldKeys().length);
    try {
        //Parameters not supported
        mgmt.buildIndex("blablub", Vertex.class).addKey(text, Mapping.TEXT.asParameter()).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Name already in use
        mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //ImplicitKeys not allowed
        mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    try {
        //Unique is only allowed for vertex
        mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException e) {
    }
    // ########### END INSPECTION & FAILURE ##############
    final int numV = 100;
    final boolean sorted = true;
    TitanVertex[] ns = new TitanVertex[numV];
    String[] strs = { "aaa", "bbb", "ccc", "ddd" };
    for (int i = 0; i < numV; i++) {
        ns[i] = tx.addVertex(i % 2 == 0 ? "person" : "organization");
        VertexProperty p1 = ns[i].property("name", "v" + i);
        VertexProperty p2 = ns[i].property("name", "u" + (i % 5));
        double w = (i * 0.5) % 5;
        long t = i;
        String txt = strs[i % (strs.length)];
        ns[i].property(VertexProperty.Cardinality.single, "weight", w);
        ns[i].property(VertexProperty.Cardinality.single, "time", t);
        ns[i].property(VertexProperty.Cardinality.single, "text", txt);
        for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
            p.property("weight", w);
            p.property("time", t);
            p.property("text", txt);
        }
        //previous or self-loop
        TitanVertex u = ns[(i > 0 ? i - 1 : i)];
        for (String label : new String[] { "connect", "related" }) {
            Edge e = ns[i].addEdge(label, u, "weight", (w++) % 5, "time", t, "text", txt);
        }
    }
    //########## QUERIES ################
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0).has("text", Cmp.EQUAL, strs[10 % strs.length]), ElementCategory.EDGE, 1, new boolean[] { false, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strs.length, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]), ElementCategory.EDGE, numV / strs.length * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[] { true, sorted }, prop2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]).has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop2.name(), prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.PROPERTY, 2 * numV / 10, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Cmp.EQUAL, 2), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex12.name(), vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "v51").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(51, 61, 71, 31, 41)).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 5, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of()), ElementCategory.VERTEX, 0, new boolean[] { true, false });
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted }, vertex3.name());
    clopen();
    //########## QUERIES (copied from above) ################
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(10, 20, 30)).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 3, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0).has("text", Cmp.EQUAL, strs[10 % strs.length]), ElementCategory.EDGE, 1, new boolean[] { false, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 1), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20).has("weight", Cmp.EQUAL, 3), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strs.length, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]), ElementCategory.EDGE, numV / strs.length * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]), ElementCategory.PROPERTY, 2 * numV / (4 * 5), new boolean[] { true, sorted }, prop2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 0.0).has("text", Cmp.EQUAL, strs[0]).has("time", Cmp.EQUAL, 0), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop2.name(), prop1.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.PROPERTY, 2 * numV / 10, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 50).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Cmp.EQUAL, 2), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex12.name(), vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "v51").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of(51, 61, 71, 31, 41)).has("name", Cmp.EQUAL, "u1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 5, new boolean[] { true, sorted }, vertex2.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 51).has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "v1").has(LABEL_NAME, "organization"), ElementCategory.VERTEX, 1, new boolean[] { false, sorted }, vertex3.name());
    evaluateQuery(tx.query().has("time", Contain.IN, ImmutableList.of()), ElementCategory.VERTEX, 0, new boolean[] { true, false });
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strs.length, new boolean[] { true, sorted }, vertex12.name());
    //Update in transaction
    for (int i = 0; i < numV / 2; i++) {
        TitanVertex v = getV(tx, ns[i]);
        v.remove();
    }
    ns = new TitanVertex[numV * 3 / 2];
    for (int i = numV; i < numV * 3 / 2; i++) {
        ns[i] = tx.addVertex(i % 2 == 0 ? "person" : "organization");
        VertexProperty p1 = ns[i].property("name", "v" + i);
        VertexProperty p2 = ns[i].property("name", "u" + (i % 5));
        double w = (i * 0.5) % 5;
        long t = i;
        String txt = strs[i % (strs.length)];
        ns[i].property(VertexProperty.Cardinality.single, "weight", w);
        ns[i].property(VertexProperty.Cardinality.single, "time", t);
        ns[i].property(VertexProperty.Cardinality.single, "text", txt);
        for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
            p.property("weight", w);
            p.property("time", t);
            p.property("text", txt);
        }
        //previous or self-loop
        TitanVertex u = ns[(i > numV ? i - 1 : i)];
        for (String label : new String[] { "connect", "related" }) {
            Edge e = ns[i].addEdge(label, u, "weight", (w++) % 5, "time", t, "text", txt);
        }
    }
    //######### UPDATED QUERIES ##########
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20), ElementCategory.PROPERTY, 0, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 20), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
    //######### END UPDATED QUERIES ##########
    newTx();
    //######### UPDATED QUERIES (copied from above) ##########
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 0, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 10).has("weight", Cmp.EQUAL, 0), ElementCategory.EDGE, 1, new boolean[] { true, sorted }, edge1.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strs[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("weight", Cmp.EQUAL, 1.5), ElementCategory.EDGE, numV / 10 * 2, new boolean[] { false, sorted });
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 20), ElementCategory.PROPERTY, 0, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 20), ElementCategory.PROPERTY, 2, new boolean[] { true, sorted }, prop1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("time", Cmp.EQUAL, numV + 30).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 1, new boolean[] { true, sorted }, vertex1.name());
    evaluateQuery(tx.query().has("name", Cmp.EQUAL, "u1"), ElementCategory.VERTEX, numV / 5, new boolean[] { true, sorted }, vertex3.name());
//*** INIVIDUAL USE CASE TESTS ******
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanVertexProperty(com.thinkaurelius.titan.core.TitanVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 2 with VertexLabel

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

the class TitanGraphTest method testVertexTTLWithCompositeIndex.

@Test
public void testVertexTTLWithCompositeIndex() throws Exception {
    if (!features.hasCellTTL()) {
        return;
    }
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Long.class).make();
    TitanGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
    TitanGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).addKey(name).addKey(time).buildCompositeIndex();
    VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make();
    mgmt.setTTL(label1, Duration.ofSeconds(1));
    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ZERO, mgmt.getTTL(time));
    assertEquals(Duration.ofSeconds(1), mgmt.getTTL(label1));
    mgmt.commit();
    TitanVertex v1 = tx.addVertex(T.label, "event", "name", "some event", "time", System.currentTimeMillis());
    tx.commit();
    Object id = v1.id();
    v1 = getV(graph, id);
    assertNotNull(v1);
    assertNotEmpty(graph.query().has("name", "some event").vertices());
    Thread.sleep(1001);
    graph.tx().rollback();
    v1 = getV(graph, id);
    assertNull(v1);
    assertEmpty(graph.query().has("name", "some event").vertices());
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 3 with VertexLabel

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

the class TitanGraphTest method testEdgeTTLLimitedByVertexTTL.

@Category({ BrittleTests.class })
@Test
public void testEdgeTTLLimitedByVertexTTL() throws Exception {
    if (!features.hasCellTTL()) {
        return;
    }
    Boolean dbCache = config.get("cache.db-cache", Boolean.class);
    if (null == dbCache) {
        dbCache = false;
    }
    EdgeLabel likes = mgmt.makeEdgeLabel("likes").make();
    // long edge TTL will be overridden by short vertex TTL
    mgmt.setTTL(likes, Duration.ofSeconds(42));
    EdgeLabel dislikes = mgmt.makeEdgeLabel("dislikes").make();
    mgmt.setTTL(dislikes, Duration.ofSeconds(1));
    EdgeLabel indifferentTo = mgmt.makeEdgeLabel("indifferentTo").make();
    VertexLabel label1 = mgmt.makeVertexLabel("person").setStatic().make();
    mgmt.setTTL(label1, Duration.ofSeconds(2));
    assertEquals(Duration.ofSeconds(42), mgmt.getTTL(likes));
    assertEquals(Duration.ofSeconds(1), mgmt.getTTL(dislikes));
    assertEquals(Duration.ZERO, mgmt.getTTL(indifferentTo));
    assertEquals(Duration.ofSeconds(2), mgmt.getTTL(label1));
    mgmt.commit();
    TitanVertex v1 = tx.addVertex("person");
    TitanVertex v2 = tx.addVertex();
    Edge v1LikesV2 = v1.addEdge("likes", v2);
    Edge v1DislikesV2 = v1.addEdge("dislikes", v2);
    Edge v1IndifferentToV2 = v1.addEdge("indifferentTo", v2);
    tx.commit();
    long commitTime = System.currentTimeMillis();
    Object v1Id = v1.id();
    Object v2id = v2.id();
    Object v1LikesV2Id = v1LikesV2.id();
    Object v1DislikesV2Id = v1DislikesV2.id();
    Object v1IndifferentToV2Id = v1IndifferentToV2.id();
    v1 = getV(graph, v1Id);
    v2 = getV(graph, v2id);
    v1LikesV2 = getE(graph, v1LikesV2Id);
    v1DislikesV2 = getE(graph, v1DislikesV2Id);
    v1IndifferentToV2 = getE(graph, v1IndifferentToV2Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v1LikesV2);
    assertNotNull(v1DislikesV2);
    assertNotNull(v1IndifferentToV2);
    assertNotEmpty(v2.query().direction(Direction.IN).labels("likes").edges());
    assertNotEmpty(v2.query().direction(Direction.IN).labels("dislikes").edges());
    assertNotEmpty(v2.query().direction(Direction.IN).labels("indifferentTo").edges());
    Thread.sleep(commitTime + 1001L - System.currentTimeMillis());
    graph.tx().rollback();
    v1 = getV(graph, v1Id);
    v2 = getV(graph, v2id);
    v1LikesV2 = getE(graph, v1LikesV2Id);
    v1DislikesV2 = getE(graph, v1DislikesV2Id);
    v1IndifferentToV2 = getE(graph, v1IndifferentToV2Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v1LikesV2);
    // this edge has expired
    assertNull(v1DislikesV2);
    assertNotNull(v1IndifferentToV2);
    assertNotEmpty(v2.query().direction(Direction.IN).labels("likes").edges());
    // expired
    assertEmpty(v2.query().direction(Direction.IN).labels("dislikes").edges());
    assertNotEmpty(v2.query().direction(Direction.IN).labels("indifferentTo").edges());
    Thread.sleep(commitTime + 2001L - System.currentTimeMillis());
    graph.tx().rollback();
    v1 = getV(graph, v1Id);
    v2 = getV(graph, v2id);
    v1LikesV2 = getE(graph, v1LikesV2Id);
    v1DislikesV2 = getE(graph, v1DislikesV2Id);
    v1IndifferentToV2 = getE(graph, v1IndifferentToV2Id);
    // the vertex itself has expired
    assertNull(v1);
    assertNotNull(v2);
    // all incident edges have necessarily expired
    assertNull(v1LikesV2);
    assertNull(v1DislikesV2);
    assertNull(v1IndifferentToV2);
    if (dbCache) {
    /* TODO: uncomment
            assertNotEmpty(v2.query().direction(Direction.IN).labels("likes").edges());
            assertNotEmpty(v2.query().direction(Direction.IN).labels("dislikes").edges());
            assertNotEmpty(v2.query().direction(Direction.IN).labels("indifferentTo").edges());
            */
    } else {
        assertEmpty(v2.query().direction(Direction.IN).labels("likes").edges());
        assertEmpty(v2.query().direction(Direction.IN).labels("dislikes").edges());
        assertEmpty(v2.query().direction(Direction.IN).labels("indifferentTo").edges());
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) EdgeLabel(com.thinkaurelius.titan.core.EdgeLabel) TitanEdge(com.thinkaurelius.titan.core.TitanEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Category(org.junit.experimental.categories.Category) RelationCategory(com.thinkaurelius.titan.graphdb.internal.RelationCategory) ElementCategory(com.thinkaurelius.titan.graphdb.internal.ElementCategory) Test(org.junit.Test)

Example 4 with VertexLabel

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

the class TitanGraphTest method testIndexQueryWithLabelsAndContainsIN.

@Test
public void testIndexQueryWithLabelsAndContainsIN() {
    // This test is based on the steps to reproduce #882
    String labelName = "labelName";
    VertexLabel label = mgmt.makeVertexLabel(labelName).make();
    PropertyKey uid = mgmt.makePropertyKey("uid").dataType(String.class).make();
    TitanGraphIndex uidCompositeIndex = mgmt.buildIndex("uidIndex", Vertex.class).indexOnly(label).addKey(uid).unique().buildCompositeIndex();
    mgmt.setConsistency(uidCompositeIndex, ConsistencyModifier.LOCK);
    finishSchema();
    TitanVertex foo = graph.addVertex(labelName);
    TitanVertex bar = graph.addVertex(labelName);
    foo.property("uid", "foo");
    bar.property("uid", "bar");
    graph.tx().commit();
    Iterable<TitanVertex> vertexes = graph.query().has("uid", Contain.IN, ImmutableList.of("foo", "bar")).has(LABEL_NAME, labelName).vertices();
    assertEquals(2, Iterables.size(vertexes));
    for (TitanVertex v : vertexes) {
        assertEquals(labelName, v.vertexLabel().name());
    }
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel) TitanGraphIndex(com.thinkaurelius.titan.core.schema.TitanGraphIndex) PropertyKey(com.thinkaurelius.titan.core.PropertyKey) Test(org.junit.Test)

Example 5 with VertexLabel

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

the class TitanBlueprintsTransaction method addVertex.

/**
     * Creates a new vertex in the graph with the given vertex id.
     * Note, that an exception is thrown if the vertex id is not a valid Titan vertex id or if a vertex with the given
     * id already exists. Only accepts long ids - all others are ignored.
     * <p/>
     * Custom id setting must be enabled via the configuration option {@link com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration#ALLOW_SETTING_VERTEX_ID}.
     * <p/>
     * Use {@link com.thinkaurelius.titan.core.util.TitanId#toVertexId(long)} to construct a valid Titan vertex id from a user id.
     *
     * @param keyValues key-value pairs of properties to characterize or attach to the vertex
     * @return New vertex
     */
@Override
public TitanVertex addVertex(Object... keyValues) {
    ElementHelper.legalPropertyKeyValueArray(keyValues);
    if (ElementHelper.getIdValue(keyValues).isPresent())
        throw Vertex.Exceptions.userSuppliedIdsNotSupported();
    Object labelValue = null;
    for (int i = 0; i < keyValues.length; i = i + 2) {
        if (keyValues[i].equals(T.label)) {
            labelValue = keyValues[i + 1];
            Preconditions.checkArgument(labelValue instanceof VertexLabel || labelValue instanceof String, "Expected a string or VertexLabel as the vertex label argument, but got: %s", labelValue);
            if (labelValue instanceof String)
                ElementHelper.validateLabel((String) labelValue);
        }
    }
    VertexLabel label = BaseVertexLabel.DEFAULT_VERTEXLABEL;
    if (labelValue != null) {
        label = (labelValue instanceof VertexLabel) ? (VertexLabel) labelValue : getOrCreateVertexLabel((String) labelValue);
    }
    final TitanVertex vertex = addVertex(null, label);
    //        for (int i = 0; i < keyValues.length; i = i + 2) {
    //            if (!keyValues[i].equals(T.id) && !keyValues[i].equals(T.label))
    //                ((StandardTitanTx)this).addPropertyInternal(vertex,getOrCreatePropertyKey((String) keyValues[i]),keyValues[i+1]);
    //        }
    com.thinkaurelius.titan.graphdb.util.ElementHelper.attachProperties(vertex, keyValues);
    return vertex;
}
Also used : TitanVertex(com.thinkaurelius.titan.core.TitanVertex) BaseVertexLabel(com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel) VertexLabel(com.thinkaurelius.titan.core.VertexLabel)

Aggregations

VertexLabel (com.thinkaurelius.titan.core.VertexLabel)15 TitanVertex (com.thinkaurelius.titan.core.TitanVertex)12 Test (org.junit.Test)12 BaseVertexLabel (com.thinkaurelius.titan.graphdb.types.system.BaseVertexLabel)11 PropertyKey (com.thinkaurelius.titan.core.PropertyKey)10 TitanGraphIndex (com.thinkaurelius.titan.core.schema.TitanGraphIndex)8 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)4 TitanEdge (com.thinkaurelius.titan.core.TitanEdge)3 TitanTransaction (com.thinkaurelius.titan.core.TitanTransaction)2 TitanVertexProperty (com.thinkaurelius.titan.core.TitanVertexProperty)2 RelationTypeIndex (com.thinkaurelius.titan.core.schema.RelationTypeIndex)2 ElementCategory (com.thinkaurelius.titan.graphdb.internal.ElementCategory)2 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)2 RelationCategory (com.thinkaurelius.titan.graphdb.internal.RelationCategory)2 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)2 Category (org.junit.experimental.categories.Category)2 RelationType (com.thinkaurelius.titan.core.RelationType)1