Search in sources :

Example 46 with ConstraintDescriptor

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);
    }
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) OptionalLong(java.util.OptionalLong) SchemaRuleNotFoundException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaRuleNotFoundException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DuplicateSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.DuplicateSchemaRuleException)

Example 47 with ConstraintDescriptor

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());
    }
}
Also used : UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)

Example 48 with ConstraintDescriptor

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);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Test(org.junit.jupiter.api.Test)

Example 49 with ConstraintDescriptor

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();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Test(org.junit.jupiter.api.Test)

Example 50 with ConstraintDescriptor

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();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

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