Search in sources :

Example 16 with EdgeLabel

use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.

the class JanusGraphTest method testGlobalGraphIndexingAndQueriesForInternalIndexes.

/* ==================================================================================
                            GLOBAL GRAPH QUERIES
     ==================================================================================*/
/**
 * Tests index definitions 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();
    JanusGraphIndex edge1 = mgmt.buildIndex("edge1", Edge.class).addKey(time).addKey(weight).buildCompositeIndex();
    JanusGraphIndex edge2 = mgmt.buildIndex("edge2", Edge.class).indexOnly(connect).addKey(text).buildCompositeIndex();
    JanusGraphIndex prop1 = mgmt.buildIndex("prop1", JanusGraphVertexProperty.class).addKey(time).buildCompositeIndex();
    JanusGraphIndex prop2 = mgmt.buildIndex("prop2", JanusGraphVertexProperty.class).addKey(weight).addKey(text).buildCompositeIndex();
    JanusGraphIndex vertex1 = mgmt.buildIndex("vertex1", Vertex.class).addKey(time).indexOnly(person).unique().buildCompositeIndex();
    JanusGraphIndex vertex12 = mgmt.buildIndex("vertex12", Vertex.class).addKey(text).indexOnly(person).buildCompositeIndex();
    JanusGraphIndex vertex2 = mgmt.buildIndex("vertex2", Vertex.class).addKey(time).addKey(name).indexOnly(organization).buildCompositeIndex();
    JanusGraphIndex 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(JanusGraphVertexProperty.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(JanusGraphVertexProperty.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 ignored) {
    }
    try {
        // Name already in use
        mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        // ImplicitKeys not allowed
        mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        // Unique is only allowed for vertex
        mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    // ########### 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(JanusGraphVertexProperty.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(JanusGraphVertexProperty.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 ignored) {
    }
    try {
        // Name already in use
        mgmt.buildIndex("edge1", Vertex.class).addKey(weight).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        // ImplicitKeys not allowed
        mgmt.buildIndex("jupdup", Vertex.class).addKey(ImplicitKey.ID).buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        // Unique is only allowed for vertex
        mgmt.buildIndex("edgexyz", Edge.class).addKey(time).unique().buildCompositeIndex();
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    // ########### END INSPECTION & FAILURE ##############
    final int numV = 100;
    final boolean sorted = true;
    JanusGraphVertex[] ns = new JanusGraphVertex[numV];
    String[] strings = { "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;
        String txt = strings[i % (strings.length)];
        ns[i].property(VertexProperty.Cardinality.single, "weight", w);
        ns[i].property(VertexProperty.Cardinality.single, "time", (long) i);
        ns[i].property(VertexProperty.Cardinality.single, "text", txt);
        for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
            p.property("weight", w);
            p.property("time", (long) i);
            p.property("text", txt);
        }
        // previous or self-loop
        JanusGraphVertex 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", (long) i, "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, strings[10 % strings.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, strings[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strings.length, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]), ElementCategory.EDGE, numV / strings.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, strings[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, strings[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, strings[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[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, strings[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strings.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, strings[10 % strings.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, strings[0]).has(LABEL_NAME, "connect"), ElementCategory.EDGE, numV / strings.length, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]).has(LABEL_NAME, "connect").limit(10), ElementCategory.EDGE, 10, new boolean[] { true, sorted }, edge2.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[0]), ElementCategory.EDGE, numV / strings.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, strings[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, strings[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, strings[2]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[3]).has(LABEL_NAME, "person"), ElementCategory.VERTEX, 0, new boolean[] { true, sorted }, vertex12.name());
    evaluateQuery(tx.query().has("text", Cmp.EQUAL, strings[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, strings[2]).has(LABEL_NAME, "person").has("time", Contain.NOT_IN, ImmutableList.of()), ElementCategory.VERTEX, numV / strings.length, new boolean[] { true, sorted }, vertex12.name());
    // Update in transaction
    for (int i = 0; i < numV / 2; i++) {
        JanusGraphVertex v = getV(tx, ns[i]);
        v.remove();
    }
    ns = new JanusGraphVertex[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;
        String txt = strings[i % (strings.length)];
        ns[i].property(VertexProperty.Cardinality.single, "weight", w);
        ns[i].property(VertexProperty.Cardinality.single, "time", (long) i);
        ns[i].property(VertexProperty.Cardinality.single, "text", txt);
        for (VertexProperty p : new VertexProperty[] { p1, p2 }) {
            p.property("weight", w);
            p.property("time", (long) i);
            p.property("text", txt);
        }
        // previous or self-loop
        JanusGraphVertex 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", (long) i, "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, strings[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, strings[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());
// *** INDIVIDUAL USE CASE TESTS ******
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) VertexLabel(org.janusgraph.core.VertexLabel) BaseVertexLabel(org.janusgraph.graphdb.types.system.BaseVertexLabel) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 17 with EdgeLabel

use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.

the class JanusGraphTest method testEdgeTTLImplicitKey.

@Test
public void testEdgeTTLImplicitKey() throws Exception {
    Duration d;
    if (!features.hasCellTTL()) {
        return;
    }
    clopen(option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true);
    assertEquals("~ttl", ImplicitKey.TTL.name());
    int ttl = 24 * 60 * 60;
    EdgeLabel likes = mgmt.makeEdgeLabel("likes").make();
    EdgeLabel hasLiked = mgmt.makeEdgeLabel("hasLiked").make();
    mgmt.setTTL(likes, Duration.ofSeconds(ttl));
    assertEquals(Duration.ofSeconds(ttl), mgmt.getTTL(likes));
    assertEquals(Duration.ZERO, mgmt.getTTL(hasLiked));
    mgmt.commit();
    JanusGraphVertex v1 = graph.addVertex(), v2 = graph.addVertex();
    Edge e1 = v1.addEdge("likes", v2);
    Edge e2 = v1.addEdge("hasLiked", v2);
    graph.tx().commit();
    // read from the edge created in this transaction
    d = e1.value("~ttl");
    assertEquals(Duration.ofDays(1), d);
    // get the edge via a vertex
    e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("likes").edges());
    d = e1.value("~ttl");
    assertEquals(Duration.ofDays(1), d);
    // returned value of ^ttl is the total time to live since commit, not remaining time
    Thread.sleep(1001);
    graph.tx().rollback();
    e1 = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("likes").edges());
    d = e1.value("~ttl");
    assertEquals(Duration.ofDays(1), d);
    // no ttl on edges of this label
    d = e2.value("~ttl");
    assertEquals(Duration.ZERO, d);
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) Duration(java.time.Duration) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 18 with EdgeLabel

use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.

the class JanusGraphTest method testVertexCentricIndexWithNull.

@Test
public void testVertexCentricIndexWithNull() {
    EdgeLabel bought = makeLabel("bought");
    PropertyKey time = makeKey("time", Long.class);
    mgmt.buildEdgeIndex(bought, "byTimeDesc", BOTH, decr, time);
    mgmt.buildEdgeIndex(bought, "byTimeIncr", BOTH, incr, time);
    finishSchema();
    JanusGraphVertex v1 = tx.addVertex(), v2 = tx.addVertex();
    v1.addEdge("bought", v2).property("time", 1);
    v1.addEdge("bought", v2).property("time", 2);
    v1.addEdge("bought", v2).property("time", 3);
    v1.addEdge("bought", v2);
    v1.addEdge("bought", v2);
    assertEquals(5, v1.query().direction(OUT).labels("bought").edgeCount());
    assertEquals(1, v1.query().direction(OUT).labels("bought").has("time", 1).edgeCount());
    assertEquals(1, v1.query().direction(OUT).labels("bought").has("time", Cmp.LESS_THAN, 3).has("time", Cmp.GREATER_THAN, 1).edgeCount());
    assertEquals(3, v1.query().direction(OUT).labels("bought").has("time", Cmp.LESS_THAN, 5).edgeCount());
    assertEquals(3, v1.query().direction(OUT).labels("bought").has("time", Cmp.GREATER_THAN, 0).edgeCount());
    assertEquals(2, v1.query().direction(OUT).labels("bought").has("time", Cmp.LESS_THAN, 3).edgeCount());
    assertEquals(1, v1.query().direction(OUT).labels("bought").has("time", Cmp.GREATER_THAN, 2).edgeCount());
    assertEquals(2, v1.query().direction(OUT).labels("bought").hasNot("time").edgeCount());
    assertEquals(5, v1.query().direction(OUT).labels("bought").edgeCount());
    newTx();
    v1 = tx.getVertex(v1.longId());
    // Queries copied from above
    assertEquals(5, v1.query().direction(OUT).labels("bought").edgeCount());
    assertEquals(1, v1.query().direction(OUT).labels("bought").has("time", 1).edgeCount());
    assertEquals(1, v1.query().direction(OUT).labels("bought").has("time", Cmp.LESS_THAN, 3).has("time", Cmp.GREATER_THAN, 1).edgeCount());
    assertEquals(3, v1.query().direction(OUT).labels("bought").has("time", Cmp.LESS_THAN, 5).edgeCount());
    assertEquals(3, v1.query().direction(OUT).labels("bought").has("time", Cmp.GREATER_THAN, 0).edgeCount());
    assertEquals(2, v1.query().direction(OUT).labels("bought").has("time", Cmp.LESS_THAN, 3).edgeCount());
    assertEquals(1, v1.query().direction(OUT).labels("bought").has("time", Cmp.GREATER_THAN, 2).edgeCount());
    assertEquals(2, v1.query().direction(OUT).labels("bought").hasNot("time").edgeCount());
    assertEquals(5, v1.query().direction(OUT).labels("bought").edgeCount());
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 19 with EdgeLabel

use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.

the class JanusGraphTest 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();
    JanusGraphVertex v1 = tx.addVertex("person");
    JanusGraphVertex 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 : VertexLabel(org.janusgraph.core.VertexLabel) BaseVertexLabel(org.janusgraph.graphdb.types.system.BaseVertexLabel) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Category(org.junit.experimental.categories.Category) RelationCategory(org.janusgraph.graphdb.internal.RelationCategory) ElementCategory(org.janusgraph.graphdb.internal.ElementCategory) Test(org.junit.Test)

Example 20 with EdgeLabel

use of org.janusgraph.core.EdgeLabel in project janusgraph by JanusGraph.

the class JanusGraphTest method testEdgeTTLTiming.

/* ==================================================================================
                            TIME TO LIVE
     ==================================================================================*/
