use of org.neo4j.internal.schema.ConstraintDescriptor 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);
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class TransactionToRecordStateVisitor method visitAddedConstraint.
@Override
public void visitAddedConstraint(ConstraintDescriptor constraint) throws KernelException {
clearSchemaState = true;
long constraintId = schemaStorage.newRuleId(cursorContext);
switch(constraint.type()) {
case UNIQUE:
visitAddedUniquenessConstraint(constraint.asUniquenessConstraint(), constraintId);
break;
case UNIQUE_EXISTS:
visitAddedNodeKeyConstraint(constraint.asNodeKeyConstraint(), constraintId);
break;
case EXISTS:
ConstraintDescriptor rule = constraintSemantics.createExistenceConstraint(constraintId, constraint);
schemaStateChanger.createSchemaRule(recordState, rule);
break;
default:
throw new IllegalStateException(constraint.type().toString());
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaReadWriteTestBase method shouldSeeUniqueConstraintFromTransaction.
@Test
void shouldSeeUniqueConstraintFromTransaction() throws Exception {
ConstraintDescriptor existing;
try (KernelTransaction transaction = beginTransaction()) {
existing = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop1)).withName("existing constraint"));
transaction.commit();
}
try (KernelTransaction transaction = beginTransaction()) {
SchemaReadCore before = transaction.schemaRead().snapshot();
ConstraintDescriptor newConstraint = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop2)).withName("new constraint"));
SchemaRead schemaRead = transaction.schemaRead();
SchemaReadCore after = schemaRead.snapshot();
assertTrue(schemaRead.constraintExists(existing));
assertTrue(schemaRead.constraintExists(newConstraint));
assertThat(asList(schemaRead.constraintsGetForLabel(label))).contains(existing, newConstraint);
assertThat(asList(before.constraintsGetForLabel(label))).contains(existing, newConstraint);
assertThat(asList(after.constraintsGetForLabel(label))).contains(existing, newConstraint);
assertThat(before.constraintGetForName("existing constraint")).isEqualTo(existing);
assertThat(after.constraintGetForName("existing constraint")).isEqualTo(existing);
assertThat(before.constraintGetForName("new constraint")).isEqualTo(newConstraint);
assertThat(after.constraintGetForName("new constraint")).isEqualTo(newConstraint);
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaReadWriteTestBase method shouldNotSeeDroppedUniqueConstraintFromTransaction.
@Test
void shouldNotSeeDroppedUniqueConstraintFromTransaction() throws Exception {
ConstraintDescriptor existing;
try (KernelTransaction transaction = beginTransaction()) {
existing = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop1)));
transaction.commit();
}
try (KernelTransaction transaction = beginTransaction()) {
SchemaReadCore before = transaction.schemaRead().snapshot();
transaction.schemaWrite().constraintDrop(existing);
SchemaRead schemaRead = transaction.schemaRead();
assertFalse(schemaRead.constraintExists(existing));
assertThat(asList(schemaRead.constraintsGetForLabel(label))).isEmpty();
assertThat(asList(schemaRead.snapshot().constraintsGetForLabel(label))).isEmpty();
assertThat(asList(before.constraintsGetForLabel(label))).isEmpty();
}
}
use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.
the class SchemaReadWriteTestBase method setUp.
@BeforeEach
void setUp() throws Exception {
try (KernelTransaction transaction = beginTransaction()) {
SchemaRead schemaRead = transaction.schemaRead();
SchemaWrite schemaWrite = transaction.schemaWrite();
Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetAll();
while (constraints.hasNext()) {
schemaWrite.constraintDrop(constraints.next());
}
Iterator<IndexDescriptor> indexes = schemaRead.indexesGetAll();
while (indexes.hasNext()) {
schemaWrite.indexDrop(indexes.next());
}
TokenWrite tokenWrite = transaction.tokenWrite();
label = tokenWrite.labelGetOrCreateForName("label");
label2 = tokenWrite.labelGetOrCreateForName("label2");
type = tokenWrite.relationshipTypeGetOrCreateForName("relationship");
prop1 = tokenWrite.propertyKeyGetOrCreateForName("prop1");
prop2 = tokenWrite.propertyKeyGetOrCreateForName("prop2");
prop3 = tokenWrite.propertyKeyGetOrCreateForName("prop3");
transaction.commit();
}
}
Aggregations