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);
}
}
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);
}
}
}
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());
}
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));
}
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);
}
Aggregations