Search in sources :

Example 11 with JanusGraphRelation

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

the class JanusGraphPartitionGraphTest method testVertexPartitioning.

@Test
public void testVertexPartitioning() {
    Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), false };
    clopen(options);
    makeVertexIndexedUniqueKey("gid", Integer.class);
    makeKey("sig", Integer.class);
    mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
    makeLabel("knows");
    makeLabel("base");
    mgmt.makeEdgeLabel("one").multiplicity(Multiplicity.ONE2ONE).make();
    mgmt.makeVertexLabel("person").make();
    mgmt.makeVertexLabel("group").partition().make();
    finishSchema();
    final Set<String> names = ImmutableSet.of("Marko", "Dan", "Stephen", "Daniel", "Josh", "Thad", "Pavel", "Matthias");
    final int numG = 10;
    final long[] gids = new long[numG];
    for (int i = 0; i < numG; i++) {
        JanusGraphVertex g = tx.addVertex("group");
        g.property(VertexProperty.Cardinality.single, "gid", i);
        g.property(VertexProperty.Cardinality.single, "sig", 0);
        for (String n : names) {
            g.property("name", n);
        }
        assertEquals(i, g.<Integer>value("gid").intValue());
        assertEquals(0, g.<Integer>value("sig").intValue());
        assertEquals("group", g.label());
        assertCount(names.size(), g.properties("name"));
        assertTrue(getId(g) > 0);
        gids[i] = getId(g);
        if (i > 0) {
            g.addEdge("base", getV(tx, gids[0]));
        }
        if (i % 2 == 1) {
            g.addEdge("one", getV(tx, gids[i - 1]));
        }
    }
    for (int i = 0; i < numG; i++) {
        JanusGraphVertex g = getV(tx, gids[i]);
        assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
        assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
        assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
        if (i > 0) {
            assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
        } else {
            assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
        }
    }
    newTx();
    for (int i = 0; i < numG; i++) {
        long gId = gids[i];
        assertTrue(idManager.isPartitionedVertex(gId));
        assertEquals(idManager.getCanonicalVertexId(gId), gId);
        JanusGraphVertex g = getV(tx, gId);
        final int canonicalPartition = getPartitionID(g);
        assertEquals(g, getOnlyElement(tx.query().has("gid", i).vertices()));
        assertEquals(i, g.<Integer>value("gid").intValue());
        assertCount(names.size(), g.properties("name"));
        // Verify that properties are distributed correctly
        JanusGraphVertexProperty p = (JanusGraphVertexProperty) getOnlyElement(g.properties("gid"));
        assertEquals(canonicalPartition, getPartitionID(p));
        for (Iterator<VertexProperty<Object>> niter = g.properties("name"); niter.hasNext(); ) {
            assertEquals(canonicalPartition, getPartitionID((JanusGraphVertex) niter.next().element()));
        }
        // Copied from above
        assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
        assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
        assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
        if (i > 0) {
            assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
        } else {
            assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
        }
    }
    clopen(options);
    final int numTx = 100;
    final int vPerTx = 10;
    Multiset<Integer> partitions = HashMultiset.create();
    for (int t = 1; t <= numTx; t++) {
        JanusGraphVertex g1 = getV(tx, gids[0]), g2 = getV(tx, gids[1]);
        assertNotNull(g1);
        JanusGraphVertex[] vs = new JanusGraphVertex[vPerTx];
        for (int vi = 0; vi < vPerTx; vi++) {
            vs[vi] = tx.addVertex("person");
            vs[vi].property(VertexProperty.Cardinality.single, "sig", t);
            Edge e = vs[vi].addEdge("knows", g1);
            e.property("sig", t);
            e = g1.addEdge("knows", vs[vi]);
            e.property("sig", t);
            if (vi % 2 == 0) {
                e = vs[vi].addEdge("knows", g2);
                e.property("sig", t);
            }
        }
        newTx();
        // Verify that all elements are in the same partition
        JanusGraphTransaction txx = graph.buildTransaction().readOnly().start();
        g1 = getV(tx, gids[0]);
        g2 = getV(tx, gids[1]);
        int partition = -1;
        for (int vi = 0; vi < vPerTx; vi++) {
            assertTrue(vs[vi].hasId());
            int pid = getPartitionID(vs[vi]);
            if (partition < 0)
                partition = pid;
            else
                assertEquals(partition, pid);
            int numRelations = 0;
            JanusGraphVertex v = getV(txx, vs[vi].longId());
            for (JanusGraphRelation r : v.query().relations()) {
                numRelations++;
                assertEquals(partition, getPartitionID(r));
                if (r instanceof JanusGraphEdge) {
                    JanusGraphVertex o = ((JanusGraphEdge) r).otherVertex(v);
                    assertTrue(o.equals(g1) || o.equals(g2));
                }
            }
            assertEquals(3 + (vi % 2 == 0 ? 1 : 0), numRelations);
        }
        partitions.add(partition);
        txx.commit();
    }
    // Verify spread across partitions; this number is a pessimistic lower bound but might fail since it is probabilistic
    // 
    assertTrue(partitions.elementSet().size() >= 3);
    newTx();
    // Verify edge querying across partitions
    JanusGraphVertex g1 = getV(tx, gids[0]);
    assertEquals(0, g1.<Integer>value("gid").intValue());
    assertEquals("group", g1.label());
    assertCount(names.size(), g1.properties("name"));
    assertCount(numTx * vPerTx, g1.query().direction(Direction.OUT).labels("knows").edges());
    assertCount(numTx * vPerTx, g1.query().direction(Direction.IN).labels("knows").edges());
    assertCount(numTx * vPerTx * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
    assertCount(numTx * vPerTx + numG, tx.query().vertices());
    newTx();
    // Restrict to partitions
    for (int t = 0; t < 10; t++) {
        int numP = random.nextInt(3) + 1;
        Set<Integer> parts = Sets.newHashSet();
        int numV = 0;
        while (parts.size() < numP) {
            int part = Iterables.get(partitions.elementSet(), random.nextInt(partitions.elementSet().size()));
            if (parts.add(part))
                numV += partitions.count(part);
        }
        numV *= vPerTx;
        int[] partitionArray = new int[numP];
        int i = 0;
        for (Integer part : parts) partitionArray[i++] = part;
        JanusGraphTransaction tx2 = graph.buildTransaction().restrictedPartitions(partitionArray).readOnly().start();
        // Copied from above
        g1 = getV(tx2, gids[0]);
        assertEquals(0, g1.<Integer>value("gid").intValue());
        assertEquals("group", g1.label());
        assertTrue(names.size() >= Iterators.size(g1.properties("name")));
        assertCount(numV, g1.query().direction(Direction.OUT).labels("knows").edges());
        assertCount(numV, g1.query().direction(Direction.IN).labels("knows").edges());
        assertCount(numV * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
        // Test local intersection
        JanusGraphVertex g2 = getV(tx2, gids[1]);
        VertexList v1 = g1.query().direction(Direction.IN).labels("knows").vertexIds();
        VertexList v2 = g2.query().direction(Direction.IN).labels("knows").vertexIds();
        assertEquals(numV, v1.size());
        assertEquals(numV / 2, v2.size());
        v1.sort();
        v2.sort();
        LongArrayList al1 = v1.getIDs();
        LongArrayList al2 = v2.getIDs();
        assertTrue(AbstractLongListUtil.isSorted(al1));
        assertTrue(AbstractLongListUtil.isSorted(al2));
        LongArrayList alr = AbstractLongListUtil.mergeJoin(al1, al2, false);
        assertEquals(numV / 2, alr.size());
        tx2.commit();
    }
}
Also used : JanusGraphRelation(org.janusgraph.core.JanusGraphRelation) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) LongArrayList(com.carrotsearch.hppc.LongArrayList) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) VertexList(org.janusgraph.core.VertexList) OLAPTest(org.janusgraph.olap.OLAPTest) Test(org.junit.jupiter.api.Test)

Aggregations

JanusGraphRelation (org.janusgraph.core.JanusGraphRelation)11 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)6 RelationType (org.janusgraph.core.RelationType)5 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)4 HashMap (java.util.HashMap)2 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty)2 JanusGraphEdge (org.janusgraph.core.JanusGraphEdge)2 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)2 JanusGraphVertexProperty (org.janusgraph.core.JanusGraphVertexProperty)2 PropertyKey (org.janusgraph.core.PropertyKey)2 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)2 StandardJanusGraphTx (org.janusgraph.graphdb.transaction.StandardJanusGraphTx)2 SystemRelationType (org.janusgraph.graphdb.types.system.SystemRelationType)2 Interval (org.janusgraph.util.datastructures.Interval)2 PointInterval (org.janusgraph.util.datastructures.PointInterval)2 RangeInterval (org.janusgraph.util.datastructures.RangeInterval)2 Test (org.junit.jupiter.api.Test)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1