Search in sources :

Example 31 with KernelStatement

use of org.neo4j.kernel.impl.api.KernelStatement in project neo4j-apoc-procedures by neo4j-contrib.

the class SchemaIndex method getLuceneIndexReader.

private SimpleIndexReader getLuceneIndexReader(String label, String key) throws SchemaRuleNotFoundException, IndexNotFoundKernelException, DuplicateSchemaRuleException {
    try (KernelStatement stmt = (KernelStatement) tx.acquireStatement()) {
        ReadOperations reads = stmt.readOperations();
        LabelSchemaDescriptor labelSchemaDescriptor = new LabelSchemaDescriptor(reads.labelGetForName(label), reads.propertyKeyGetForName(key));
        IndexDescriptor descriptor = reads.indexGetForSchema(labelSchemaDescriptor);
        IndexReader indexReader = stmt.getStoreStatement().getIndexReader(descriptor);
        if (indexReader.getClass().getName().endsWith("FusionIndexReader")) {
            try {
                Field field = indexReader.getClass().getDeclaredField("luceneReader");
                field.setAccessible(true);
                return (SimpleIndexReader) field.get(indexReader);
            } catch (IllegalAccessException | NoSuchFieldException e) {
                throw new RuntimeException("Error accessing index reader", e);
            }
        }
        return (SimpleIndexReader) indexReader;
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Field(java.lang.reflect.Field) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) SimpleIndexReader(org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader) SimpleIndexReader(org.neo4j.kernel.api.impl.schema.reader.SimpleIndexReader) SortedIndexReader(org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) LabelSchemaDescriptor(org.neo4j.kernel.api.schema.LabelSchemaDescriptor) IndexDescriptor(org.neo4j.kernel.api.schema.index.IndexDescriptor)

Example 32 with KernelStatement

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

the class Neo4jTransactionalContext method commitAndRestartTx.

@Override
public long commitAndRestartTx() {
    /*
        * This method is use by the Cypher runtime to cater for PERIODIC COMMIT, which allows a single query to
        * periodically, after x number of rows, to commit a transaction and spawn a new one.
        *
        * To still keep track of the running stream after switching transactions, we need to open the new transaction
        * before closing the old one. This way, a query will not disappear and appear when switching transactions.
        *
        * Since our transactions are thread bound, we must first unbind the old transaction from the thread before
        * creating a new one. And then we need to do that thread switching again to close the old transaction.
        */
    checkNotTerminated();
    collectTransactionExecutionStatistic();
    // (1) Remember old statement
    QueryRegistry oldQueryRegistry = statement.queryRegistration();
    Statement oldStatement = statement;
    KernelTransaction oldKernelTx = transaction.kernelTransaction();
    // (2) Create and register new transaction
    kernelTransaction = transactionFactory.beginKernelTransaction(transactionType, securityContext, clientInfo);
    statement = (KernelStatement) kernelTransaction.acquireStatement();
    statement.queryRegistration().registerExecutingQuery(executingQuery);
    transaction.setTransaction(kernelTransaction);
    // (3) Commit and close old transaction (and unregister as a side effect of that)
    oldQueryRegistry.unregisterExecutingQuery(executingQuery);
    try {
        oldStatement.close();
        return oldKernelTx.commit();
    } catch (Throwable t) {
        // Corner case: The old transaction might have been terminated by the user. Now we also need to
        // terminate the new transaction.
        transaction.rollback();
        throw new RuntimeException(t);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) QueryRegistry(org.neo4j.kernel.api.QueryRegistry) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement)

Example 33 with KernelStatement

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

the class Neo4jTransactionalContextFactory method newContextForQuery.

@Override
public TransactionalContext newContextForQuery(InternalTransaction tx, ExecutingQuery executingQuery) {
    KernelStatement initialStatement = (KernelStatement) tx.kernelTransaction().acquireStatement();
    initialStatement.queryRegistration().startQueryExecution(executingQuery);
    return contextCreator.create(tx, initialStatement, executingQuery);
}
Also used : KernelStatement(org.neo4j.kernel.impl.api.KernelStatement)

Example 34 with KernelStatement

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

the class Neo4jTransactionalContextFactory method newContext.

@Override
public final Neo4jTransactionalContext newContext(InternalTransaction tx, String queryText, MapValue queryParameters) {
    KernelStatement initialStatement = (KernelStatement) tx.kernelTransaction().acquireStatement();
    var executingQuery = initialStatement.queryRegistration().startQueryExecution(queryText, queryParameters);
    return contextCreator.create(tx, initialStatement, executingQuery);
}
Also used : KernelStatement(org.neo4j.kernel.impl.api.KernelStatement)

Aggregations

KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)34 Test (org.junit.Test)17 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)14 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)12 IndexReader (org.neo4j.storageengine.api.schema.IndexReader)10 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)9 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)8 IndexQuery (org.neo4j.kernel.api.schema_new.IndexQuery)8 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 NodeItem (org.neo4j.storageengine.api.NodeItem)6 Test (org.junit.jupiter.api.Test)4 QueryRegistry (org.neo4j.kernel.api.QueryRegistry)4 UniquenessConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor)4 Statement (org.neo4j.kernel.api.Statement)3 ConstraintDescriptor (org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)3 RelationshipItem (org.neo4j.storageengine.api.RelationshipItem)3 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 InOrder (org.mockito.InOrder)2