Search in sources :

Example 31 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaStore method buildConstraintRule.

private static SchemaRule buildConstraintRule(long id, Map<String, Value> props) throws MalformedSchemaRuleException {
    SchemaDescriptor schema = buildSchemaDescriptor(props);
    String constraintRuleType = getString(PROP_CONSTRAINT_RULE_TYPE, props);
    String name = getString(PROP_SCHEMA_RULE_NAME, props);
    OptionalLong ownedIndex = getOptionalLong(PROP_OWNED_INDEX, props);
    ConstraintDescriptor constraint;
    switch(constraintRuleType) {
        case "UNIQUE":
            constraint = ConstraintDescriptorFactory.uniqueForSchema(schema);
            if (ownedIndex.isPresent()) {
                constraint = constraint.withOwnedIndexId(ownedIndex.getAsLong());
            }
            return constraint.withId(id).withName(name);
        case "EXISTS":
            constraint = ConstraintDescriptorFactory.existsForSchema(schema);
            return constraint.withId(id).withName(name);
        case "UNIQUE_EXISTS":
            constraint = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
            if (ownedIndex.isPresent()) {
                constraint = constraint.withOwnedIndexId(ownedIndex.getAsLong());
            }
            return constraint.withId(id).withName(name);
        default:
            throw new MalformedSchemaRuleException("Did not recognize constraint rule type: " + constraintRuleType);
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) OptionalLong(java.util.OptionalLong)

Example 32 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor 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 33 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaCacheTest method shouldRemoveNodeConstraints.

@Test
void shouldRemoveNodeConstraints() {
    // given
    SchemaCache cache = new SchemaCache(new ConstraintSemantics(), indexConfigCompleter);
    ConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForLabel(1, 5, 6).withId(1);
    ConstraintDescriptor constraint2 = ConstraintDescriptorFactory.uniqueForLabel(1, 5).withId(2);
    ConstraintDescriptor constraint3 = ConstraintDescriptorFactory.uniqueForLabel(2, 5).withId(3);
    cache.addSchemaRule(constraint1);
    cache.addSchemaRule(constraint2);
    cache.addSchemaRule(constraint3);
    assertEquals(asSet(constraint2), cache.getUniquenessConstraintsRelatedTo(entityTokens(1), entityTokens(), properties(5), true, NODE));
    // and when
    cache.removeSchemaRule(constraint1.getId());
    cache.removeSchemaRule(constraint2.getId());
    cache.removeSchemaRule(constraint3.getId());
    // then
    assertTrue(cache.getUniquenessConstraintsRelatedTo(entityTokens(1), entityTokens(), properties(5), true, NODE).isEmpty());
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 34 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaCacheTest method hasConstraintRuleShouldMatchBySchemaAndType.

@Test
void hasConstraintRuleShouldMatchBySchemaAndType() {
    ConstraintDescriptor existing = uniquenessConstraint(1, 2, 3, 4);
    // Different rule id, but same type and schema.
    ConstraintDescriptor checked = uniquenessConstraint(0, 2, 3, 4);
    SchemaCache cache = newSchemaCache(existing);
    assertTrue(cache.hasConstraintRule(checked));
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

Example 35 with ConstraintDescriptor

use of org.neo4j.internal.schema.ConstraintDescriptor in project neo4j by neo4j.

the class SchemaCacheTest method shouldListConstraintsForSchema.

@Test
void shouldListConstraintsForSchema() {
    // Given
    ConstraintDescriptor rule1 = uniquenessConstraint(0, 1, 1, 0);
    ConstraintDescriptor rule2 = uniquenessConstraint(1, 2, 1, 0);
    ConstraintDescriptor rule3 = nodePropertyExistenceConstraint(2, 1, 2);
    SchemaCache cache = newSchemaCache();
    cache.addSchemaRule(rule1);
    cache.addSchemaRule(rule2);
    cache.addSchemaRule(rule3);
    // When
    Set<ConstraintDescriptor> listed = asSet(cache.constraintsForSchema(rule3.schema()));
    // Then
    assertEquals(singleton(rule3), listed);
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Test(org.junit.jupiter.api.Test)

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