Search in sources :

Example 1 with NodeExistenceConstraintDescriptor

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

the class BuiltInProceduresTest method givenNodePropExistenceConstraint.

private void givenNodePropExistenceConstraint(String label, String propKey) {
    int labelId = token(label, labels);
    int propId = token(propKey, propKeys);
    final NodeExistenceConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForLabel(labelId, propId).withName("MyExistenceConstraint");
    constraints.add(constraint);
}
Also used : NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)

Example 2 with NodeExistenceConstraintDescriptor

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

the class RecordStorageReaderTestBase method createNodePropertyExistenceConstraint.

protected void createNodePropertyExistenceConstraint(Label label, String propertyKey) throws Exception {
    TxState txState = new TxState();
    NodeExistenceConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForLabel(getOrCreateLabelId(label), 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) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)

Example 3 with NodeExistenceConstraintDescriptor

use of org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor 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 4 with NodeExistenceConstraintDescriptor

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

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfNodePropertyExistenceConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfNodePropertyExistenceConstraintCreationFails() throws Exception {
    // given
    NodeExistenceConstraintDescriptor constraint = existsForSchema(schema);
    storageReaderWithConstraints(constraint);
    int labelId = schema.getLabelId();
    int propertyId = schema.getPropertyId();
    when(tokenHolders.labelTokens().getTokenById(labelId)).thenReturn(new NamedToken("Label", labelId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", labelId));
    // when
    try {
        operations.nodePropertyExistenceConstraintCreate(schema, "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.LABEL, labelId);
    order.verify(storageReader).constraintsGetForSchema(schema);
    order.verify(locks).releaseExclusive(ResourceTypes.LABEL, labelId);
}
Also used : AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 5 with NodeExistenceConstraintDescriptor

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

the class SchemaCheckerTest method shouldPopulateMandatoryPropertiesMap.

@Test
void shouldPopulateMandatoryPropertiesMap() throws Exception {
    // given
    try (AutoCloseable ignored = tx()) {
        var cursorContext = CursorContext.NULL;
        NodeExistenceConstraintDescriptor constraint1 = ConstraintDescriptorFactory.existsForLabel(label1, propertyKey1).withId(schemaStore.nextId(cursorContext)).withName(NAME);
        NodeExistenceConstraintDescriptor constraint2 = ConstraintDescriptorFactory.existsForLabel(label2, propertyKey1, propertyKey2).withId(schemaStore.nextId(cursorContext)).withName(NAME2);
        RelExistenceConstraintDescriptor constraint3 = ConstraintDescriptorFactory.existsForRelType(relationshipType1, propertyKey2).withId(schemaStore.nextId(cursorContext)).withName(NAME);
        RelExistenceConstraintDescriptor constraint4 = ConstraintDescriptorFactory.existsForRelType(relationshipType2, propertyKey1, propertyKey2).withId(schemaStore.nextId(cursorContext)).withName(NAME2);
        schemaStorage.writeSchemaRule(constraint1, cursorContext, INSTANCE);
        schemaStorage.writeSchemaRule(constraint2, cursorContext, INSTANCE);
        schemaStorage.writeSchemaRule(constraint3, cursorContext, INSTANCE);
        schemaStorage.writeSchemaRule(constraint4, cursorContext, INSTANCE);
    }
    // when
    check();
    // then
    assertEquals(IntSets.mutable.of(propertyKey1), mandatoryNodeProperties.remove(label1));
    assertEquals(IntSets.mutable.of(propertyKey1, propertyKey2), mandatoryNodeProperties.remove(label2));
    assertTrue(mandatoryNodeProperties.isEmpty());
    assertEquals(IntSets.mutable.of(propertyKey2), mandatoryRelationshipProperties.remove(relationshipType1));
    assertEquals(IntSets.mutable.of(propertyKey1, propertyKey2), mandatoryRelationshipProperties.remove(relationshipType2));
    assertTrue(mandatoryRelationshipProperties.isEmpty());
}
Also used : RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

NodeExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)5 Test (org.junit.jupiter.api.Test)2 RelExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor)2 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)1 NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)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