Search in sources :

Example 1 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaStorageIT method shouldReturnCorrectUniquenessRuleForLabelAndProperty.

@Test
void shouldReturnCorrectUniquenessRuleForLabelAndProperty() throws SchemaRuleNotFoundException, DuplicateSchemaRuleException {
    // Given
    createSchema(uniquenessConstraint(LABEL1, PROP1), uniquenessConstraint(LABEL2, PROP1));
    // When
    ConstraintDescriptor rule = storage.constraintsGetSingle(ConstraintDescriptorFactory.uniqueForLabel(labelId(LABEL1), propId(PROP1)), NULL);
    // Then
    assertNotNull(rule);
    assertRule(rule, LABEL1, PROP1, ConstraintType.UNIQUE);
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 2 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor 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 3 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class Operations method constraintDrop.

@Override
public void constraintDrop(String name) throws SchemaKernelException {
    exclusiveSchemaNameLock(name);
    ConstraintDescriptor constraint = allStoreHolder.constraintGetForName(name);
    if (constraint == null) {
        throw new DropConstraintFailureException(name, new NoSuchConstraintException(name));
    }
    constraintDrop(constraint);
}
Also used : NoSuchConstraintException(org.neo4j.kernel.api.exceptions.schema.NoSuchConstraintException) 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) DropConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException)

Example 4 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class Operations method nodePropertyExistenceConstraintCreate.

@Override
public ConstraintDescriptor nodePropertyExistenceConstraintCreate(LabelSchemaDescriptor schema, String name) throws KernelException {
    ConstraintDescriptor constraint = lockAndValidatePropertyExistenceConstraint(schema, name);
    // enforce constraints
    enforceNodePropertyExistenceConstraint(schema);
    // create constraint
    ktx.txState().constraintDoAdd(constraint);
    return constraint;
}
Also used : 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)

Example 5 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class Operations method lockAndValidatePropertyExistenceConstraint.

private ConstraintDescriptor lockAndValidatePropertyExistenceConstraint(SchemaDescriptor descriptor, String name) throws KernelException {
    // Lock constraint schema.
    exclusiveSchemaLock(descriptor);
    ktx.assertOpen();
    try {
        // Verify data integrity.
        assertValidDescriptor(descriptor, SchemaKernelException.OperationContext.CONSTRAINT_CREATION);
        ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForSchema(descriptor).withName(name);
        constraint = ensureConstraintHasName(constraint);
        exclusiveSchemaNameLock(constraint.getName());
        assertNoBlockingSchemaRulesExists(constraint);
        return constraint;
    } catch (SchemaKernelException e) {
        exclusiveSchemaUnlock(descriptor);
        throw e;
    }
}
Also used : 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) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)

Aggregations

ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)107 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)34 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)32 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)21 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)20 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)19 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)16 TokenRead (org.neo4j.internal.kernel.api.TokenRead)9 ArrayList (java.util.ArrayList)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)6 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)6 SchemaStore (org.neo4j.kernel.impl.store.SchemaStore)6 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)5 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)5 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)5 SchemaRule (org.neo4j.internal.schema.SchemaRule)5 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)5 OptionalLong (java.util.OptionalLong)4