Search in sources :

Example 11 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex 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 12 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class ManagementSystem method createMixedIndex.

private JanusGraphIndex createMixedIndex(String indexName, ElementCategory elementCategory, JanusGraphSchemaType constraint, String backingIndex) {
    Preconditions.checkArgument(graph.getIndexSerializer().containsIndex(backingIndex), "Unknown external index backend: %s", backingIndex);
    checkIndexName(indexName);
    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, false);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, backingIndex);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, Cardinality.LIST);
    def.setValue(TypeDefinitionCategory.STATUS, SchemaStatus.ENABLED);
    JanusGraphSchemaVertex indexVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.GRAPHINDEX, indexName, def);
    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof JanusGraphSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (JanusGraphSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    return new JanusGraphIndexWrapper(indexVertex.asIndexType());
}
Also used : JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) TypeDefinitionMap(org.janusgraph.graphdb.types.TypeDefinitionMap)

Example 13 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class ManagementSystem method commit.

@Override
public synchronized void commit() {
    ensureOpen();
    // Commit config changes
    if (transactionalConfig.hasMutations()) {
        DataOutput out = graph.getDataSerializer().getDataOutput(128);
        out.writeObjectNotNull(MgmtLogType.CONFIG_MUTATION);
        transactionalConfig.logMutations(out);
        sysLog.add(out.getStaticBuffer());
    }
    transactionalConfig.commit();
    // Commit underlying transaction
    transaction.commit();
    // Communicate schema changes
    if (!updatedTypes.isEmpty() || evictGraphFromCache) {
        managementLogger.sendCacheEviction(updatedTypes, evictGraphFromCache, updatedTypeTriggers, getOpenInstancesInternal());
        for (JanusGraphSchemaVertex schemaVertex : updatedTypes) {
            schemaCache.expireSchemaElement(schemaVertex.longId());
        }
    }
    if (graphShutdownRequired)
        graph.close();
    close();
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)

Example 14 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class ManagementSystem method createCompositeIndex.

private JanusGraphIndex createCompositeIndex(String indexName, ElementCategory elementCategory, boolean unique, JanusGraphSchemaType constraint, PropertyKey... keys) {
    checkIndexName(indexName);
    Preconditions.checkArgument(keys != null && keys.length > 0, "Need to provide keys to index [%s]", indexName);
    Preconditions.checkArgument(!unique || elementCategory == ElementCategory.VERTEX, "Unique indexes can only be created on vertices [%s]", indexName);
    boolean allSingleKeys = true;
    boolean oneNewKey = false;
    for (PropertyKey key : keys) {
        Preconditions.checkArgument(key != null && key instanceof PropertyKeyVertex, "Need to provide valid keys: %s", key);
        if (key.cardinality() != Cardinality.SINGLE)
            allSingleKeys = false;
        if (key.isNew())
            oneNewKey = true;
        else
            updatedTypes.add((PropertyKeyVertex) key);
    }
    Cardinality indexCardinality;
    if (unique)
        indexCardinality = Cardinality.SINGLE;
    else
        indexCardinality = (allSingleKeys ? Cardinality.SET : Cardinality.LIST);
    TypeDefinitionMap def = new TypeDefinitionMap();
    def.setValue(TypeDefinitionCategory.INTERNAL_INDEX, true);
    def.setValue(TypeDefinitionCategory.ELEMENT_CATEGORY, elementCategory);
    def.setValue(TypeDefinitionCategory.BACKING_INDEX, Token.INTERNAL_INDEX_NAME);
    def.setValue(TypeDefinitionCategory.INDEXSTORE_NAME, indexName);
    def.setValue(TypeDefinitionCategory.INDEX_CARDINALITY, indexCardinality);
    def.setValue(TypeDefinitionCategory.STATUS, oneNewKey ? SchemaStatus.ENABLED : SchemaStatus.INSTALLED);
    JanusGraphSchemaVertex indexVertex = transaction.makeSchemaVertex(JanusGraphSchemaCategory.GRAPHINDEX, indexName, def);
    for (int i = 0; i < keys.length; i++) {
        Parameter[] paras = { ParameterType.INDEX_POSITION.getParameter(i) };
        addSchemaEdge(indexVertex, keys[i], TypeDefinitionCategory.INDEX_FIELD, paras);
    }
    Preconditions.checkArgument(constraint == null || (elementCategory.isValidConstraint(constraint) && constraint instanceof JanusGraphSchemaVertex));
    if (constraint != null) {
        addSchemaEdge(indexVertex, (JanusGraphSchemaVertex) constraint, TypeDefinitionCategory.INDEX_SCHEMA_CONSTRAINT, null);
    }
    updateSchemaVertex(indexVertex);
    JanusGraphIndexWrapper index = new JanusGraphIndexWrapper(indexVertex.asIndexType());
    if (!oneNewKey)
        updateIndex(index, SchemaAction.REGISTER_INDEX);
    return index;
}
Also used : Cardinality(org.janusgraph.core.Cardinality) JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex) Parameter(org.janusgraph.core.schema.Parameter) PropertyKeyVertex(org.janusgraph.graphdb.types.vertices.PropertyKeyVertex) TypeDefinitionMap(org.janusgraph.graphdb.types.TypeDefinitionMap) PropertyKey(org.janusgraph.core.PropertyKey)

