Search in sources :

Example 6 with QueryExecutionEngine

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

the class TransactionHandleTest method shouldInterruptTransaction.

@Test
public void shouldInterruptTransaction() throws Exception {
    // given
    TransitionalPeriodTransactionMessContainer kernel = mockKernel();
    TransitionalTxManagementKernelTransaction tx = mock(TransitionalTxManagementKernelTransaction.class);
    when(kernel.newTransaction(any(Type.class), any(SecurityContext.class), anyLong())).thenReturn(tx);
    TransactionRegistry registry = mock(TransactionRegistry.class);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    QueryExecutionEngine executionEngine = mock(QueryExecutionEngine.class);
    TransactionHandle handle = getTransactionHandle(kernel, executionEngine, registry);
    ExecutionResultSerializer output = mock(ExecutionResultSerializer.class);
    Statement statement = new Statement("MATCH (n) RETURN n", map(), false, (ResultDataContent[]) null);
    handle.execute(statements(statement), output, mock(HttpServletRequest.class));
    // when
    handle.terminate();
    // then
    verify(tx, times(1)).terminate();
}
Also used : QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) HttpServletRequest(javax.servlet.http.HttpServletRequest) Type(org.neo4j.kernel.api.KernelTransaction.Type) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) Test(org.junit.Test)

Example 7 with QueryExecutionEngine

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

the class TransactionHandleTest method startTransactionWithRequestedTimeout.

@Test
public void startTransactionWithRequestedTimeout() {
    QueryExecutionEngine executionEngine = mock(QueryExecutionEngine.class);
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    ExecutionResultSerializer output = mock(ExecutionResultSerializer.class);
    TransitionalPeriodTransactionMessContainer txManagerFacade = mockKernel();
    TransactionHandle handle = new TransactionHandle(txManagerFacade, executionEngine, queryService, mock(TransactionRegistry.class), uriScheme, true, AUTH_DISABLED, 100, NullLogProvider.getInstance());
    handle.commit(statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output, mock(HttpServletRequest.class));
    verify(txManagerFacade).newTransaction(Type.implicit, AUTH_DISABLED, 100);
}
Also used : QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) Test(org.junit.Test)

Example 8 with QueryExecutionEngine

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

the class TransactionHandleTest method shouldHandleExecutionEngineThrowingUndeclaredCheckedExceptions.

@Test
public void shouldHandleExecutionEngineThrowingUndeclaredCheckedExceptions() throws Exception {
    // given
    QueryExecutionEngine executionEngine = mock(QueryExecutionEngine.class);
    when(executionEngine.executeQuery(eq("match (n) return n"), eq(map()), any(TransactionalContext.class))).thenAnswer(invocationOnMock -> {
        throw new Exception("BOO");
    });
    TransactionRegistry registry = mock(TransactionRegistry.class);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    TransactionHandle handle = new TransactionHandle(mockKernel(), executionEngine, queryService, registry, uriScheme, false, AUTH_DISABLED, anyLong(), NullLogProvider.getInstance());
    ExecutionResultSerializer output = mock(ExecutionResultSerializer.class);
    // when
    Statement statement = new Statement("match (n) return n", map(), false, (ResultDataContent[]) null);
    handle.commit(statements(statement), output, mock(HttpServletRequest.class));
    // then
    verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(output);
    outputOrder.verify(output).errors(argThat(hasErrors(Status.Statement.ExecutionFailed)));
    outputOrder.verify(output).finish();
    verifyNoMoreInteractions(output);
}
Also used : QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) HttpServletRequest(javax.servlet.http.HttpServletRequest) InOrder(org.mockito.InOrder) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) SyntaxException(org.neo4j.cypher.SyntaxException) Test(org.junit.Test)

Example 9 with QueryExecutionEngine

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

the class TransactionHandleTest method deadlockExceptionHasCorrectStatus.

@Test
@SuppressWarnings("unchecked")
public void deadlockExceptionHasCorrectStatus() throws Exception {
    // given
    QueryExecutionEngine executionEngine = mock(QueryExecutionEngine.class);
    when(executionEngine.executeQuery(anyString(), anyMap(), any(TransactionalContext.class))).thenThrow(new DeadlockDetectedException("deadlock"));
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    TransactionHandle handle = new TransactionHandle(mockKernel(), executionEngine, queryService, mock(TransactionRegistry.class), uriScheme, true, AUTH_DISABLED, anyLong(), NullLogProvider.getInstance());
    ExecutionResultSerializer output = mock(ExecutionResultSerializer.class);
    // when
    handle.execute(statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output, mock(HttpServletRequest.class));
    // then
    verify(output).errors(argThat(hasErrors(Status.Transaction.DeadlockDetected)));
}
Also used : QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) Test(org.junit.Test)

