@Test public void scanOperator() throws Exception { ParallelFlux<Integer> source = Flux.range(1, 4).parallel(3); ParallelFlatMap<Integer, Integer> test = new ParallelFlatMap<>(source, i -> Flux.range(1, i), true, 12, Queues.small(), 123, Queues.small()); assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(source); assertThat(test.scan(Scannable.Attr.PREFETCH)) .isEqualTo(123) .isNotEqualTo(source.getPrefetch()); assertThat(test.scan(Scannable.Attr.DELAY_ERROR)).isTrue(); }
@Override @Nullable public Object scanUnsafe(Attr key) { if (key == Attr.PARENT) return source; if (key == Attr.PREFETCH) return getPrefetch(); if (key == Attr.DELAY_ERROR) return delayError; return null; }
@Override public void subscribe(CoreSubscriber<? super R>[] subscribers) { if (!validate(subscribers)) { return; } int n = subscribers.length; @SuppressWarnings("unchecked") CoreSubscriber<T>[] parents = new CoreSubscriber[n]; for (int i = 0; i < n; i++) { parents[i] = new FluxFlatMap.FlatMapMain<>(subscribers[i], mapper, delayError, maxConcurrency, mainQueueSupplier, prefetch, innerQueueSupplier); } source.subscribe(parents); } }
@Test public void parallelism() { ParallelFlux<Integer> source = Flux.range(1, 4).parallel(3); ParallelFlatMap<Integer, Integer> test = new ParallelFlatMap<>(source, i -> Flux.range(1, i), false, 12, Queues.small(), 123, Queues.small()); assertThat(test.parallelism()) .isEqualTo(3) .isEqualTo(source.parallelism()); }
/** * Generates and flattens Publishers on each 'rail', optionally delaying errors, * having a total number of simultaneous subscriptions to the inner Publishers and * using the given prefetch amount for the inner Publishers. * * @param <R> the result type * @param mapper the function to map each rail's value into a Publisher * @param delayError should the errors from the main and the inner sources delayed * till everybody terminates? * @param maxConcurrency the maximum number of simultaneous subscriptions to the * generated inner Publishers * @param prefetch the number of items to prefetch from each inner Publisher * * @return the new {@link ParallelFlux} instance */ public final <R> ParallelFlux<R> flatMap(Function<? super T, ? extends Publisher<? extends R>> mapper, boolean delayError, int maxConcurrency, int prefetch) { return onAssembly(new ParallelFlatMap<>(this, mapper, delayError, maxConcurrency, Queues.get(maxConcurrency), prefetch, Queues.get(prefetch))); }
/** * Generates and flattens Publishers on each 'rail', optionally delaying errors, * having a total number of simultaneous subscriptions to the inner Publishers and * using the given prefetch amount for the inner Publishers. * * @param <R> the result type * @param mapper the function to map each rail's value into a Publisher * @param delayError should the errors from the main and the inner sources delayed * till everybody terminates? * @param maxConcurrency the maximum number of simultaneous subscriptions to the * generated inner Publishers * @param prefetch the number of items to prefetch from each inner Publisher * * @return the new {@link ParallelFlux} instance */ public final <R> ParallelFlux<R> flatMap(Function<? super T, ? extends Publisher<? extends R>> mapper, boolean delayError, int maxConcurrency, int prefetch) { return onAssembly(new ParallelFlatMap<>(this, mapper, delayError, maxConcurrency, Queues.get(maxConcurrency), prefetch, Queues.get(prefetch))); }
@Override @Nullable public Object scanUnsafe(Attr key) { if (key == Attr.PARENT) return source; if (key == Attr.PREFETCH) return getPrefetch(); if (key == Attr.DELAY_ERROR) return delayError; return null; }
@Override public void subscribe(CoreSubscriber<? super R>[] subscribers) { if (!validate(subscribers)) { return; } int n = subscribers.length; @SuppressWarnings("unchecked") CoreSubscriber<T>[] parents = new CoreSubscriber[n]; for (int i = 0; i < n; i++) { parents[i] = new FluxFlatMap.FlatMapMain<>(subscribers[i], mapper, delayError, maxConcurrency, mainQueueSupplier, prefetch, innerQueueSupplier); } source.subscribe(parents); } }