Search in sources :

Example 1 with NodeKeyConstraintDescriptor

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

the class ConstraintDescriptorTest method shouldCreateNodeKeyConstraintWithOwnedIndex.

@Test
void shouldCreateNodeKeyConstraintWithOwnedIndex() {
    // GIVEN
    NodeKeyConstraintDescriptor descriptor = nodeKeyForLabel(LABEL_ID, PROPERTY_ID_1);
    NodeKeyConstraintDescriptor constraint = descriptor.withId(RULE_ID).withOwnedIndexId(RULE_ID_2);
    // THEN
    assertThat(constraint).isEqualTo(descriptor);
    assertThat(constraint.ownedIndexId()).isEqualTo(RULE_ID_2);
}
Also used : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 2 with NodeKeyConstraintDescriptor

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

the class SchemaRuleSerialization35 method readConstraintRule.

// READ CONSTRAINT
private static ConstraintDescriptor readConstraintRule(long id, ByteBuffer source) throws MalformedSchemaRuleException {
    SchemaDescriptor schema;
    byte constraintRuleType = source.get();
    String name;
    switch(constraintRuleType) {
        case EXISTS_CONSTRAINT:
            schema = readSchema(source);
            name = readRuleName(source).orElse(null);
            return ConstraintDescriptorFactory.existsForSchema(schema).withId(id).withName(name);
        case UNIQUE_CONSTRAINT:
            long ownedUniqueIndex = source.getLong();
            schema = readSchema(source);
            UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema(schema);
            name = readRuleName(source).orElse(null);
            return descriptor.withId(id).withOwnedIndexId(ownedUniqueIndex).withName(name);
        case UNIQUE_EXISTS_CONSTRAINT:
            long ownedNodeKeyIndex = source.getLong();
            schema = readSchema(source);
            NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
            name = readRuleName(source).orElse(null);
            return nodeKeyConstraintDescriptor.withId(id).withOwnedIndexId(ownedNodeKeyIndex).withName(name);
        default:
            throw new MalformedSchemaRuleException(format("Got unknown constraint rule type '%d'.", constraintRuleType));
    }
}
Also used : LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)

Example 3 with NodeKeyConstraintDescriptor

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

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

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfNodeKeyConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfNodeKeyConstraintCreationFails() throws Exception {
    // given
    NodeKeyConstraintDescriptor constraint = nodeKeyForSchema(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.nodeKeyConstraintCreate(IndexPrototype.uniqueForSchema(schema).withName("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 : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 5 with NodeKeyConstraintDescriptor

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

the class RecordStorageReaderTestBase method createNodeKeyConstraint.

protected void createNodeKeyConstraint(Label label, String propertyKey) throws Exception {
    IndexDescriptor index = createUniqueIndex(label, propertyKey);
    TxState txState = new TxState();
    int labelId = getOrCreateLabelId(label);
    int propertyKeyId = getOrCreatePropertyKeyId(propertyKey);
    NodeKeyConstraintDescriptor constraint = ConstraintDescriptorFactory.nodeKeyForLabel(labelId, propertyKeyId);
    constraint = constraint.withName(index.getName()).withOwnedIndexId(index.getId());
    txState.constraintDoAdd(constraint);
    apply(txState);
}
Also used : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) TxState(org.neo4j.kernel.impl.api.state.TxState) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

NodeKeyConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor)9 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)3 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)3 Test (org.junit.jupiter.api.Test)2 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)2 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)2 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)2 MalformedSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)1 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)1 NodeExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.NodeExistenceConstraintDescriptor)1 RelExistenceConstraintDescriptor (org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor)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