Search in sources :

Example 16 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement 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.decr, 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)

Example 17 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.

the class TxFactoryJanus method makeIndicesVertexCentric.

private static void makeIndicesVertexCentric(JanusGraphManagement management) {
    ResourceBundle keys = ResourceBundle.getBundle("indices-edges");
    Set<String> edgeLabels = keys.keySet();
    for (String edgeLabel : edgeLabels) {
        String[] propertyKeyStrings = keys.getString(edgeLabel).split(",");
        // Get all the property keys we need
        Set<PropertyKey> propertyKeys = stream(propertyKeyStrings).map(keyId -> {
            PropertyKey key = management.getPropertyKey(keyId);
            if (key == null) {
                throw new RuntimeException("Trying to create edge index on label [" + edgeLabel + "] but the property [" + keyId + "] does not exist");
            }
            return key;
        }).collect(Collectors.toSet());
        // Get the edge and indexing information
        RelationType relationType = management.getRelationType(edgeLabel);
        EdgeLabel label = management.getEdgeLabel(edgeLabel);
        // Create index on each property key
        for (PropertyKey key : propertyKeys) {
            if (management.getRelationIndex(relationType, edgeLabel + "by" + key.name()) == null) {
                management.buildEdgeIndex(label, edgeLabel + "by" + key.name(), Direction.BOTH, Order.decr, key);
            }
        }
        // Create index on all property keys
        String propertyKeyId = propertyKeys.stream().map(Namifiable::name).collect(Collectors.joining("_"));
        if (management.getRelationIndex(relationType, edgeLabel + "by" + propertyKeyId) == null) {
            PropertyKey[] allKeys = propertyKeys.toArray(new PropertyKey[propertyKeys.size()]);
            management.buildEdgeIndex(label, edgeLabel + "by" + propertyKeyId, Direction.BOTH, Order.decr, allKeys);
        }
    }
}
Also used : TraversalStrategies(org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies) StandardJanusGraphTx(org.janusgraph.graphdb.transaction.StandardJanusGraphTx) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResourceBundle(java.util.ResourceBundle) JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) GraknTx(ai.grakn.GraknTx) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) Map(java.util.Map) ErrorMessage(ai.grakn.util.ErrorMessage) PathRetractionStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.PathRetractionStrategy) Transaction(org.apache.tinkerpop.gremlin.structure.Transaction) RelationType(org.janusgraph.core.RelationType) Namifiable(org.janusgraph.core.Namifiable) Logger(org.slf4j.Logger) Properties(java.util.Properties) ImmutableMap(com.google.common.collect.ImmutableMap) JanusGraphFactory(org.janusgraph.core.JanusGraphFactory) PropertyKey(org.janusgraph.core.PropertyKey) StandardJanusGraph(org.janusgraph.graphdb.database.StandardJanusGraph) Set(java.util.Set) IOException(java.io.IOException) LazyBarrierStrategy(org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.LazyBarrierStrategy) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) EdgeLabel(org.janusgraph.core.EdgeLabel) JanusGraph(org.janusgraph.core.JanusGraph) Collectors(java.util.stream.Collectors) GraknConfigKey(ai.grakn.GraknConfigKey) Direction(org.apache.tinkerpop.gremlin.structure.Direction) VertexLabel(org.janusgraph.core.VertexLabel) Schema(ai.grakn.util.Schema) Arrays.stream(java.util.Arrays.stream) GraknTxJanus(ai.grakn.kb.internal.GraknTxJanus) InputStream(java.io.InputStream) RelationType(org.janusgraph.core.RelationType) EdgeLabel(org.janusgraph.core.EdgeLabel) ResourceBundle(java.util.ResourceBundle) PropertyKey(org.janusgraph.core.PropertyKey)

Example 18 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.

the class TxFactoryJanus method makeIndicesComposite.

