Search in sources :

Example 51 with JanusGraphVertex

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

Example 52 with JanusGraphVertex

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

the class JanusGraphTest method testVertexCentricEdgeIndexOnSimpleMultiplicityShouldWork.

@Test
public void testVertexCentricEdgeIndexOnSimpleMultiplicityShouldWork() {
    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.SINGLE).make();
    EdgeLabel friend = mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.SIMPLE).make();
    mgmt.buildEdgeIndex(friend, "byTime", Direction.OUT, decr, time);
    finishSchema();
    assertEquals(SchemaStatus.ENABLED, mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime").getIndexStatus());
    JanusGraphVertex v = tx.addVertex();
    v = getV(tx, v);
    for (int i = 200; i < 210; i++) {
        JanusGraphVertex o = tx.addVertex();
        v.addEdge("friend", o, "time", i);
    }
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 199, 210).orderBy("time", decr), EDGE, 10, 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 53 with JanusGraphVertex

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

the class VertexListTest method testLists.

@Test
public void testLists() {
    int num = 13;
    JanusGraph g = JanusGraphFactory.open("inmemory");
    StandardJanusGraphTx tx = (StandardJanusGraphTx) g.newTransaction();
    VertexLongList vll = new VertexLongList(tx);
    VertexArrayList val = new VertexArrayList(tx);
    for (int i = 0; i < num; i++) {
        JanusGraphVertex v = tx.addVertex();
        vll.add(v);
        val.add(v);
    }
    assertEquals(num, Iterables.size(vll));
    assertEquals(num, Iterables.size(val));
    vll.sort();
    val.sort();
    assertTrue(vll.isSorted());
    assertTrue(val.isSorted());
    for (Iterable<JanusGraphVertex> iterable : new Iterable[] { val, vll }) {
        Iterator<JanusGraphVertex> iterator = iterable.iterator();
        JanusGraphVertex previous = null;
        for (int i = 0; i < num; i++) {
            JanusGraphVertex next = iterator.next();
            if (previous != null)
                assertTrue(previous.longId() < next.longId());
            previous = next;
        }
        try {
            iterator.next();
            fail();
        } catch (NoSuchElementException ignored) {
        }
    }
    tx.commit();
    g.close();
}
Also used : VertexLongList(org.janusgraph.graphdb.query.vertex.VertexLongList) VertexArrayList(org.janusgraph.graphdb.query.vertex.VertexArrayList) JanusGraph(org.janusgraph.core.JanusGraph) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 54 with JanusGraphVertex

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

the class JanusGraphIndexTest method testEdgeTTLWithMixedIndices.

@Test
public void testEdgeTTLWithMixedIndices() throws Exception {
    if (!features.hasCellTTL() || !indexFeatures.supportsDocumentTTL()) {
        return;
    }
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey text = mgmt.makePropertyKey("text").dataType(String.class).make();
    PropertyKey time = makeKey("time", Long.class);
    EdgeLabel label = mgmt.makeEdgeLabel("likes").make();
    final int likesTTLSeconds = (int) TestGraphConfigs.getTTL(TimeUnit.SECONDS);
    mgmt.setTTL(label, Duration.ofSeconds(likesTTLSeconds));
    mgmt.buildIndex("index1", Edge.class).addKey(name, getStringMapping()).addKey(time).buildMixedIndex(INDEX);
    mgmt.buildIndex("index2", Edge.class).indexOnly(label).addKey(text, getTextMapping()).buildMixedIndex(INDEX);
    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ofSeconds(likesTTLSeconds), mgmt.getTTL(label));
    finishSchema();
    JanusGraphVertex v1 = tx.addVertex(), v2 = tx.addVertex(), v3 = tx.addVertex();
    Edge e1 = v1.addEdge("likes", v2, "name", "v1 likes v2", "text", "this will help to identify the edge");
    long time1 = System.currentTimeMillis();
    e1.property("time", time1);
    Edge e2 = v2.addEdge("likes", v3, "name", "v2 likes v3", "text", "this won't match anything");
    long time2 = time1 + 1;
    e2.property("time", time2);
    tx.commit();
    clopen();
    Object e1Id = e1.id();
    e2.id();
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 1, new boolean[] { true, true }, "index2");
    evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    v1 = getV(tx, v1.id());
    v2 = getV(tx, v2.id());
    v3 = getV(tx, v3.id());
    e1 = getE(tx, e1Id);
    e2 = getE(tx, e1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertNotNull(e1);
    assertNotNull(e2);
    assertNotEmpty(v1.query().direction(Direction.OUT).edges());
    assertNotEmpty(v2.query().direction(Direction.OUT).edges());
    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(likesTTLSeconds * 1.25), TimeUnit.SECONDS));
    clopen();
    // ...indexes have expired
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 0, new boolean[] { true, true }, "index2");
    evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 0, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC, "index1");
    v1 = getV(tx, v1.id());
    v2 = getV(tx, v2.id());
    v3 = getV(tx, v3.id());
    e1 = getE(tx, e1Id);
    e2 = getE(tx, e1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    // edges have expired from the graph...
    assertNull(e1);
    assertNull(e2);
    assertEmpty(v1.query().direction(Direction.OUT).edges());
    assertEmpty(v2.query().direction(Direction.OUT).edges());
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 55 with JanusGraphVertex

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

the class JanusGraphIndexTest method assertGraphOfTheGods.

public static void assertGraphOfTheGods(JanusGraph graphOfTheGods) {
    assertCount(12, graphOfTheGods.query().vertices());
    assertCount(3, graphOfTheGods.query().has(LABEL_NAME, "god").vertices());
    JanusGraphVertex h = getOnlyVertex(graphOfTheGods.query().has("name", "hercules"));
    assertEquals(30, h.<Integer>value("age").intValue());
    assertEquals("demigod", h.label());
    assertCount(5, h.query().direction(Direction.BOTH).edges());
    graphOfTheGods.tx().commit();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex)

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