Search in sources :

Example 1 with CountingIterator

use of com.datastax.oss.driver.internal.core.util.CountingIterator in project java-driver by datastax.

the class ContinuousCqlRequestHandler method createResultSet.

@NonNull
@Override
protected DefaultContinuousAsyncResultSet createResultSet(@NonNull Statement<?> statement, @NonNull Rows rows, @NonNull ExecutionInfo executionInfo, @NonNull ColumnDefinitions columnDefinitions) {
    Queue<List<ByteBuffer>> data = rows.getData();
    CountingIterator<Row> iterator = new CountingIterator<Row>(data.size()) {

        @Override
        protected Row computeNext() {
            List<ByteBuffer> rowData = data.poll();
            return (rowData == null) ? endOfData() : new DefaultRow(columnDefinitions, rowData, context);
        }
    };
    DseRowsMetadata metadata = (DseRowsMetadata) rows.getMetadata();
    return new DefaultContinuousAsyncResultSet(iterator, columnDefinitions, metadata.continuousPageNumber, !metadata.isLastContinuousPage, executionInfo, this);
}
Also used : CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) List(java.util.List) Row(com.datastax.oss.driver.api.core.cql.Row) DefaultRow(com.datastax.oss.driver.internal.core.cql.DefaultRow) DseRowsMetadata(com.datastax.dse.protocol.internal.response.result.DseRowsMetadata) DefaultRow(com.datastax.oss.driver.internal.core.cql.DefaultRow) ByteBuffer(java.nio.ByteBuffer) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with CountingIterator

use of com.datastax.oss.driver.internal.core.util.CountingIterator in project java-driver by datastax.

the class GraphPagingIT method synchronous_paging_with_options_when_auto.

@UseDataProvider(location = ContinuousPagingITBase.class, value = "pagingOptions")
@Test
public void synchronous_paging_with_options_when_auto(Options options) {
    // given
    DriverExecutionProfile profile = enableGraphPaging(options, PagingEnabledOptions.AUTO);
    if (options.sizeInBytes) {
        // Page sizes in bytes are not supported with graph queries
        return;
    }
    // when
    GraphResultSet result = SESSION_RULE.session().execute(ScriptGraphStatement.newInstance("g.V().hasLabel('person').values('name')").setGraphName(SESSION_RULE.getGraphName()).setTraversalSource("g").setExecutionProfile(profile));
    // then
    List<GraphNode> nodes = result.all();
    assertThat(((CountingIterator) result.iterator()).remaining()).isZero();
    assertThat(nodes).hasSize(options.expectedRows);
    for (int i = 1; i <= nodes.size(); i++) {
        GraphNode node = nodes.get(i - 1);
        assertThat(node.asString()).isEqualTo("user" + i);
    }
    assertThat(result.getRequestExecutionInfo()).isNotNull();
    assertThat(result.getRequestExecutionInfo().getCoordinator().getEndPoint().resolve()).isEqualTo(firstCcmNode());
    assertIfMultiPage(result, options.expectedPages);
    validateMetrics(SESSION_RULE.session());
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) MultiPageGraphResultSet(com.datastax.dse.driver.internal.core.graph.MultiPageGraphResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 3 with CountingIterator

use of com.datastax.oss.driver.internal.core.util.CountingIterator in project java-driver by datastax.

the class GraphResultSetTestBase method mockPage.

/**
 * Mocks an async result set where column 0 has type INT, with rows with the provided data.
 */
protected AsyncGraphResultSet mockPage(boolean nextPage, Integer... data) {
    AsyncGraphResultSet page = mock(AsyncGraphResultSet.class);
    ExecutionInfo executionInfo = mock(ExecutionInfo.class);
    when(page.getRequestExecutionInfo()).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<GraphNode> iterator = new CountingIterator<GraphNode>(queue.size()) {

        @Override
        protected GraphNode 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 : CompletableFuture(java.util.concurrent.CompletableFuture) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode)

Example 4 with CountingIterator

use of com.datastax.oss.driver.internal.core.util.CountingIterator 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 5 with CountingIterator

use of com.datastax.oss.driver.internal.core.util.CountingIterator in project java-driver by datastax.

the class GraphPagingIT method synchronous_paging_with_options.

@UseDataProvider(location = ContinuousPagingITBase.class, value = "pagingOptions")
@Test
public void synchronous_paging_with_options(Options options) {
    // given
    DriverExecutionProfile profile = enableGraphPaging(options, PagingEnabledOptions.ENABLED);
    if (options.sizeInBytes) {
        // Page sizes in bytes are not supported with graph queries
        return;
    }
    // when
    GraphResultSet result = SESSION_RULE.session().execute(ScriptGraphStatement.newInstance("g.V().hasLabel('person').values('name')").setGraphName(SESSION_RULE.getGraphName()).setTraversalSource("g").setExecutionProfile(profile));
    // then
    List<GraphNode> nodes = result.all();
    assertThat(((CountingIterator) result.iterator()).remaining()).isZero();
    assertThat(nodes).hasSize(options.expectedRows);
    for (int i = 1; i <= nodes.size(); i++) {
        GraphNode node = nodes.get(i - 1);
        assertThat(node.asString()).isEqualTo("user" + i);
    }
    assertThat(result.getRequestExecutionInfo()).isNotNull();
    assertThat(result.getRequestExecutionInfo().getCoordinator().getEndPoint().resolve()).isEqualTo(firstCcmNode());
    assertIfMultiPage(result, options.expectedPages);
    validateMetrics(SESSION_RULE.session());
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) MultiPageGraphResultSet(com.datastax.dse.driver.internal.core.graph.MultiPageGraphResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

CountingIterator (com.datastax.oss.driver.internal.core.util.CountingIterator)8 MultiPageGraphResultSet (com.datastax.dse.driver.internal.core.graph.MultiPageGraphResultSet)4 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)4 Test (org.junit.Test)4 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 ColumnDefinitions (com.datastax.oss.driver.api.core.cql.ColumnDefinitions)2 ContinuousAsyncResultSet (com.datastax.dse.driver.api.core.cql.continuous.ContinuousAsyncResultSet)1 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)1 GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)1 DseRowsMetadata (com.datastax.dse.protocol.internal.response.result.DseRowsMetadata)1 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)1 DefaultRow (com.datastax.oss.driver.internal.core.cql.DefaultRow)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1