Search in sources :

Example 6 with AlreadyConstrainedException

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

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails() throws Exception {
    // given
    RelationTypeSchemaDescriptor descriptor = SchemaDescriptor.forRelType(11, 13);
    RelExistenceConstraintDescriptor constraint = existsForSchema(descriptor);
    storageReaderWithConstraints(constraint);
    int relTypeId = descriptor.getRelTypeId();
    int propertyId = descriptor.getPropertyId();
    when(tokenHolders.relationshipTypeTokens().getTokenById(relTypeId)).thenReturn(new NamedToken("Label", relTypeId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", relTypeId));
    // when
    try {
        operations.relationshipPropertyExistenceConstraintCreate(descriptor, "constraint name");
        fail("Expected an exception because this schema should already be constrained.");
    } catch (AlreadyConstrainedException ignore) {
    // Good.
    }
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
    order.verify(storageReader).constraintsGetForSchema(descriptor);
    order.verify(locks).releaseExclusive(ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
}
Also used : RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 7 with AlreadyConstrainedException

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

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfNodeKeyConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfNodeKeyConstraintCreationFails() throws Exception {
    // given
    NodeKeyConstraintDescriptor constraint = nodeKeyForSchema(schema);
    storageReaderWithConstraints(constraint);
    int labelId = schema.getLabelId();
    int propertyId = schema.getPropertyId();
    when(tokenHolders.labelTokens().getTokenById(labelId)).thenReturn(new NamedToken("Label", labelId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", labelId));
    // when
    try {
        operations.nodeKeyConstraintCreate(IndexPrototype.uniqueForSchema(schema).withName("constraint name"));
        fail("Expected an exception because this schema should already be constrained.");
    } catch (AlreadyConstrainedException ignore) {
    // Good.
    }
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.LABEL, labelId);
    order.verify(storageReader).constraintsGetForSchema(schema);
    order.verify(locks).releaseExclusive(ResourceTypes.LABEL, labelId);
}
Also used : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 8 with AlreadyConstrainedException

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

the class Operations method indexBackedConstraintCreate.

@SuppressWarnings("unchecked")
private <T extends IndexBackedConstraintDescriptor> T indexBackedConstraintCreate(T constraint, IndexPrototype prototype) throws KernelException {
    try {
        if (allStoreHolder.constraintExists(constraint)) {
            throw new AlreadyConstrainedException(constraint, CONSTRAINT_CREATION, token);
        }
        if (prototype.getIndexType() != IndexType.BTREE) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index with index type " + prototype.getIndexType() + ".");
        }
        if (prototype.schema().isFulltextSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a full-text schema: " + prototype.schema().userDescription(token));
        }
        if (prototype.schema().isRelationshipTypeSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a relationship type schema: " + prototype.schema().userDescription(token));
        }
        if (prototype.schema().isAnyTokenSchemaDescriptor()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using an any token schema: " + prototype.schema().userDescription(token));
        }
        if (!prototype.isUnique()) {
            throw new CreateConstraintFailureException(constraint, "Cannot create index backed constraint using an index prototype that is not unique: " + prototype.userDescription(token));
        }
        IndexDescriptor index = constraintIndexCreator.createUniquenessConstraintIndex(ktx, constraint, prototype);
        if (!allStoreHolder.constraintExists(constraint)) {
            // This looks weird, but since we release the label lock while awaiting population of the index
            // backing this constraint there can be someone else getting ahead of us, creating this exact
            // constraint
            // before we do, so now getting out here under the lock we must check again and if it exists
            // we must at this point consider this an idempotent operation because we verified earlier
            // that it didn't exist and went on to create it.
            constraint = (T) constraint.withOwnedIndexId(index.getId());
            ktx.txState().constraintDoAdd(constraint, index);
        } else {
            constraint = (T) allStoreHolder.constraintsGetForSchema(constraint.schema());
        }
        return constraint;
    } catch (UniquePropertyValueValidationException | TransactionFailureException | AlreadyConstrainedException e) {
        throw new CreateConstraintFailureException(constraint, e);
    }
}
Also used : UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) CreateConstraintFailureException(org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 9 with AlreadyConstrainedException

use of org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException 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

AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)9 Test (org.junit.jupiter.api.Test)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)4 NamedToken (org.neo4j.token.api.NamedToken)4 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)3 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)3 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)2 CreateConstraintFailureException (org.neo4j.internal.kernel.api.exceptions.schema.CreateConstraintFailureException)2 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)2 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)2 AlreadyIndexedException (org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException)2 EquivalentSchemaRuleAlreadyExistsException (org.neo4j.kernel.api.exceptions.schema.EquivalentSchemaRuleAlreadyExistsException)2 UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)2 IOException (java.io.IOException)1 Test (org.junit.Test)1 KernelException (org.neo4j.exceptions.KernelException)1 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)1 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)1 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)1 NodeExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)1