Search in sources :

Example 6 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState 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 TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState 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 TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState in project neo4j by neo4j.

the class ConstraintIndexCreatorTest method shouldDropIndexIfPopulationFails.

@Test
public void shouldDropIndexIfPopulationFails() throws Exception {
    // given
    StatementOperationParts constraintCreationContext = mockedParts();
    KernelStatement state = mockedState();
    IndexingService indexingService = mock(IndexingService.class);
    StubKernel kernel = new StubKernel();
    when(constraintCreationContext.schemaReadOperations().indexGetCommittedId(state, index)).thenReturn(2468L);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexingService.getIndexProxy(2468L)).thenReturn(indexProxy);
    IndexEntryConflictException cause = new IndexEntryConflictException(2, 1, "a");
    doThrow(new IndexPopulationFailedKernelException(SchemaBoundary.map(descriptor), "some index", cause)).when(indexProxy).awaitStoreScanCompleted();
    PropertyAccessor propertyAccessor = mock(PropertyAccessor.class);
    ConstraintIndexCreator creator = new ConstraintIndexCreator(() -> kernel, indexingService, propertyAccessor, false);
    // when
    try {
        creator.createUniquenessConstraintIndex(state, constraintCreationContext.schemaReadOperations(), descriptor);
        fail("expected exception");
    }// then
     catch (UniquePropertyValueValidationException e) {
        assertEquals("Existing data does not satisfy CONSTRAINT ON ( label[123]:label[123] ) ASSERT label[123].property[456] IS UNIQUE.", e.getMessage());
    }
    assertEquals(2, kernel.statements.size());
    TransactionState tx1 = kernel.statements.get(0).txState();
    NewIndexDescriptor newIndex = NewIndexDescriptorFactory.uniqueForLabel(123, 456);
    verify(tx1).indexRuleDoAdd(newIndex);
    verifyNoMoreInteractions(tx1);
    verify(constraintCreationContext.schemaReadOperations()).indexGetCommittedId(state, index);
    verifyNoMoreInteractions(constraintCreationContext.schemaReadOperations());
    TransactionState tx2 = kernel.statements.get(1).txState();
    verify(tx2).indexDoDrop(newIndex);
    verifyNoMoreInteractions(tx2);
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) PropertyAccessor(org.neo4j.kernel.api.index.PropertyAccessor) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) StatementOperationParts(org.neo4j.kernel.impl.api.StatementOperationParts) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) IndexPopulationFailedKernelException(org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Test(org.junit.Test)

Example 9 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexScanWithIndexQuery.

@Test
public void shouldConsiderTransactionStateDuringIndexScanWithIndexQuery() throws Exception {
    // Given
    TransactionState txState = mock(TransactionState.class);
    KernelStatement statement = mock(KernelStatement.class);
    when(statement.hasTxStateWithChanges()).thenReturn(true);
    when(statement.txState()).thenReturn(txState);
    when(txState.indexUpdatesForScan(index)).thenReturn(new DiffSets<>(Collections.singleton(42L), Collections.singleton(44L)));
    when(txState.addedAndRemovedNodes()).thenReturn(new DiffSets<>(Collections.singleton(45L), Collections.singleton(46L)));
    StoreReadLayer storeReadLayer = mock(StoreReadLayer.class);
    IndexReader indexReader = addMockedIndexReader(statement);
    IndexQuery query = IndexQuery.exists(index.schema().getPropertyId());
    when(indexReader.query(query)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
    StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
    // When
    PrimitiveLongIterator results = context.indexQuery(statement, index, query);
    // Then
    assertEquals(asSet(42L, 43L), PrimitiveLongCollections.toSet(results));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) Test(org.junit.Test)

Example 10 with TransactionState

use of org.neo4j.kernel.api.txstate.TransactionState in project neo4j by neo4j.

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexRangeSeekByPrefix.

@Test
public void shouldConsiderTransactionStateDuringIndexRangeSeekByPrefix() throws Exception {
    // Given
    TransactionState txState = mock(TransactionState.class);
    KernelStatement statement = mock(KernelStatement.class);
    when(statement.hasTxStateWithChanges()).thenReturn(true);
    when(statement.txState()).thenReturn(txState);
    when(txState.indexUpdatesForRangeSeekByPrefix(index, "prefix")).thenReturn(new DiffSets<>(Collections.singleton(42L), Collections.singleton(44L)));
    when(txState.addedAndRemovedNodes()).thenReturn(new DiffSets<>(Collections.singleton(45L), Collections.singleton(46L)));
    StoreReadLayer storeReadLayer = mock(StoreReadLayer.class);
    IndexReader indexReader = addMockedIndexReader(statement);
    IndexQuery.StringPrefixPredicate query = IndexQuery.stringPrefix(index.schema().getPropertyId(), "prefix");
    when(indexReader.query(query)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
    StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
    // When
    PrimitiveLongIterator results = context.indexQuery(statement, index, query);
    // Then
    assertEquals(asSet(42L, 43L), PrimitiveLongCollections.toSet(results));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) StateHandlingStatementOperations(org.neo4j.kernel.impl.api.StateHandlingStatementOperations) Test(org.junit.Test)

Aggregations

TransactionState (org.neo4j.kernel.api.txstate.TransactionState)16 Test (org.junit.Test)13 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)13 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)13 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)9 IndexReader (org.neo4j.storageengine.api.schema.IndexReader)9 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)8 IndexQuery (org.neo4j.kernel.api.schema_new.IndexQuery)8 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)4 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)3 StorageStatement (org.neo4j.storageengine.api.StorageStatement)2 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1 Cursor (org.neo4j.cursor.Cursor)1 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)1 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)1 IndexPopulationFailedKernelException (org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException)1 ConstraintValidationException (org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException)1 CreateConstraintFailureException (org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)1