use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.
the class ReactiveResultSetSubscriptionTest method should_report_error_on_first_page.
@Test
public void should_report_error_on_first_page() {
CompletableFuture<AsyncResultSet> future1 = new CompletableFuture<>();
TestSubscriber<ReactiveRow> mainSubscriber = new TestSubscriber<>();
TestSubscriber<ColumnDefinitions> colDefsSubscriber = new TestSubscriber<>();
TestSubscriber<ExecutionInfo> execInfosSubscriber = new TestSubscriber<>();
TestSubscriber<Boolean> wasAppliedSubscriber = new TestSubscriber<>();
ReactiveResultSetSubscription<AsyncResultSet> subscription = new ReactiveResultSetSubscription<>(mainSubscriber, colDefsSubscriber, execInfosSubscriber, wasAppliedSubscriber);
mainSubscriber.onSubscribe(subscription);
subscription.start(() -> future1);
future1.completeExceptionally(new UnavailableException(null, null, 0, 0));
mainSubscriber.awaitTermination();
assertThat(mainSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
assertThat(colDefsSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
assertThat(execInfosSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
assertThat(wasAppliedSubscriber.getError()).isNotNull().isInstanceOf(UnavailableException.class);
}
use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.
the class RequestLogFormatter method appendValues.
/**
* @return the number of values that can still be appended after this, or -1 if the max was
* reached by this call.
*/
protected int appendValues(Request request, int maxValues, int maxValueLength, boolean addSeparator, StringBuilder builder) {
if (request instanceof BatchStatement) {
BatchStatement batch = (BatchStatement) request;
for (BatchableStatement<?> child : batch) {
maxValues = appendValues(child, maxValues, maxValueLength, addSeparator, builder);
if (addSeparator) {
addSeparator = false;
}
if (maxValues < 0) {
return -1;
}
}
} else if (request instanceof BoundStatement) {
BoundStatement statement = (BoundStatement) request;
ColumnDefinitions definitions = statement.getPreparedStatement().getVariableDefinitions();
List<ByteBuffer> values = statement.getValues();
assert definitions.size() == values.size();
if (definitions.size() > 0) {
if (addSeparator) {
builder.append(' ');
}
builder.append('[');
for (int i = 0; i < definitions.size(); i++) {
if (i > 0) {
builder.append(", ");
}
maxValues -= 1;
if (maxValues < 0) {
builder.append(FURTHER_VALUES_TRUNCATED);
return -1;
}
builder.append(definitions.get(i).getName().asCql(true)).append('=');
if (!statement.isSet(i)) {
builder.append("<UNSET>");
} else {
ByteBuffer value = values.get(i);
DataType type = definitions.get(i).getType();
appendValue(value, type, maxValueLength, builder);
}
}
builder.append(']');
}
} else if (request instanceof SimpleStatement) {
SimpleStatement statement = (SimpleStatement) request;
if (!statement.getPositionalValues().isEmpty()) {
if (addSeparator) {
builder.append(' ');
}
builder.append('[');
int i = 0;
for (Object value : statement.getPositionalValues()) {
if (i > 0) {
builder.append(", ");
}
maxValues -= 1;
if (maxValues < 0) {
builder.append(FURTHER_VALUES_TRUNCATED);
return -1;
}
builder.append('v').append(i).append('=');
appendValue(value, maxValueLength, builder);
i += 1;
}
builder.append(']');
} else if (!statement.getNamedValues().isEmpty()) {
if (addSeparator) {
builder.append(' ');
}
builder.append('[');
int i = 0;
for (Map.Entry<CqlIdentifier, Object> entry : statement.getNamedValues().entrySet()) {
if (i > 0) {
builder.append(", ");
}
maxValues -= 1;
if (maxValues < 0) {
builder.append(FURTHER_VALUES_TRUNCATED);
return -1;
}
builder.append(entry.getKey().asCql(true)).append('=');
appendValue(entry.getValue(), maxValueLength, builder);
i += 1;
}
builder.append(']');
}
}
return maxValues;
}
use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.
the class QueryTraceFetcherTest method sessionRow.
private CompletionStage<AsyncResultSet> sessionRow(Integer duration) {
Row row = mock(Row.class);
ColumnDefinitions definitions = mock(ColumnDefinitions.class);
when(row.getColumnDefinitions()).thenReturn(definitions);
when(row.getString("request")).thenReturn("mock request");
if (duration == null) {
when(row.isNull("duration")).thenReturn(true);
} else {
when(row.getInt("duration")).thenReturn(duration);
}
when(row.getInetAddress("coordinator")).thenReturn(address);
when(definitions.contains("coordinator_port")).thenReturn(true);
when(row.getInt("coordinator_port")).thenReturn(PORT);
when(row.getMap("parameters", String.class, String.class)).thenReturn(ImmutableMap.of("key1", "value1", "key2", "value2"));
when(row.isNull("started_at")).thenReturn(false);
when(row.getInstant("started_at")).thenReturn(Instant.EPOCH);
AsyncResultSet rs = mock(AsyncResultSet.class);
when(rs.one()).thenReturn(row);
return CompletableFuture.completedFuture(rs);
}
use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.
the class ResultSetTestBase method mockPage.
/**
* Mocks an async result set where column 0 has type INT, with rows with the provided data.
*/
protected AsyncResultSet mockPage(boolean nextPage, Integer... data) {
AsyncResultSet page = mock(AsyncResultSet.class);
ColumnDefinitions columnDefinitions = mock(ColumnDefinitions.class);
when(page.getColumnDefinitions()).thenReturn(columnDefinitions);
ExecutionInfo executionInfo = mock(ExecutionInfo.class);
when(page.getExecutionInfo()).thenReturn(executionInfo);
if (nextPage) {
when(page.hasMorePages()).thenReturn(true);
when(page.fetchNextPage()).thenReturn(spy(new CompletableFuture<>()));
} else {
when(page.hasMorePages()).thenReturn(false);
when(page.fetchNextPage()).thenThrow(new IllegalStateException());
}
// Emulate DefaultAsyncResultSet's internals (this is a bit sketchy, maybe it would be better
// to use real DefaultAsyncResultSet instances)
Queue<Integer> queue = Lists.newLinkedList(Arrays.asList(data));
CountingIterator<Row> iterator = new CountingIterator<Row>(queue.size()) {
@Override
protected Row computeNext() {
Integer index = queue.poll();
return (index == null) ? endOfData() : mockRow(index);
}
};
when(page.currentPage()).thenReturn(() -> iterator);
when(page.remaining()).thenAnswer(invocation -> iterator.remaining());
return page;
}
use of com.datastax.oss.driver.api.core.cql.ColumnDefinitions in project java-driver by datastax.
the class StatementSizeTest method setup.
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ByteBuffer preparedId = ByteBuffer.wrap(PREPARED_ID);
when(preparedStatement.getId()).thenReturn(preparedId);
ByteBuffer resultMetadataId = ByteBuffer.wrap(RESULT_METADATA_ID);
when(preparedStatement.getResultMetadataId()).thenReturn(resultMetadataId);
ColumnDefinitions columnDefinitions = DefaultColumnDefinitions.valueOf(ImmutableList.of(phonyColumnDef("ks", "table", "c1", -1, ProtocolConstants.DataType.INT), phonyColumnDef("ks", "table", "c2", -1, ProtocolConstants.DataType.VARCHAR)));
when(preparedStatement.getVariableDefinitions()).thenReturn(columnDefinitions);
when(driverContext.getProtocolVersion()).thenReturn(DefaultProtocolVersion.V5);
when(driverContext.getCodecRegistry()).thenReturn(CodecRegistry.DEFAULT);
when(driverContext.getProtocolVersionRegistry()).thenReturn(new DefaultProtocolVersionRegistry(null));
when(config.getDefaultProfile()).thenReturn(defaultProfile);
when(driverContext.getConfig()).thenReturn(config);
when(driverContext.getTimestampGenerator()).thenReturn(timestampGenerator);
}
Aggregations