Search in sources :

Example 11 with ColumnDefinitions

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

the class ContinuousPagingReactiveIT method should_execute_reactively.

@Test
@UseDataProvider("pagingOptions")
public void should_execute_reactively(Options options) {
    CqlSession session = sessionRule.session();
    SimpleStatement statement = SimpleStatement.newInstance("SELECT v from test where k=?", KEY);
    DriverExecutionProfile profile = options.asProfile(session);
    ContinuousReactiveResultSet rs = session.executeContinuouslyReactive(statement.setExecutionProfile(profile));
    List<ReactiveRow> results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results).hasSize(options.expectedRows);
    Set<ExecutionInfo> expectedExecInfos = new LinkedHashSet<>();
    for (int i = 0; i < results.size(); i++) {
        ReactiveRow row = results.get(i);
        assertThat(row.getInt("v")).isEqualTo(i);
        expectedExecInfos.add(row.getExecutionInfo());
    }
    List<ExecutionInfo> execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    // DSE may send an empty page as it can't always know if it's done paging or not yet.
    // See: CASSANDRA-8871. In this case, this page's execution info appears in
    // rs.getExecutionInfos(), but is not present in expectedExecInfos since the page did not
    // contain any rows.
    assertThat(execInfos).containsAll(expectedExecInfos);
    List<ColumnDefinitions> colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    ReactiveRow first = results.get(0);
    assertThat(colDefs).hasSize(1).containsExactly(first.getColumnDefinitions());
    List<Boolean> wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(first.wasApplied());
    validateMetrics(session);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 12 with ColumnDefinitions

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

the class AbstractBufferedRateExecutor method toStringWithValues.

private static String toStringWithValues(BoundStatement boundStatement, ProtocolVersion protocolVersion) {
    CodecRegistry codecRegistry = boundStatement.codecRegistry();
    PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
    String query = preparedStatement.getQuery();
    ColumnDefinitions defs = preparedStatement.getVariableDefinitions();
    int index = 0;
    for (ColumnDefinition def : defs) {
        DataType type = def.getType();
        TypeCodec<Object> codec = codecRegistry.codecFor(type);
        if (boundStatement.getBytesUnsafe(index) != null) {
            Object value = codec.decode(boundStatement.getBytesUnsafe(index), protocolVersion);
            String replacement = Matcher.quoteReplacement(codec.format(value));
            query = query.replaceFirst("\\?", replacement);
        }
        index++;
    }
    return query;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) DataType(com.datastax.oss.driver.api.core.type.DataType) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition)

Example 13 with ColumnDefinitions

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

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

the class QueryTraceFetcherTest method eventRow.

private Row eventRow(int i) {
    Row row = mock(Row.class);
    ColumnDefinitions definitions = mock(ColumnDefinitions.class);
    when(row.getColumnDefinitions()).thenReturn(definitions);
    when(row.getString("activity")).thenReturn("mock activity " + i);
    when(row.getUuid("event_id")).thenReturn(Uuids.startOf(i));
    when(row.getInetAddress("source")).thenReturn(address);
    when(definitions.contains("source_port")).thenReturn(true);
    when(row.getInt("source_port")).thenReturn(PORT);
    when(row.getInt("source_elapsed")).thenReturn(i);
    when(row.getString("thread")).thenReturn("mock thread " + i);
    return row;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 15 with ColumnDefinitions

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

the class Conversions method toPreparedStatement.

public static DefaultPreparedStatement toPreparedStatement(Prepared response, PrepareRequest request, InternalDriverContext context) {
    ColumnDefinitions variableDefinitions = toColumnDefinitions(response.variablesMetadata, context);
    int[] pkIndicesInResponse = response.variablesMetadata.pkIndices;
    // null means a legacy protocol version that doesn't provide the info, try to compute it
    List<Integer> pkIndices = (pkIndicesInResponse == null) ? computePkIndices(variableDefinitions, context) : Ints.asList(pkIndicesInResponse);
    return new DefaultPreparedStatement(ByteBuffer.wrap(response.preparedQueryId).asReadOnlyBuffer(), request.getQuery(), variableDefinitions, pkIndices, (response.resultMetadataId == null) ? null : ByteBuffer.wrap(response.resultMetadataId).asReadOnlyBuffer(), toColumnDefinitions(response.resultMetadata, context), request.getKeyspace(), NullAllowingImmutableMap.copyOf(request.getCustomPayload()), request.getExecutionProfileNameForBoundStatements(), request.getExecutionProfileForBoundStatements(), request.getRoutingKeyspaceForBoundStatements(), request.getRoutingKeyForBoundStatements(), request.getRoutingTokenForBoundStatements(), NullAllowingImmutableMap.copyOf(request.getCustomPayloadForBoundStatements()), request.areBoundStatementsIdempotent(), request.getTimeoutForBoundStatements(), request.getPagingStateForBoundStatements(), request.getPageSizeForBoundStatements(), request.getConsistencyLevelForBoundStatements(), request.getSerialConsistencyLevelForBoundStatements(), request.areBoundStatementsTracing(), context.getCodecRegistry(), context.getProtocolVersion());
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions)

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