Search in sources :

Example 16 with KernelTransactionImplementation

use of org.neo4j.kernel.impl.api.KernelTransactionImplementation in project neo4j by neo4j.

the class ConstraintIndexCreatorTest method createTransaction.

private KernelTransactionImplementation createTransaction() {
    KernelTransactionImplementation transaction = mock(KernelTransactionImplementation.class);
    try {
        StorageEngine storageEngine = mock(StorageEngine.class);
        StorageReader storageReader = mock(StorageReader.class);
        when(storageEngine.newReader()).thenReturn(storageReader);
        Locks.Client locks = mock(Locks.Client.class);
        when(transaction.lockClient()).thenReturn(locks);
        when(transaction.tokenRead()).thenReturn(tokenRead);
        when(transaction.schemaRead()).thenReturn(schemaRead);
        when(transaction.schemaWrite()).thenReturn(schemaWrite);
        TransactionState transactionState = mock(TransactionState.class);
        when(transaction.txState()).thenReturn(transactionState);
        when(transaction.indexUniqueCreate(any(IndexPrototype.class))).thenAnswer(i -> i.<IndexPrototype>getArgument(0).materialise(INDEX_ID));
        when(transaction.newStorageReader()).thenReturn(mock(StorageReader.class));
    } catch (InvalidTransactionTypeKernelException e) {
        fail("Expected write transaction");
    }
    return transaction;
}
Also used : StorageReader(org.neo4j.storageengine.api.StorageReader) TransactionState(org.neo4j.kernel.api.txstate.TransactionState) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Locks(org.neo4j.kernel.impl.locking.Locks) StorageEngine(org.neo4j.storageengine.api.StorageEngine)

Example 17 with KernelTransactionImplementation

use of org.neo4j.kernel.impl.api.KernelTransactionImplementation in project neo4j by neo4j.

the class ConstraintIndexCreatorTest method logMessagesAboutConstraintCreation.

@Test
void logMessagesAboutConstraintCreation() throws SchemaKernelException, UniquePropertyValueValidationException, TransactionFailureException, IndexNotFoundKernelException {
    IndexProxy indexProxy = mock(IndexProxy.class);
    IndexingService indexingService = mock(IndexingService.class);
    when(indexingService.getIndexProxy(index)).thenReturn(indexProxy);
    when(indexProxy.getDescriptor()).thenReturn(index);
    when(schemaRead.indexGetForName(constraint.getName())).thenReturn(IndexDescriptor.NO_INDEX);
    ConstraintIndexCreator creator = new ConstraintIndexCreator(() -> kernel, indexingService, logProvider);
    KernelTransactionImplementation transaction = createTransaction();
    creator.createUniquenessConstraintIndex(transaction, constraint, prototype);
    String constraintString = constraint.userDescription(tokenRead);
    assertThat(logProvider).containsMessages(format("Starting constraint creation: %s.", constraintString), format("Constraint %s populated, starting verification.", constraintString), format("Constraint %s verified.", constraintString));
}
Also used : ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) Test(org.junit.jupiter.api.Test)

Example 18 with KernelTransactionImplementation

use of org.neo4j.kernel.impl.api.KernelTransactionImplementation in project neo4j by neo4j.

the class ConstraintIndexCreatorTest method shouldReleaseLabelLockWhileAwaitingIndexPopulation.

@Test
void shouldReleaseLabelLockWhileAwaitingIndexPopulation() throws Exception {
    // given
    IndexingService indexingService = mock(IndexingService.class);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexingService.getIndexProxy(index)).thenReturn(indexProxy);
    when(schemaRead.index(schema)).thenReturn(Iterators.emptyResourceIterator());
    when(schemaRead.indexGetForName(constraint.getName())).thenReturn(IndexDescriptor.NO_INDEX);
    ConstraintIndexCreator creator = new ConstraintIndexCreator(() -> kernel, indexingService, logProvider);
    // when
    KernelTransactionImplementation transaction = createTransaction();
    creator.createUniquenessConstraintIndex(transaction, constraint, prototype);
    // then
    verify(transaction.lockClient()).releaseExclusive(ResourceTypes.LABEL, schema.getLabelId());
    verify(transaction.lockClient()).acquireExclusive(transaction.lockTracer(), ResourceTypes.LABEL, schema.getLabelId());
}
Also used : ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) Test(org.junit.jupiter.api.Test)

Example 19 with KernelTransactionImplementation

use of org.neo4j.kernel.impl.api.KernelTransactionImplementation in project neo4j by neo4j.

the class ConstraintIndexCreatorTest method shouldDropIndexIfPopulationFails.

