Search in sources :

Example 86 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project aai-graphadmin by onap.

the class AuditJanusGraph method populateIndexes.

/**
 * Populate indexes.
 */
private void populateIndexes() {
    JanusGraphManagement mgmt = graph.openManagement();
    Iterable<JanusGraphIndex> iterable = mgmt.getGraphIndexes(Vertex.class);
    Iterator<JanusGraphIndex> JanusGraphIndexes = iterable.iterator();
    JanusGraphIndex JanusGraphIndex;
    while (JanusGraphIndexes.hasNext()) {
        JanusGraphIndex = JanusGraphIndexes.next();
        if (JanusGraphIndex.isCompositeIndex()) {
            DBIndex index = new DBIndex();
            LinkedHashSet<DBProperty> dbProperties = new LinkedHashSet<>();
            index.setName(JanusGraphIndex.name());
            index.setUnique(JanusGraphIndex.isUnique());
            PropertyKey[] keys = JanusGraphIndex.getFieldKeys();
            for (PropertyKey key : keys) {
                dbProperties.add(this.properties.get(key.name()));
            }
            index.setProperties(dbProperties);
            index.setStatus(JanusGraphIndex.getIndexStatus(keys[0]));
            this.indexes.put(index.getName(), index);
        }
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) LinkedHashSet(java.util.LinkedHashSet) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) PropertyKey(org.janusgraph.core.PropertyKey)

Example 87 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project aai-graphadmin by onap.

the class AuditJanusGraph method populateEdgeLabels.

/**
 * Populate edge labels.
 */
