Search in sources :

Example 1 with SchemaRuleNotFoundException

use of org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException in project neo4j by neo4j.

the class TransactionToRecordStateVisitor method visitRemovedConstraint.

@Override
public void visitRemovedConstraint(ConstraintDescriptor constraint) {
    clearSchemaState = true;
    try {
        ConstraintDescriptor rule = schemaStorage.constraintsGetSingle(constraint, cursorContext);
        schemaStateChanger.dropSchemaRule(recordState, rule);
        if (constraint.enforcesUniqueness()) {
            // Remove the index for the constraint as well
            IndexDescriptor[] indexes = schemaStorage.indexGetForSchema(constraint.schema(), cursorContext);
            for (IndexDescriptor index : indexes) {
                OptionalLong owningConstraintId = index.getOwningConstraintId();
                if (owningConstraintId.isPresent() && owningConstraintId.getAsLong() == rule.getId()) {
                    visitRemovedIndex(index);
                }
            // Note that we _could_ also go through all the matching indexes that have isUnique == true and no owning constraint id, and remove those
            // as well. These might be orphaned indexes from failed constraint creations. However, since we want to allow multiple indexes and
            // constraints on the same schema, they could also be constraint indexes that are currently populating for other constraints, and if that's
            // the case, then we cannot remove them, since that would ruin the constraint they are being built for.
            }
        }
    } catch (SchemaRuleNotFoundException e) {
        throw new IllegalStateException("Constraint to be removed should exist, since its existence should have been validated earlier " + "and the schema should have been locked.", e);
    } catch (DuplicateSchemaRuleException e) {
        throw new IllegalStateException("Multiple constraints found for specified label and property.", e);
    }
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) OptionalLong(java.util.OptionalLong) SchemaRuleNotFoundException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DuplicateSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.DuplicateSchemaRuleException)

Example 2 with SchemaRuleNotFoundException

use of org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException in project neo4j by neo4j.

the class SchemaImpl method getIndexReference.

private static IndexDescriptor getIndexReference(SchemaRead schemaRead, TokenRead tokenRead, IndexDefinitionImpl index) throws SchemaRuleException {
    // Use the precise embedded index reference when available.
    IndexDescriptor reference = index.getIndexReference();
    if (reference != null) {
        return reference;
    }
    // Otherwise attempt to reverse engineer the schema that will let us look up the real IndexReference.
    int[] propertyKeyIds = resolveAndValidatePropertyKeys(tokenRead, index.getPropertyKeysArrayShared());
    SchemaDescriptor schema;
    if (index.isNodeIndex()) {
        int[] labelIds = resolveAndValidateTokens("Label", index.getLabelArrayShared(), Label::name, tokenRead::nodeLabel);
        if (index.isMultiTokenIndex()) {
            schema = fulltext(EntityType.NODE, labelIds, propertyKeyIds);
        } else if (index.getIndexType() == IndexType.LOOKUP) {
            schema = forAnyEntityTokens(EntityType.NODE);
        } else {
            schema = forLabel(labelIds[0], propertyKeyIds);
        }
    } else if (index.isRelationshipIndex()) {
        int[] relTypes = resolveAndValidateTokens("Relationship type", index.getRelationshipTypesArrayShared(), RelationshipType::name, tokenRead::relationshipType);
        if (index.isMultiTokenIndex()) {
            schema = fulltext(EntityType.RELATIONSHIP, relTypes, propertyKeyIds);
        } else if (index.getIndexType() == IndexType.LOOKUP) {
            schema = forAnyEntityTokens(EntityType.RELATIONSHIP);
        } else {
            schema = forRelType(relTypes[0], propertyKeyIds);
        }
    } else {
        throw new IllegalArgumentException("The given index is neither a node index, nor a relationship index: " + index + ".");
    }
    Iterator<IndexDescriptor> iterator = schemaRead.index(schema);
    if (!iterator.hasNext()) {
        throw new SchemaRuleNotFoundException(schema, tokenRead);
    }
    reference = iterator.next();
    if (iterator.hasNext()) {
        throw new DuplicateSchemaRuleException(schema, tokenRead);
    }
    return reference;
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) Label(org.neo4j.graphdb.Label) SchemaDescriptor.forLabel(org.neo4j.internal.schema.SchemaDescriptor.forLabel) SchemaRuleNotFoundException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DuplicateSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.DuplicateSchemaRuleException)

Aggregations

DuplicateSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.DuplicateSchemaRuleException)2 SchemaRuleNotFoundException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException)2 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2 OptionalLong (java.util.OptionalLong)1 Label (org.neo4j.graphdb.Label)1 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)1 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)1 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)1 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)1 SchemaDescriptor.forLabel (org.neo4j.internal.schema.SchemaDescriptor.forLabel)1 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)1 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)1