Search in sources :

Example 31 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex 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 32 with JanusGraphIndex

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

the class ManagementSystem method updateIndex.

/* --------------
    Schema Update
     --------------- */
@Override
public IndexJobFuture updateIndex(Index index, SchemaAction updateAction) {
    Preconditions.checkArgument(index != null, "Need to provide an index");
    Preconditions.checkArgument(updateAction != null, "Need to provide update action");
    JanusGraphSchemaVertex schemaVertex = getSchemaVertex(index);
    Set<JanusGraphSchemaVertex> dependentTypes;
    Set<PropertyKeyVertex> keySubset = ImmutableSet.of();
    if (index instanceof RelationTypeIndex) {
        dependentTypes = ImmutableSet.of((JanusGraphSchemaVertex) ((InternalRelationType) schemaVertex).getBaseType());
        if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
            return null;
    } else if (index instanceof JanusGraphIndex) {
        IndexType indexType = schemaVertex.asIndexType();
        dependentTypes = Sets.newHashSet();
        if (indexType.isCompositeIndex()) {
            if (!updateAction.isApplicableStatus(schemaVertex.getStatus()))
                return null;
            for (PropertyKey key : ((JanusGraphIndex) index).getFieldKeys()) {
                dependentTypes.add((PropertyKeyVertex) key);
            }
        } else {
            keySubset = Sets.newHashSet();
            MixedIndexType mixedIndexType = (MixedIndexType) indexType;
            Set<SchemaStatus> applicableStatus = updateAction.getApplicableStatus();
            for (ParameterIndexField field : mixedIndexType.getFieldKeys()) {
                if (applicableStatus.contains(field.getStatus()))
                    keySubset.add((PropertyKeyVertex) field.getFieldKey());
            }
            if (keySubset.isEmpty())
                return null;
            dependentTypes.addAll(keySubset);
        }
    } else
        throw new UnsupportedOperationException("Updates not supported for index: " + index);
    IndexIdentifier indexId = new IndexIdentifier(index);
    StandardScanner.Builder builder;
    IndexJobFuture future;
    switch(updateAction) {
        case REGISTER_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.REGISTERED, keySubset));
            future = new EmptyIndexJobFuture();
            break;
        case REINDEX:
            builder = graph.getBackend().buildEdgeScanJob();
            builder.setFinishJob(indexId.getIndexJobFinisher(graph, SchemaAction.ENABLE_INDEX));
            builder.setJobId(indexId);
            builder.setJob(VertexJobConverter.convert(graph, new IndexRepairJob(indexId.indexName, indexId.relationTypeName)));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        case ENABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.ENABLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            future = new EmptyIndexJobFuture();
            break;
        case DISABLE_INDEX:
            setStatus(schemaVertex, SchemaStatus.INSTALLED, keySubset);
            updatedTypes.add(schemaVertex);
            if (!keySubset.isEmpty())
                updatedTypes.addAll(dependentTypes);
            setUpdateTrigger(new UpdateStatusTrigger(graph, schemaVertex, SchemaStatus.DISABLED, keySubset));
            future = new EmptyIndexJobFuture();
            break;
        case REMOVE_INDEX:
            if (index instanceof RelationTypeIndex) {
                builder = graph.getBackend().buildEdgeScanJob();
            } else {
                JanusGraphIndex graphIndex = (JanusGraphIndex) index;
                if (graphIndex.isMixedIndex())
                    throw new UnsupportedOperationException("External mixed indexes must be removed in the indexing system directly.");
                builder = graph.getBackend().buildGraphIndexScanJob();
            }
            builder.setFinishJob(indexId.getIndexJobFinisher());
            builder.setJobId(indexId);
            builder.setJob(new IndexRemoveJob(graph, indexId.indexName, indexId.relationTypeName));
            try {
                future = builder.execute();
            } catch (BackendException e) {
                throw new JanusGraphException(e);
            }
            break;
        default:
            throw new UnsupportedOperationException("Update action not supported: " + updateAction);
    }
    return future;
}
Also used : Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) JanusGraphException(org.janusgraph.core.JanusGraphException) IndexRepairJob(org.janusgraph.graphdb.olap.job.IndexRepairJob) ParameterIndexField(org.janusgraph.graphdb.types.ParameterIndexField) RelationTypeIndex(org.janusgraph.core.schema.RelationTypeIndex) BackendException(org.janusgraph.diskstorage.BackendException) StandardScanner(org.janusgraph.diskstorage.keycolumnvalue.scan.StandardScanner) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) IndexType(org.janusgraph.graphdb.types.IndexType) CompositeIndexType(org.janusgraph.graphdb.types.CompositeIndexType) MixedIndexType(org.janusgraph.graphdb.types.MixedIndexType) PropertyKey(org.janusgraph.core.PropertyKey) IndexRemoveJob(org.janusgraph.graphdb.olap.job.IndexRemoveJob)

