Search in sources :

Example 16 with ColumnDefinitions

use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions 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)

Example 17 with ColumnDefinitions

use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.

the class Conversions method getResultDefinitions.

public static ColumnDefinitions getResultDefinitions(Rows rows, Statement<?> statement, InternalDriverContext context) {
    RowsMetadata rowsMetadata = rows.getMetadata();
    if (rowsMetadata.columnSpecs.isEmpty()) {
        // If the response has no metadata, it means the request had SKIP_METADATA set, the driver
        // only ever does that for bound statements.
        BoundStatement boundStatement = (BoundStatement) statement;
        return boundStatement.getPreparedStatement().getResultSetDefinitions();
    } else {
        // The response has metadata, always use it above anything else we might have locally.
        ColumnDefinitions definitions = toColumnDefinitions(rowsMetadata, context);
        // prepared statement's copy of the metadata
        if (rowsMetadata.newResultMetadataId != null) {
            BoundStatement boundStatement = (BoundStatement) statement;
            PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
            preparedStatement.setResultMetadata(ByteBuffer.wrap(rowsMetadata.newResultMetadataId).asReadOnlyBuffer(), definitions);
        }
        return definitions;
    }
}
Also used : RowsMetadata(com.datastax.oss.protocol.internal.response.result.RowsMetadata) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 18 with ColumnDefinitions

use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.

the class DefaultContinuousResultSetTest method mockPage.

