Search in sources :

Example 41 with JanusGraphVertex

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

the class JanusGraphTest method testVertexCentricIndexOrderingOnEdgePropertyWithCardinalityList.

@Test
public void testVertexCentricIndexOrderingOnEdgePropertyWithCardinalityList() {
    clopen(option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250));
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).cardinality(Cardinality.LIST).make();
    EdgeLabel friend = mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.MULTI).make();
    mgmt.buildEdgeIndex(friend, "byTime", Direction.OUT, decr, time);
    finishSchema();
    JanusGraphVertex v = tx.addVertex();
    for (int i = 200; i < 210; i++) {
        JanusGraphVertex o = tx.addVertex();
        v.addEdge("friend", o, "time", i);
    }
    assertEquals(SchemaStatus.ENABLED, mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime").getIndexStatus());
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 201, 205).orderBy("time", decr), EDGE, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    tx.commit();
    finishSchema();
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 42 with JanusGraphVertex

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

the class JanusGraphTest 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];
    JanusGraphVertex[] nodes = new JanusGraphVertex[noVertices];
    long[] nodeIds = new long[noVertices];
    List<Edge>[] 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++) {
        JanusGraphVertex 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) {
            JanusGraphVertex 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(((JanusGraphEdge) r).longId());
        }
    }
    clopen();
    nodes = new JanusGraphVertex[noVertices];
    for (int i = 0; i < noVertices; i++) {
        JanusGraphVertex 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++) {
        JanusGraphVertex 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 (Object o : n.query().direction(Direction.OUT).labels("knows").edges()) {
            JanusGraphEdge r = (JanusGraphEdge) o;
            JanusGraphVertex 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 (Object r : n.query().direction(Direction.OUT).edges()) {
            edgeIds.add(((JanusGraphEdge) r).longId());
        }
        assertTrue(edgeIds + " vs " + nodeEdgeIds[i], edgeIds.equals(nodeEdgeIds[i]));
    }
    newTx();
    // Bulk vertex retrieval
    long[] vertexIdsOne = new long[noVertices / 10];
    System.arraycopy(nodeIds, 0, vertexIdsOne, 0, vertexIdsOne.length);
    // All non-cached
    verifyVerticesRetrieval(vertexIdsOne, Lists.newArrayList(tx.getVertices(vertexIdsOne)));
    // All cached
    verifyVerticesRetrieval(vertexIdsOne, Lists.newArrayList(tx.getVertices(vertexIdsOne)));
    long[] vertexIdsTwo = new long[noVertices / 10 * 2];
    System.arraycopy(nodeIds, 0, vertexIdsTwo, 0, vertexIdsTwo.length);
    // Partially cached
    verifyVerticesRetrieval(vertexIdsTwo, Lists.newArrayList(tx.getVertices(vertexIdsTwo)));
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) StandardEdgeLabelMaker(org.janusgraph.graphdb.types.StandardEdgeLabelMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) ArrayList(java.util.ArrayList) OrderList(org.janusgraph.graphdb.internal.OrderList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) VertexList(org.janusgraph.core.VertexList) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 43 with JanusGraphVertex

use of org.janusgraph.core.JanusGraphVertex 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 44 with JanusGraphVertex

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

the class JanusGraphTest method testMultivaluedVertexProperty.

/**
 * Tests multi-valued properties with special focus on indexing and incident unidirectional edges
 * which is not tested in {@link #testSchemaTypes()}
 * <p/>
 * -->TODO: split and move this into other test cases: ordering to query, indexing to index
 */