Example 15 with JanusGraphSchemaVertex

use of org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex in project janusgraph by JanusGraph.

the class IndexRepairJob method validateIndexStatus.

/**
 * Check that our target index is in either the ENABLED or REGISTERED state.
 */
@Override
protected void validateIndexStatus() {
    JanusGraphSchemaVertex schemaVertex = managementSystem.getSchemaVertex(index);
    Set<SchemaStatus> acceptableStatuses = SchemaAction.REINDEX.getApplicableStatus();
    boolean isValidIndex = true;
    String invalidIndexHint;
    if (index instanceof RelationTypeIndex || (index instanceof JanusGraphIndex && ((JanusGraphIndex) index).isCompositeIndex())) {
        SchemaStatus actualStatus = schemaVertex.getStatus();
        isValidIndex = acceptableStatuses.contains(actualStatus);
        invalidIndexHint = String.format("The index has status %s, but one of %s is required", actualStatus, acceptableStatuses);
    } else {
        Preconditions.checkArgument(index instanceof JanusGraphIndex, "Unexpected index: %s", index);
        JanusGraphIndex graphIndex = (JanusGraphIndex) index;
        Preconditions.checkArgument(graphIndex.isMixedIndex());
        Map<String, SchemaStatus> invalidKeyStatuses = new HashMap<>();
        int acceptableFields = 0;
        for (PropertyKey key : graphIndex.getFieldKeys()) {
            SchemaStatus status = graphIndex.getIndexStatus(key);
            if (status != SchemaStatus.DISABLED && !acceptableStatuses.contains(status)) {
                isValidIndex = false;
                invalidKeyStatuses.put(key.name(), status);
                log.warn("Index {} has key {} in an invalid status {}", index, key, status);
            }
            if (acceptableStatuses.contains(status))
                acceptableFields++;
        }
        invalidIndexHint = String.format("The following index keys have invalid status: %s (status must be one of %s)", Joiner.on(",").withKeyValueSeparator(" has status ").join(invalidKeyStatuses), acceptableStatuses);
        if (isValidIndex && acceptableFields == 0) {
            isValidIndex = false;
            invalidIndexHint = "The index does not contain any valid keys";
        }
    }
    Preconditions.checkArgument(isValidIndex, "The index %s is in an invalid state and cannot be indexed. %s", indexName, invalidIndexHint);
    // TODO consider retrieving the current Job object and calling killJob() if !isValidIndex -- would be more efficient than throwing an exception on the first pair processed by each mapper
    log.debug("Index {} is valid for re-indexing");
}
Also used : JanusGraphSchemaVertex(org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)

Aggregations

JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)15 JanusGraphIndex (org.janusgraph.core.schema.JanusGraphIndex)4 CompositeIndexType (org.janusgraph.graphdb.types.CompositeIndexType)4 IndexType (org.janusgraph.graphdb.types.IndexType)4 MixedIndexType (org.janusgraph.graphdb.types.MixedIndexType)4 TypeDefinitionMap (org.janusgraph.graphdb.types.TypeDefinitionMap)4 JanusGraphException (org.janusgraph.core.JanusGraphException)3 RelationTypeIndex (org.janusgraph.core.schema.RelationTypeIndex)3 InternalRelationType (org.janusgraph.graphdb.internal.InternalRelationType)3 IndexTypeWrapper (org.janusgraph.graphdb.types.indextype.IndexTypeWrapper)3 PropertyKey (org.janusgraph.core.PropertyKey)2 RelationType (org.janusgraph.core.RelationType)2 Parameter (org.janusgraph.core.schema.Parameter)2 InternalRelation (org.janusgraph.graphdb.internal.InternalRelation)2 InternalVertex (org.janusgraph.graphdb.internal.InternalVertex)2 SchemaSource (org.janusgraph.graphdb.types.SchemaSource)2 PropertyKeyVertex (org.janusgraph.graphdb.types.vertices.PropertyKeyVertex)2 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 Predicates (com.google.common.base.Predicates)1