Search in sources :

Example 21 with JanusGraphIndex

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

the class ManagementSystem method iterateIndexes.

private String iterateIndexes(String pattern, Iterable<JanusGraphIndex> indexes) {
    StringBuilder sb = new StringBuilder();
    for (JanusGraphIndex index : indexes) {
        String type = getIndexType(index);
        PropertyKey[] keys = index.getFieldKeys();
        String[][] keyStatus = getKeyStatus(keys, index);
        sb.append(String.format(pattern, index.name(), type, index.isUnique(), index.getBackingIndex(), keyStatus[0][0] + ":", keyStatus[0][1]));
        if (keyStatus.length > 1) {
            for (int i = 1; i < keyStatus.length; i++) {
                sb.append(String.format(pattern, "", "", "", "", keyStatus[i][0] + ":", keyStatus[i][1]));
            }
        }
    }
    return sb.toString();
}
Also used : JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey)

Example 22 with JanusGraphIndex

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

the class JanusGraphAppTest method createSchema.

@Test
public void createSchema() throws ConfigurationException, IOException {
    final JanusGraphApp app = new JanusGraphApp(CONF_FILE);
    final GraphTraversalSource g = app.openGraph();
    app.createSchema();
    final JanusGraph janusGraph = (JanusGraph) g.getGraph();
    final JanusGraphManagement management = janusGraph.openManagement();
    final List<String> vertexLabels = StreamSupport.stream(management.getVertexLabels().spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
    final List<String> expectedVertexLabels = Stream.of("titan", "location", "god", "demigod", "human", "monster").collect(Collectors.toList());
    assertTrue(vertexLabels.containsAll(expectedVertexLabels));
    final List<String> edgeLabels = StreamSupport.stream(management.getRelationTypes(EdgeLabel.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
    final List<String> expectedEdgeLabels = Stream.of("father", "mother", "brother", "pet", "lives", "battled").collect(Collectors.toList());
    assertTrue(edgeLabels.containsAll(expectedEdgeLabels));
    final EdgeLabel father = management.getEdgeLabel("father");
    assertTrue(father.isDirected());
    assertFalse(father.isUnidirected());
    assertEquals(Multiplicity.MANY2ONE, father.multiplicity());
    final List<String> propertyKeys = StreamSupport.stream(management.getRelationTypes(PropertyKey.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
    final List<String> expectedPropertyKeys = Stream.of("name", "age", "time", "place", "reason").collect(Collectors.toList());
    assertTrue(propertyKeys.containsAll(expectedPropertyKeys));
    final PropertyKey place = management.getPropertyKey("place");
    assertEquals(Cardinality.SINGLE, place.cardinality());
    assertEquals(Geoshape.class, place.dataType());
    final JanusGraphIndex nameIndex = management.getGraphIndex("nameIndex");
    assertTrue(nameIndex.isCompositeIndex());
    assertEquals(nameIndex.getIndexedElement(), JanusGraphVertex.class);
    final PropertyKey[] nameIndexKeys = nameIndex.getFieldKeys();
    assertEquals(1, nameIndexKeys.length);
    assertEquals("name", nameIndexKeys[0].name());
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) JanusGraph(org.janusgraph.core.JanusGraph) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.jupiter.api.Test)

Example 23 with JanusGraphIndex

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

the class AbstractIndexManagementIT method testRepairGraphIndex.

@Test
public void testRepairGraphIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();
    // Load the "Graph of the Gods" sample data (WITHOUT mixed index coverage)
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);
    // Create and enable a graph index on age
    JanusGraphManagement m = graph.openManagement();
    PropertyKey age = m.getPropertyKey("age");
    m.buildIndex("verticesByAge", Vertex.class).addKey(age).buildCompositeIndex();
    m.commit();
    graph.tx().commit();
    // Block until the SchemaStatus transitions to REGISTERED
    assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "verticesByAge").status(SchemaStatus.REGISTERED).call().getSucceeded());
    m = graph.openManagement();
    JanusGraphIndex index = m.getGraphIndex("verticesByAge");
    m.updateIndex(index, SchemaAction.ENABLE_INDEX);
    m.commit();
    graph.tx().commit();
    // Block until the SchemaStatus transitions to ENABLED
    assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "verticesByAge").status(SchemaStatus.ENABLED).call().getSucceeded());
    // Run a query that hits the index but erroneously returns nothing because we haven't repaired yet
    assertFalse(graph.query().has("age", 10000).vertices().iterator().hasNext());
    // Repair
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    index = m.getGraphIndex("verticesByAge");
    ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REINDEX).get();
    assertEquals(6, metrics.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
    // Test the index
    Iterable<JanusGraphVertex> hits = graph.query().has("age", 4500).vertices();
    assertNotNull(hits);
    assertEquals(1, Iterables.size(hits));
    JanusGraphVertex v = Iterables.getOnlyElement(hits);
    assertNotNull(v);
    assertEquals("neptune", v.value("name"));
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) ScanMetrics(org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.jupiter.api.Test) JanusGraphBaseTest(org.janusgraph.graphdb.JanusGraphBaseTest)

