Search in sources :

Example 16 with JanusGraphVertex

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

the class JanusGraphTest method testIndexUpdatesWithReindexAndRemove.

@Test
public void testIndexUpdatesWithReindexAndRemove() throws InterruptedException, ExecutionException {
    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));
    // Types without index
    PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).make();
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make();
    EdgeLabel friend = mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.MULTI).make();
    PropertyKey sensor = mgmt.makePropertyKey("sensor").dataType(Double.class).cardinality(Cardinality.LIST).make();
    finishSchema();
    RelationTypeIndex pindex, eindex;
    JanusGraphIndex graphIndex;
    // Add some sensor & friend data
    JanusGraphVertex v = tx.addVertex();
    for (int i = 0; i < 10; i++) {
        v.property("sensor", i, "time", i);
        v.property("name", "v" + i);
        JanusGraphVertex o = tx.addVertex();
        v.addEdge("friend", o, "time", i);
    }
    newTx();
    // Indexes should not yet be in use
    v = getV(tx, v);
    evaluateQuery(v.query().keys("sensor").interval("time", 1, 5).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().keys("sensor").interval("time", 101, 105).orderBy("time", decr), PROPERTY, 0, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 1, 5).orderBy("time", decr), EDGE, 4, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 101, 105).orderBy("time", decr), EDGE, 0, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(tx.query().has("name", "v5"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().has("name", "v105"), ElementCategory.VERTEX, 0, new boolean[] { false, true });
    newTx();
    // Create indexes after the fact
    finishSchema();
    sensor = mgmt.getPropertyKey("sensor");
    time = mgmt.getPropertyKey("time");
    name = mgmt.getPropertyKey("name");
    friend = mgmt.getEdgeLabel("friend");
    mgmt.buildPropertyIndex(sensor, "byTime", decr, time);
    mgmt.buildEdgeIndex(friend, "byTime", Direction.OUT, decr, time);
    mgmt.buildIndex("bySensorReading", Vertex.class).addKey(name).buildCompositeIndex();
    finishSchema();
    newTx();
    // Add some sensor & friend data that should already be indexed even though index is not yet enabled
    v = getV(tx, v);
    for (int i = 100; i < 110; i++) {
        v.property("sensor", i, "time", i);
        v.property("name", "v" + i);
        JanusGraphVertex o = tx.addVertex();
        v.addEdge("friend", o, "time", i);
    }
    tx.commit();
    // Should not yet be able to enable since not yet registered
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    eindex = mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime");
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    try {
        mgmt.updateIndex(pindex, SchemaAction.ENABLE_INDEX);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        mgmt.updateIndex(eindex, SchemaAction.ENABLE_INDEX);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        mgmt.updateIndex(graphIndex, SchemaAction.ENABLE_INDEX);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    mgmt.commit();
    ManagementUtil.awaitVertexIndexUpdate(graph, "byTime", "sensor", 10, ChronoUnit.SECONDS);
    ManagementUtil.awaitGraphIndexUpdate(graph, "bySensorReading", 5, ChronoUnit.SECONDS);
    finishSchema();
    // Verify new status
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    eindex = mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime");
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    assertEquals(SchemaStatus.REGISTERED, pindex.getIndexStatus());
    assertEquals(SchemaStatus.REGISTERED, eindex.getIndexStatus());
    assertEquals(SchemaStatus.REGISTERED, graphIndex.getIndexStatus(graphIndex.getFieldKeys()[0]));
    finishSchema();
    // Simply enable without reindex
    eindex = mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime");
    mgmt.updateIndex(eindex, SchemaAction.ENABLE_INDEX);
    finishSchema();
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "byTime", "friend").status(SchemaStatus.ENABLED).timeout(10L, ChronoUnit.SECONDS).call().getSucceeded());
    // Reindex the other two
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    ScanMetrics reindexSensorByTime = mgmt.updateIndex(pindex, SchemaAction.REINDEX).get();
    finishSchema();
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    ScanMetrics reindexBySensorReading = mgmt.updateIndex(graphIndex, SchemaAction.REINDEX).get();
    finishSchema();
    assertNotEquals(0, reindexSensorByTime.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
    assertNotEquals(0, reindexBySensorReading.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
    // Every index should now be enabled
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    eindex = mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime");
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    assertEquals(SchemaStatus.ENABLED, eindex.getIndexStatus());
    assertEquals(SchemaStatus.ENABLED, pindex.getIndexStatus());
    assertEquals(SchemaStatus.ENABLED, graphIndex.getIndexStatus(graphIndex.getFieldKeys()[0]));
    // Add some more sensor & friend data
    newTx();
    v = getV(tx, v);
    for (int i = 200; i < 210; i++) {
        v.property("sensor", i, "time", i);
        v.property("name", "v" + i);
        JanusGraphVertex o = tx.addVertex();
        v.addEdge("friend", o, "time", i);
    }
    newTx();
    // Use indexes now but only see new data for property and graph index
    v = getV(tx, v);
    evaluateQuery(v.query().keys("sensor").interval("time", 1, 5).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().keys("sensor").interval("time", 101, 105).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().keys("sensor").interval("time", 201, 205).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 1, 5).orderBy("time", decr), EDGE, 0, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 101, 105).orderBy("time", decr), EDGE, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    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);
    evaluateQuery(tx.query().has("name", "v5"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "bySensorReading");
    evaluateQuery(tx.query().has("name", "v105"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "bySensorReading");
    evaluateQuery(tx.query().has("name", "v205"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "bySensorReading");
    finishSchema();
    eindex = mgmt.getRelationIndex(mgmt.getRelationType("friend"), "byTime");
    ScanMetrics reindexFriendByTime = mgmt.updateIndex(eindex, SchemaAction.REINDEX).get();
    finishSchema();
    assertNotEquals(0, reindexFriendByTime.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
    finishSchema();
    newTx();
    // It should now have all the answers
    v = getV(tx, v);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 1, 5).orderBy("time", decr), EDGE, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 101, 105).orderBy("time", decr), EDGE, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    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);
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    mgmt.updateIndex(pindex, SchemaAction.DISABLE_INDEX);
    mgmt.updateIndex(graphIndex, SchemaAction.DISABLE_INDEX);
    mgmt.commit();
    tx.commit();
    ManagementUtil.awaitVertexIndexUpdate(graph, "byTime", "sensor", 10, ChronoUnit.SECONDS);
    ManagementUtil.awaitGraphIndexUpdate(graph, "bySensorReading", 5, ChronoUnit.SECONDS);
    finishSchema();
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    assertEquals(SchemaStatus.DISABLED, pindex.getIndexStatus());
    assertEquals(SchemaStatus.DISABLED, graphIndex.getIndexStatus(graphIndex.getFieldKeys()[0]));
    finishSchema();
    newTx();
    // The two disabled indexes should force full scans
    v = getV(tx, v);
    evaluateQuery(v.query().keys("sensor").interval("time", 1, 5).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().keys("sensor").interval("time", 101, 105).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().keys("sensor").interval("time", 201, 205).orderBy("time", decr), PROPERTY, 4, 1, new boolean[] { false, false }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 1, 5).orderBy("time", decr), EDGE, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    evaluateQuery(v.query().labels("friend").direction(OUT).interval("time", 101, 105).orderBy("time", decr), EDGE, 4, 1, new boolean[] { true, true }, tx.getPropertyKey("time"), Order.DESC);
    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);
    evaluateQuery(tx.query().has("name", "v5"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().has("name", "v105"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    evaluateQuery(tx.query().has("name", "v205"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
    tx.commit();
    finishSchema();
    pindex = mgmt.getRelationIndex(mgmt.getRelationType("sensor"), "byTime");
    graphIndex = mgmt.getGraphIndex("bySensorReading");
    ScanMetrics pmetrics = mgmt.updateIndex(pindex, SchemaAction.REMOVE_INDEX).get();
    ScanMetrics graphIndexMetrics = mgmt.updateIndex(graphIndex, SchemaAction.REMOVE_INDEX).get();
    finishSchema();
    assertEquals(30, pmetrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
    assertEquals(30, graphIndexMetrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) ScanMetrics(org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 17 with JanusGraphVertex

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

the class JanusGraphTest method testStaleVertex.

@Test
public void testStaleVertex() {
    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makePropertyKey("age").dataType(Integer.class).make();
    mgmt.buildIndex("byName", Vertex.class).addKey(name).unique().buildCompositeIndex();
    finishSchema();
    JanusGraphVertex cartman = graph.addVertex("name", "cartman", "age", 10);
    graph.addVertex("name", "stan", "age", 8);
    graph.tx().commit();
    cartman = Iterables.getOnlyElement(graph.query().has("name", "cartman").vertices());
    graph.tx().commit();
    JanusGraphVertexProperty p = (JanusGraphVertexProperty) cartman.properties().next();
    assertTrue((p.longId()) > 0);
    graph.tx().commit();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphVertexProperty(org.janusgraph.core.JanusGraphVertexProperty) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 18 with JanusGraphVertex

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

the class JanusGraphTest method testHasNot.

// ................................................
@Test
public void testHasNot() {
    JanusGraphVertex v1, v2;
    v1 = graph.addVertex();
    v2 = graph.query().hasNot("abcd").vertices().iterator().next();
    assertEquals(v1, v2);
    v2 = graph.query().hasNot("abcd", true).vertices().iterator().next();
    assertEquals(v1, v2);
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Test(org.junit.Test)

Example 19 with JanusGraphVertex

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

the class JanusGraphTest method testImplicitKey.

/**
 * Test the correct application of {@link org.janusgraph.graphdb.types.system.ImplicitKey}
 * to vertices, edges, and properties.
 * <p/>
 * Additionally tests RelationIdentifier since this is closely related to ADJACENT and JANUSGRAPHID implicit keys.
 */
@Test
public void testImplicitKey() {
    JanusGraphVertex v = graph.addVertex("name", "Dan"), u = graph.addVertex();
    Edge e = v.addEdge("knows", u);
    graph.tx().commit();
    RelationIdentifier eid = (RelationIdentifier) e.id();
    assertEquals(v.id(), v.value(ID_NAME));
    assertEquals(eid, e.value(ID_NAME));
    assertEquals("knows", e.value(LABEL_NAME));
    assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.value(LABEL_NAME));
    assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, eid).edges());
    assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, RelationIdentifier.get(new long[] { 4, 5, 6, 7 })).edges());
    assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has("~nid", eid.getRelationId()).edges());
    assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has("~nid", 110111).edges());
    // Test edge retrieval
    assertNotNull(getE(graph, eid));
    assertEquals(eid, getE(graph, eid).id());
    // Test adjacent constraint
    assertEquals(1, v.query().direction(BOTH).has("~adjacent", u.id()).edgeCount());
    assertCount(1, v.query().direction(BOTH).has("~adjacent", (int) getId(u)).edges());
    try {
        // Not a valid vertex
        assertCount(0, v.query().direction(BOTH).has("~adjacent", 110111).edges());
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) RelationIdentifier(org.janusgraph.graphdb.relations.RelationIdentifier) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 20 with JanusGraphVertex

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

the class JanusGraphTest method testUnsettingTTL.

@Test
public void testUnsettingTTL() throws InterruptedException {
    if (!features.hasCellTTL()) {
        return;
    }
    int initialTTLMillis = 2000;
    // Define schema: one edge label with a short ttl
    EdgeLabel likes = mgmt.makeEdgeLabel("likes").make();
    mgmt.setTTL(likes, Duration.ofMillis(initialTTLMillis));
    mgmt.commit();
    graph.tx().rollback();
    // Insert two vertices with a TTLed edge
    JanusGraphVertex v1 = graph.addVertex();
    JanusGraphVertex v2 = graph.addVertex();
    v1.addEdge("likes", v2);
    graph.tx().commit();
    // Let the edge die
    Thread.sleep((long) Math.ceil(initialTTLMillis * 1.25));
    // Edge should be gone
    assertEquals(2, Iterators.size(graph.vertices()));
    assertEquals(0, Iterators.size(graph.edges()));
    graph.tx().rollback();
    // Remove the TTL on the edge label
    mgmt = graph.openManagement();
    mgmt.setTTL(mgmt.getEdgeLabel("likes"), Duration.ZERO);
    mgmt.commit();
    Thread.sleep(1L);
    // Check that the edge is still gone, add a new edge
    assertEquals(2, Iterators.size(graph.vertices()));
    assertEquals(0, Iterators.size(graph.edges()));
    v1 = graph.addVertex();
    v2 = graph.addVertex();
    v1.addEdge("likes", v2);
    graph.tx().commit();
    // Sleep past when it would have expired under the original config
    Thread.sleep((long) Math.ceil(initialTTLMillis * 1.25));
    // Edge must not be dead
    assertEquals(4, Iterators.size(graph.vertices()));
    assertEquals(1, Iterators.size(graph.edges()));
    graph.tx().rollback();
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) EdgeLabel(org.janusgraph.core.EdgeLabel) 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