private static void makeIndicesComposite(JanusGraphManagement management) {
    ResourceBundle keys = ResourceBundle.getBundle("indices-composite");
    Set<String> keyString = keys.keySet();
    for (String propertyKeyLabel : keyString) {
        String indexLabel = "by" + propertyKeyLabel;
        JanusGraphIndex index = management.getGraphIndex(indexLabel);
        if (index == null) {
            boolean isUnique = Boolean.parseBoolean(keys.getString(propertyKeyLabel));
            PropertyKey key = management.getPropertyKey(propertyKeyLabel);
            JanusGraphManagement.IndexBuilder indexBuilder = management.buildIndex(indexLabel, Vertex.class).addKey(key);
            if (isUnique) {
                indexBuilder.unique();
            }
            indexBuilder.buildCompositeIndex();
        }
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) ResourceBundle(java.util.ResourceBundle) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey)

Example 19 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.

the class TxFactoryJanus method buildJanusIndexes.

private static void buildJanusIndexes(JanusGraph graph) {
    JanusGraphManagement management = graph.openManagement();
    makeVertexLabels(management);
    makeEdgeLabels(management);
    makePropertyKeys(management);
    makeIndicesVertexCentric(management);
    makeIndicesComposite(management);
    management.commit();
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement)

Example 20 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project grakn by graknlabs.

the class TxFactoryJanusTest method testGraphConfig.

@Test
public void testGraphConfig() throws InterruptedException {
    JanusGraphManagement management = sharedGraph.openManagement();
    // Test Composite Indices
    String byId = "by" + Schema.VertexProperty.ID.name();
    String byIndex = "by" + Schema.VertexProperty.INDEX.name();
    String byValueString = "by" + Schema.VertexProperty.VALUE_STRING.name();
    String byValueLong = "by" + Schema.VertexProperty.VALUE_LONG.name();
    String byValueDouble = "by" + Schema.VertexProperty.VALUE_DOUBLE.name();
    String byValueBoolean = "by" + Schema.VertexProperty.VALUE_BOOLEAN.name();
    assertEquals(byId, management.getGraphIndex(byId).toString());
    assertEquals(byIndex, management.getGraphIndex(byIndex).toString());
    assertEquals(byValueString, management.getGraphIndex(byValueString).toString());
    assertEquals(byValueLong, management.getGraphIndex(byValueLong).toString());
    assertEquals(byValueDouble, management.getGraphIndex(byValueDouble).toString());
    assertEquals(byValueBoolean, management.getGraphIndex(byValueBoolean).toString());
    // Text Edge Indices
    ResourceBundle keys = ResourceBundle.getBundle("indices-edges");
    Set<String> keyString = keys.keySet();
    for (String label : keyString) {
        assertNotNull(management.getEdgeLabel(label));
    }
    // Test Properties
    Arrays.stream(Schema.VertexProperty.values()).forEach(property -> assertNotNull(management.getPropertyKey(property.name())));
    Arrays.stream(Schema.EdgeProperty.values()).forEach(property -> assertNotNull(management.getPropertyKey(property.name())));
    // Test Labels
    Arrays.stream(Schema.BaseType.values()).forEach(label -> assertNotNull(management.getVertexLabel(label.name())));
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) ResourceBundle(java.util.ResourceBundle) Test(org.junit.Test)

Aggregations

JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)27 PropertyKey (org.janusgraph.core.PropertyKey)10 Test (org.junit.Test)10 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)9 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)8 JanusGraphBaseTest (org.janusgraph.graphdb.JanusGraphBaseTest)7 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 JanusGraph (org.janusgraph.core.JanusGraph)5 EdgeLabel (org.janusgraph.core.EdgeLabel)4 ScanMetrics (org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics)4 ResourceBundle (java.util.ResourceBundle)3 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)3 RelationType (org.janusgraph.core.RelationType)3 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 JanusGraphException (org.janusgraph.core.JanusGraphException)2 Geoshape (org.janusgraph.core.attribute.Geoshape)2 SchemaStatus (org.janusgraph.core.schema.SchemaStatus)2