private static ContinuousAsyncResultSet mockPage(boolean nextPage, Integer... data) {
    ContinuousAsyncResultSet page = Mockito.mock(ContinuousAsyncResultSet.class);
    ColumnDefinitions columnDefinitions = Mockito.mock(ColumnDefinitions.class);
    Mockito.when(page.getColumnDefinitions()).thenReturn(columnDefinitions);
    ExecutionInfo executionInfo = Mockito.mock(ExecutionInfo.class);
    Mockito.when(page.getExecutionInfo()).thenReturn(executionInfo);
    if (nextPage) {
        Mockito.when(page.hasMorePages()).thenReturn(true);
        Mockito.when(page.fetchNextPage()).thenReturn(Mockito.spy(new CompletableFuture<>()));
    } else {
        Mockito.when(page.hasMorePages()).thenReturn(false);
        Mockito.when(page.fetchNextPage()).thenThrow(new IllegalStateException());
    }
    Iterator<Integer> rows = Arrays.asList(data).iterator();
    CountingIterator<Row> iterator = new CountingIterator<Row>(data.length) {

        @Override
        protected Row computeNext() {
            return rows.hasNext() ? mockRow(rows.next()) : endOfData();
        }
    };
    Mockito.when(page.currentPage()).thenReturn(() -> iterator);
    Mockito.when(page.remaining()).thenAnswer(invocation -> iterator.remaining());
    return page;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) CompletableFuture(java.util.concurrent.CompletableFuture) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) ContinuousAsyncResultSet(com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 19 with ColumnDefinitions

use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.

the class CqlRequestReactiveProcessorTest method should_complete_single_page_result.

@Test
@UseDataProvider(value = "allDseAndOssProtocolVersions", location = DseTestDataProviders.class)
public void should_complete_single_page_result(ProtocolVersion version) {
    try (RequestHandlerTestHarness harness = RequestHandlerTestHarness.builder().withProtocolVersion(version).withResponse(node1, defaultFrameOf(singleDseRow())).build()) {
        DefaultSession session = harness.getSession();
        InternalDriverContext context = harness.getContext();
        ReactiveResultSet publisher = new CqlRequestReactiveProcessor(new CqlRequestAsyncProcessor()).process(UNDEFINED_IDEMPOTENCE_STATEMENT, session, context, "test");
        List<ReactiveRow> rows = Flowable.fromPublisher(publisher).toList().blockingGet();
        assertThat(rows).hasSize(1);
        ReactiveRow row = rows.get(0);
        assertThat(row.getString("message")).isEqualTo("hello, world");
        ExecutionInfo executionInfo = row.getExecutionInfo();
        assertThat(executionInfo.getCoordinator()).isEqualTo(node1);
        assertThat(executionInfo.getErrors()).isEmpty();
        assertThat(executionInfo.getIncomingPayload()).isEmpty();
        assertThat(executionInfo.getPagingState()).isNull();
        assertThat(executionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
        assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
        assertThat(executionInfo.getWarnings()).isEmpty();
        Flowable<ExecutionInfo> execInfosFlowable = Flowable.fromPublisher(publisher.getExecutionInfos());
        assertThat(execInfosFlowable.toList().blockingGet()).containsExactly(executionInfo);
        Flowable<ColumnDefinitions> colDefsFlowable = Flowable.fromPublisher(publisher.getColumnDefinitions());
        assertThat(colDefsFlowable.toList().blockingGet()).containsExactly(row.getColumnDefinitions());
        Flowable<Boolean> wasAppliedFlowable = Flowable.fromPublisher(publisher.wasApplied());
        assertThat(wasAppliedFlowable.toList().blockingGet()).containsExactly(row.wasApplied());
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CqlRequestAsyncProcessor(com.datastax.oss.driver.internal.core.cql.CqlRequestAsyncProcessor) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 20 with ColumnDefinitions

use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.

the class CqlRequestReactiveProcessorTest method should_complete_multi_page_result.

@Test
@UseDataProvider(value = "allDseAndOssProtocolVersions", location = DseTestDataProviders.class)
public void should_complete_multi_page_result(ProtocolVersion version) {
    RequestHandlerTestHarness.Builder builder = RequestHandlerTestHarness.builder().withProtocolVersion(version);
    PoolBehavior node1Behavior = builder.customBehavior(node1);
    try (RequestHandlerTestHarness harness = builder.build()) {
        DefaultSession session = harness.getSession();
        InternalDriverContext context = harness.getContext();
        // The 2nd page is obtained by an "external" call to session.executeAsync(),
        // so we need to mock that.
        CompletableFuture<AsyncResultSet> page2Future = new CompletableFuture<>();
        when(session.executeAsync(any(Statement.class))).thenAnswer(invocation -> page2Future);
        ExecutionInfo mockInfo = mock(ExecutionInfo.class);
        ReactiveResultSet publisher = new CqlRequestReactiveProcessor(new CqlRequestAsyncProcessor()).process(UNDEFINED_IDEMPOTENCE_STATEMENT, session, context, "test");
        Flowable<ReactiveRow> rowsPublisher = Flowable.fromPublisher(publisher).cache();
        rowsPublisher.subscribe();
        // emulate arrival of page 1
        node1Behavior.setResponseSuccess(defaultFrameOf(DseTestFixtures.tenDseRows(1, false)));
        // emulate arrival of page 2 following the call to session.executeAsync()
        page2Future.complete(Conversions.toResultSet(DseTestFixtures.tenDseRows(2, true), mockInfo, harness.getSession(), harness.getContext()));
        List<ReactiveRow> rows = rowsPublisher.toList().blockingGet();
        assertThat(rows).hasSize(20);
        ReactiveRow first = rows.get(0);
        ExecutionInfo firstExecutionInfo = first.getExecutionInfo();
        assertThat(firstExecutionInfo.getCoordinator()).isEqualTo(node1);
        assertThat(firstExecutionInfo.getErrors()).isEmpty();
        assertThat(firstExecutionInfo.getIncomingPayload()).isEmpty();
        assertThat(firstExecutionInfo.getPagingState()).isNotNull();
        assertThat(firstExecutionInfo.getSpeculativeExecutionCount()).isEqualTo(0);
        assertThat(firstExecutionInfo.getSuccessfulExecutionIndex()).isEqualTo(0);
        assertThat(firstExecutionInfo.getWarnings()).isEmpty();
        ReactiveRow inSecondPage = rows.get(10);
        ExecutionInfo secondExecutionInfo = inSecondPage.getExecutionInfo();
        assertThat(secondExecutionInfo).isSameAs(mockInfo);
        Flowable<ExecutionInfo> execInfosFlowable = Flowable.fromPublisher(publisher.getExecutionInfos());
        assertThat(execInfosFlowable.toList().blockingGet()).containsExactly(firstExecutionInfo, secondExecutionInfo);
        Flowable<ColumnDefinitions> colDefsFlowable = Flowable.fromPublisher(publisher.getColumnDefinitions());
        assertThat(colDefsFlowable.toList().blockingGet()).containsExactly(first.getColumnDefinitions());
        Flowable<Boolean> wasAppliedFlowable = Flowable.fromPublisher(publisher.wasApplied());
        assertThat(wasAppliedFlowable.toList().blockingGet()).containsExactly(first.wasApplied());
    }
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Statement(com.datastax.oss.driver.api.core.cql.Statement) RequestHandlerTestHarness(com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CqlRequestAsyncProcessor(com.datastax.oss.driver.internal.core.cql.CqlRequestAsyncProcessor) CompletableFuture(java.util.concurrent.CompletableFuture) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)25 Test (org.junit.Test)14 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)13 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)11 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)7 ReactiveResultSet (com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet)6 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)6 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)6 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)5 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)4 Row (com.datastax.oss.driver.api.core.cql.Row)4 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)4 EmptyColumnDefinitions (com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions)4 RequestHandlerTestHarness (com.datastax.oss.driver.internal.core.cql.RequestHandlerTestHarness)4 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)4 ByteBuffer (java.nio.ByteBuffer)4 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)3 ContinuousReactiveResultSet (com.datastax.dse.driver.api.core.cql.continuous.reactive.ContinuousReactiveResultSet)2