Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Publisher#merge(...) operators #2533

Merged
merged 4 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static io.servicetalk.concurrent.internal.SubscriberUtils.deliverErrorFromSource;
import static io.servicetalk.utils.internal.DurationUtils.toNanos;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;

/**
* An asynchronous computation that produces 0, 1 or more elements and may or may not terminate successfully or with
Expand Down Expand Up @@ -1521,6 +1522,78 @@ public final <R> Publisher<R> flatMapConcatIterable(Function<? super T, ? extend
return new PublisherConcatMapIterable<>(this, mapper);
}

/**
* Merge two {@link Publisher}s together. There is no guaranteed ordering of events emitted from the returned
* {@link Publisher}.
* <p>
* This method provides similar capabilities as expanding each result into a collection and concatenating each
* collection in sequential programming:
* <pre>{@code
* List<T> mergedResults = ...; // concurrent safe list
* for (T t : resultOfThisPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (T t : resultOfOtherPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (Future<R> future : futures) {
* future.get(); // Throws if the processing for this item failed.
* }
* return mergedResults;
* }</pre>
* @param other The {@link Publisher} to merge with.
* @return A {@link Publisher} which is the result of this {@link Publisher} and {@code other} merged together.
* @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX merge operator</a>
* @see #mergeDelayError(Publisher)
* @see #mergeAll(Publisher[])
*/
public final Publisher<T> merge(Publisher<? extends T> other) {
return from(this, other).flatMapMerge(identity(), 2);
}

/**
* Merge two {@link Publisher}s together. There is no guaranteed ordering of events emitted from the returned
* {@link Publisher}. If either {@link Publisher} fails the error propagation will be delayed until both terminate.
* <p>
* This method provides similar capabilities as expanding each result into a collection and concatenating each
* collection in sequential programming:
* <pre>{@code
* List<T> mergedResults = ...; // concurrent safe list
* for (T t : resultOfThisPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (T t : resultOfOtherPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* List<Throwable> errors = ...;
* for (Future<R> future : futures) {
* try {
* future.get(); // Throws if the processing for this item failed.
* } catch (Throwable cause) {
* errors.add(cause);
* }
* }
* throwExceptionIfNotEmpty(errors);
* return mergedResults;
* }</pre>
* @param other The {@link Publisher} to merge with,
* @return A {@link Publisher} which is the result of this {@link Publisher} and {@code other} merged together.
* @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX merge operator</a>
* @see #merge(Publisher)
* @see #mergeAllDelayError(Publisher[])
*/
public final Publisher<T> mergeDelayError(Publisher<? extends T> other) {
return from(this, other).flatMapMergeDelayError(identity(), 2, 2);
}

/**
* Invokes the {@code onSubscribe} {@link Consumer} argument when
* {@link Subscriber#onSubscribe(PublisherSource.Subscription)} is called for {@link Subscriber}s of the returned
Expand Down Expand Up @@ -4014,6 +4087,162 @@ public static <T> Publisher<T> defer(Supplier<? extends Publisher<? extends T>>
return new PublisherDefer<>(publisherSupplier);
}

/**
* Merge all {@link Publisher}s together. There is no guaranteed ordering of events emitted from the returned
* {@link Publisher}.
* <p>
* This method provides similar capabilities as expanding each result into a collection and concatenating each
* collection in sequential programming:
* <pre>{@code
* List<T> mergedResults = ...; // concurrent safe list
* for (T t : resultOfPublisher1()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (T t : resultOfOtherPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (Future<R> future : futures) {
* future.get(); // Throws if the processing for this item failed.
* }
* return mergedResults;
* }</pre>
* @param publishers The {@link Publisher}s to merge together.
* @param <T> Type of items emitted by the returned {@link Publisher}.
* @return A {@link Publisher} which is the result of this {@link Publisher} and {@code other} merged together.
* @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX merge operator</a>
* @see #mergeAll(int, Publisher[])
* @see #mergeAllDelayError(Publisher[])
*/
@SafeVarargs
public static <T> Publisher<T> mergeAll(Publisher<? extends T>... publishers) {
return from(publishers).flatMapMerge(identity());
}

/**
* Merge all {@link Publisher}s together. There is no guaranteed ordering of events emitted from the returned
* {@link Publisher}.
* <p>
* This method provides similar capabilities as expanding each result into a collection and concatenating each
* collection in sequential programming:
* <pre>{@code
* List<T> mergedResults = ...; // concurrent safe list
* for (T t : resultOfPublisher1()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (T t : resultOfOtherPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (Future<R> future : futures) {
* future.get(); // Throws if the processing for this item failed.
* }
* return mergedResults;
* }</pre>
* @param maxConcurrency The maximum amount of {@link Publisher}s from {@code publishers} to subscribe to
* concurrently.
* @param publishers The {@link Publisher}s to merge together.
* @param <T> Type of items emitted by the returned {@link Publisher}.
* @return A {@link Publisher} which is the result of this {@link Publisher} and {@code other} merged together.
* @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX merge operator</a>
* @see #mergeAllDelayError(int, Publisher[])
*/
@SafeVarargs
public static <T> Publisher<T> mergeAll(int maxConcurrency, Publisher<? extends T>... publishers) {
return from(publishers).flatMapMerge(identity(), maxConcurrency);
}

