Search in sources :

Example 1 with RelExistenceConstraintDescriptor

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

the class GraphDbStructureGuide method showUniqueConstraints.

private static void showUniqueConstraints(DbStructureVisitor visitor, KernelTransaction ktx, TokenNameLookup nameLookup) {
    Iterator<ConstraintDescriptor> constraints = ktx.schemaRead().constraintsGetAll();
    while (constraints.hasNext()) {
        ConstraintDescriptor constraint = constraints.next();
        String userDescription = constraint.userDescription(nameLookup);
        if (constraint.isUniquenessConstraint()) {
            visitor.visitUniqueConstraint(constraint.asUniquenessConstraint(), userDescription);
        } else if (constraint.isNodePropertyExistenceConstraint()) {
            NodeExistenceConstraintDescriptor existenceConstraint = constraint.asNodePropertyExistenceConstraint();
            visitor.visitNodePropertyExistenceConstraint(existenceConstraint, userDescription);
        } else if (constraint.isRelationshipPropertyExistenceConstraint()) {
            RelExistenceConstraintDescriptor existenceConstraint = constraint.asRelationshipPropertyExistenceConstraint();
            visitor.visitRelationshipPropertyExistenceConstraint(existenceConstraint, userDescription);
        } else if (constraint.isNodeKeyConstraint()) {
            NodeKeyConstraintDescriptor nodeKeyConstraint = constraint.asNodeKeyConstraint();
            visitor.visitNodeKeyConstraint(nodeKeyConstraint, userDescription);
        } else {
            throw new IllegalArgumentException("Unknown constraint type: " + constraint.getClass() + ", " + "constraint: " + constraint);
        }
    }
}
Also used : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)

Example 2 with RelExistenceConstraintDescriptor

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

the class PlainOperationsTest method shouldNotAcquireSchemaReadLockLazilyBeforeGettingAllConstraintsFromSnapshot.

@Test
void shouldNotAcquireSchemaReadLockLazilyBeforeGettingAllConstraintsFromSnapshot() {
    // given
    int labelId = 1;
    int relTypeId = 2;
    UniquenessConstraintDescriptor uniquenessConstraint = uniqueForLabel(labelId, 2, 3, 3);
    RelExistenceConstraintDescriptor existenceConstraint = existsForRelType(relTypeId, 3, 4, 5);
    when(storageReaderSnapshot.constraintsGetAll()).thenReturn(Iterators.iterator(uniquenessConstraint, existenceConstraint));
    // when
    Iterator<ConstraintDescriptor> result = allStoreHolder.snapshot().constraintsGetAll();
    // then
    assertThat(Iterators.count(result)).isEqualTo(2L);
    assertThat(asList(result)).isEmpty();
    verify(storageReaderSnapshot).constraintsGetAll();
    verifyNoMoreInteractions(locks);
}
Also used : RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 3 with RelExistenceConstraintDescriptor

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

the class PlainOperationsTest method shouldAcquireSchemaReadLockLazilyBeforeGettingAllConstraints.

@Test
void shouldAcquireSchemaReadLockLazilyBeforeGettingAllConstraints() {
    // given
    int labelId = 1;
    int relTypeId = 2;
    UniquenessConstraintDescriptor uniquenessConstraint = uniqueForLabel(labelId, 2, 3, 3);
    RelExistenceConstraintDescriptor existenceConstraint = existsForRelType(relTypeId, 3, 4, 5);
    when(storageReader.constraintsGetAll()).thenReturn(Iterators.iterator(uniquenessConstraint, existenceConstraint));
    when(storageReader.constraintExists(uniquenessConstraint)).thenReturn(true);
    when(storageReader.constraintExists(existenceConstraint)).thenReturn(true);
    // when
    Iterator<ConstraintDescriptor> result = allStoreHolder.constraintsGetAll();
    // then
    assertThat(Iterators.count(result)).isEqualTo(2L);
    assertThat(asList(result)).isEmpty();
    order.verify(storageReader).constraintsGetAll();
    order.verify(locks, atLeastOnce()).acquireShared(LockTracer.NONE, ResourceTypes.LABEL, labelId);
    order.verify(locks, atLeastOnce()).acquireShared(LockTracer.NONE, ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
}
Also used : RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 4 with RelExistenceConstraintDescriptor

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

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails() throws Exception {
    // given
    RelationTypeSchemaDescriptor descriptor = SchemaDescriptor.forRelType(11, 13);
    RelExistenceConstraintDescriptor constraint = existsForSchema(descriptor);
    storageReaderWithConstraints(constraint);
    int relTypeId = descriptor.getRelTypeId();
    int propertyId = descriptor.getPropertyId();
    when(tokenHolders.relationshipTypeTokens().getTokenById(relTypeId)).thenReturn(new NamedToken("Label", relTypeId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", relTypeId));
    // when
    try {
        operations.relationshipPropertyExistenceConstraintCreate(descriptor, "constraint name");
        fail("Expected an exception because this schema should already be constrained.");
    } catch (AlreadyConstrainedException ignore) {
    // Good.
    }
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
    order.verify(storageReader).constraintsGetForSchema(descriptor);
    order.verify(locks).releaseExclusive(ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
}
Also used : RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 5 with RelExistenceConstraintDescriptor

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

the class RecordStorageReaderTestBase method createRelPropertyExistenceConstraint.

protected void createRelPropertyExistenceConstraint(RelationshipType relationshipType, String propertyKey) throws Exception {
    TxState txState = new TxState();
    RelExistenceConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForRelType(getOrCreateRelationshipTypeId(relationshipType), getOrCreatePropertyKeyId(propertyKey));
    long id = commitContext.reserveSchema();
    txState.constraintDoAdd(constraint.withId(id).withName("constraint_" + id));
    apply(txState);
}
Also used : TxState(org.neo4j.kernel.impl.api.state.TxState) RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor)

Aggregations

RelExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor)6 Test (org.junit.jupiter.api.Test)4 NodeExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)3 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)3 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)2 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)2 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)1 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)1 TxState (org.neo4j.kernel.impl.api.state.TxState)1 NamedToken (org.neo4j.token.api.NamedToken)1