Search in sources :

Example 66 with JanusGraphVertex

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

the class JanusGraphPerformanceMemoryTest method testTransactionalMemory.

@Test
public void testTransactionalMemory() throws Exception {
    makeVertexIndexedUniqueKey("uid", Long.class);
    makeKey("name", String.class);
    PropertyKey time = makeKey("time", Integer.class);
    mgmt.makeEdgeLabel("friend").signature(time).directed().make();
    finishSchema();
    final Random random = new Random();
    final int rounds = 100;
    final int commitSize = 1500;
    final AtomicInteger uidCounter = new AtomicInteger(0);
    Thread[] writeThreads = new Thread[4];
    long start = System.currentTimeMillis();
    for (int t = 0; t < writeThreads.length; t++) {
        writeThreads[t] = new Thread(() -> {
            for (int r = 0; r < rounds; r++) {
                JanusGraphTransaction tx = graph.newTransaction();
                JanusGraphVertex previous = null;
                for (int c = 0; c < commitSize; c++) {
                    JanusGraphVertex v = tx.addVertex();
                    long uid = uidCounter.incrementAndGet();
                    v.property(VertexProperty.Cardinality.single, "uid", uid);
                    v.property(VertexProperty.Cardinality.single, "name", "user" + uid);
                    if (previous != null) {
                        v.addEdge("friend", previous, "time", Math.abs(random.nextInt()));
                    }
                    previous = v;
                }
                tx.commit();
            }
        });
        writeThreads[t].start();
    }
    for (final Thread writeThread : writeThreads) {
        writeThread.join();
    }
    System.out.println("Write time for " + (rounds * commitSize * writeThreads.length) + " vertices & edges: " + (System.currentTimeMillis() - start));
    final int maxUID = uidCounter.get();
    final int trials = 1000;
    final String fixedName = "john";
    Thread[] readThreads = new Thread[Runtime.getRuntime().availableProcessors() * 2];
    start = System.currentTimeMillis();
    for (int t = 0; t < readThreads.length; t++) {
        readThreads[t] = new Thread(() -> {
            JanusGraphTransaction tx = graph.newTransaction();
            long randomUniqueId = random.nextInt(maxUID) + 1;
            getVertex(tx, "uid", randomUniqueId).property(VertexProperty.Cardinality.single, "name", fixedName);
            for (int t1 = 1; t1 <= trials; t1++) {
                JanusGraphVertex v = getVertex(tx, "uid", random.nextInt(maxUID) + 1);
                assertCount(2, v.properties());
                int count = 0;
                for (Object e : v.query().direction(Direction.BOTH).edges()) {
                    count++;
                    assertTrue(((JanusGraphEdge) e).<Integer>value("time") >= 0);
                }
                assertTrue(count <= 2);
            // if (t%(trials/10)==0) System.out.println(t);
            }
            assertEquals(fixedName, getVertex(tx, "uid", randomUniqueId).value("name"));
            tx.commit();
        });
        readThreads[t].start();
    }
    for (final Thread readThread : readThreads) {
        readThread.join();
    }
    System.out.println("Read time for " + (trials * readThreads.length) + " vertex lookups: " + (System.currentTimeMillis() - start));
}
Also used : JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 67 with JanusGraphVertex

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

the class JanusGraphTest 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();
    final JanusGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
    final JanusGraphIndex 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();
    JanusGraphVertex 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 : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.janusgraph.core.VertexLabel) BaseVertexLabel(org.janusgraph.graphdb.types.system.BaseVertexLabel) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 68 with JanusGraphVertex

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

the class JanusGraphTest method testBasic.

/**
 * Very simple graph operation to ensure minimal functionality and cleanup
 */
@Test
public void testBasic() throws BackendException {
    PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class);
    finishSchema();
    JanusGraphVertex n1 = tx.addVertex();
    uid = tx.getPropertyKey("name");
    n1.property(uid.name(), "abcd");
    clopen();
    long nid = n1.longId();
    uid = tx.getPropertyKey("name");
    assertTrue(getV(tx, nid) != null);
    assertTrue(getV(tx, uid.longId()) != null);
    assertMissing(tx, nid + 64);
    uid = tx.getPropertyKey(uid.name());
    n1 = getV(tx, nid);
    assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd")));
    // TODO: how to expose relations?
    assertEquals(1, Iterables.size(n1.query().relations()));
    assertEquals("abcd", n1.value(uid.name()));
    assertCount(1, tx.query().vertices());
    close();
    JanusGraphFactory.drop(graph);
    open(config);
    assertEmpty(tx.query().vertices());
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 69 with JanusGraphVertex

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

the class JanusGraphTest method testWithoutIndex.

@Test
public void testWithoutIndex() {
    PropertyKey kid = mgmt.makePropertyKey("kid").dataType(Long.class).make();
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makeEdgeLabel("knows").signature(kid).make();
    finishSchema();
    Random random = new Random();
    int numV = 1000;
    JanusGraphVertex previous = null;
    for (int i = 0; i < numV; i++) {
        JanusGraphVertex v = graph.addVertex("kid", random.nextInt(numV), "name", "v" + i);
        if (previous != null) {
            Edge e = v.addEdge("knows", previous, "kid", random.nextInt(numV / 2));
        }
        previous = v;
    }
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
    clopen();
    // Copied from above
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(500).vertices(), "kid", Order.ASC, 500);
    verifyElementOrder(graph.query().orderBy("kid", incr).limit(300).edges(), "kid", Order.ASC, 300);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(400).vertices(), "kid", Order.DESC, 400);
    verifyElementOrder(graph.query().orderBy("kid", decr).limit(200).edges(), "kid", Order.DESC, 200);
}
Also used : Random(java.util.Random) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 70 with JanusGraphVertex

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

the class JanusGraphTest method testEdgesExceedCacheSize.

@Test
public void testEdgesExceedCacheSize() {
    // Add a vertex with as many edges as the tx-cache-size. (20000 by default)
    int numEdges = graph.getConfiguration().getTxVertexCacheSize();
    JanusGraphVertex parentVertex = graph.addVertex();
    for (int i = 0; i < numEdges; i++) {
        JanusGraphVertex childVertex = graph.addVertex();
        parentVertex.addEdge("friend", childVertex);
    }
    graph.tx().commit();
    assertCount(numEdges, parentVertex.query().direction(Direction.OUT).edges());
    // Remove an edge.
    parentVertex.query().direction(OUT).edges().iterator().next().remove();
    // Check that getEdges returns one fewer.
    assertCount(numEdges - 1, parentVertex.query().direction(Direction.OUT).edges());
    // Run the same check one more time.
    // This fails! (Expected: 19999. Actual: 20000.)
    assertCount(numEdges - 1, parentVertex.query().direction(Direction.OUT).edges());
}
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