Search in sources :

Example 1 with TransactionState

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

the class ReplicatedTokenHolder method createCommands.

private byte[] createCommands(String tokenName) {
    StorageEngine storageEngine = dependencies.resolveDependency(StorageEngine.class);
    Collection<StorageCommand> commands = new ArrayList<>();
    TransactionState txState = new TxState();
    int tokenId = Math.toIntExact(idGeneratorFactory.get(tokenIdType).nextId());
    createToken(txState, tokenName, tokenId);
    try (StorageStatement statement = storageEngine.storeReadLayer().newStatement()) {
        storageEngine.createCommands(commands, txState, statement, ResourceLocker.NONE, Long.MAX_VALUE);
    } catch (CreateConstraintFailureException | TransactionFailureException | ConstraintValidationException e) {
        throw new RuntimeException("Unable to create token '" + tokenName + "'", e);
    }
    return ReplicatedTokenRequestSerializer.commandBytes(commands);
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) StorageStatement(org.neo4j.storageengine.api.StorageStatement) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) ArrayList(java.util.ArrayList) StorageEngine(org.neo4j.storageengine.api.StorageEngine) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) TxState(org.neo4j.kernel.impl.api.state.TxState) CreateConstraintFailureException(org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException)

Example 2 with TransactionState

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

the class StateHandlingStatementOperations method relationshipDelete.

@Override
public void relationshipDelete(final KernelStatement state, long relationshipId) throws EntityNotFoundException, InvalidTransactionTypeKernelException, AutoIndexingKernelException {
    try (Cursor<RelationshipItem> cursor = relationshipCursorById(state, relationshipId)) {
        RelationshipItem relationship = cursor.get();
        // NOTE: We implicitly delegate to neoStoreTransaction via txState.legacyState here. This is because that
        // call returns modified properties, which node manager uses to update legacy tx state. This will be cleaned up
        // once we've removed legacy tx state.
        autoIndexing.relationships().entityRemoved(state.dataWriteOperations(), relationshipId);
        final TransactionState txState = state.txState();
        if (txState.relationshipIsAddedInThisTx(relationship.id())) {
            txState.relationshipDoDeleteAddedInThisTx(relationship.id());
        } else {
            txState.relationshipDoDelete(relationship.id(), relationship.type(), relationship.startNode(), relationship.endNode());
        }
    }
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) RelationshipItem(org.neo4j.storageengine.api.RelationshipItem)

Example 3 with TransactionState

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

the class StateHandlingStatementOperationsTest method shouldGetConstraintsByLabelAndProperty.

@Test
public void shouldGetConstraintsByLabelAndProperty() throws Exception {
    // given
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema(descriptor);
    TransactionState txState = new TxState();
    KernelStatement state = mockedState(txState);
    when(inner.constraintsGetForSchema(constraint.schema())).thenAnswer(invocation -> Iterators.emptyIterator());
    StateHandlingStatementOperations context = newTxStateOps(inner);
    context.uniquePropertyConstraintCreate(state, descriptor);
    // when
    Set<ConstraintDescriptor> result = Iterables.asSet(asIterable(context.constraintsGetForSchema(state, constraint.schema())));
    // then
    assertEquals(asSet(constraint), 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) Test(org.junit.Test)

Example 4 with TransactionState

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

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexBetweenRangeSeekByStringWithIndexQuery.

@Test
public void shouldConsiderTransactionStateDuringIndexBetweenRangeSeekByStringWithIndexQuery() 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.indexUpdatesForRangeSeekByString(index, "Anne", true, "Bill", false)).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.StringRangePredicate rangePredicate = IndexQuery.range(index.schema().getPropertyId(), "Anne", true, "Bill", false);
    when(indexReader.query(rangePredicate)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
    StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
    // When
    PrimitiveLongIterator results = context.indexQuery(statement, index, rangePredicate);
    // 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 5 with TransactionState

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

the class StateHandlingStatementOperationsTest method shouldConsiderTransactionStateDuringIndexRangeSeekByContainsWithIndexQuery.

@Test
public void shouldConsiderTransactionStateDuringIndexRangeSeekByContainsWithIndexQuery() 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.StringContainsPredicate indexQuery = IndexQuery.stringContains(index.schema().getPropertyId(), "contains");
    when(indexReader.query(indexQuery)).thenReturn(PrimitiveLongCollections.resourceIterator(PrimitiveLongCollections.iterator(43L, 44L, 46L), null));
    StateHandlingStatementOperations context = newTxStateOps(storeReadLayer);
    // When
    PrimitiveLongIterator results = context.indexQuery(statement, index, indexQuery);
    // 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)38 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)13 Test (org.junit.Test)12 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)12 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 AddedAndRemoved (org.neo4j.kernel.impl.newapi.TxStateIndexChanges.AddedAndRemoved)5 AddedWithValuesAndRemoved (org.neo4j.kernel.impl.newapi.TxStateIndexChanges.AddedWithValuesAndRemoved)5 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)4 CursorContext (org.neo4j.io.pagecache.context.CursorContext)3 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)3 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)3 KernelException (org.neo4j.exceptions.KernelException)2 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)2 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)2 AccessMode (org.neo4j.internal.kernel.api.security.AccessMode)2 AdminAccessMode (org.neo4j.internal.kernel.api.security.AdminAccessMode)2 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)2