private void populateEdgeLabels() {
    JanusGraphManagement mgmt = graph.openManagement();
    Iterable<EdgeLabel> iterable = mgmt.getRelationTypes(EdgeLabel.class);
    Iterator<EdgeLabel> JanusGraphEdgeLabels = iterable.iterator();
    EdgeLabel edgeLabel;
    while (JanusGraphEdgeLabels.hasNext()) {
        edgeLabel = JanusGraphEdgeLabels.next();
        EdgeProperty edgeProperty = new EdgeProperty();
        edgeProperty.setName(edgeLabel.name());
        edgeProperty.setMultiplicity(edgeLabel.multiplicity());
        this.edgeLabels.put(edgeProperty.getName(), edgeProperty);
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) EdgeLabel(org.janusgraph.core.EdgeLabel)

Example 88 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project aai-graphadmin by onap.

the class AuditJanusGraph method populateProperties.

/**
 * Populate properties.
 */
private void populateProperties() {
    JanusGraphManagement mgmt = graph.openManagement();
    Iterable<PropertyKey> iterable = mgmt.getRelationTypes(PropertyKey.class);
    Iterator<PropertyKey> JanusGraphProperties = iterable.iterator();
    PropertyKey propKey;
    while (JanusGraphProperties.hasNext()) {
        propKey = JanusGraphProperties.next();
        DBProperty prop = new DBProperty();
        prop.setName(propKey.name());
        prop.setCardinality(propKey.cardinality());
        prop.setTypeClass(propKey.dataType());
        this.properties.put(prop.getName(), prop);
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) PropertyKey(org.janusgraph.core.PropertyKey)

Example 89 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project aai-graphadmin by onap.

the class SchemaModInternal4Hist method execute.

public void execute() {
    JanusGraphManagement graphMgt = null;
    boolean success = false;
    try {
        // Make sure this property is in the DB.
        graphMgt = engine.asAdmin().getManagementSystem();
        if (graphMgt == null) {
            String emsg = "Not able to get a graph Management object in SchemaModInternal4Hist.java\n";
            logAndPrint(logger, emsg);
            System.exit(1);
        }
        PropertyKey origPropKey = graphMgt.getPropertyKey(propName);
        if (origPropKey == null) {
            String emsg = "The propName = [" + propName + "] is not defined in our graph. ";
            logAndPrint(logger, emsg);
            System.exit(1);
        }
        // Rename this property to a backup name (old name with "retired_"
        // appended plus a dateStr)
        FormatDate fd = new FormatDate("MMddHHmm", "GMT");
        String dteStr = fd.getDateTime();
        String retiredName = propName + "-" + dteStr + "-RETIRED";
        graphMgt.changeName(origPropKey, retiredName);
        // Create a new property using the original property name and the
        // targetDataType
        PropertyKey freshPropKey = graphMgt.makePropertyKey(propName).dataType(type).cardinality(cardinality).make();
        // Create an index if needed (regular index will be used instead of unique for history)
        if (indexType.equals("index") || indexType.equals("uniqueIndex")) {
            String freshIndexName = propName + dteStr;
            graphMgt.buildIndex(freshIndexName, Vertex.class).addKey(freshPropKey).buildCompositeIndex();
        }
        logAndPrint(logger, "Committing schema changes with graphMgt.commit()");
        graphMgt.commit();
        engine.commit();
        Graph grTmp2 = engine.startTransaction();
        // For each node that has this property, update the new from the old
        // and then remove the
        // old property from that node
        Iterator<Vertex> verts = grTmp2.traversal().V().has(retiredName);
        int vtxCount = 0;
        while (verts.hasNext()) {
            vtxCount++;
            Vertex tmpVtx = verts.next();
            String tmpVid = tmpVtx.id().toString();
            Object origVal = tmpVtx.<Object>property(retiredName).orElse(null);
            if (preserveData) {
                tmpVtx.property(propName, origVal);
                logAndPrint(logger, "INFO -- just did the add of the freshPropertyKey and updated it with the orig value (" + origVal.toString() + ")");
            } else {
            // existing nodes just won't have that property anymore
            // Not sure if we'd ever actually want to do this -- maybe
            // we'd do this if the new
            // data type was not compatible with the old?
            }
            tmpVtx.property(retiredName).remove();
            logAndPrint(logger, "INFO -- just did the remove of the " + retiredName + " from this vertex. (vid=" + tmpVid + ")");
        }
        success = true;
    } catch (Exception ex) {
        logAndPrint(logger, "Threw a regular Exception: ");
        logAndPrint(logger, ex.getMessage());
    } finally {
        if (graphMgt != null && graphMgt.isOpen()) {
            // Any changes that worked correctly should have already done
            // their commits.
            graphMgt.rollback();
        }
        if (engine != null) {
            if (success) {
                engine.commit();
            } else {
                engine.rollback();
            }
        }
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) FormatDate(org.onap.aai.util.FormatDate) Graph(org.apache.tinkerpop.gremlin.structure.Graph) PropertyKey(org.janusgraph.core.PropertyKey)

Example 90 with JanusGraphManagement

use of org.janusgraph.core.schema.JanusGraphManagement in project aai-aai-common by onap.

the class AAIGraph method loadSchema.

private void loadSchema(JanusGraph graph) {
    // Load the propertyKeys, indexes and edge-Labels into the DB
    JanusGraphManagement graphMgt = graph.openManagement();
    logger.info("-- loading schema into JanusGraph");
    if ("true".equals(SpringContextAware.getApplicationContext().getEnvironment().getProperty("history.enabled", "false"))) {
        SchemaGenerator4Hist.loadSchemaIntoJanusGraph(graphMgt, IN_MEMORY);
    } else {
        SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt, IN_MEMORY);
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement)

Aggregations

JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)92 PropertyKey (org.janusgraph.core.PropertyKey)44 Test (org.junit.jupiter.api.Test)26 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)24 JanusGraph (org.janusgraph.core.JanusGraph)23 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)20 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)17 StandardJanusGraph (org.janusgraph.graphdb.database.StandardJanusGraph)12 HashMap (java.util.HashMap)9 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)9 ArrayList (java.util.ArrayList)8 Before (org.junit.Before)8 Test (org.junit.Test)8 JanusGraphDBEngine (org.onap.aai.serialization.engines.JanusGraphDBEngine)8 TransactionalGraphEngine (org.onap.aai.serialization.engines.TransactionalGraphEngine)8 JanusGraphBaseTest (org.janusgraph.graphdb.JanusGraphBaseTest)7 RepeatedIfExceptionsTest (io.github.artsok.RepeatedIfExceptionsTest)6 EdgeLabel (org.janusgraph.core.EdgeLabel)6 Map (java.util.Map)5 JanusGraphException (org.janusgraph.core.JanusGraphException)5