Search in sources :

Example 11 with TransactionalContext

use of org.neo4j.kernel.impl.query.TransactionalContext in project neo4j by neo4j.

the class TransactionStateMachineSPI method executeQuery.

@Override
public BoltResultHandle executeQuery(BoltQuerySource querySource, SecurityContext securityContext, String statement, Map<String, Object> params, ThrowingAction<KernelException> onFail) throws QueryExecutionKernelException {
    InternalTransaction internalTransaction = queryService.beginTransaction(implicit, securityContext);
    ClientConnectionInfo sourceDetails = new BoltConnectionInfo(querySource.principalName, querySource.clientName, querySource.connectionDescriptor.clientAddress, querySource.connectionDescriptor.serverAddress);
    TransactionalContext transactionalContext = contextFactory.newContext(sourceDetails, internalTransaction, statement, params);
    return new BoltResultHandle() {

        @Override
        public BoltResult start() throws KernelException {
            try {
                Result run = queryExecutionEngine.executeQuery(statement, params, transactionalContext);
                return new CypherAdapterStream(run, clock);
            } catch (KernelException e) {
                onFail.apply();
                throw new QueryExecutionKernelException(e);
            } catch (Throwable e) {
                onFail.apply();
                throw e;
            }
        }

        @Override
        public void terminate() {
            transactionalContext.terminate();
        }
    };
}
Also used : ClientConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo) BoltConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.BoltConnectionInfo) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) BoltResultHandle(org.neo4j.bolt.v1.runtime.TransactionStateMachine.BoltResultHandle) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Result(org.neo4j.graphdb.Result)

Example 12 with TransactionalContext

use of org.neo4j.kernel.impl.query.TransactionalContext in project neo4j by neo4j.

the class CypherAdapterStreamTest method shouldIncludeBasicMetadata.

@Test
public void shouldIncludeBasicMetadata() throws Throwable {
    // Given
    QueryStatistics queryStatistics = mock(QueryStatistics.class);
    when(queryStatistics.containsUpdates()).thenReturn(true);
    when(queryStatistics.getNodesCreated()).thenReturn(1);
    when(queryStatistics.getNodesDeleted()).thenReturn(2);
    when(queryStatistics.getRelationshipsCreated()).thenReturn(3);
    when(queryStatistics.getRelationshipsDeleted()).thenReturn(4);
    when(queryStatistics.getPropertiesSet()).thenReturn(5);
    when(queryStatistics.getIndexesAdded()).thenReturn(6);
    when(queryStatistics.getIndexesRemoved()).thenReturn(7);
    when(queryStatistics.getConstraintsAdded()).thenReturn(8);
    when(queryStatistics.getConstraintsRemoved()).thenReturn(9);
    when(queryStatistics.getLabelsAdded()).thenReturn(10);
    when(queryStatistics.getLabelsRemoved()).thenReturn(11);
    Result result = mock(Result.class);
    when(result.getQueryExecutionType()).thenReturn(query(READ_WRITE));
    when(result.getQueryStatistics()).thenReturn(queryStatistics);
    when(result.getNotifications()).thenReturn(Collections.emptyList());
    Clock clock = mock(Clock.class);
    when(clock.millis()).thenReturn(0L, 1337L);
    TransactionalContext tc = mock(TransactionalContext.class);
    CypherAdapterStream stream = new CypherAdapterStream(result, clock);
    // When
    Map<String, Object> meta = metadataOf(stream);
    // Then
    assertThat(meta.get("type").toString(), equalTo("rw"));
    assertThat(meta.get("stats"), equalTo(map("nodes-created", 1, "nodes-deleted", 2, "relationships-created", 3, "relationships-deleted", 4, "properties-set", 5, "indexes-added", 6, "indexes-removed", 7, "constraints-added", 8, "constraints-removed", 9, "labels-added", 10, "labels-removed", 11)));
    assertThat(meta.get("result_consumed_after"), equalTo(1337L));
}
Also used : QueryStatistics(org.neo4j.graphdb.QueryStatistics) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) Clock(java.time.Clock) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Example 13 with TransactionalContext

use of org.neo4j.kernel.impl.query.TransactionalContext in project neo4j by neo4j.

the class CypherAdapterStreamTest method shouldIncludeProfileIfPresent.

