use of com.datastax.oss.protocol.internal.response.result.Prepared in project java-driver by datastax.
the class CqlPrepareHandlerTest method simplePrepared.
private static Message simplePrepared() {
RowsMetadata variablesMetadata = new RowsMetadata(ImmutableList.of(new ColumnSpec("ks", "table", "key", 0, RawType.PRIMITIVES.get(ProtocolConstants.DataType.VARCHAR))), null, new int[] { 0 }, null);
RowsMetadata resultMetadata = new RowsMetadata(ImmutableList.of(new ColumnSpec("ks", "table", "message", 0, RawType.PRIMITIVES.get(ProtocolConstants.DataType.VARCHAR))), null, new int[] {}, null);
return new Prepared(Bytes.fromHexString("0xffff").array(), null, variablesMetadata, resultMetadata);
}
use of com.datastax.oss.protocol.internal.response.result.Prepared in project java-driver by datastax.
the class AdminRequestHandler method onResponse.
@Override
public void onResponse(Frame responseFrame) {
if (timeoutFuture != null) {
timeoutFuture.cancel(true);
}
Message message = responseFrame.message;
LOG.debug("[{}] Got response {}", logPrefix, responseFrame.message);
if (!expectedResponseType.isInstance(message)) {
// Note that this also covers error responses, no need to get too fancy here
setFinalError(new UnexpectedResponseException(debugString, message));
} else if (expectedResponseType == Rows.class) {
Rows rows = (Rows) message;
ByteBuffer pagingState = rows.getMetadata().pagingState;
AdminRequestHandler nextHandler = (pagingState == null) ? null : this.copy(pagingState);
// The public factory methods guarantee that expectedResponseType and ResultT always match:
@SuppressWarnings("unchecked") ResultT result = (ResultT) new AdminResult(rows, nextHandler, channel.protocolVersion());
setFinalResult(result);
} else if (expectedResponseType == Prepared.class) {
Prepared prepared = (Prepared) message;
@SuppressWarnings("unchecked") ResultT result = (ResultT) ByteBuffer.wrap(prepared.preparedQueryId);
setFinalResult(result);
} else if (expectedResponseType == com.datastax.oss.protocol.internal.response.result.Void.class) {
setFinalResult(null);
} else {
setFinalError(new AssertionError("Unhandled response type" + expectedResponseType));
}
}
use of com.datastax.oss.protocol.internal.response.result.Prepared in project java-driver by datastax.
the class CqlRequestHandlerTest method should_reprepare_on_the_fly_if_not_prepared.
@Test
public void should_reprepare_on_the_fly_if_not_prepared() throws InterruptedException {
ByteBuffer mockId = Bytes.fromHexString("0xffff");
PreparedStatement preparedStatement = mock(PreparedStatement.class);
when(preparedStatement.getId()).thenReturn(mockId);
ColumnDefinitions columnDefinitions = mock(ColumnDefinitions.class);
when(columnDefinitions.size()).thenReturn(0);
when(preparedStatement.getResultSetDefinitions()).thenReturn(columnDefinitions);
BoundStatement boundStatement = mock(BoundStatement.class);
when(boundStatement.getPreparedStatement()).thenReturn(preparedStatement);
when(boundStatement.getValues()).thenReturn(Collections.emptyList());
when(boundStatement.getNowInSeconds()).thenReturn(Statement.NO_NOW_IN_SECONDS);
RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder();
// For the first attempt that gets the UNPREPARED response
PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
// For the second attempt that succeeds
harnessBuilder.withResponse(node1, defaultFrameOf(singleRow()));
try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
// The handler will look for the info to reprepare in the session's cache, put it there
ConcurrentMap<ByteBuffer, RepreparePayload> repreparePayloads = new ConcurrentHashMap<>();
repreparePayloads.put(mockId, new RepreparePayload(mockId, "mock query", null, Collections.emptyMap()));
when(harness.getSession().getRepreparePayloads()).thenReturn(repreparePayloads);
CompletionStage<AsyncResultSet> resultSetFuture = new CqlRequestHandler(boundStatement, harness.getSession(), harness.getContext(), "test").handle();
// Before we proceed, mock the PREPARE exchange that will occur as soon as we complete the
// first response.
node1Behavior.mockFollowupRequest(Prepare.class, defaultFrameOf(new Prepared(Bytes.getArray(mockId), null, null, null)));
node1Behavior.setWriteSuccess();
node1Behavior.setResponseSuccess(defaultFrameOf(new Unprepared("mock message", Bytes.getArray(mockId))));
// Should now re-prepare, re-execute and succeed.
assertThatStage(resultSetFuture).isSuccess();
}
}
use of com.datastax.oss.protocol.internal.response.result.Prepared in project java-driver by datastax.
the class Conversions method toResultSet.
public static AsyncResultSet toResultSet(Result result, ExecutionInfo executionInfo, CqlSession session, InternalDriverContext context) {
if (result instanceof Rows) {
Rows rows = (Rows) result;
Statement<?> statement = (Statement<?>) executionInfo.getRequest();
ColumnDefinitions columnDefinitions = getResultDefinitions(rows, statement, context);
return new DefaultAsyncResultSet(columnDefinitions, executionInfo, rows.getData(), session, context);
} else if (result instanceof Prepared) {
// This should never happen
throw new IllegalArgumentException("Unexpected PREPARED response to a CQL query");
} else {
// Void, SetKeyspace, SchemaChange
return DefaultAsyncResultSet.empty(executionInfo);
}
}
Aggregations