Search in sources :

Example 1 with AlreadyIndexedException

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

the class Operations method assertNoBlockingSchemaRulesExists.

private void assertNoBlockingSchemaRulesExists(ConstraintDescriptor constraint) throws EquivalentSchemaRuleAlreadyExistsException, IndexWithNameAlreadyExistsException, ConstraintWithNameAlreadyExistsException, AlreadyConstrainedException, AlreadyIndexedException {
    final String name = constraint.getName();
    if (name == null) {
        throw new IllegalStateException("Expected constraint to always have a name by this point");
    }
    // Equivalent constraint
    final List<ConstraintDescriptor> constraintsWithSameSchema = Iterators.asList(allStoreHolder.constraintsGetForSchema(constraint.schema()));
    for (ConstraintDescriptor constraintWithSameSchema : constraintsWithSameSchema) {
        if (constraint.equals(constraintWithSameSchema) && constraint.getName().equals(constraintWithSameSchema.getName())) {
            throw new EquivalentSchemaRuleAlreadyExistsException(constraintWithSameSchema, CONSTRAINT_CREATION, token);
        }
    }
    // Name conflict with other schema rule
    assertSchemaRuleWithNameDoesNotExist(name);
    // Already constrained
    for (ConstraintDescriptor constraintWithSameSchema : constraintsWithSameSchema) {
        final boolean creatingExistenceConstraint = constraint.type() == ConstraintType.EXISTS;
        final boolean existingIsExistenceConstraint = constraintWithSameSchema.type() == ConstraintType.EXISTS;
        if (creatingExistenceConstraint == existingIsExistenceConstraint) {
            throw new AlreadyConstrainedException(constraintWithSameSchema, CONSTRAINT_CREATION, token);
        }
    }
    // Already indexed
    if (constraint.type() != ConstraintType.EXISTS) {
        Iterator<IndexDescriptor> existingIndexes = allStoreHolder.index(constraint.schema());
        if (existingIndexes.hasNext()) {
            IndexDescriptor existingIndex = existingIndexes.next();
            throw new AlreadyIndexedException(existingIndex.schema(), CONSTRAINT_CREATION, token);
        }
    }
    // is being populated we will end up with two indexes on the same schema.
    if (constraint.isIndexBackedConstraint() && ktx.hasTxStateWithChanges()) {
        for (ConstraintDescriptor droppedConstraint : ktx.txState().constraintsChanges().getRemoved()) {
            // If dropped and new constraint have similar backing index we cannot allow this constraint creation
            if (droppedConstraint.isIndexBackedConstraint() && constraint.schema().equals(droppedConstraint.schema())) {
                throw new UnsupportedOperationException(format("Trying to create constraint '%s' in same transaction as dropping '%s'. " + "This is not supported because they are both backed by similar indexes. " + "Please drop constraint in a separate transaction before creating the new one.", constraint.getName(), droppedConstraint.getName()));
            }
        }
    }
}
Also used : AlreadyIndexedException(org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException) EquivalentSchemaRuleAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 2 with AlreadyIndexedException

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

the class Operations method assertNoBlockingSchemaRulesExists.

private void assertNoBlockingSchemaRulesExists(IndexPrototype prototype) throws EquivalentSchemaRuleAlreadyExistsException, IndexWithNameAlreadyExistsException, ConstraintWithNameAlreadyExistsException, AlreadyIndexedException, AlreadyConstrainedException {
    Optional<String> prototypeName = prototype.getName();
    if (prototypeName.isEmpty()) {
        throw new IllegalStateException("Expected index to always have a name by this point");
    }
    String name = prototypeName.get();
    // Equivalent index
    IndexDescriptor indexWithSameSchema = IndexDescriptor.NO_INDEX;
    Iterator<IndexDescriptor> indexesWithSameSchema = allStoreHolder.index(prototype.schema());
    while (indexesWithSameSchema.hasNext()) {
        indexWithSameSchema = indexesWithSameSchema.next();
        if (indexWithSameSchema.getName().equals(name) && indexWithSameSchema.isUnique() == prototype.isUnique()) {
            throw new EquivalentSchemaRuleAlreadyExistsException(indexWithSameSchema, INDEX_CREATION, token);
        }
    }
    // Name conflict with other schema rule
    assertSchemaRuleWithNameDoesNotExist(name);
    // Already constrained
    final Iterator<ConstraintDescriptor> constraintWithSameSchema = allStoreHolder.constraintsGetForSchema(prototype.schema());
    while (constraintWithSameSchema.hasNext()) {
        final ConstraintDescriptor constraint = constraintWithSameSchema.next();
        if (constraint.type() != ConstraintType.EXISTS) {
            throw new AlreadyConstrainedException(constraint, INDEX_CREATION, token);
        }
    }
    // Already indexed
    if (indexWithSameSchema != IndexDescriptor.NO_INDEX) {
        throw new AlreadyIndexedException(prototype.schema(), INDEX_CREATION, token);
    }
}
Also used : AlreadyIndexedException(org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException) EquivalentSchemaRuleAlreadyExistsException(org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)2 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)2 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)2 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)2 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)2 AlreadyIndexedException (org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException)2 EquivalentSchemaRuleAlreadyExistsException (org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException)2