use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.
the class JsonMessageBodyReader method readFrom.
@Override
public InputEventStream readFrom(Class<InputEventStream> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws WebApplicationException {
Map<Statement, InputStatement> inputStatements = new HashMap<>();
Map<String, Object> parameters = new HashMap<>();
parameters.put(STATEMENTS_KEY, inputStatements);
StatementDeserializer statementDeserializer = new StatementDeserializer(this.jsonFactory, entityStream);
return new InputEventStream(parameters, () -> {
InputStatement inputStatement = statementDeserializer.read();
if (inputStatement == null) {
return null;
}
Statement statement = new Statement(inputStatement.statement(), inputStatement.parameters());
inputStatements.put(statement, inputStatement);
return statement;
});
}
use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.
the class AbstractCypherResource method executeStatementsInNewTransaction.
@POST
public Response executeStatementsInNewTransaction(InputEventStream inputEventStream, @Context HttpServletRequest request, @Context HttpHeaders headers) {
try (var memoryTracker = createMemoryTracker()) {
InputEventStream inputStream = ensureNotNull(inputEventStream);
var graphDatabaseAPI = httpTransactionManager.getGraphDatabaseAPI(databaseName);
return graphDatabaseAPI.map(databaseAPI -> {
if (isDatabaseNotAvailable(databaseAPI)) {
return createNonAvailableDatabaseResponse(inputStream.getParameters());
}
memoryTracker.allocateHeap(Invocation.SHALLOW_SIZE);
final TransactionFacade transactionFacade = httpTransactionManager.createTransactionFacade(databaseAPI, memoryTracker);
TransactionHandle transactionHandle = createNewTransactionHandle(transactionFacade, request, headers, memoryTracker, false);
Invocation invocation = new Invocation(log, transactionHandle, uriScheme.txCommitUri(transactionHandle.getId()), memoryPool, inputStream, false);
OutputEventStreamImpl outputStream = new OutputEventStreamImpl(inputStream.getParameters(), transactionHandle, uriScheme, invocation::execute);
return Response.created(transactionHandle.uri()).entity(outputStream).build();
}).orElse(createNonExistentDatabaseResponse(inputStream.getParameters()));
}
}
use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.
the class InvocationTest method shouldResumeTransactionWhenExecutingStatementsOnSecondRequest.
@Test
void shouldResumeTransactionWhenExecutingStatementsOnSecondRequest() {
// given
when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
TransactionHandle handle = getTransactionHandle(executionEngine, registry);
InputEventStream inputEventStream = mock(InputEventStream.class);
Statement statement = new Statement("query", map());
when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
mockDefaultResult();
Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, false);
invocation.execute(outputEventStream);
reset(transactionContext, registry, internalTransaction, outputEventStream);
when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
// when
invocation.execute(outputEventStream);
// then
InOrder order = inOrder(transactionContext, registry, internalTransaction);
order.verify(internalTransaction).execute("query", emptyMap());
order.verify(registry).release(1337L, handle);
InOrder outputOrder = inOrder(outputEventStream);
outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
verifyDefaultResultRows(outputOrder);
outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.OPEN, uriScheme.txCommitUri(1337L), 0);
verifyNoMoreInteractions(outputEventStream);
}
use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.
the class InvocationTest method shouldHandleAuthorizationErrorWhenStartingTransaction.
@Test
void shouldHandleAuthorizationErrorWhenStartingTransaction() {
// given
when(databaseFacade.beginTransaction(any(), any(), any())).thenThrow(new AuthorizationViolationException("Forbidden"));
when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
TransactionHandle handle = getTransactionHandle(executionEngine, registry);
InputEventStream inputEventStream = mock(InputEventStream.class);
Statement statement = new Statement("query", map());
when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
mockDefaultResult();
Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
// when
invocation.execute(outputEventStream);
// then
verifyNoMoreInteractions(log);
InOrder outputOrder = inOrder(outputEventStream);
outputOrder.verify(outputEventStream).writeFailure(Status.Security.Forbidden, "Forbidden");
outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.NO_TRANSACTION, uriScheme.txCommitUri(1337L), -1);
verifyNoMoreInteractions(outputEventStream);
}
use of org.neo4j.server.http.cypher.format.api.InputEventStream in project neo4j by neo4j.
the class InvocationTest method shouldHandleConnectionErrorWhenReadingStatementsInImplicitTransaction.
@Test
void shouldHandleConnectionErrorWhenReadingStatementsInImplicitTransaction() {
// given
when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
TransactionHandle handle = getTransactionHandle(executionEngine, registry);
InputEventStream inputEventStream = mock(InputEventStream.class);
when(inputEventStream.read()).thenThrow(new ConnectionException("Connection error", new IOException("Broken pipe")));
Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
// when
var e = assertThrows(ConnectionException.class, () -> invocation.execute(outputEventStream));
assertEquals("Connection error", e.getMessage());
// then
verify(internalTransaction).rollback();
verify(registry).forget(1337L);
verifyNoInteractions(outputEventStream);
}
Aggregations