Example 24 with JanusGraphIndex

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

the class ManagementUtil method awaitIndexUpdate.

private static void awaitIndexUpdate(JanusGraph g, String indexName, String relationTypeName, long time, TemporalUnit unit) {
    Preconditions.checkArgument(g != null && g.isOpen(), "Need to provide valid, open graph instance");
    Preconditions.checkArgument(time > 0 && unit != null, "Need to provide valid time interval");
    Preconditions.checkArgument(StringUtils.isNotBlank(indexName), "Need to provide an index name");
    StandardJanusGraph graph = (StandardJanusGraph) g;
    TimestampProvider times = graph.getConfiguration().getTimestampProvider();
    Instant end = times.getTime().plus(Duration.of(time, unit));
    boolean isStable = false;
    while (times.getTime().isBefore(end)) {
        JanusGraphManagement management = graph.openManagement();
        try {
            if (StringUtils.isNotBlank(relationTypeName)) {
                RelationTypeIndex idx = management.getRelationIndex(management.getRelationType(relationTypeName), indexName);
                Preconditions.checkNotNull(idx, "Index could not be found: %s @ %s", indexName, relationTypeName);
                isStable = idx.getIndexStatus().isStable();
            } else {
                JanusGraphIndex idx = management.getGraphIndex(indexName);
                Preconditions.checkNotNull(idx, "Index could not be found: %s", indexName);
                isStable = true;
                for (PropertyKey key : idx.getFieldKeys()) {
                    if (!idx.getIndexStatus(key).isStable())
                        isStable = false;
                }
            }
        } finally {
            management.rollback();
        }
        if (isStable)
            break;
        try {
            times.sleepFor(Duration.ofMillis(500));
        } catch (InterruptedException ignored) {
        }
    }
    if (!isStable)
        throw new JanusGraphException("Index did not stabilize within the given amount of time. For sufficiently long " + "wait periods this is most likely caused by a failed/incorrectly shut down JanusGraph instance or a lingering transaction.");
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) TimestampProvider(org.janusgraph.diskstorage.util.time.TimestampProvider) Instant(java.time.Instant) JanusGraphException(org.janusgraph.core.JanusGraphException) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) PropertyKey(org.janusgraph.core.PropertyKey) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

Example 25 with JanusGraphIndex

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

the class GraphOfTheGodsFactory method load.

