Search in sources :

Example 1 with UniquenessConstraintDescriptor

use of org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class Operations method uniquePropertyConstraintCreate.

@Override
public ConstraintDescriptor uniquePropertyConstraintCreate(IndexPrototype prototype) throws KernelException {
    SchemaDescriptor schema = prototype.schema();
    exclusiveSchemaLock(schema);
    ktx.assertOpen();
    prototype = ensureIndexPrototypeHasIndexProvider(prototype);
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
    try {
        assertValidDescriptor(schema, SchemaKernelException.OperationContext.CONSTRAINT_CREATION);
        if (prototype.getName().isEmpty()) {
            constraint = ensureConstraintHasName(constraint);
            prototype = prototype.withName(constraint.getName());
        } else {
            constraint = constraint.withName(prototype.getName().get());
        }
        exclusiveSchemaNameLock(constraint.getName());
        assertNoBlockingSchemaRulesExists(constraint);
    } catch (KernelException e) {
        // Try not to hold on to exclusive schema locks when we don't strictly need them.
        exclusiveSchemaUnlock(schema);
        throw e;
    }
    // Create constraints
    constraint = indexBackedConstraintCreate(constraint, prototype);
    return constraint;
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException) UnspecifiedKernelException(org.neo4j.exceptions.UnspecifiedKernelException) IndexBrokenKernelException(org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException) PropertyKeyIdNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)

Example 2 with UniquenessConstraintDescriptor

use of org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class TxStateTest method shouldAddUniquenessConstraint.

@Test
void shouldAddUniquenessConstraint() {
    // when
    LabelSchemaDescriptor schema = forLabel(1, 17);
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
    state.constraintDoAdd(constraint, IndexPrototype.uniqueForSchema(schema).withName("constraint_7").materialise(7));
    // then
    DiffSets<ConstraintDescriptor> diff = state.constraintsChangesForLabel(1);
    assertEquals(singleton(constraint), diff.getAdded());
    assertTrue(diff.getRemoved().isEmpty());
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 3 with UniquenessConstraintDescriptor

use of org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class TxStateTest method addingUniquenessConstraintShouldBeIdempotent.

@Test
void addingUniquenessConstraintShouldBeIdempotent() {
    // given
    LabelSchemaDescriptor schema = forLabel(1, 17);
    UniquenessConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForSchema(schema);
    state.constraintDoAdd(constraint1, IndexPrototype.uniqueForSchema(schema).withName("constraint_7").materialise(7));
    // when
    UniquenessConstraintDescriptor constraint2 = ConstraintDescriptorFactory.uniqueForSchema(schema);
    state.constraintDoAdd(constraint2, IndexPrototype.uniqueForSchema(schema).withName("constraint_19").materialise(19));
    // then
    assertEquals(constraint1, constraint2);
    assertEquals(singleton(constraint1), state.constraintsChangesForLabel(1).getAdded());
}
Also used : LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 4 with UniquenessConstraintDescriptor

use of org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class TxStateTest method dataRevisionMustNotChangeOnSchemaChanges.

@Test
void dataRevisionMustNotChangeOnSchemaChanges() {
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.indexDoAdd(indexOn_1_1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.indexDoDrop(indexOn_1_1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.indexDoUnRemove(indexOn_1_1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    UniquenessConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForLabel(1, 17);
    state.constraintDoAdd(constraint1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.constraintDoDrop(constraint1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.constraintDoUnRemove(constraint1);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    IndexBackedConstraintDescriptor constraint2 = ConstraintDescriptorFactory.nodeKeyForLabel(0, 0);
    state.constraintDoAdd(constraint2, IndexPrototype.uniqueForSchema(forLabel(0, 0)).withName("index").materialise(0));
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.labelDoCreateForName("Label", false, 0);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.relationshipTypeDoCreateForName("REL", false, 0);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    state.propertyKeyDoCreateForName("prop", false, 0);
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
    // This is not strictly a schema-change, but it is a "non-data" change in that these will not transform into store updates.
    // Or schema updates for that matter. We only do these to speed up the transaction state filtering of schema index query results.
    state.indexDoUpdateEntry(indexOn_1_1.schema(), 0, ValueTuple.of(Values.booleanValue(true)), ValueTuple.of(Values.booleanValue(false)));
    assertThat(state.getDataRevision()).isEqualTo(0L);
    assertFalse(state.hasDataChanges());
}
Also used : IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 5 with UniquenessConstraintDescriptor

use of org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class BuiltInProceduresTest method givenUniqueConstraint.

private void givenUniqueConstraint(String label, String propKey) {
    int labelId = token(label, labels);
    int propId = token(propKey, propKeys);
    LabelSchemaDescriptor schema = forLabel(labelId, propId);
    int id = uniqueIndexes.size() + 1000;
    final String name = "constraint_" + id;
    IndexDescriptor index = IndexPrototype.uniqueForSchema(schema, EMPTY.getProviderDescriptor()).withName(name).materialise(id);
    uniqueIndexes.add(index);
    final UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForLabel(labelId, propId).withName(name);
    constraints.add(constraint);
}
Also used : LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)26 Test (org.junit.jupiter.api.Test)17 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)10 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)7 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)6 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)6 RepeatedTest (org.junit.jupiter.api.RepeatedTest)4 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)4 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)2 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)2 NodeExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)2 RelExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor)2 KernelException (org.neo4j.exceptions.KernelException)1 UnspecifiedKernelException (org.neo4j.exceptions.UnspecifiedKernelException)1 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)1 IndexNotApplicableKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException)1 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)1 MalformedSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)1 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)1