Search in sources :

Example 1 with KillableCompletionStage

use of io.crate.concurrent.KillableCompletionStage in project crate by crate.

the class BatchPagingIteratorTest method testBatchPagingIteratorWithPagedSource.

@Test
public void testBatchPagingIteratorWithPagedSource() throws Exception {
    List<Object[]> expectedResult = StreamSupport.stream(RowGenerator.range(0, 10).spliterator(), false).map(Row::materialize).collect(Collectors.toList());
    Supplier<BatchIterator<Row>> batchIteratorSupplier = () -> {
        BatchSimulatingIterator<Row> source = new BatchSimulatingIterator<>(TestingBatchIterators.range(0, 10), 2, 5, executor);
        Function<Integer, KillableCompletionStage<? extends Iterable<? extends KeyIterable<Integer, Row>>>> fetchMore = exhausted -> {
            List<Row> rows = new ArrayList<>();
            while (source.moveNext()) {
                rows.add(new RowN(source.currentElement().materialize()));
            }
            if (source.allLoaded()) {
                return KillableCompletionStage.whenKilled(CompletableFuture.completedFuture(singletonList(new KeyIterable<>(1, rows))), t -> {
                });
            }
            // but to simulate multiple pages and fetchMore calls
            try {
                return KillableCompletionStage.whenKilled(source.loadNextBatch().toCompletableFuture().thenApply(ignored -> {
                    while (source.moveNext()) {
                        rows.add(new RowN(source.currentElement().materialize()));
                    }
                    return singleton(new KeyIterable<>(1, rows));
                }), t -> {
                });
            } catch (Exception e) {
                return KillableCompletionStage.failed(e);
            }
        };
        return new BatchPagingIterator<>(PassThroughPagingIterator.repeatable(), fetchMore, source::allLoaded, throwable -> {
            if (throwable == null) {
                source.close();
            } else {
                source.kill(throwable);
            }
        });
    };
    BatchIteratorTester tester = new BatchIteratorTester(batchIteratorSupplier);
    tester.verifyResultAndEdgeCaseBehaviour(expectedResult);
}
Also used : BatchIterator(io.crate.data.BatchIterator) BatchSimulatingIterator(io.crate.testing.BatchSimulatingIterator) CompletableFuture(java.util.concurrent.CompletableFuture) BatchIteratorTester(io.crate.testing.BatchIteratorTester) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) RowN(io.crate.data.RowN) Collections.singleton(java.util.Collections.singleton) After(org.junit.After) StreamSupport(java.util.stream.StreamSupport) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KillableCompletionStage(io.crate.concurrent.KillableCompletionStage) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) RowGenerator(io.crate.testing.RowGenerator) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TestingBatchIterators(io.crate.testing.TestingBatchIterators) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Row(io.crate.data.Row) ArrayList(java.util.ArrayList) BatchIterator(io.crate.data.BatchIterator) Function(java.util.function.Function) RowN(io.crate.data.RowN) BatchSimulatingIterator(io.crate.testing.BatchSimulatingIterator) BatchIteratorTester(io.crate.testing.BatchIteratorTester) Row(io.crate.data.Row) Test(org.junit.Test)

Aggregations

KillableCompletionStage (io.crate.concurrent.KillableCompletionStage)1 BatchIterator (io.crate.data.BatchIterator)1 Row (io.crate.data.Row)1 RowN (io.crate.data.RowN)1 BatchIteratorTester (io.crate.testing.BatchIteratorTester)1 BatchSimulatingIterator (io.crate.testing.BatchSimulatingIterator)1 RowGenerator (io.crate.testing.RowGenerator)1 TestingBatchIterators (io.crate.testing.TestingBatchIterators)1 ArrayList (java.util.ArrayList)1 Collections.singleton (java.util.Collections.singleton)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1