Search in sources :

Example 1 with Prepared

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);
}
Also used : RowsMetadata(com.datastax.oss.protocol.internal.response.result.RowsMetadata) ColumnSpec(com.datastax.oss.protocol.internal.response.result.ColumnSpec) Prepared(com.datastax.oss.protocol.internal.response.result.Prepared)

Example 2 with Prepared

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));
    }
}
Also used : Message(com.datastax.oss.protocol.internal.Message) Prepared(com.datastax.oss.protocol.internal.response.result.Prepared) ByteBuffer(java.nio.ByteBuffer) Rows(com.datastax.oss.protocol.internal.response.result.Rows)

Example 3 with Prepared

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();
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Prepared(com.datastax.oss.protocol.internal.response.result.Prepared) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) ByteBuffer(java.nio.ByteBuffer) RepreparePayload(com.datastax.oss.driver.internal.core.session.RepreparePayload) Unprepared(com.datastax.oss.protocol.internal.response.error.Unprepared) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test)

Example 4 with Prepared

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);
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) BatchableStatement(com.datastax.oss.driver.api.core.cql.BatchableStatement) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) Prepared(com.datastax.oss.protocol.internal.response.result.Prepared) Rows(com.datastax.oss.protocol.internal.response.result.Rows)

Aggregations

Prepared (com.datastax.oss.protocol.internal.response.result.Prepared)4 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)2 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)2 Rows (com.datastax.oss.protocol.internal.response.result.Rows)2 ByteBuffer (java.nio.ByteBuffer)2 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 BatchableStatement (com.datastax.oss.driver.api.core.cql.BatchableStatement)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 Statement (com.datastax.oss.driver.api.core.cql.Statement)1 RepreparePayload (com.datastax.oss.driver.internal.core.session.RepreparePayload)1 Message (com.datastax.oss.protocol.internal.Message)1 Unprepared (com.datastax.oss.protocol.internal.response.error.Unprepared)1 ColumnSpec (com.datastax.oss.protocol.internal.response.result.ColumnSpec)1 RowsMetadata (com.datastax.oss.protocol.internal.response.result.RowsMetadata)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1