@Test public void shouldGetConstraintsByLabel() throws Exception { // given UniquenessConstraint constraint1 = new UniquenessConstraint(11, 66); UniquenessConstraint constraint2 = new UniquenessConstraint(11, 99); TxState txState = new TxStateImpl( mock(OldTxStateBridge.class), mock(PersistenceManager.class), mock(IdGeneration.class)); KernelStatement state = mockedState(txState); when(inner.constraintsGetForLabelAndPropertyKey(10, 66)) .thenAnswer(asAnswer(Collections.emptyList())); when(inner.constraintsGetForLabelAndPropertyKey(11, 99)) .thenAnswer(asAnswer(Collections.emptyList())); when(inner.constraintsGetForLabel(10)).thenAnswer(asAnswer(Collections.emptyList())); when(inner.constraintsGetForLabel(11)).thenAnswer(asAnswer(asIterable(constraint1))); StateHandlingStatementOperations context = newTxStateOps(inner); context.uniquenessConstraintCreate(state, 10, 66); context.uniquenessConstraintCreate(state, 11, 99); // when Set<UniquenessConstraint> result = asSet(asIterable(context.constraintsGetForLabel(state, 11))); // then assertEquals(asSet(constraint1, constraint2), result); }
@Override public Iterator<UniquenessConstraint> constraintsGetForLabelAndPropertyKey( KernelStatement state, int labelId, int propertyKeyId) { return applyConstraintsDiff( state, storeLayer.constraintsGetForLabelAndPropertyKey(labelId, propertyKeyId), labelId, propertyKeyId); }
@Test public void shouldNotAddConstraintAlreadyExistsInTheStore() throws Exception { // given UniquenessConstraint constraint = new UniquenessConstraint(10, 66); TxState txState = mock(TxState.class); when(txState.nodesWithLabelChanged(anyInt())).thenReturn(DiffSets.<Long>emptyDiffSets()); KernelStatement state = mockedState(txState); when(inner.constraintsGetForLabelAndPropertyKey(10, 66)) .thenAnswer(asAnswer(asList(constraint))); StateHandlingStatementOperations context = newTxStateOps(inner); // when context.uniquenessConstraintCreate(state, 10, 66); // then verify(txState).constraintIndexDoUnRemove(any(IndexDescriptor.class)); }
@Override public UniquenessConstraint uniquenessConstraintCreate( KernelStatement state, int labelId, int propertyKeyId) throws CreateConstraintFailureException { UniquenessConstraint constraint = new UniquenessConstraint(labelId, propertyKeyId); try { IndexDescriptor index = new IndexDescriptor(labelId, propertyKeyId); if (state.txState().constraintIndexDoUnRemove(index)) // ..., DROP, *CREATE* { // creation is undoing a drop state.txState().constraintIndexDiffSetsByLabel(labelId).unRemove(index); if (!state.txState().constraintDoUnRemove(constraint)) // CREATE, ..., DROP, *CREATE* { // ... the drop we are undoing did itself undo a prior create... state.txState().constraintsChangesForLabel(labelId).unRemove(constraint); state .txState() .constraintDoAdd(constraint, state.txState().indexCreatedForConstraint(constraint)); } } else // *CREATE* { // create from scratch for (Iterator<UniquenessConstraint> it = storeLayer.constraintsGetForLabelAndPropertyKey(labelId, propertyKeyId); it.hasNext(); ) { if (it.next().equals(labelId, propertyKeyId)) { return constraint; } } long indexId = constraintIndexCreator.createUniquenessConstraintIndex( state, this, labelId, propertyKeyId); state.txState().constraintDoAdd(constraint, indexId); } return constraint; } catch (ConstraintVerificationFailedKernelException | DropIndexFailureException | TransactionFailureException e) { throw new CreateConstraintFailureException(constraint, e); } }