| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 27 |
import io.reactivex.rxjava3.internal.util.*; |
| 28 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 29 |
|
| 30 |
/** |
| 31 |
* Maps upstream values into SingleSources and merges their signals into one sequence. |
| 32 |
* @param <T> the source value type |
| 33 |
* @param <R> the result value type |
| 34 |
*/ |
| 35 |
public final class FlowableFlatMapSingle<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 36 |
|
| 37 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 38 |
|
| 39 |
final boolean delayErrors; |
| 40 |
|
| 41 |
final int maxConcurrency; |
| 42 |
|
| 43 |
public FlowableFlatMapSingle(Flowable<T> source, Function<? super T, ? extends SingleSource<? extends R>> mapper, |
| 44 |
boolean delayError, int maxConcurrency) { |
| 45 |
super(source); |
| 46 |
this.mapper = mapper; |
| 47 |
this.delayErrors = delayError; |
| 48 |
this.maxConcurrency = maxConcurrency; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 53 |
source.subscribe(new FlatMapSingleSubscriber<>(s, mapper, delayErrors, maxConcurrency)); |
| 54 |
} |
| 55 |
|
| 56 |
static final class FlatMapSingleSubscriber<T, R> |
| 57 |
extends AtomicInteger |
| 58 |
implements FlowableSubscriber<T>, Subscription { |
| 59 |
|
| 60 |
private static final long serialVersionUID = 8600231336733376951L; |
| 61 |
|
| 62 |
final Subscriber<? super R> downstream; |
| 63 |
|
| 64 |
final boolean delayErrors; |
| 65 |
|
| 66 |
final int maxConcurrency; |
| 67 |
|
| 68 |
final AtomicLong requested; |
| 69 |
|
| 70 |
final CompositeDisposable set; |
| 71 |
|
| 72 |
final AtomicInteger active; |
| 73 |
|
| 74 |
final AtomicThrowable errors; |
| 75 |
|
| 76 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 77 |
|
| 78 |
final AtomicReference<SpscLinkedArrayQueue<R>> queue; |
| 79 |
|
| 80 |
Subscription upstream; |
| 81 |
|
| 82 |
volatile boolean cancelled; |
| 83 |
|
| 84 |
FlatMapSingleSubscriber(Subscriber<? super R> actual, |
| 85 |
Function<? super T, ? extends SingleSource<? extends R>> mapper, boolean delayErrors, int maxConcurrency) { |
| 86 |
this.downstream = actual; |
| 87 |
this.mapper = mapper; |
| 88 |
this.delayErrors = delayErrors; |
| 89 |
this.maxConcurrency = maxConcurrency; |
| 90 |
this.requested = new AtomicLong(); |
| 91 |
this.set = new CompositeDisposable(); |
| 92 |
this.errors = new AtomicThrowable(); |
| 93 |
this.active = new AtomicInteger(1); |
| 94 |
this.queue = new AtomicReference<>(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onSubscribe(Subscription s) { |
| 99 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 100 |
this.upstream = s; |
| 101 |
|
| 102 |
downstream.onSubscribe(this); |
| 103 |
|
| 104 |
int m = maxConcurrency; |
| 105 |
if (m == Integer.MAX_VALUE) { |
| 106 |
s.request(Long.MAX_VALUE); |
| 107 |
} else { |
| 108 |
s.request(maxConcurrency); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void onNext(T t) { |
| 115 |
SingleSource<? extends R> ms; |
| 116 |
|
| 117 |
try { |
| 118 |
ms = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null SingleSource"); |
| 119 |
} catch (Throwable ex) { |
| 120 |
Exceptions.throwIfFatal(ex); |
| 121 |
upstream.cancel(); |
| 122 |
onError(ex); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
active.getAndIncrement(); |
| 127 |
|
| 128 |
InnerObserver inner = new InnerObserver(); |
| 129 |
|
| 130 |
if (!cancelled && set.add(inner)) { |
| 131 |
ms.subscribe(inner); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void onError(Throwable t) { |
| 137 |
active.decrementAndGet(); |
| 138 |
if (errors.tryAddThrowableOrReport(t)) { |
| 139 |
if (!delayErrors) { |
| 140 |
set.dispose(); |
| 141 |
} |
| 142 |
drain(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void onComplete() { |
| 148 |
active.decrementAndGet(); |
| 149 |
drain(); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void cancel() { |
| 154 |
cancelled = true; |
| 155 |
upstream.cancel(); |
| 156 |
set.dispose(); |
| 157 |
errors.tryTerminateAndReport(); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void request(long n) { |
| 162 |
if (SubscriptionHelper.validate(n)) { |
| 163 |
BackpressureHelper.add(requested, n); |
| 164 |
drain(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void innerSuccess(InnerObserver inner, R value) { |
| 169 |
set.delete(inner); |
| 170 |
if (get() == 0 && compareAndSet(0, 1)) { |
| 171 |
boolean d = active.decrementAndGet() == 0; |
| 172 |
if (requested.get() != 0) { |
| 173 |
downstream.onNext(value); |
| 174 |
|
| 175 |
SpscLinkedArrayQueue<R> q = queue.get(); |
| 176 |
|
| 177 |
if (d && (q == null || q.isEmpty())) { |
| 178 |
errors.tryTerminateConsumer(downstream); |
| 179 |
return; |
| 180 |
} |
| 181 |
BackpressureHelper.produced(requested, 1); |
| 182 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 183 |
upstream.request(1); |
| 184 |
} |
| 185 |
} else { |
| 186 |
SpscLinkedArrayQueue<R> q = getOrCreateQueue(); |
| 187 |
synchronized (q) { |
| 188 |
q.offer(value); |
| 189 |
} |
| 190 |
} |
| 191 |
if (decrementAndGet() == 0) { |
| 192 |
return; |
| 193 |
} |
| 194 |
} else { |
| 195 |
SpscLinkedArrayQueue<R> q = getOrCreateQueue(); |
| 196 |
synchronized (q) { |
| 197 |
q.offer(value); |
| 198 |
} |
| 199 |
active.decrementAndGet(); |
| 200 |
if (getAndIncrement() != 0) { |
| 201 |
return; |
| 202 |
} |
| 203 |
} |
| 204 |
drainLoop(); |
| 205 |
} |
| 206 |
|
| 207 |
SpscLinkedArrayQueue<R> getOrCreateQueue() { |
| 208 |
SpscLinkedArrayQueue<R> current = queue.get(); |
| 209 |
if (current != null) { |
| 210 |
return current; |
| 211 |
} |
| 212 |
current = new SpscLinkedArrayQueue<>(Flowable.bufferSize()); |
| 213 |
if (queue.compareAndSet(null, current)) { |
| 214 |
return current; |
| 215 |
} |
| 216 |
return queue.get(); |
| 217 |
} |
| 218 |
|
| 219 |
void innerError(InnerObserver inner, Throwable e) { |
| 220 |
set.delete(inner); |
| 221 |
if (errors.tryAddThrowableOrReport(e)) { |
| 222 |
if (!delayErrors) { |
| 223 |
upstream.cancel(); |
| 224 |
set.dispose(); |
| 225 |
} else { |
| 226 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 227 |
upstream.request(1); |
| 228 |
} |
| 229 |
} |
| 230 |
active.decrementAndGet(); |
| 231 |
drain(); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
void drain() { |
| 236 |
if (getAndIncrement() == 0) { |
| 237 |
drainLoop(); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void clear() { |
| 242 |
SpscLinkedArrayQueue<R> q = queue.get(); |
| 243 |
if (q != null) { |
| 244 |
q.clear(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void drainLoop() { |
| 249 |
int missed = 1; |
| 250 |
Subscriber<? super R> a = downstream; |
| 251 |
AtomicInteger n = active; |
| 252 |
AtomicReference<SpscLinkedArrayQueue<R>> qr = queue; |
| 253 |
|
| 254 |
for (;;) { |
| 255 |
long r = requested.get(); |
| 256 |
long e = 0L; |
| 257 |
|
| 258 |
while (e != r) { |
| 259 |
if (cancelled) { |
| 260 |
clear(); |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if (!delayErrors) { |
| 265 |
Throwable ex = errors.get(); |
| 266 |
if (ex != null) { |
| 267 |
clear(); |
| 268 |
errors.tryTerminateConsumer(downstream); |
| 269 |
return; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
boolean d = n.get() == 0; |
| 274 |
SpscLinkedArrayQueue<R> q = qr.get(); |
| 275 |
R v = q != null ? q.poll() : null; |
| 276 |
boolean empty = v == null; |
| 277 |
|
| 278 |
if (d && empty) { |
| 279 |
errors.tryTerminateConsumer(a); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
if (empty) { |
| 284 |
break; |
| 285 |
} |
| 286 |
|
| 287 |
a.onNext(v); |
| 288 |
|
| 289 |
e++; |
| 290 |
} |
| 291 |
|
| 292 |
if (e == r) { |
| 293 |
if (cancelled) { |
| 294 |
clear(); |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
if (!delayErrors) { |
| 299 |
Throwable ex = errors.get(); |
| 300 |
if (ex != null) { |
| 301 |
clear(); |
| 302 |
errors.tryTerminateConsumer(a); |
| 303 |
return; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
boolean d = n.get() == 0; |
| 308 |
SpscLinkedArrayQueue<R> q = qr.get(); |
| 309 |
boolean empty = q == null || q.isEmpty(); |
| 310 |
|
| 311 |
if (d && empty) { |
| 312 |
errors.tryTerminateConsumer(a); |
| 313 |
return; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if (e != 0L) { |
| 318 |
BackpressureHelper.produced(requested, e); |
| 319 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 320 |
upstream.request(e); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
missed = addAndGet(-missed); |
| 325 |
if (missed == 0) { |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
final class InnerObserver extends AtomicReference<Disposable> |
| 332 |
implements SingleObserver<R>, Disposable { |
| 333 |
private static final long serialVersionUID = -502562646270949838L; |
| 334 |
|
| 335 |
@Override |
| 336 |
public void onSubscribe(Disposable d) { |
| 337 |
DisposableHelper.setOnce(this, d); |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public void onSuccess(R value) { |
| 342 |
innerSuccess(this, value); |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public void onError(Throwable e) { |
| 347 |
innerError(this, e); |
| 348 |
} |
| 349 |
|
| 350 |
@Override |
| 351 |
public boolean isDisposed() { |
| 352 |
return DisposableHelper.isDisposed(get()); |
| 353 |
} |
| 354 |
|
| 355 |
@Override |
| 356 |
public void dispose() { |
| 357 |
DisposableHelper.dispose(this); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |