use of org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException in project neo4j by neo4j.
the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails.
@Test
void shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails() throws Exception {
// given
RelationTypeSchemaDescriptor descriptor = SchemaDescriptor.forRelType(11, 13);
RelExistenceConstraintDescriptor constraint = existsForSchema(descriptor);
storageReaderWithConstraints(constraint);
int relTypeId = descriptor.getRelTypeId();
int propertyId = descriptor.getPropertyId();
when(tokenHolders.relationshipTypeTokens().getTokenById(relTypeId)).thenReturn(new NamedToken("Label", relTypeId));
when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", relTypeId));
// when
try {
operations.relationshipPropertyExistenceConstraintCreate(descriptor, "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.RELATIONSHIP_TYPE, relTypeId);
order.verify(storageReader).constraintsGetForSchema(descriptor);
order.verify(locks).releaseExclusive(ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
}
use of org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException 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.kernel.api.exceptions.schema.AlreadyConstrainedException in project neo4j by neo4j.
the class Operations method indexBackedConstraintCreate.
@SuppressWarnings("unchecked")
private <T extends IndexBackedConstraintDescriptor> T indexBackedConstraintCreate(T constraint, IndexPrototype prototype) throws KernelException {
try {
if (allStoreHolder.constraintExists(constraint)) {
throw new AlreadyConstrainedException(constraint, CONSTRAINT_CREATION, token);
}
if (prototype.getIndexType() != IndexType.BTREE) {
throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index with index type " + prototype.getIndexType() + ".");
}
if (prototype.schema().isFulltextSchemaDescriptor()) {
throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a full-text schema: " + prototype.schema().userDescription(token));
}
if (prototype.schema().isRelationshipTypeSchemaDescriptor()) {
throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using a relationship type schema: " + prototype.schema().userDescription(token));
}
if (prototype.schema().isAnyTokenSchemaDescriptor()) {
throw new CreateConstraintFailureException(constraint, "Cannot create backing constraint index using an any token schema: " + prototype.schema().userDescription(token));
}
if (!prototype.isUnique()) {
throw new CreateConstraintFailureException(constraint, "Cannot create index backed constraint using an index prototype that is not unique: " + prototype.userDescription(token));
}
IndexDescriptor index = constraintIndexCreator.createUniquenessConstraintIndex(ktx, constraint, prototype);
if (!allStoreHolder.constraintExists(constraint)) {
// This looks weird, but since we release the label lock while awaiting population of the index
// backing this constraint there can be someone else getting ahead of us, creating this exact
// constraint
// before we do, so now getting out here under the lock we must check again and if it exists
// we must at this point consider this an idempotent operation because we verified earlier
// that it didn't exist and went on to create it.
constraint = (T) constraint.withOwnedIndexId(index.getId());
ktx.txState().constraintDoAdd(constraint, index);
} else {
constraint = (T) allStoreHolder.constraintsGetForSchema(constraint.schema());
}
return constraint;
} catch (UniquePropertyValueValidationException | TransactionFailureException | AlreadyConstrainedException e) {
throw new CreateConstraintFailureException(constraint, e);
}
}
use of org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException in project neo4j by neo4j.
the class Operations method assertNoBlockingSchemaRulesExists.
private void assertNoBlockingSchemaRulesExists(IndexPrototype prototype) throws EquivalentSchemaRuleAlreadyExistsException, IndexWithNameAlreadyExistsException, ConstraintWithNameAlreadyExistsException, AlreadyIndexedException, AlreadyConstrainedException {
Optional<String> prototypeName = prototype.getName();
if (prototypeName.isEmpty()) {
throw new IllegalStateException("Expected index to always have a name by this point");
}
String name = prototypeName.get();
// Equivalent index
IndexDescriptor indexWithSameSchema = IndexDescriptor.NO_INDEX;
Iterator<IndexDescriptor> indexesWithSameSchema = allStoreHolder.index(prototype.schema());
while (indexesWithSameSchema.hasNext()) {
indexWithSameSchema = indexesWithSameSchema.next();
if (indexWithSameSchema.getName().equals(name) && indexWithSameSchema.isUnique() == prototype.isUnique()) {
throw new EquivalentSchemaRuleAlreadyExistsException(indexWithSameSchema, INDEX_CREATION, token);
}
}
// Name conflict with other schema rule
assertSchemaRuleWithNameDoesNotExist(name);
// Already constrained
final Iterator<ConstraintDescriptor> constraintWithSameSchema = allStoreHolder.constraintsGetForSchema(prototype.schema());
while (constraintWithSameSchema.hasNext()) {
final ConstraintDescriptor constraint = constraintWithSameSchema.next();
if (constraint.type() != ConstraintType.EXISTS) {
throw new AlreadyConstrainedException(constraint, INDEX_CREATION, token);
}
}
// Already indexed
if (indexWithSameSchema != IndexDescriptor.NO_INDEX) {
throw new AlreadyIndexedException(prototype.schema(), INDEX_CREATION, token);
}
}
Aggregations