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);
}
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;
}
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();
}
}
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;
}
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());
}
Aggregations