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);
}
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()));
}
}
}
}
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);
}
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;
}
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;
}
}
Aggregations