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