Search in sources :

Example 51 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class JanusGraphTest method testVertexTTLWithCompositeIndex.

@Test
@FeatureFlag(feature = JanusGraphFeature.CellTtl)
public void testVertexTTLWithCompositeIndex() throws Exception {
    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 : CacheVertex(org.janusgraph.graphdb.vertices.CacheVertex) 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) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test) FeatureFlag(org.janusgraph.testutil.FeatureFlag)

Example 52 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method shouldUpdateIndexFieldsAfterIndexModification.

@RepeatedIfExceptionsTest(repeats = 4, minSuccess = 2)
public void shouldUpdateIndexFieldsAfterIndexModification() throws InterruptedException, ExecutionException {
    clopen(option(FORCE_INDEX_USAGE), true, option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(5000));
    String key1 = "testKey1";
    String key2 = "testKey2";
    String key3 = "testKey3";
    String vertexL = "testVertexLabel";
    String indexName = "mixed";
    PropertyKey p1 = mgmt.makePropertyKey(key1).dataType(Long.class).make();
    PropertyKey p2 = mgmt.makePropertyKey(key2).dataType(Long.class).make();
    mgmt.makeVertexLabel(vertexL).make();
    JanusGraphIndex index = mgmt.buildIndex(indexName, Vertex.class).indexOnly(mgmt.getVertexLabel(vertexL)).addKey(mgmt.getPropertyKey(key1)).addKey(mgmt.getPropertyKey(key2)).buildMixedIndex(INDEX);
    if (index.getIndexStatus(p1) == SchemaStatus.INSTALLED) {
        mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REGISTER_INDEX).get();
        mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX).get();
    } else if (index.getIndexStatus(p1) == SchemaStatus.REGISTERED) {
        mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX).get();
    }
    mgmt.commit();
    JanusGraphVertex vertex = graph.addVertex(vertexL);
    vertex.property(key1, 111L);
    vertex.property(key2, 222L);
    graph.tx().commit();
    // By default ES indexes documents each second. By sleeping here we guarantee that documents are indexed.
    // It is just for testing. A better approach is to use Refresh API.
    Thread.sleep(1500L);
    // we open an implicit transaction and do not commit/rollback it by intention, so that we can ensure
    // adding new property to the index wouldn't cause existing transactions to fail
    assertEquals(1, graph.traversal().V().hasLabel(vertexL).has(key1, 111L).count().next());
    assertEquals(1, graph.traversal().V().hasLabel(vertexL).has(key1, 111L).toList().size());
    JanusGraph graph2 = JanusGraphFactory.open(config);
    assertEquals(1, graph2.traversal().V().hasLabel(vertexL).has(key1, 111L).count().next());
    assertEquals(1, graph2.traversal().V().hasLabel(vertexL).has(key1, 111L).toList().size());
    mgmt = graph.openManagement();
    PropertyKey testKey3 = mgmt.makePropertyKey(key3).dataType(Long.class).make();
    mgmt.addIndexKey(mgmt.getGraphIndex(indexName), testKey3);
    mgmt.commit();
    ManagementSystem.awaitGraphIndexStatus(graph, indexName).status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();
    mgmt = graph.openManagement();
    mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REINDEX).get();
    mgmt.commit();
    graph.addVertex(T.label, vertexL, key1, 1L, key2, 2L, key3, 3L);
    graph.tx().commit();
    assertTrue(graph.traversal().V().hasLabel(vertexL).has(key3, 3L).hasNext());
    try {
        graph2.addVertex(T.label, vertexL, key1, 1L, key2, 2L, key3, 3L);
        // this assertion might be flaky which is why we mark this test as RepeatedIfExceptionsTest.
        // the reason is, we cannot make sure when the schema update broadcast will be received by the graph2
        // instance.
        JanusGraphException ex = assertThrows(JanusGraphException.class, () -> graph2.tx().commit());
        assertEquals("testKey3 is not available in mixed index mixed", ex.getCause().getMessage());
        // graph2 needs some time to read from ManagementLogger asynchronously and updates cache
        // LOG_READ_INTERVAL is 5 seconds, so we wait for the same time here to ensure periodic read is triggered
        Thread.sleep(5000);
        graph2.tx().commit();
        assertTrue(graph2.traversal().V().hasLabel(vertexL).has(key3, 3L).hasNext());
    } finally {
        graph2.close();
    }
}
Also used : JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraph(org.janusgraph.core.JanusGraph) JanusGraphException(org.janusgraph.core.JanusGraphException) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) RepeatedIfExceptionsTest(io.github.artsok.RepeatedIfExceptionsTest)

Example 53 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class JanusGraphIndexTest method shouldAwaitMultipleStatuses.