@Test
public void shouldIncludeProfileIfPresent() throws Throwable {
    // Given
    QueryStatistics queryStatistics = mock(QueryStatistics.class);
    when(queryStatistics.containsUpdates()).thenReturn(false);
    Result result = mock(Result.class);
    when(result.getQueryExecutionType()).thenReturn(explained(READ_ONLY));
    when(result.getQueryStatistics()).thenReturn(queryStatistics);
    when(result.getNotifications()).thenReturn(Collections.emptyList());
    when(result.getExecutionPlanDescription()).thenReturn(plan("Join", map("arg1", 1), 2, 4, 3, 1, singletonList("id1"), plan("Scan", map("arg2", 1), 2, 4, 7, 1, singletonList("id2"))));
    TransactionalContext tc = mock(TransactionalContext.class);
    CypherAdapterStream stream = new CypherAdapterStream(result, Clock.systemUTC());
    // When
    Map<String, Object> meta = metadataOf(stream);
    // Then
    assertThat(meta.get("profile").toString(), equalTo("{args={arg1=1}, pageCacheMisses=3, " + "children=[{args={arg2=1}, pageCacheMisses=7, " + "children=[], dbHits=2, identifiers=[id2], operatorType=Scan, rows=1, pageCacheHits=4}], dbHits=2, " + "identifiers=[id1], operatorType=Join, rows=1, pageCacheHits=4}"));
}
Also used : QueryStatistics(org.neo4j.graphdb.QueryStatistics) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Example 14 with TransactionalContext

use of org.neo4j.kernel.impl.query.TransactionalContext in project neo4j by neo4j.

the class CypherAdapterStreamTest method shouldIncludePlanIfPresent.

@Test
public void shouldIncludePlanIfPresent() throws Throwable {
    // Given
    QueryStatistics queryStatistics = mock(QueryStatistics.class);
    when(queryStatistics.containsUpdates()).thenReturn(false);
    Result result = mock(Result.class);
    when(result.getQueryExecutionType()).thenReturn(explained(READ_ONLY));
    when(result.getQueryStatistics()).thenReturn(queryStatistics);
    when(result.getNotifications()).thenReturn(Collections.emptyList());
    when(result.getExecutionPlanDescription()).thenReturn(plan("Join", map("arg1", 1), singletonList("id1"), plan("Scan", map("arg2", 1), singletonList("id2"))));
    TransactionalContext tc = mock(TransactionalContext.class);
    CypherAdapterStream stream = new CypherAdapterStream(result, Clock.systemUTC());
    // When
    Map<String, Object> meta = metadataOf(stream);
    // Then
    assertThat(meta.get("plan").toString(), equalTo("{args={arg1=1}, children=[{args={arg2=1}, children=[], identifiers=[id2], operatorType=Scan}], identifiers=[id1], operatorType=Join}"));
}
Also used : QueryStatistics(org.neo4j.graphdb.QueryStatistics) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) BoltResult(org.neo4j.bolt.v1.runtime.spi.BoltResult) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Example 15 with TransactionalContext

use of org.neo4j.kernel.impl.query.TransactionalContext in project neo4j by neo4j.

the class CypherSession method evaluate.

@Override
public Pair<String, String> evaluate(String script) {
    if (StringUtils.EMPTY.equals(script.trim())) {
        return Pair.of(StringUtils.EMPTY, null);
    }
    String resultString;
    try {
        TransactionalContext tc = cypherExecutor.createTransactionContext(script, emptyMap(), request);
        ExecutionEngine engine = cypherExecutor.getExecutionEngine();
        Result result = engine.executeQuery(script, emptyMap(), tc);
        resultString = result.resultAsString();
    } catch (SyntaxException error) {
        resultString = error.getMessage();
    } catch (Exception exception) {
        log.error("Unknown error executing cypher query", exception);
        resultString = "Error: " + exception.getClass().getSimpleName() + " - " + exception.getMessage();
    }
    return Pair.of(resultString, null);
}
Also used : ExecutionEngine(org.neo4j.cypher.internal.javacompat.ExecutionEngine) SyntaxException(org.neo4j.cypher.SyntaxException) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) SyntaxException(org.neo4j.cypher.SyntaxException) Result(org.neo4j.graphdb.Result)

Aggregations

TransactionalContext (org.neo4j.kernel.impl.query.TransactionalContext)20 Result (org.neo4j.graphdb.Result)16 Test (org.junit.Test)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 QueryExecutionEngine (org.neo4j.kernel.impl.query.QueryExecutionEngine)10 InOrder (org.mockito.InOrder)9 Notification (org.neo4j.graphdb.Notification)7 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)6 BoltResult (org.neo4j.bolt.v1.runtime.spi.BoltResult)5 QueryStatistics (org.neo4j.graphdb.QueryStatistics)4 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)3 Map (java.util.Map)2 Matchers.anyString (org.mockito.Matchers.anyString)2 CypherException (org.neo4j.cypher.CypherException)2 SyntaxException (org.neo4j.cypher.SyntaxException)2 Type (org.neo4j.kernel.api.KernelTransaction.Type)2 KernelException (org.neo4j.kernel.api.exceptions.KernelException)2 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)2 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)2 NullLogProvider (org.neo4j.logging.NullLogProvider)2