@Test
public void testEdgeTTLTiming() throws Exception {
    if (!features.hasCellTTL()) {
        return;
    }
    EdgeLabel label1 = mgmt.makeEdgeLabel("likes").make();
    int ttl1 = 1;
    int ttl2 = 4;
    mgmt.setTTL(label1, Duration.ofSeconds(ttl1));
    EdgeLabel label2 = mgmt.makeEdgeLabel("dislikes").make();
    mgmt.setTTL(label2, Duration.ofSeconds(ttl2));
    EdgeLabel label3 = mgmt.makeEdgeLabel("indifferentTo").make();
    assertEquals(Duration.ofSeconds(ttl1), mgmt.getTTL(label1));
    assertEquals(Duration.ofSeconds(ttl2), mgmt.getTTL(label2));
    assertEquals(Duration.ZERO, mgmt.getTTL(label3));
    mgmt.commit();
    JanusGraphVertex v1 = graph.addVertex(), v2 = graph.addVertex(), v3 = graph.addVertex();
    v1.addEdge("likes", v2);
    v2.addEdge("dislikes", v1);
    v3.addEdge("indifferentTo", v1);
    // initial, pre-commit state of the edges.  They are not yet subject to TTL
    assertNotEmpty(v1.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v2.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v3.query().direction(Direction.OUT).vertices());
    long commitTime = System.currentTimeMillis();
    graph.tx().commit();
    // edges are now subject to TTL, although we must commit() or rollback() to see it
    assertNotEmpty(v1.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v2.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v3.query().direction(Direction.OUT).vertices());
    Thread.sleep(commitTime + (ttl1 * 1000L + 200) - System.currentTimeMillis());
    graph.tx().rollback();
    // e1 has dropped out
    assertEmpty(v1.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v2.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v3.query().direction(Direction.OUT).vertices());
    Thread.sleep(commitTime + (ttl2 * 1000L + 500) - System.currentTimeMillis());
    graph.tx().rollback();
    // both e1 and e2 have dropped out.  e3 has no TTL, and so remains
    assertEmpty(v1.query().direction(Direction.OUT).vertices());
    assertEmpty(v2.query().direction(Direction.OUT).vertices());
    assertNotEmpty(v3.query().direction(Direction.OUT).vertices());
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) Test(org.junit.Test)

Aggregations

EdgeLabel (org.janusgraph.core.EdgeLabel)26 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)21 PropertyKey (org.janusgraph.core.PropertyKey)21 Test (org.junit.Test)21 Edge (org.apache.tinkerpop.gremlin.structure.Edge)9 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)9 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)6 VertexLabel (org.janusgraph.core.VertexLabel)6 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)6 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)6 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)5 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)5 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)5 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)4 JanusGraph (org.janusgraph.core.JanusGraph)4 SchemaViolationException (org.janusgraph.core.SchemaViolationException)4 VertexList (org.janusgraph.core.VertexList)4