@Test
public <V> void testMultivaluedVertexProperty() {
    /*
         * Constant test data
         *
         * The values list below must have at least two elements. The string
         * literals were chosen arbitrarily and have no special significance.
         */
    final String foo = "foo", bar = "bar", weight = "weight";
    final List<String> values = ImmutableList.of("four", "score", "and", "seven");
    assertTrue("Values list must have multiple elements for this test to make sense", 2 <= values.size());
    // Create property with name pname and a vertex
    PropertyKey w = makeKey(weight, Integer.class);
    PropertyKey f = ((StandardPropertyKeyMaker) mgmt.makePropertyKey(foo)).dataType(String.class).cardinality(Cardinality.LIST).sortKey(w).sortOrder(Order.DESC).make();
    mgmt.buildIndex(foo, Vertex.class).addKey(f).buildCompositeIndex();
    PropertyKey b = mgmt.makePropertyKey(bar).dataType(String.class).cardinality(Cardinality.LIST).make();
    mgmt.buildIndex(bar, Vertex.class).addKey(b).buildCompositeIndex();
    finishSchema();
    JanusGraphVertex v = tx.addVertex();
    // Insert prop values
    int i = 0;
    for (String s : values) {
        v.property(foo, s, weight, ++i);
        v.property(bar, s, weight, i);
    }
    // Verify correct number of properties
    assertCount(values.size(), v.properties(foo));
    assertCount(values.size(), v.properties(bar));
    // Verify order
    for (String prop : new String[] { foo, bar }) {
        int sum = 0;
        int index = values.size();
        for (Object o : v.query().labels(foo).properties()) {
            JanusGraphVertexProperty<String> p = (JanusGraphVertexProperty<String>) o;
            assertTrue(values.contains(p.value()));
            int weightAsInteger = p.value(weight);
            sum += weightAsInteger;
            if (prop == foo)
                assertEquals(index, weightAsInteger);
            index--;
        }
        assertEquals(values.size() * (values.size() + 1) / 2, sum);
    }
    assertCount(1, tx.query().has(foo, values.get(1)).vertices());
    assertCount(1, tx.query().has(foo, values.get(3)).vertices());
    assertCount(1, tx.query().has(bar, values.get(1)).vertices());
    assertCount(1, tx.query().has(bar, values.get(3)).vertices());
    // Check that removing properties works
    asStream(v.properties(foo)).forEach(Property::remove);
    // Check that the properties were actually deleted from v
    assertEmpty(v.properties(foo));
    // Reopen database
    clopen();
    assertCount(0, tx.query().has(foo, values.get(1)).vertices());
    assertCount(0, tx.query().has(foo, values.get(3)).vertices());
    assertCount(1, tx.query().has(bar, values.get(1)).vertices());
    assertCount(1, tx.query().has(bar, values.get(3)).vertices());
    // Retrieve and check our test vertex
    v = getV(tx, v);
    assertEmpty(v.properties(foo));
    assertCount(values.size(), v.properties(bar));
    // Reinsert prop values
    for (String s : values) {
        v.property(foo, s);
    }
    assertCount(values.size(), v.properties(foo));
    // Check that removing properties works
    asStream(v.properties(foo)).forEach(Property::remove);
    // Check that the properties were actually deleted from v
    assertEmpty(v.properties(foo));
}
Also used : StandardPropertyKeyMaker(org.janusgraph.graphdb.types.StandardPropertyKeyMaker) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) Property(org.apache.tinkerpop.gremlin.structure.Property) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 45 with JanusGraphVertex

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

the class JanusGraphTest method testGlobalIteration.

/**
 * Iterating over all vertices and edges in a graph
 */
@Test
public void testGlobalIteration() {
    int numV = 50;
    int deleteV = 5;
    JanusGraphVertex previous = tx.addVertex("count", 0);
    for (int i = 1; i < numV; i++) {
        JanusGraphVertex 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 (Object v : tx.query().limit(deleteV).vertices()) {
        ((JanusGraphVertex) 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 : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Test(org.junit.Test)

Aggregations

JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)87 Test (org.junit.Test)72 PropertyKey (org.janusgraph.core.PropertyKey)54 EdgeLabel (org.janusgraph.core.EdgeLabel)20 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)18 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)18 Edge (org.apache.tinkerpop.gremlin.structure.Edge)17 VertexLabel (org.janusgraph.core.VertexLabel)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)13 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)12 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)11 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)8 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)8 Instant (java.time.Instant)5 ArrayList (java.util.ArrayList)5 Random (java.util.Random)5 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)5 JanusGraph (org.janusgraph.core.JanusGraph)5