Example 10 with QueryExecutionEngine

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

the class CypherService method cypher.

@POST
@SuppressWarnings({ "unchecked", "ParameterCanBeLocal" })
public Response cypher(String body, @Context HttpServletRequest request, @QueryParam(INCLUDE_STATS_PARAM) boolean includeStats, @QueryParam(INCLUDE_PLAN_PARAM) boolean includePlan, @QueryParam(PROFILE_PARAM) boolean profile) throws BadInputException {
    usage.get(features).flag(http_cypher_endpoint);
    Map<String, Object> command = input.readMap(body);
    if (!command.containsKey(QUERY_KEY)) {
        return output.badRequest(new InvalidArgumentsException("You have to provide the 'query' parameter."));
    }
    String query = (String) command.get(QUERY_KEY);
    Map<String, Object> params;
    try {
        params = (Map<String, Object>) (command.containsKey(PARAMS_KEY) && command.get(PARAMS_KEY) != null ? command.get(PARAMS_KEY) : new HashMap<String, Object>());
    } catch (ClassCastException e) {
        return output.badRequest(new IllegalArgumentException("Parameters must be a JSON map"));
    }
    try {
        QueryExecutionEngine executionEngine = cypherExecutor.getExecutionEngine();
        boolean periodicCommitQuery = executionEngine.isPeriodicCommit(query);
        CommitOnSuccessfulStatusCodeRepresentationWriteHandler handler = (CommitOnSuccessfulStatusCodeRepresentationWriteHandler) output.getRepresentationWriteHandler();
        if (periodicCommitQuery) {
            handler.closeTransaction();
        }
        TransactionalContext tc = cypherExecutor.createTransactionContext(query, params, request);
        Result result;
        if (profile) {
            result = executionEngine.profileQuery(query, params, tc);
            includePlan = true;
        } else {
            result = executionEngine.executeQuery(query, params, tc);
            includePlan = result.getQueryExecutionType().requestedExecutionPlanDescription();
        }
        if (periodicCommitQuery) {
            handler.setTransaction(database.beginTx());
        }
        return output.ok(new CypherResultRepresentation(result, includeStats, includePlan));
    } catch (Throwable e) {
        if (e.getCause() instanceof CypherException) {
            return output.badRequest(e.getCause());
        } else {
            return output.badRequest(e);
        }
    }
}
Also used : CypherResultRepresentation(org.neo4j.server.rest.repr.CypherResultRepresentation) HashMap(java.util.HashMap) Result(org.neo4j.graphdb.Result) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) CommitOnSuccessfulStatusCodeRepresentationWriteHandler(org.neo4j.server.rest.transactional.CommitOnSuccessfulStatusCodeRepresentationWriteHandler) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) CypherException(org.neo4j.cypher.CypherException) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException) POST(javax.ws.rs.POST)

Aggregations

QueryExecutionEngine (org.neo4j.kernel.impl.query.QueryExecutionEngine)21 Test (org.junit.Test)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 TransactionalContext (org.neo4j.kernel.impl.query.TransactionalContext)12 InOrder (org.mockito.InOrder)10 Result (org.neo4j.graphdb.Result)8 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)8 Notification (org.neo4j.graphdb.Notification)7 DependencyResolver (org.neo4j.graphdb.DependencyResolver)3 LogProvider (org.neo4j.logging.LogProvider)3 SyntaxException (org.neo4j.cypher.SyntaxException)2 CommunityCompatibilityFactory (org.neo4j.cypher.internal.CommunityCompatibilityFactory)2 GraphDatabaseCypherService (org.neo4j.cypher.javacompat.internal.GraphDatabaseCypherService)2 DeadlockDetectedException (org.neo4j.kernel.DeadlockDetectedException)2 KernelAPI (org.neo4j.kernel.api.KernelAPI)2 Type (org.neo4j.kernel.api.KernelTransaction.Type)2 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)2 LogService (org.neo4j.kernel.impl.logging.LogService)2 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)2 Monitors (org.neo4j.kernel.monitoring.Monitors)2