Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatMap.java

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