use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.
the class TxStateTest method addingUniquenessConstraintShouldBeIdempotent.
@Test
public void addingUniquenessConstraintShouldBeIdempotent() throws Exception {
// given
UniquenessConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForLabel(1, 17);
state.constraintDoAdd(constraint1, 7);
// when
UniquenessConstraintDescriptor constraint2 = ConstraintDescriptorFactory.uniqueForLabel(1, 17);
state.constraintDoAdd(constraint2, 19);
// then
assertEquals(constraint1, constraint2);
assertEquals(singleton(constraint1), state.constraintsChangesForLabel(1).getAdded());
}
use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.
the class IntegrityValidatorTest method shouldValidateUniquenessIndexes.
@Test
public void shouldValidateUniquenessIndexes() throws Exception {
// Given
NeoStores store = mock(NeoStores.class);
IndexingService indexes = mock(IndexingService.class);
IntegrityValidator validator = new IntegrityValidator(store, indexes);
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForLabel(1, 1);
doThrow(new UniquePropertyValueValidationException(constraint, ConstraintValidationException.Phase.VERIFICATION, new RuntimeException())).when(indexes).validateIndex(2L);
ConstraintRule record = ConstraintRule.constraintRule(1L, constraint, 2L);
// When
try {
validator.validateSchemaRule(record);
fail("Should have thrown integrity error.");
} catch (Exception e) {
// good
}
}
use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.
the class ConstraintEnforcingEntityOperations method nodeSetProperty.
@Override
public Property nodeSetProperty(KernelStatement state, long nodeId, DefinedProperty property) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException, ConstraintValidationException {
try (Cursor<NodeItem> cursor = nodeCursorById(state, nodeId)) {
NodeItem node = cursor.get();
Iterator<ConstraintDescriptor> constraints = getConstraintsInvolvingProperty(state, property.propertyKeyId());
Iterator<UniquenessConstraintDescriptor> uniquenessConstraints = new CastingIterator<>(constraints, UniquenessConstraintDescriptor.class);
nodeSchemaMatcher.onMatchingSchema(state, uniquenessConstraints, node, property.propertyKeyId(), constraint -> {
validateNoExistingNodeWithExactValues(state, constraint, getAllPropertyValues(state, constraint.schema(), node, property), node.id());
});
}
return entityWriteOperations.nodeSetProperty(state, nodeId, property);
}
use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.
the class StateHandlingStatementOperations method uniquePropertyConstraintCreate.
@Override
public UniquenessConstraintDescriptor uniquePropertyConstraintCreate(KernelStatement state, LabelSchemaDescriptor descriptor) throws CreateConstraintFailureException {
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
try {
if (state.hasTxStateWithChanges() && // ..., DROP, *CREATE*
state.txState().indexDoUnRemove(constraint.ownedIndexDescriptor())) {
// creation is undoing a drop
if (// CREATE, ..., DROP, *CREATE*
!state.txState().constraintDoUnRemove(constraint)) {
// ... the drop we are undoing did itself undo a prior create...
state.txState().constraintDoAdd(constraint, state.txState().indexCreatedForConstraint(constraint));
}
} else // *CREATE*
{
// create from scratch
Iterator<ConstraintDescriptor> it = storeLayer.constraintsGetForSchema(descriptor);
while (it.hasNext()) {
if (it.next().equals(constraint)) {
return constraint;
}
}
long indexId = constraintIndexCreator.createUniquenessConstraintIndex(state, this, descriptor);
state.txState().constraintDoAdd(constraint, indexId);
}
return constraint;
} catch (UniquePropertyValueValidationException | DropIndexFailureException | TransactionFailureException e) {
throw new CreateConstraintFailureException(constraint, e);
}
}
use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.
the class ConstraintIndexCreator method createUniquenessConstraintIndex.
/**
* You MUST hold a schema write lock before you call this method.
* However the schema write lock is temporarily released while populating the index backing the constraint.
* It goes a little like this:
* <ol>
* <li>Prerequisite: Getting here means that there's an open schema transaction which has acquired the
* SCHEMA WRITE lock.</li>
* <li>Index schema rule which is backing the constraint is created in a nested mini-transaction
* which doesn't acquire any locking, merely adds tx state and commits so that the index rule is applied
* to the store, which triggers the index population</li>
* <li>Release the SCHEMA WRITE lock</li>
* <li>Await index population to complete</li>
* <li>Acquire the SCHEMA WRITE lock (effectively blocking concurrent transactions changing
* data related to this constraint, and it so happens, most other transactions as well) and verify
* the uniqueness of the built index</li>
* <li>Leave this method, knowing that the uniqueness constraint rule will be added to tx state
* and this tx committed, which will create the uniqueness constraint</li>
* </ol>
*/
public long createUniquenessConstraintIndex(KernelStatement state, SchemaReadOperations schema, LabelSchemaDescriptor descriptor) throws TransactionFailureException, CreateConstraintFailureException, DropIndexFailureException, UniquePropertyValueValidationException {
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
NewIndexDescriptor index = createConstraintIndex(descriptor);
boolean success = false;
boolean reacquiredSchemaLock = false;
Client locks = state.locks().pessimistic();
try {
long indexId = schema.indexGetCommittedId(state, index);
// Release the SCHEMA WRITE lock during index population.
// At this point the integrity of the constraint to be created was checked
// while holding the lock and the index rule backing the soon-to-be-created constraint
// has been created. Now it's just the population left, which can take a long time
releaseSchemaLock(locks);
awaitConstrainIndexPopulation(constraint, indexId);
// Index population was successful, but at this point we don't know if the uniqueness constraint holds.
// Acquire SCHEMA WRITE lock and verify the constraints here in this user transaction
// and if everything checks out then it will be held until after the constraint has been
// created and activated.
acquireSchemaLock(state, locks);
reacquiredSchemaLock = true;
indexingService.getIndexProxy(indexId).verifyDeferredConstraints(propertyAccessor);
success = true;
return indexId;
} catch (SchemaRuleNotFoundException | IndexNotFoundKernelException e) {
throw new IllegalStateException(String.format("Index (%s) that we just created does not exist.", descriptor));
} catch (IndexEntryConflictException e) {
throw new UniquePropertyValueValidationException(constraint, VERIFICATION, e);
} catch (InterruptedException | IOException e) {
throw new CreateConstraintFailureException(constraint, e);
} finally {
if (!success) {
if (!reacquiredSchemaLock) {
acquireSchemaLock(state, locks);
}
dropUniquenessConstraintIndex(index);
}
}
}
Aggregations