@Test
void shouldDropIndexIfPopulationFails() throws Exception {
    // given
    IndexingService indexingService = mock(IndexingService.class);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexingService.getIndexProxy(index)).thenReturn(indexProxy);
    when(indexProxy.getDescriptor()).thenReturn(index);
    when(schemaRead.indexGetForName(constraint.getName())).thenReturn(IndexDescriptor.NO_INDEX, index);
    IndexEntryConflictException cause = new IndexEntryConflictException(2, 1, Values.of("a"));
    doThrow(new IndexPopulationFailedKernelException("some index", cause)).when(indexProxy).awaitStoreScanCompleted(anyLong(), any());
    when(schemaRead.index(any(SchemaDescriptor.class))).thenReturn(// first claim it doesn't exist, because it doesn't... so
    Iterators.emptyResourceIterator()).thenReturn(// then after it failed claim it does exist
    Iterators.iterator(index));
    ConstraintIndexCreator creator = new ConstraintIndexCreator(() -> kernel, indexingService, logProvider);
    // when
    KernelTransactionImplementation transaction = createTransaction();
    UniquePropertyValueValidationException exception = assertThrows(UniquePropertyValueValidationException.class, () -> creator.createUniquenessConstraintIndex(transaction, constraint, prototype));
    assertEquals("Existing data does not satisfy Constraint( name='constraint', type='UNIQUENESS', schema=(:Label {prop}) ): " + "Both node 2 and node 1 share the property value ( String(\"a\") )", exception.getMessage());
    assertEquals(2, kernel.transactions.size());
    KernelTransactionImplementation tx1 = kernel.transactions.get(0);
    verify(tx1).indexUniqueCreate(prototype);
    verify(schemaRead, times(2)).indexGetForName(constraint.getName());
    verifyNoMoreInteractions(schemaRead);
    KernelTransactionImplementation kti2 = kernel.transactions.get(1);
    verify(kti2).addIndexDoDropToTxState(index);
}
Also used : ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) IndexPopulationFailedKernelException(org.neo4j.kernel.api.exceptions.index.IndexPopulationFailedKernelException) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Test(org.junit.jupiter.api.Test)

Example 20 with KernelTransactionImplementation

use of org.neo4j.kernel.impl.api.KernelTransactionImplementation in project neo4j by neo4j.

the class ConstraintIndexCreatorTest method shouldThrowOnExistingOrphanedConstraintIndexWithSameName.

@Test
void shouldThrowOnExistingOrphanedConstraintIndexWithSameName() throws Exception {
    // given
    IndexingService indexingService = mock(IndexingService.class);
    long orphanedConstraintIndexId = 111;
    String orphanedName = "constraint";
    IndexDescriptor orphanedIndex = IndexPrototype.uniqueForSchema(schema).withName(orphanedName).materialise(orphanedConstraintIndexId);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexingService.getIndexProxy(orphanedIndex)).thenReturn(indexProxy);
    when(schemaRead.index(schema)).thenReturn(Iterators.iterator(orphanedIndex));
    when(schemaRead.indexGetForName(orphanedName)).thenReturn(orphanedIndex);
    when(schemaRead.indexGetOwningUniquenessConstraintId(orphanedIndex)).thenReturn(// which means it has no owner
    null);
    ConstraintIndexCreator creator = new ConstraintIndexCreator(() -> kernel, indexingService, logProvider);
    // when
    KernelTransactionImplementation transaction = createTransaction();
    assertThrows(AlreadyConstrainedException.class, () -> creator.createUniquenessConstraintIndex(transaction, constraint, prototype));
    // then
    assertEquals(0, kernel.transactions.size(), "There should have been no need to acquire a statement to create the constraint index");
    verify(schemaRead).indexGetForName(constraint.getName());
    verifyNoMoreInteractions(schemaRead);
}
Also used : ConstraintIndexCreator(org.neo4j.kernel.impl.api.state.ConstraintIndexCreator) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)35 Test (org.junit.jupiter.api.Test)20 ConstraintIndexCreator (org.neo4j.kernel.impl.api.state.ConstraintIndexCreator)13 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)12 IndexingService (org.neo4j.kernel.impl.api.index.IndexingService)8 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)7 StorageReader (org.neo4j.storageengine.api.StorageReader)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)6 DbmsRuntimeRepository (org.neo4j.dbms.database.DbmsRuntimeRepository)5 Transaction (org.neo4j.graphdb.Transaction)5 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)5 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)5 CommandCreationContext (org.neo4j.storageengine.api.CommandCreationContext)5 InOrder (org.mockito.InOrder)4 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)4 Test (org.junit.Test)3 SecurityAuthorizationHandler (org.neo4j.internal.kernel.api.security.SecurityAuthorizationHandler)3 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)3 IndexingProvidersService (org.neo4j.kernel.impl.api.index.IndexingProvidersService)3