Example 33 with JanusGraphIndex

use of org.janusgraph.core.schema.JanusGraphIndex in project atlas by apache.

the class AtlasJanusGraphManagement method addMixedIndex.

@Override
public void addMixedIndex(String indexName, AtlasPropertyKey propertyKey) {
    PropertyKey janusKey = AtlasJanusObjectFactory.createPropertyKey(propertyKey);
    JanusGraphIndex vertexIndex = management.getGraphIndex(indexName);
    management.addIndexKey(vertexIndex, janusKey);
}
Also used : JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) AtlasPropertyKey(org.apache.atlas.repository.graphdb.AtlasPropertyKey) PropertyKey(org.janusgraph.core.PropertyKey)

Example 34 with JanusGraphIndex

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

the class GraphIndexStatusWatcher method call.

@Override
public GraphIndexStatusReport call() throws InterruptedException {
    Preconditions.checkNotNull(g, "Graph instance must not be null");
    Preconditions.checkNotNull(graphIndexName, "Index name must not be null");
    Preconditions.checkNotNull(statuses, "Target statuses must not be null");
    Preconditions.checkArgument(statuses.size() > 0, "Target statuses must include at least one status");
    Map<String, SchemaStatus> notConverged = new HashMap<>();
    Map<String, SchemaStatus> converged = new HashMap<>();
    JanusGraphIndex idx;
    Timer t = new Timer(TimestampProviders.MILLI).start();
    boolean timedOut;
    while (true) {
        JanusGraphManagement management = null;
        try {
            management = g.openManagement();
            idx = management.getGraphIndex(graphIndexName);
            for (PropertyKey pk : idx.getFieldKeys()) {
                SchemaStatus s = idx.getIndexStatus(pk);
                LOGGER.debug("Key {} has status {}", pk, s);
                if (!statuses.contains(s))
                    notConverged.put(pk.toString(), s);
                else
                    converged.put(pk.toString(), s);
            }
        } finally {
            if (null != management)
                // Let an exception here propagate up the stack
                management.rollback();
        }
        if (!notConverged.isEmpty()) {
            String waitingOn = StringUtils.join(notConverged, "=", ",");
            LOGGER.info("Some key(s) on index {} do not currently have status(es) {}: {}", graphIndexName, statuses, waitingOn);
        } else {
            LOGGER.info("All {} key(s) on index {} have status(es) {}", converged.size(), graphIndexName, statuses);
            return new GraphIndexStatusReport(true, graphIndexName, statuses, notConverged, converged, t.elapsed());
        }
        timedOut = null != timeout && 0 < t.elapsed().compareTo(timeout);
        if (timedOut) {
            LOGGER.info("Timed out ({}) while waiting for index {} to converge on status(es) {}", timeout, graphIndexName, statuses);
            return new GraphIndexStatusReport(false, graphIndexName, statuses, notConverged, converged, t.elapsed());
        }
        notConverged.clear();
        converged.clear();
        Thread.sleep(poll.toMillis());
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) Timer(org.janusgraph.diskstorage.util.time.Timer) HashMap(java.util.HashMap) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) SchemaStatus(org.janusgraph.core.schema.SchemaStatus) PropertyKey(org.janusgraph.core.PropertyKey)

Example 35 with JanusGraphIndex

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

the class AbstractIndexManagementIT method testRemoveGraphIndex.

@Test
public void testRemoveGraphIndex() throws InterruptedException, BackendException, ExecutionException {
    prepareGraphIndex();
    // Remove index
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    JanusGraphManagement m = graph.openManagement();
    JanusGraphIndex index = m.getGraphIndex("name");
    ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REMOVE_INDEX).get();
    assertEquals(12, metrics.getCustom(IndexRemoveJob.DELETED_RECORDS_COUNT));
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) JanusGraphIndex(org.janusgraph.core.schema.JanusGraphIndex) ScanMetrics(org.janusgraph.diskstorage.keycolumnvalue.scan.ScanMetrics) Test(org.junit.jupiter.api.Test) JanusGraphBaseTest(org.janusgraph.graphdb.JanusGraphBaseTest)

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