Search in sources :

Example 6 with UniquenessConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldNotAddConstraintAlreadyExistsInTheStore.

@Test
public void shouldNotAddConstraintAlreadyExistsInTheStore() throws Exception {
    // given
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
    TransactionState txState = mock(TransactionState.class);
    when(txState.nodesWithLabelChanged(anyInt())).thenReturn(new DiffSets<Long>());
    when(txState.hasChanges()).thenReturn(true);
    KernelStatement state = mockedState(txState);
    when(inner.constraintsGetForSchema(any())).thenReturn(iterator(constraint));
    StateHandlingStatementOperations context = newTxStateOps(inner);
    // when
    context.uniquePropertyConstraintCreate(state, descriptor);
    // then
    verify(txState).indexDoUnRemove(eq(constraint.ownedIndexDescriptor()));
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) Matchers.anyLong(org.mockito.Matchers.anyLong) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) Test(org.junit.Test)

Example 7 with UniquenessConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldGetConstraintsByLabel.

@Test
public void shouldGetConstraintsByLabel() throws Exception {
    // given
    UniquenessConstraintDescriptor constraint1 = ConstraintDescriptorFactory.uniqueForLabel(2, 3);
    UniquenessConstraintDescriptor constraint2 = ConstraintDescriptorFactory.uniqueForLabel(4, 5);
    TransactionState txState = new TxState();
    KernelStatement state = mockedState(txState);
    when(inner.constraintsGetForSchema(constraint1.schema())).thenAnswer(invocation -> Iterators.emptyIterator());
    when(inner.constraintsGetForSchema(constraint2.schema())).thenAnswer(invocation -> Iterators.emptyIterator());
    when(inner.constraintsGetForLabel(1)).thenAnswer(invocation -> Iterators.emptyIterator());
    when(inner.constraintsGetForLabel(2)).thenAnswer(invocation -> iterator(constraint1));
    StateHandlingStatementOperations context = newTxStateOps(inner);
    context.uniquePropertyConstraintCreate(state, constraint1.ownedIndexDescriptor().schema());
    context.uniquePropertyConstraintCreate(state, constraint2.ownedIndexDescriptor().schema());
    // when
    Set<ConstraintDescriptor> result = Iterables.asSet(asIterable(context.constraintsGetForLabel(state, 2)));
    // then
    assertEquals(asSet(constraint1), result);
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) Test(org.junit.Test)

Example 8 with UniquenessConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class DataIntegrityValidatingStatementOperations method uniquePropertyConstraintCreate.

@Override
public UniquenessConstraintDescriptor uniquePropertyConstraintCreate(KernelStatement state, LabelSchemaDescriptor descriptor) throws AlreadyConstrainedException, CreateConstraintFailureException, AlreadyIndexedException, RepeatedPropertyInCompositeSchemaException {
    assertValidDescriptor(descriptor, OperationContext.CONSTRAINT_CREATION);
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
    assertConstraintDoesNotExist(state, constraint);
    // It is not allowed to create uniqueness constraints on indexed label/property pairs
    assertIndexDoesNotExist(state, OperationContext.CONSTRAINT_CREATION, SchemaBoundary.map(descriptor));
    return schemaWriteDelegate.uniquePropertyConstraintCreate(state, descriptor);
}
Also used : UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor)

Example 9 with UniquenessConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class ConstraintEnforcingEntityOperations method nodeAddLabel.

@Override
public boolean nodeAddLabel(KernelStatement state, long nodeId, int labelId) throws EntityNotFoundException, ConstraintValidationException {
    try (Cursor<NodeItem> cursor = nodeCursorById(state, nodeId)) {
        NodeItem node = cursor.get();
        Iterator<ConstraintDescriptor> constraints = schemaReadOperations.constraintsGetForLabel(state, labelId);
        while (constraints.hasNext()) {
            ConstraintDescriptor constraint = constraints.next();
            if (constraint.type() == UNIQUE) {
                UniquenessConstraintDescriptor uniqueConstraint = (UniquenessConstraintDescriptor) constraint;
                ExactPredicate[] propertyValues = getAllPropertyValues(state, uniqueConstraint.schema(), node);
                if (propertyValues != null) {
                    validateNoExistingNodeWithExactValues(state, uniqueConstraint, propertyValues, node.id());
                }
            }
        }
    }
    return entityWriteOperations.nodeAddLabel(state, nodeId, labelId);
}
Also used : NodeItem(org.neo4j.storageengine.api.NodeItem) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) NodeExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor) RelExistenceConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) ExactPredicate(org.neo4j.kernel.api.schema_new.IndexQuery.ExactPredicate)

Example 10 with UniquenessConstraintDescriptor

use of org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor in project neo4j by neo4j.

the class TxStateTest method shouldAddUniquenessConstraint.

@Test
public void shouldAddUniquenessConstraint() throws Exception {
    // when
    UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForLabel(1, 17);
    state.constraintDoAdd(constraint, 7);
    // then
    ReadableDiffSets<ConstraintDescriptor> diff = state.constraintsChangesForLabel(1);
    assertEquals(singleton(constraint), diff.getAdded());
    assertTrue(diff.getRemoved().isEmpty());
}
Also used : ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) UniquenessConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor) Test(org.junit.Test)

Aggregations

UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)20 Test (org.junit.Test)12 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)7 NodeExistenceConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor)4 RelExistenceConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor)4 UniquePropertyValueValidationException (org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException)3 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)3 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)3 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)3 ReadOperations (org.neo4j.kernel.api.ReadOperations)2 SchemaWriteOperations (org.neo4j.kernel.api.SchemaWriteOperations)2 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)2 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)2 NodeItem (org.neo4j.storageengine.api.NodeItem)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1 ResourceIterator (org.neo4j.graphdb.ResourceIterator)1 CastingIterator (org.neo4j.helpers.collection.CastingIterator)1 Statement (org.neo4j.kernel.api.Statement)1