Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFlatMap.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.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.*;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
26 import io.reactivex.rxjava3.internal.util.*;
27 import io.reactivex.rxjava3.operators.*;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29
30 public final class FlowableFlatMap<T, U> extends AbstractFlowableWithUpstream<T, U> {
31 final Function<? super T, ? extends Publisher<? extends U>> mapper;
32 final boolean delayErrors;
33 final int maxConcurrency;
34 final int bufferSize;
35
36 public FlowableFlatMap(Flowable<T> source,
37 Function<? super T, ? extends Publisher<? extends U>> mapper,
38 boolean delayErrors, int maxConcurrency, int bufferSize) {
39 super(source);
40 this.mapper = mapper;
41 this.delayErrors = delayErrors;
42 this.maxConcurrency = maxConcurrency;
43 this.bufferSize = bufferSize;
44 }
45
46 @Override
47 protected void subscribeActual(Subscriber<? super U> s) {
48 if (FlowableScalarXMap.tryScalarXMapSubscribe(source, s, mapper)) {
49 return;
50 }
51 source.subscribe(subscribe(s, mapper, delayErrors, maxConcurrency, bufferSize));
52 }
53
54 public static <T, U> FlowableSubscriber<T> subscribe(Subscriber<? super U> s,
55 Function<? super T, ? extends Publisher<? extends U>> mapper,
56 boolean delayErrors, int maxConcurrency, int bufferSize) {
57 return new MergeSubscriber<>(s, mapper, delayErrors, maxConcurrency, bufferSize);
58 }
59
60 static final class MergeSubscriber<T, U> extends AtomicInteger implements FlowableSubscriber<T>, Subscription {
61
62 private static final long serialVersionUID = -2117620485640801370L;
63
64 final Subscriber<? super U> downstream;
65 final Function<? super T, ? extends Publisher<? extends U>> mapper;
66 final boolean delayErrors;
67 final int maxConcurrency;
68 final int bufferSize;
69
70 volatile SimplePlainQueue<U> queue;
71
72 volatile boolean done;
73
74 final AtomicThrowable errors = new AtomicThrowable();
75
76 volatile boolean cancelled;
77
78 final AtomicReference<InnerSubscriber<?, ?>[]> subscribers = new AtomicReference<>();
79
80 static final InnerSubscriber<?, ?>[] EMPTY = new InnerSubscriber<?, ?>[0];
81
82 static final InnerSubscriber<?, ?>[] CANCELLED = new InnerSubscriber<?, ?>[0];
83
84 final AtomicLong requested = new AtomicLong();
85
86 Subscription upstream;
87
88 long uniqueId;
89 long lastId;
90 int lastIndex;
91
92 int scalarEmitted;
93 final int scalarLimit;
94
95 MergeSubscriber(Subscriber<? super U> actual, Function<? super T, ? extends Publisher<? extends U>> mapper,
96 boolean delayErrors, int maxConcurrency, int bufferSize) {
97 this.downstream = actual;
98 this.mapper = mapper;
99 this.delayErrors = delayErrors;
100 this.maxConcurrency = maxConcurrency;
101 this.bufferSize = bufferSize;
102 this.scalarLimit = Math.max(1, maxConcurrency >> 1);
103 subscribers.lazySet(EMPTY);
104 }
105
106 @Override
107 public void onSubscribe(Subscription s) {
108 if (SubscriptionHelper.validate(this.upstream, s)) {
109 this.upstream = s;
110 downstream.onSubscribe(this);
111 if (!cancelled) {
112 if (maxConcurrency == Integer.MAX_VALUE) {
113 s.request(Long.MAX_VALUE);
114 } else {
115 s.request(maxConcurrency);
116 }
117 }
118 }
119 }
120
121 @SuppressWarnings("unchecked")
122 @Override
123 public void onNext(T t) {
124 // safeguard against misbehaving sources
125 if (done) {
126 return;
127 }
128 Publisher<? extends U> p;
129 try {
130 p = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher");
131 } catch (Throwable e) {
132 Exceptions.throwIfFatal(e);
133 upstream.cancel();
134 onError(e);
135 return;
136 }
137 if (p instanceof Supplier) {
138 U u;
139
140 try {
141 u = ((Supplier<U>)p).get();
142 } catch (Throwable ex) {
143 Exceptions.throwIfFatal(ex);
144 errors.tryAddThrowableOrReport(ex);
145 drain();
146 return;
147 }
148
149 if (u != null) {
150 tryEmitScalar(u);
151 } else {
152 if (maxConcurrency != Integer.MAX_VALUE
153 && !cancelled && ++scalarEmitted == scalarLimit) {
154 scalarEmitted = 0;
155 upstream.request(scalarLimit);
156 }
157 }
158 } else {
159 InnerSubscriber<T, U> inner = new InnerSubscriber<>(this, bufferSize, uniqueId++);
160 if (addInner(inner)) {
161 p.subscribe(inner);
162 }
163 }
164 }
165
166 boolean addInner(InnerSubscriber<T, U> inner) {
167 for (;;) {
168 InnerSubscriber<?, ?>[] a = subscribers.get();
169 if (a == CANCELLED) {
170 inner.dispose();
171 return false;
172 }
173 int n = a.length;
174 InnerSubscriber<?, ?>[] b = new InnerSubscriber[n + 1];
175 System.arraycopy(a, 0, b, 0, n);
176 b[n] = inner;
177 if (subscribers.compareAndSet(a, b)) {
178 return true;
179 }
180 }
181 }
182
183 void removeInner(InnerSubscriber<T, U> inner) {
184 for (;;) {
185 InnerSubscriber<?, ?>[] a = subscribers.get();
186 int n = a.length;
187 if (n == 0) {
188 return;
189 }
190 int j = -1;
191 for (int i = 0; i < n; i++) {
192 if (a[i] == inner) {
193 j = i;
194 break;
195 }
196 }
197 if (j < 0) {
198 return;
199 }
200 InnerSubscriber<?, ?>[] b;
201 if (n == 1) {
202 b = EMPTY;
203 } else {
204 b = new InnerSubscriber<?, ?>[n - 1];
205 System.arraycopy(a, 0, b, 0, j);
206 System.arraycopy(a, j + 1, b, j, n - j - 1);
207 }
208 if (subscribers.compareAndSet(a, b)) {
209 return;
210 }
211 }
212 }
213
214 SimpleQueue<U> getMainQueue() {
215 SimplePlainQueue<U> q = queue;
216 if (q == null) {
217 if (maxConcurrency == Integer.MAX_VALUE) {
218 q = new SpscLinkedArrayQueue<>(bufferSize);
219 } else {
220 q = new SpscArrayQueue<>(maxConcurrency);
221 }
222 queue = q;
223 }
224 return q;
225 }
226
227 void tryEmitScalar(U value) {
228 if (get() == 0 && compareAndSet(0, 1)) {
229 long r = requested.get();
230 SimpleQueue<U> q = queue;
231 if (r != 0L && (q == null || q.isEmpty())) {
232 downstream.onNext(value);
233 if (r != Long.MAX_VALUE) {
234 requested.decrementAndGet();
235 }
236 if (maxConcurrency != Integer.MAX_VALUE
237 && !cancelled && ++scalarEmitted == scalarLimit) {
238 scalarEmitted = 0;
239 upstream.request(scalarLimit);
240 }
241 } else {
242 if (q == null) {
243 q = getMainQueue();
244 }
245 if (!q.offer(value)) {
246 onError(new MissingBackpressureException("Scalar queue full?!"));
247 }
248 }
249 if (decrementAndGet() == 0) {
250 return;
251 }
252 } else {
253 SimpleQueue<U> q = getMainQueue();
254 if (!q.offer(value)) {
255 onError(new MissingBackpressureException("Scalar queue full?!"));
256 return;
257 }
258 if (getAndIncrement() != 0) {
259 return;
260 }
261 }
262 drainLoop();
263 }
264
265 void tryEmit(U value, InnerSubscriber<T, U> inner) {
266 if (get() == 0 && compareAndSet(0, 1)) {
267 long r = requested.get();
268 SimpleQueue<U> q = inner.queue;
269 if (r != 0L && (q == null || q.isEmpty())) {
270 downstream.onNext(value);
271 if (r != Long.MAX_VALUE) {
272 requested.decrementAndGet();
273 }
274 inner.requestMore(1);
275 } else {
276 if (q == null) {
277 q = new SpscArrayQueue<>(bufferSize);
278 inner.queue = q;
279 }
280 if (!q.offer(value)) {
281 onError(new MissingBackpressureException("Inner queue full?!"));
282 }
283 }
284 if (decrementAndGet() == 0) {
285 return;
286 }
287 } else {
288 SimpleQueue<U> q = inner.queue;
289 if (q == null) {
290 q = new SpscArrayQueue<>(bufferSize);
291 inner.queue = q;
292 }
293 if (!q.offer(value)) {
294 onError(new MissingBackpressureException("Inner queue full?!"));
295 return;
296 }
297 if (getAndIncrement() != 0) {
298 return;
299 }
300 }
301 drainLoop();
302 }
303
304 @Override
305 public void onError(Throwable t) {
306 // safeguard against misbehaving sources
307 if (done) {
308 RxJavaPlugins.onError(t);
309 return;
310 }
311 if (errors.tryAddThrowableOrReport(t)) {
312 done = true;
313 if (!delayErrors) {
314 for (InnerSubscriber<?, ?> a : subscribers.getAndSet(CANCELLED)) {
315 a.dispose();
316 }
317 }
318 drain();
319 }
320 }
321
322 @Override
323 public void onComplete() {
324 // safeguard against misbehaving sources
325 if (done) {
326 return;
327 }
328 done = true;
329 drain();
330 }
331
332 @Override
333 public void request(long n) {
334 if (SubscriptionHelper.validate(n)) {
335 BackpressureHelper.add(requested, n);
336 drain();
337 }
338 }
339
340 @Override
341 public void cancel() {
342 if (!cancelled) {
343 cancelled = true;
344 upstream.cancel();
345 disposeAll();
346 if (getAndIncrement() == 0) {
347 SimpleQueue<U> q = queue;
348 if (q != null) {
349 q.clear();
350 }
351 }
352 }
353 }
354
355 void drain() {
356 if (getAndIncrement() == 0) {
357 drainLoop();
358 }
359 }
360
361 void drainLoop() {
362 final Subscriber<? super U> child = this.downstream;
363 int missed = 1;
364 for (;;) {
365 if (checkTerminate()) {
366 return;
367 }
368 SimplePlainQueue<U> svq = queue;
369
370 long r = requested.get();
371 boolean unbounded = r == Long.MAX_VALUE;
372
373 long replenishMain = 0;
374
375 if (svq != null) {
376 long scalarEmission = 0;
377 U o = null;
378 while (r != 0L) {
379 o = svq.poll();
380
381 if (checkTerminate()) {
382 return;
383 }
384 if (o == null) {
385 break;
386 }
387
388 child.onNext(o);
389
390 replenishMain++;
391 scalarEmission++;
392 r--;
393 }
394 if (scalarEmission != 0L) {
395 if (unbounded) {
396 r = Long.MAX_VALUE;
397 } else {
398 r = requested.addAndGet(-scalarEmission);
399 }
400 }
401 }
402
403 boolean d = done;
404 svq = queue;
405 InnerSubscriber<?, ?>[] inner = subscribers.get();
406 int n = inner.length;
407
408 if (d && (svq == null || svq.isEmpty()) && n == 0) {
409 errors.tryTerminateConsumer(downstream);
410 return;
411 }
412
413 boolean innerCompleted = false;
414 if (n != 0) {
415 long startId = lastId;
416 int index = lastIndex;
417
418 if (n <= index || inner[index].id != startId) {
419 if (n <= index) {
420 index = 0;
421 }
422 int j = index;
423 for (int i = 0; i < n; i++) {
424 if (inner[j].id == startId) {
425 break;
426 }
427 j++;
428 if (j == n) {
429 j = 0;
430 }
431 }
432 index = j;
433 lastIndex = j;
434 lastId = inner[j].id;
435 }
436
437 int j = index;
438 sourceLoop:
439 for (int i = 0; i < n; i++) {
440 if (checkTerminate()) {
441 return;
442 }
443
444 @SuppressWarnings("unchecked")
445 InnerSubscriber<T, U> is = (InnerSubscriber<T, U>)inner[j];
446
447 U o = null;
448 for (;;) {
449 SimpleQueue<U> q = is.queue;
450 if (q == null) {
451 break;
452 }
453 long produced = 0;
454 while (r != 0L) {
455 if (checkTerminate()) {
456 return;
457 }
458
459 try {
460 o = q.poll();
461 } catch (Throwable ex) {
462 Exceptions.throwIfFatal(ex);
463 is.dispose();
464 errors.tryAddThrowableOrReport(ex);
465 if (!delayErrors) {
466 upstream.cancel();
467 }
468 if (checkTerminate()) {
469 return;
470 }
471 removeInner(is);
472 innerCompleted = true;
473 i++;
474 continue sourceLoop;
475 }
476 if (o == null) {
477 break;
478 }
479
480 child.onNext(o);
481
482 r--;
483 produced++;
484 }
485 if (produced != 0L) {
486 if (!unbounded) {
487 r = requested.addAndGet(-produced);
488 } else {
489 r = Long.MAX_VALUE;
490 }
491 is.requestMore(produced);
492 }
493 if (r == 0 || o == null) {
494 break;
495 }
496 }
497 boolean innerDone = is.done;
498 SimpleQueue<U> innerQueue = is.queue;
499 if (innerDone && (innerQueue == null || innerQueue.isEmpty())) {
500 removeInner(is);
501 if (checkTerminate()) {
502 return;
503 }
504 replenishMain++;
505 innerCompleted = true;
506 }
507 if (r == 0L) {
508 break;
509 }
510
511 j++;
512 if (j == n) {
513 j = 0;
514 }
515 }
516 lastIndex = j;
517 lastId = inner[j].id;
518 }
519
520 if (replenishMain != 0L && !cancelled) {
521 upstream.request(replenishMain);
522 }
523 if (innerCompleted) {
524 continue;
525 }
526 missed = addAndGet(-missed);
527 if (missed == 0) {
528 break;
529 }
530 }
531 }
532
533 boolean checkTerminate() {
534 if (cancelled) {
535 clearScalarQueue();
536 return true;
537 }
538 if (!delayErrors && errors.get() != null) {
539 clearScalarQueue();
540 errors.tryTerminateConsumer(downstream);
541 return true;
542 }
543 return false;
544 }
545
546 void clearScalarQueue() {
547 SimpleQueue<U> q = queue;
548 if (q != null) {
549 q.clear();
550 }
551 }
552
553 void disposeAll() {
554 InnerSubscriber<?, ?>[] a = subscribers.getAndSet(CANCELLED);
555 if (a != CANCELLED) {
556 for (InnerSubscriber<?, ?> inner : a) {
557 inner.dispose();
558 }
559 errors.tryTerminateAndReport();
560 }
561 }
562
563 void innerError(InnerSubscriber<T, U> inner, Throwable t) {
564 if (errors.tryAddThrowableOrReport(t)) {
565 inner.done = true;
566 if (!delayErrors) {
567 upstream.cancel();
568 for (InnerSubscriber<?, ?> a : subscribers.getAndSet(CANCELLED)) {
569 a.dispose();
570 }
571 }
572 drain();
573 }
574 }
575 }
576
577 static final class InnerSubscriber<T, U> extends AtomicReference<Subscription>
578 implements FlowableSubscriber<U>, Disposable {
579
580 private static final long serialVersionUID = -4606175640614850599L;
581 final long id;
582 final MergeSubscriber<T, U> parent;
583 final int limit;
584 final int bufferSize;
585
586 volatile boolean done;
587 volatile SimpleQueue<U> queue;
588 long produced;
589 int fusionMode;
590
591 InnerSubscriber(MergeSubscriber<T, U> parent, int bufferSize, long id) {
592 this.id = id;
593 this.parent = parent;
594 this.bufferSize = bufferSize;
595 this.limit = bufferSize >> 2;
596 }
597
598 @Override
599 public void onSubscribe(Subscription s) {
600 if (SubscriptionHelper.setOnce(this, s)) {
601
602 if (s instanceof QueueSubscription) {
603 @SuppressWarnings("unchecked")
604 QueueSubscription<U> qs = (QueueSubscription<U>) s;
605 int m = qs.requestFusion(QueueSubscription.ANY | QueueSubscription.BOUNDARY);
606 if (m == QueueSubscription.SYNC) {
607 fusionMode = m;
608 queue = qs;
609 done = true;
610 parent.drain();
611 return;
612 }
613 if (m == QueueSubscription.ASYNC) {
614 fusionMode = m;
615 queue = qs;
616 }
617
618 }
619
620 s.request(bufferSize);
621 }
622 }
623
624 @Override
625 public void onNext(U t) {
626 if (fusionMode != QueueSubscription.ASYNC) {
627 parent.tryEmit(t, this);
628 } else {
629 parent.drain();
630 }
631 }
632
633 @Override
634 public void onError(Throwable t) {
635 lazySet(SubscriptionHelper.CANCELLED);
636 parent.innerError(this, t);
637 }
638
639 @Override
640 public void onComplete() {
641 done = true;
642 parent.drain();
643 }
644
645 void requestMore(long n) {
646 if (fusionMode != QueueSubscription.SYNC) {
647 long p = produced + n;
648 if (p >= limit) {
649 produced = 0;
650 get().request(p);
651 } else {
652 produced = p;
653 }
654 }
655 }
656
657 @Override
658 public void dispose() {
659 SubscriptionHelper.cancel(this);
660 }
661
662 @Override
663 public boolean isDisposed() {
664 return get() == SubscriptionHelper.CANCELLED;
665 }
666 }
667 }