/**
* Merge all {@link Publisher}s together. There is no guaranteed ordering of events emitted from the returned
* {@link Publisher}. If any {@link Publisher} terminates in an error, the error propagation will be delayed until
* all terminate.
* <p>
* This method provides similar capabilities as expanding each result into a collection and concatenating each
* collection in sequential programming:
* <pre>{@code
* List<T> mergedResults = ...; // concurrent safe list
* for (T t : resultOfPublisher1()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (T t : resultOfOtherPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* List<Throwable> errors = ...;
* for (Future<R> future : futures) {
* try {
* future.get(); // Throws if the processing for this item failed.
* } catch (Throwable cause) {
* errors.add(cause);
* }
* }
* throwExceptionIfNotEmpty(errors);
* return mergedResults;
* }</pre>
* @param publishers The {@link Publisher}s to merge together.
* @param <T> Type of items emitted by the returned {@link Publisher}.
* @return A {@link Publisher} which is the result of this {@link Publisher} and {@code other} merged together.
* @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX merge operator</a>
* @see #mergeAllDelayError(int, Publisher[])
* @see #mergeAll(Publisher[])
*/
@SafeVarargs
public static <T> Publisher<T> mergeAllDelayError(Publisher<? extends T>... publishers) {
return from(publishers).flatMapMergeDelayError(identity());
}

/**
* Merge all {@link Publisher}s together. There is no guaranteed ordering of events emitted from the returned
* {@link Publisher}. If any {@link Publisher} terminates in an error, the error propagation will be delayed until
* all terminate.
* <p>
* This method provides similar capabilities as expanding each result into a collection and concatenating each
* collection in sequential programming:
* <pre>{@code
* List<T> mergedResults = ...; // concurrent safe list
* for (T t : resultOfPublisher1()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* for (T t : resultOfOtherPublisher()) {
* futures.add(e.submit(() -> {
* return mergedResults.add(t);
* }));
* }
* List<Throwable> errors = ...;
* for (Future<R> future : futures) {
* try {
* future.get(); // Throws if the processing for this item failed.
* } catch (Throwable cause) {
* errors.add(cause);
* }
* }
* throwExceptionIfNotEmpty(errors);
* return mergedResults;
* }</pre>
* @param maxConcurrency The maximum amount of {@link Publisher}s from {@code publishers} to subscribe to
* concurrently.
* @param publishers The {@link Publisher}s to merge together.
* @param <T> Type of items emitted by the returned {@link Publisher}.
* @return A {@link Publisher} which is the result of this {@link Publisher} and {@code other} merged together.
* @see <a href="https://reactivex.io/documentation/operators/merge.html">ReactiveX merge operator</a>
* @see #mergeAll(Publisher[])
*/
@SafeVarargs
public static <T> Publisher<T> mergeAllDelayError(int maxConcurrency, Publisher<? extends T>... publishers) {
return from(publishers).flatMapMergeDelayError(identity(), maxConcurrency);
}

//
// Static Utility Methods End
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static io.servicetalk.concurrent.internal.ConcurrentUtils.tryAcquireLock;
import static io.servicetalk.concurrent.internal.SubscriberUtils.checkDuplicateSubscription;
import static io.servicetalk.concurrent.internal.SubscriberUtils.isRequestNValid;
import static io.servicetalk.concurrent.internal.SubscriberUtils.newExceptionForInvalidRequestN;
import static io.servicetalk.concurrent.internal.TerminalNotification.complete;
import static io.servicetalk.concurrent.internal.TerminalNotification.error;
import static io.servicetalk.utils.internal.PlatformDependent.newUnboundedMpscQueue;
Expand Down Expand Up @@ -175,6 +176,10 @@ public void request(final long n) {
incMappedDemand(n);
} else {
subscription.request(n);
// If the upstream source has already sent an onComplete signal, it won't be able to send an error.
// We propagate invalid demand upstream to clean-up upstream (if necessary) and force an error here to
// ensure we see an error.
enqueueAndDrain(error(newExceptionForInvalidRequestN(n)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static io.servicetalk.concurrent.internal.ConcurrentUtils.tryAcquireLock;
import static io.servicetalk.concurrent.internal.SubscriberUtils.checkDuplicateSubscription;
import static io.servicetalk.concurrent.internal.SubscriberUtils.isRequestNValid;
import static io.servicetalk.concurrent.internal.SubscriberUtils.newExceptionForInvalidRequestN;
import static io.servicetalk.concurrent.internal.TerminalNotification.complete;
import static io.servicetalk.concurrent.internal.TerminalNotification.error;
import static io.servicetalk.utils.internal.PlatformDependent.newUnboundedMpscQueue;
Expand Down Expand Up @@ -161,6 +162,10 @@ public void request(long n) {
}
} else {
subscription.request(n);
// If the upstream source has already sent an onComplete signal, it won't be able to send an error.
// We propagate invalid demand upstream to clean-up upstream (if necessary) and force an error here to
// ensure we see an error.
enqueueAndDrain(error(newExceptionForInvalidRequestN(n)));
}
}

Expand Down
Loading