@Test
public void shouldAwaitMultipleStatuses() throws InterruptedException, ExecutionException {
    final PropertyKey key1 = makeKey("key1", String.class);
    final JanusGraphIndex index = mgmt.buildIndex("randomMixedIndex", Vertex.class).addKey(key1).buildMixedIndex(INDEX);
    if (index.getIndexStatus(key1) == SchemaStatus.INSTALLED) {
        mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.REGISTER_INDEX).get();
        mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get();
    } else if (index.getIndexStatus(key1) == SchemaStatus.REGISTERED) {
        mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get();
    }
    final PropertyKey key2 = makeKey("key2", String.class);
    mgmt.addIndexKey(index, key2);
    mgmt.commit();
    // key1 now has status ENABLED, let's ensure we can watch for REGISTERED and ENABLED
    try {
        ManagementSystem.awaitGraphIndexStatus(graph, "randomMixedIndex").status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();
    } catch (final Exception e) {
        fail("Failed to awaitGraphIndexStatus on multiple statuses.");
    }
}
Also used : JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) JanusGraphException(org.janusgraph.core.JanusGraphException) ExecutionException(java.util.concurrent.ExecutionException) BackendException(org.janusgraph.diskstorage.BackendException) RepeatedIfExceptionsTest(io.github.artsok.RepeatedIfExceptionsTest) Test(org.junit.jupiter.api.Test)

Example 54 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class JanusGraphBaseTest method getExternalIndex.

public JanusGraphIndex getExternalIndex(Class<? extends Element> clazz, String backingIndex) {
    String prefix;
    if (Vertex.class.isAssignableFrom(clazz))
        prefix = "v";
    else if (Edge.class.isAssignableFrom(clazz))
        prefix = "e";
    else if (JanusGraphVertexProperty.class.isAssignableFrom(clazz))
        prefix = "p";
    else
        throw new AssertionError(clazz.toString());
    final String indexName = prefix + backingIndex;
    JanusGraphIndex index = mgmt.getGraphIndex(indexName);
    if (index == null) {
        index = mgmt.buildIndex(indexName, clazz).buildMixedIndex(backingIndex);
    }
    return index;
}
Also used : JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) JanusGraphEdge(org.janusgraph.core.JanusGraphEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 55 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.

the class JanusGraphOperationCountingTest method checkPropertyLockingAndIndex.

@Test
public void checkPropertyLockingAndIndex() {
    PropertyKey uid = makeKey("uid", String.class);
    JanusGraphIndex index = mgmt.buildIndex("uid", Vertex.class).unique().addKey(uid).buildCompositeIndex();
    mgmt.setConsistency(index, ConsistencyModifier.LOCK);
    mgmt.makePropertyKey("name").dataType(String.class).make();
    mgmt.makePropertyKey("age").dataType(Integer.class).make();
    finishSchema();
    metricsPrefix = "checkPropertyLockingAndIndex";
    JanusGraphTransaction tx = graph.buildTransaction().groupName(metricsPrefix).start();
    JanusGraphVertex v = tx.addVertex("uid", "v1", "age", 25, "name", "john");
    assertEquals(25, v.property("age").value());
    tx.commit();
    verifyStoreMetrics(EDGESTORE_NAME);
    verifyLockingOverwrite(1);
    verifyStoreMetrics(METRICS_STOREMANAGER_NAME, ImmutableMap.of(M_MUTATE, 1L));
    resetMetrics();
    tx = graph.buildTransaction().groupName(metricsPrefix).start();
    v = Iterables.getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices());
    assertEquals(25, v.property("age").value());
    tx.commit();
    verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L));
    verifyStoreMetrics(INDEXSTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L));
    verifyStoreMetrics(METRICS_STOREMANAGER_NAME);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.jupiter.api.Test)

Aggregations

JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)55 PropertyKey (org.janusgraph.core.PropertyKey)42 Test (org.junit.jupiter.api.Test)25 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)20 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)19 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)17 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 HashMap (java.util.HashMap)10 VertexLabel (org.janusgraph.core.VertexLabel)10 JanusGraph (org.janusgraph.core.JanusGraph)9 JanusGraphException (org.janusgraph.core.JanusGraphException)9 RepeatedIfExceptionsTest (io.github.artsok.RepeatedIfExceptionsTest)8 EdgeLabel (org.janusgraph.core.EdgeLabel)8 BaseVertexLabel (org.janusgraph.graphdb.types.system.BaseVertexLabel)8 BackendException (org.janusgraph.diskstorage.BackendException)7 ScanMetrics (org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics)7 ManagementSystem (org.janusgraph.graphdb.database.management.ManagementSystem)7 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)7 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)7