public static void load(final JanusGraph graph, String mixedIndexName, boolean uniqueNameCompositeIndex) {
    if (graph instanceof StandardJanusGraph) {
        Preconditions.checkState(mixedIndexNullOrExists((StandardJanusGraph) graph, mixedIndexName), ERR_NO_INDEXING_BACKEND, mixedIndexName);
    }
    // Create Schema
    JanusGraphManagement management = graph.openManagement();
    final PropertyKey name = management.makePropertyKey("name").dataType(String.class).make();
    JanusGraphManagement.IndexBuilder nameIndexBuilder = management.buildIndex("name", Vertex.class).addKey(name);
    if (uniqueNameCompositeIndex)
        nameIndexBuilder.unique();
    JanusGraphIndex nameIndex = nameIndexBuilder.buildCompositeIndex();
    management.setConsistency(nameIndex, ConsistencyModifier.LOCK);
    final PropertyKey age = management.makePropertyKey("age").dataType(Integer.class).make();
    if (null != mixedIndexName)
        management.buildIndex("vertices", Vertex.class).addKey(age).buildMixedIndex(mixedIndexName);
    final PropertyKey time = management.makePropertyKey("time").dataType(Integer.class).make();
    final PropertyKey reason = management.makePropertyKey("reason").dataType(String.class).make();
    final PropertyKey place = management.makePropertyKey("place").dataType(Geoshape.class).make();
    if (null != mixedIndexName)
        management.buildIndex("edges", Edge.class).addKey(reason).addKey(place).buildMixedIndex(mixedIndexName);
    management.makeEdgeLabel("father").multiplicity(Multiplicity.MANY2ONE).make();
    management.makeEdgeLabel("mother").multiplicity(Multiplicity.MANY2ONE).make();
    EdgeLabel battled = management.makeEdgeLabel("battled").signature(time).make();
    management.buildEdgeIndex(battled, "battlesByTime", Direction.BOTH, Order.desc, time);
    management.makeEdgeLabel("lives").signature(reason).make();
    management.makeEdgeLabel("pet").make();
    management.makeEdgeLabel("brother").make();
    management.makeVertexLabel("titan").make();
    management.makeVertexLabel("location").make();
    management.makeVertexLabel("god").make();
    management.makeVertexLabel("demigod").make();
    management.makeVertexLabel("human").make();
    management.makeVertexLabel("monster").make();
    management.commit();
    JanusGraphTransaction tx = graph.newTransaction();
    // vertices
    Vertex saturn = tx.addVertex(T.label, "titan", "name", "saturn", "age", 10000);
    Vertex sky = tx.addVertex(T.label, "location", "name", "sky");
    Vertex sea = tx.addVertex(T.label, "location", "name", "sea");
    Vertex jupiter = tx.addVertex(T.label, "god", "name", "jupiter", "age", 5000);
    Vertex neptune = tx.addVertex(T.label, "god", "name", "neptune", "age", 4500);
    Vertex hercules = tx.addVertex(T.label, "demigod", "name", "hercules", "age", 30);
    Vertex alcmene = tx.addVertex(T.label, "human", "name", "alcmene", "age", 45);
    Vertex pluto = tx.addVertex(T.label, "god", "name", "pluto", "age", 4000);
    Vertex nemean = tx.addVertex(T.label, "monster", "name", "nemean");
    Vertex hydra = tx.addVertex(T.label, "monster", "name", "hydra");
    Vertex cerberus = tx.addVertex(T.label, "monster", "name", "cerberus");
    Vertex tartarus = tx.addVertex(T.label, "location", "name", "tartarus");
    // edges
    jupiter.addEdge("father", saturn);
    jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
    jupiter.addEdge("brother", neptune);
    jupiter.addEdge("brother", pluto);
    neptune.addEdge("lives", sea).property("reason", "loves waves");
    neptune.addEdge("brother", jupiter);
    neptune.addEdge("brother", pluto);
    hercules.addEdge("father", jupiter);
    hercules.addEdge("mother", alcmene);
    hercules.addEdge("battled", nemean, "time", 1, "place", Geoshape.point(38.1f, 23.7f));
    hercules.addEdge("battled", hydra, "time", 2, "place", Geoshape.point(37.7f, 23.9f));
    hercules.addEdge("battled", cerberus, "time", 12, "place", Geoshape.point(39f, 22f));
    pluto.addEdge("brother", jupiter);
    pluto.addEdge("brother", neptune);
    pluto.addEdge("lives", tartarus, "reason", "no fear of death");
    pluto.addEdge("pet", cerberus);
    cerberus.addEdge("lives", tartarus);
    // commit the transaction to disk
    tx.commit();
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) Geoshape(org.janusgraph.core.attribute.Geoshape) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph)

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