Search in sources :

Example 1 with TransactionCountingStateVisitor

use of org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor in project neo4j by neo4j.

the class RecordStorageEngine method createCommands.

/**
 * @throws TransactionFailureException if command generation fails or some prerequisite of some command didn't validate,
 * for example if trying to delete a node that still has relationships.
 * @throws CreateConstraintFailureException if this transaction was set to create a constraint and that failed.
 * @throws ConstraintValidationException if this transaction was set to create a constraint and some data violates that constraint.
 */
@SuppressWarnings("resource")
@Override
public void createCommands(Collection<StorageCommand> commands, ReadableTransactionState txState, StorageReader storageReader, CommandCreationContext commandCreationContext, ResourceLocker locks, LockTracer lockTracer, long lastTransactionIdWhenStarted, TxStateVisitor.Decorator additionalTxStateVisitor, CursorContext cursorContext, MemoryTracker transactionMemoryTracker) throws KernelException {
    if (txState != null) {
        KernelVersion version = neoStores.getMetaDataStore().kernelVersion();
        Preconditions.checkState(version.isAtLeast(KernelVersion.V4_2), "Can not write older version than %s. Requested %s", KernelVersion.V4_2, version);
        // We can make this cast here because we expected that the storageReader passed in here comes from
        // this storage engine itself, anything else is considered a bug. And we do know the inner workings
        // of the storage statements that we create.
        RecordStorageCommandCreationContext creationContext = (RecordStorageCommandCreationContext) commandCreationContext;
        LogCommandSerialization serialization = RecordStorageCommandReaderFactory.INSTANCE.get(version);
        TransactionRecordState recordState = creationContext.createTransactionRecordState(integrityValidator, lastTransactionIdWhenStarted, locks, lockTracer, serialization, lockVerificationFactory.create(locks, txState, neoStores, schemaRuleAccess));
        // Visit transaction state and populate these record state objects
        TxStateVisitor txStateVisitor = new TransactionToRecordStateVisitor(recordState, schemaState, schemaRuleAccess, constraintSemantics, cursorContext);
        CountsRecordState countsRecordState = new CountsRecordState(serialization);
        txStateVisitor = additionalTxStateVisitor.apply(txStateVisitor);
        txStateVisitor = new TransactionCountingStateVisitor(txStateVisitor, storageReader, txState, countsRecordState, cursorContext);
        try (TxStateVisitor visitor = txStateVisitor) {
            txState.accept(visitor);
        }
        // Convert record state into commands
        recordState.extractCommands(commands, transactionMemoryTracker);
        countsRecordState.extractCommands(commands, transactionMemoryTracker);
        // Verify sufficient locks
        CommandLockVerification commandLockVerification = commandLockVerificationFactory.create(locks, txState, neoStores, schemaRuleAccess);
        commandLockVerification.verifySufficientlyLocked(commands);
    }
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) TxStateVisitor(org.neo4j.storageengine.api.txstate.TxStateVisitor) TransactionCountingStateVisitor(org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor)

Example 2 with TransactionCountingStateVisitor

use of org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor in project neo4j by neo4j.

the class AllStoreHolder method countsForNodeInTxState.

private long countsForNodeInTxState(int labelId) {
    long count = 0;
    if (ktx.hasTxStateWithChanges()) {
        CountsDelta counts = new CountsDelta();
        try {
            TransactionState txState = ktx.txState();
            CursorContext cursorContext = ktx.cursorContext();
            try (var countingVisitor = new TransactionCountingStateVisitor(EMPTY, storageReader, txState, counts, cursorContext)) {
                txState.accept(countingVisitor);
            }
            if (counts.hasChanges()) {
                count += counts.nodeCount(labelId, cursorContext);
            }
        } catch (KernelException e) {
            throw new IllegalArgumentException("Unexpected error: " + e.getMessage());
        }
    }
    return count;
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) CursorContext(org.neo4j.io.pagecache.context.CursorContext) CountsDelta(org.neo4j.storageengine.api.CountsDelta) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException) TransactionCountingStateVisitor(org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor)

Example 3 with TransactionCountingStateVisitor

use of org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor in project neo4j by neo4j.

the class AllStoreHolder method countsForRelationshipInTxState.

private long countsForRelationshipInTxState(int startLabelId, int typeId, int endLabelId) {
    long count = 0;
    if (ktx.hasTxStateWithChanges()) {
        CursorContext cursorContext = ktx.cursorContext();
        CountsDelta counts = new CountsDelta();
        try {
            TransactionState txState = ktx.txState();
            try (var countingVisitor = new TransactionCountingStateVisitor(EMPTY, storageReader, txState, counts, cursorContext)) {
                txState.accept(countingVisitor);
            }
            if (counts.hasChanges()) {
                count += counts.relationshipCount(startLabelId, typeId, endLabelId, cursorContext);
            }
        } catch (KernelException e) {
            throw new IllegalArgumentException("Unexpected error: " + e.getMessage());
        }
    }
    return count;
}
Also used : TransactionState(org.neo4j.kernel.api.txstate.TransactionState) CursorContext(org.neo4j.io.pagecache.context.CursorContext) CountsDelta(org.neo4j.storageengine.api.CountsDelta) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException) TransactionCountingStateVisitor(org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor)

Example 4 with TransactionCountingStateVisitor

use of org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor in project neo4j by neo4j.

the class TransactionCountingStateVisitorTraceIT method traceStateWithChanges.

private void traceStateWithChanges(Consumer<Transaction> transactionalOperation) throws KernelException {
    try (var transaction = database.beginTx()) {
        var internalTransaction = (InternalTransaction) transaction;
        KernelTransactionImplementation kernelTransaction = (KernelTransactionImplementation) internalTransaction.kernelTransaction();
        var cursorContext = kernelTransaction.cursorContext();
        transactionalOperation.accept(transaction);
        cursorContext.getCursorTracer().reportEvents();
        assertZeroCursor(cursorContext);
        var transactionState = kernelTransaction.txState();
        var counts = new CountsDelta();
        try (StorageReader storageReader = kernelTransaction.newStorageReader();
            var stateVisitor = new TransactionCountingStateVisitor(EMPTY, storageReader, transactionState, counts, cursorContext)) {
            transactionState.accept(stateVisitor);
        }
        assertTwoCursor(cursorContext);
    }
}
Also used : StorageReader(org.neo4j.storageengine.api.StorageReader) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) CountsDelta(org.neo4j.storageengine.api.CountsDelta) TransactionCountingStateVisitor(org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor)

Aggregations

TransactionCountingStateVisitor (org.neo4j.storageengine.api.txstate.TransactionCountingStateVisitor)4 CountsDelta (org.neo4j.storageengine.api.CountsDelta)3 KernelException (org.neo4j.exceptions.KernelException)2 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)2 CursorContext (org.neo4j.io.pagecache.context.CursorContext)2 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)2 KernelVersion (org.neo4j.kernel.KernelVersion)1 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)1 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)1 StorageReader (org.neo4j.storageengine.api.StorageReader)1 TxStateVisitor (org.neo4j.storageengine.api.txstate.TxStateVisitor)1