/** * Sorts the 'rails' according to the comparator and returns a full sorted list as a * Publisher. * <p> * This operator requires a finite source ParallelFlux. * * @param comparator the comparator to compare elements * @param capacityHint the expected number of total elements * * @return the new Mono instance */ public final Mono<List<T>> collectSortedList(Comparator<? super T> comparator, int capacityHint) { int ch = capacityHint / parallelism() + 1; ParallelFlux<List<T>> railReduced = reduce(() -> new ArrayList<>(ch), (a, b) -> { a.add(b); return a; }); ParallelFlux<List<T>> railSorted = railReduced.map(list -> { list.sort(comparator); return list; }); Mono<List<T>> merged = railSorted.reduce((a, b) -> sortedMerger(a, b, comparator)); return merged; }
/** * Sorts the 'rails' of this {@link ParallelFlux} and returns a Publisher that * sequentially picks the smallest next value from the rails. * <p> * This operator requires a finite source ParallelFlux. * * @param comparator the comparator to use * @param capacityHint the expected number of total elements * * @return the new Flux instance */ public final Flux<T> sorted(Comparator<? super T> comparator, int capacityHint) { int ch = capacityHint / parallelism() + 1; ParallelFlux<List<T>> railReduced = reduce(() -> new ArrayList<>(ch), (a, b) -> { a.add(b); return a; }); ParallelFlux<List<T>> railSorted = railReduced.map(list -> { list.sort(comparator); return list; }); return Flux.onAssembly(new ParallelMergeSort<>(railSorted, comparator)); }
@Override protected List<Scenario<String, String>> scenarios_operatorSuccess() { return Arrays.asList( scenario(f -> f.reduce(() -> item(0), (a, b) -> a)) ); }
@Test public void testPrefetch() { assertThat(Flux.range(1, 10) .parallel(3) .reduce(() -> 0, (a, b) -> a + b) .getPrefetch()).isEqualTo(Integer.MAX_VALUE); }
@Test public void failCombination() { StepVerifier.create(Flux.range(1, 10) .parallel(3) .reduce(() -> 0, (a, b) -> { throw new RuntimeException("test"); })) .verifyErrorMessage("test"); }
@Test public void failInitial() { Supplier<Integer> as = () -> { throw new RuntimeException("test"); }; StepVerifier.create(Flux.range(1, 10) .parallel(3) .reduce(as, (a, b) -> b + a)) .verifyErrorMessage("test"); }
@Test public void reduceFull() { for (int i = 1; i <= Runtime.getRuntime() .availableProcessors() * 2; i++) { AssertSubscriber<Integer> ts = AssertSubscriber.create(); Flux.range(1, 10) .parallel(i) .reduce((a, b) -> a + b) .subscribe(ts); ts.assertValues(55); } }
/** * Sorts the 'rails' according to the comparator and returns a full sorted list as a * Publisher. * <p> * This operator requires a finite source ParallelFlux. * * @param comparator the comparator to compare elements * @param capacityHint the expected number of total elements * * @return the new Mono instance */ public final Mono<List<T>> collectSortedList(Comparator<? super T> comparator, int capacityHint) { int ch = capacityHint / parallelism() + 1; ParallelFlux<List<T>> railReduced = reduce(() -> new ArrayList<>(ch), (a, b) -> { a.add(b); return a; }); ParallelFlux<List<T>> railSorted = railReduced.map(list -> { list.sort(comparator); return list; }); Mono<List<T>> merged = railSorted.reduce((a, b) -> sortedMerger(a, b, comparator)); return merged; }
/** * Sorts the 'rails' of this {@link ParallelFlux} and returns a Publisher that * sequentially picks the smallest next value from the rails. * <p> * This operator requires a finite source ParallelFlux. * * @param comparator the comparator to use * @param capacityHint the expected number of total elements * * @return the new Flux instance */ public final Flux<T> sorted(Comparator<? super T> comparator, int capacityHint) { int ch = capacityHint / parallelism() + 1; ParallelFlux<List<T>> railReduced = reduce(() -> new ArrayList<>(ch), (a, b) -> { a.add(b); return a; }); ParallelFlux<List<T>> railSorted = railReduced.map(list -> { list.sort(comparator); return list; }); return Flux.onAssembly(new ParallelMergeSort<>(railSorted, comparator)); }