| 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.AtomicInteger; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.functions.*; |
| 24 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableConcatMap.*; |
| 25 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 26 |
import io.reactivex.rxjava3.internal.util.*; |
| 27 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 28 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 29 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 30 |
|
| 31 |
public final class FlowableConcatMapScheduler<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 32 |
|
| 33 |
final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 34 |
|
| 35 |
final int prefetch; |
| 36 |
|
| 37 |
final ErrorMode errorMode; |
| 38 |
|
| 39 |
final Scheduler scheduler; |
| 40 |
|
| 41 |
public FlowableConcatMapScheduler(Flowable<T> source, |
| 42 |
Function<? super T, ? extends Publisher<? extends R>> mapper, |
| 43 |
int prefetch, ErrorMode errorMode, Scheduler scheduler) { |
| 44 |
super(source); |
| 45 |
this.mapper = mapper; |
| 46 |
this.prefetch = prefetch; |
| 47 |
this.errorMode = errorMode; |
| 48 |
this.scheduler = scheduler; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 53 |
switch (errorMode) { |
| 54 |
case BOUNDARY: |
| 55 |
source.subscribe(new ConcatMapDelayed<>(s, mapper, prefetch, false, scheduler.createWorker())); |
| 56 |
break; |
| 57 |
case END: |
| 58 |
source.subscribe(new ConcatMapDelayed<>(s, mapper, prefetch, true, scheduler.createWorker())); |
| 59 |
break; |
| 60 |
default: |
| 61 |
source.subscribe(new ConcatMapImmediate<>(s, mapper, prefetch, scheduler.createWorker())); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
abstract static class BaseConcatMapSubscriber<T, R> |
| 66 |
extends AtomicInteger |
| 67 |
implements FlowableSubscriber<T>, ConcatMapSupport<R>, Subscription, Runnable { |
| 68 |
|
| 69 |
private static final long serialVersionUID = -3511336836796789179L; |
| 70 |
|
| 71 |
final ConcatMapInner<R> inner; |
| 72 |
|
| 73 |
final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 74 |
|
| 75 |
final int prefetch; |
| 76 |
|
| 77 |
final int limit; |
| 78 |
|
| 79 |
final Scheduler.Worker worker; |
| 80 |
|
| 81 |
Subscription upstream; |
| 82 |
|
| 83 |
int consumed; |
| 84 |
|
| 85 |
SimpleQueue<T> queue; |
| 86 |
|
| 87 |
volatile boolean done; |
| 88 |
|
| 89 |
volatile boolean cancelled; |
| 90 |
|
| 91 |
final AtomicThrowable errors; |
| 92 |
|
| 93 |
volatile boolean active; |
| 94 |
|
| 95 |
int sourceMode; |
| 96 |
|
| 97 |
BaseConcatMapSubscriber( |
| 98 |
Function<? super T, ? extends Publisher<? extends R>> mapper, |
| 99 |
int prefetch, Scheduler.Worker worker) { |
| 100 |
this.mapper = mapper; |
| 101 |
this.prefetch = prefetch; |
| 102 |
this.limit = prefetch - (prefetch >> 2); |
| 103 |
this.inner = new ConcatMapInner<>(this); |
| 104 |
this.errors = new AtomicThrowable(); |
| 105 |
this.worker = worker; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public final void onSubscribe(Subscription s) { |
| 110 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 111 |
this.upstream = s; |
| 112 |
|
| 113 |
if (s instanceof QueueSubscription) { |
| 114 |
@SuppressWarnings("unchecked") QueueSubscription<T> f = (QueueSubscription<T>)s; |
| 115 |
int m = f.requestFusion(QueueSubscription.ANY | QueueSubscription.BOUNDARY); |
| 116 |
if (m == QueueSubscription.SYNC) { |
| 117 |
sourceMode = m; |
| 118 |
queue = f; |
| 119 |
done = true; |
| 120 |
|
| 121 |
subscribeActual(); |
| 122 |
|
| 123 |
schedule(); |
| 124 |
return; |
| 125 |
} |
| 126 |
if (m == QueueSubscription.ASYNC) { |
| 127 |
sourceMode = m; |
| 128 |
queue = f; |
| 129 |
|
| 130 |
subscribeActual(); |
| 131 |
|
| 132 |
s.request(prefetch); |
| 133 |
return; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
queue = new SpscArrayQueue<>(prefetch); |
| 138 |
|
| 139 |
subscribeActual(); |
| 140 |
|
| 141 |
s.request(prefetch); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
abstract void schedule(); |
| 146 |
|
| 147 |
abstract void subscribeActual(); |
| 148 |
|
| 149 |
@Override |
| 150 |
public final void onNext(T t) { |
| 151 |
if (sourceMode != QueueSubscription.ASYNC) { |
| 152 |
if (!queue.offer(t)) { |
| 153 |
upstream.cancel(); |
| 154 |
onError(new IllegalStateException("Queue full?!")); |
| 155 |
return; |
| 156 |
} |
| 157 |
} |
| 158 |
schedule(); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public final void onComplete() { |
| 163 |
done = true; |
| 164 |
schedule(); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public final void innerComplete() { |
| 169 |
active = false; |
| 170 |
schedule(); |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
static final class ConcatMapImmediate<T, R> |
| 176 |
extends BaseConcatMapSubscriber<T, R> { |
| 177 |
|
| 178 |
private static final long serialVersionUID = 7898995095634264146L; |
| 179 |
|
| 180 |
final Subscriber<? super R> downstream; |
| 181 |
|
| 182 |
final AtomicInteger wip; |
| 183 |
|
| 184 |
ConcatMapImmediate(Subscriber<? super R> actual, |
| 185 |
Function<? super T, ? extends Publisher<? extends R>> mapper, |
| 186 |
int prefetch, Scheduler.Worker worker) { |
| 187 |
super(mapper, prefetch, worker); |
| 188 |
this.downstream = actual; |
| 189 |
this.wip = new AtomicInteger(); |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
void subscribeActual() { |
| 194 |
downstream.onSubscribe(this); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void onError(Throwable t) { |
| 199 |
if (errors.tryAddThrowableOrReport(t)) { |
| 200 |
inner.cancel(); |
| 201 |
|
| 202 |
if (getAndIncrement() == 0) { |
| 203 |
errors.tryTerminateConsumer(downstream); |
| 204 |
worker.dispose(); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
boolean tryEnter() { |
| 210 |
return get() == 0 && compareAndSet(0, 1); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public void innerNext(R value) { |
| 215 |
if (tryEnter()) { |
| 216 |
downstream.onNext(value); |
| 217 |
if (compareAndSet(1, 0)) { |
| 218 |
return; |
| 219 |
} |
| 220 |
errors.tryTerminateConsumer(downstream); |
| 221 |
worker.dispose(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public void innerError(Throwable e) { |
| 227 |
if (errors.tryAddThrowableOrReport(e)) { |
| 228 |
upstream.cancel(); |
| 229 |
|
| 230 |
if (getAndIncrement() == 0) { |
| 231 |
errors.tryTerminateConsumer(downstream); |
| 232 |
worker.dispose(); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void request(long n) { |
| 239 |
inner.request(n); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public void cancel() { |
| 244 |
if (!cancelled) { |
| 245 |
cancelled = true; |
| 246 |
|
| 247 |
inner.cancel(); |
| 248 |
upstream.cancel(); |
| 249 |
worker.dispose(); |
| 250 |
errors.tryTerminateAndReport(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
void schedule() { |
| 256 |
if (wip.getAndIncrement() == 0) { |
| 257 |
worker.schedule(this); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public void run() { |
| 263 |
for (;;) { |
| 264 |
if (cancelled) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
if (!active) { |
| 269 |
boolean d = done; |
| 270 |
|
| 271 |
T v; |
| 272 |
|
| 273 |
try { |
| 274 |
v = queue.poll(); |
| 275 |
} catch (Throwable e) { |
| 276 |
Exceptions.throwIfFatal(e); |
| 277 |
upstream.cancel(); |
| 278 |
errors.tryAddThrowableOrReport(e); |
| 279 |
errors.tryTerminateConsumer(downstream); |
| 280 |
worker.dispose(); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
boolean empty = v == null; |
| 285 |
|
| 286 |
if (d && empty) { |
| 287 |
downstream.onComplete(); |
| 288 |
worker.dispose(); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
if (!empty) { |
| 293 |
Publisher<? extends R> p; |
| 294 |
|
| 295 |
try { |
| 296 |
p = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null Publisher"); |
| 297 |
} catch (Throwable e) { |
| 298 |
Exceptions.throwIfFatal(e); |
| 299 |
|
| 300 |
upstream.cancel(); |
| 301 |
errors.tryAddThrowableOrReport(e); |
| 302 |
errors.tryTerminateConsumer(downstream); |
| 303 |
worker.dispose(); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
if (sourceMode != QueueSubscription.SYNC) { |
| 308 |
int c = consumed + 1; |
| 309 |
if (c == limit) { |
| 310 |
consumed = 0; |
| 311 |
upstream.request(c); |
| 312 |
} else { |
| 313 |
consumed = c; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if (p instanceof Supplier) { |
| 318 |
@SuppressWarnings("unchecked") |
| 319 |
Supplier<R> supplier = (Supplier<R>) p; |
| 320 |
|
| 321 |
R vr; |
| 322 |
|
| 323 |
try { |
| 324 |
vr = supplier.get(); |
| 325 |
} catch (Throwable e) { |
| 326 |
Exceptions.throwIfFatal(e); |
| 327 |
upstream.cancel(); |
| 328 |
errors.tryAddThrowableOrReport(e); |
| 329 |
errors.tryTerminateConsumer(downstream); |
| 330 |
worker.dispose(); |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
if (vr == null || cancelled) { |
| 335 |
continue; |
| 336 |
} |
| 337 |
|
| 338 |
if (inner.isUnbounded()) { |
| 339 |
if (tryEnter()) { |
| 340 |
downstream.onNext(vr); |
| 341 |
if (!compareAndSet(1, 0)) { |
| 342 |
errors.tryTerminateConsumer(downstream); |
| 343 |
worker.dispose(); |
| 344 |
return; |
| 345 |
} |
| 346 |
} |
| 347 |
continue; |
| 348 |
} else { |
| 349 |
active = true; |
| 350 |
inner.setSubscription(new SimpleScalarSubscription<>(vr, inner)); |
| 351 |
} |
| 352 |
|
| 353 |
} else { |
| 354 |
active = true; |
| 355 |
p.subscribe(inner); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
if (wip.decrementAndGet() == 0) { |
| 360 |
break; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
static final class ConcatMapDelayed<T, R> |
| 367 |
extends BaseConcatMapSubscriber<T, R> { |
| 368 |
|
| 369 |
private static final long serialVersionUID = -2945777694260521066L; |
| 370 |
|
| 371 |
final Subscriber<? super R> downstream; |
| 372 |
|
| 373 |
final boolean veryEnd; |
| 374 |
|
| 375 |
ConcatMapDelayed(Subscriber<? super R> actual, |
| 376 |
Function<? super T, ? extends Publisher<? extends R>> mapper, |
| 377 |
int prefetch, boolean veryEnd, Scheduler.Worker worker) { |
| 378 |
super(mapper, prefetch, worker); |
| 379 |
this.downstream = actual; |
| 380 |
this.veryEnd = veryEnd; |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
void subscribeActual() { |
| 385 |
downstream.onSubscribe(this); |
| 386 |
} |
| 387 |
|
| 388 |
@Override |
| 389 |
public void onError(Throwable t) { |
| 390 |
if (errors.tryAddThrowableOrReport(t)) { |
| 391 |
done = true; |
| 392 |
schedule(); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
@Override |
| 397 |
public void innerNext(R value) { |
| 398 |
downstream.onNext(value); |
| 399 |
} |
| 400 |
|
| 401 |
@Override |
| 402 |
public void innerError(Throwable e) { |
| 403 |
if (errors.tryAddThrowableOrReport(e)) { |
| 404 |
if (!veryEnd) { |
| 405 |
upstream.cancel(); |
| 406 |
done = true; |
| 407 |
} |
| 408 |
active = false; |
| 409 |
schedule(); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public void request(long n) { |
| 415 |
inner.request(n); |
| 416 |
} |
| 417 |
|
| 418 |
@Override |
| 419 |
public void cancel() { |
| 420 |
if (!cancelled) { |
| 421 |
cancelled = true; |
| 422 |
|
| 423 |
inner.cancel(); |
| 424 |
upstream.cancel(); |
| 425 |
worker.dispose(); |
| 426 |
errors.tryTerminateAndReport(); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
@Override |
| 431 |
void schedule() { |
| 432 |
if (getAndIncrement() == 0) { |
| 433 |
worker.schedule(this); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
@Override |
| 438 |
public void run() { |
| 439 |
|
| 440 |
for (;;) { |
| 441 |
if (cancelled) { |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
if (!active) { |
| 446 |
|
| 447 |
boolean d = done; |
| 448 |
|
| 449 |
if (d && !veryEnd) { |
| 450 |
Throwable ex = errors.get(); |
| 451 |
if (ex != null) { |
| 452 |
errors.tryTerminateConsumer(downstream); |
| 453 |
worker.dispose(); |
| 454 |
return; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
T v; |
| 459 |
|
| 460 |
try { |
| 461 |
v = queue.poll(); |
| 462 |
} catch (Throwable e) { |
| 463 |
Exceptions.throwIfFatal(e); |
| 464 |
upstream.cancel(); |
| 465 |
errors.tryAddThrowableOrReport(e); |
| 466 |
errors.tryTerminateConsumer(downstream); |
| 467 |
worker.dispose(); |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
boolean empty = v == null; |
| 472 |
|
| 473 |
if (d && empty) { |
| 474 |
errors.tryTerminateConsumer(downstream); |
| 475 |
worker.dispose(); |
| 476 |
return; |
| 477 |
} |
| 478 |
|
| 479 |
if (!empty) { |
| 480 |
Publisher<? extends R> p; |
| 481 |
|
| 482 |
try { |
| 483 |
p = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null Publisher"); |
| 484 |
} catch (Throwable e) { |
| 485 |
Exceptions.throwIfFatal(e); |
| 486 |
|
| 487 |
upstream.cancel(); |
| 488 |
errors.tryAddThrowableOrReport(e); |
| 489 |
errors.tryTerminateConsumer(downstream); |
| 490 |
worker.dispose(); |
| 491 |
return; |
| 492 |
} |
| 493 |
|
| 494 |
if (sourceMode != QueueSubscription.SYNC) { |
| 495 |
int c = consumed + 1; |
| 496 |
if (c == limit) { |
| 497 |
consumed = 0; |
| 498 |
upstream.request(c); |
| 499 |
} else { |
| 500 |
consumed = c; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
if (p instanceof Supplier) { |
| 505 |
@SuppressWarnings("unchecked") |
| 506 |
Supplier<R> supplier = (Supplier<R>) p; |
| 507 |
|
| 508 |
R vr; |
| 509 |
|
| 510 |
try { |
| 511 |
vr = supplier.get(); |
| 512 |
} catch (Throwable e) { |
| 513 |
Exceptions.throwIfFatal(e); |
| 514 |
errors.tryAddThrowableOrReport(e); |
| 515 |
if (!veryEnd) { |
| 516 |
upstream.cancel(); |
| 517 |
errors.tryTerminateConsumer(downstream); |
| 518 |
worker.dispose(); |
| 519 |
return; |
| 520 |
} |
| 521 |
vr = null; |
| 522 |
} |
| 523 |
|
| 524 |
if (vr == null || cancelled) { |
| 525 |
continue; |
| 526 |
} |
| 527 |
|
| 528 |
if (inner.isUnbounded()) { |
| 529 |
downstream.onNext(vr); |
| 530 |
continue; |
| 531 |
} else { |
| 532 |
active = true; |
| 533 |
inner.setSubscription(new SimpleScalarSubscription<>(vr, inner)); |
| 534 |
} |
| 535 |
} else { |
| 536 |
active = true; |
| 537 |
p.subscribe(inner); |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
if (decrementAndGet() == 0) { |
| 542 |
break; |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
} |