Search in sources :

Example 6 with ColumnDefinitions

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);
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 7 with ColumnDefinitions

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;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) DataType(com.datastax.oss.driver.api.core.type.DataType) List(java.util.List) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) ByteBuffer(java.nio.ByteBuffer)

Example 8 with ColumnDefinitions

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);
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 9 with ColumnDefinitions

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;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 10 with ColumnDefinitions

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);
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) DefaultProtocolVersionRegistry(com.datastax.oss.driver.internal.core.DefaultProtocolVersionRegistry) ByteBuffer(java.nio.ByteBuffer) Before(org.junit.Before)

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