Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFlatMapMaybe.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.*;
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 MaybeSources 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 FlowableFlatMapMaybe<T, R> extends AbstractFlowableWithUpstream<T, R> {
36
37 final Function<? super T, ? extends MaybeSource<? extends R>> mapper;
38
39 final boolean delayErrors;
40
41 final int maxConcurrency;
42
43 public FlowableFlatMapMaybe(Flowable<T> source, Function<? super T, ? extends MaybeSource<? 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 FlatMapMaybeSubscriber<>(s, mapper, delayErrors, maxConcurrency));
54 }
55
56 static final class FlatMapMaybeSubscriber<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 MaybeSource<? extends R>> mapper;
77
78 final AtomicReference<SpscLinkedArrayQueue<R>> queue;
79
80 Subscription upstream;
81
82 volatile boolean cancelled;
83
84 FlatMapMaybeSubscriber(Subscriber<? super R> actual,
85 Function<? super T, ? extends MaybeSource<? 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 MaybeSource<? extends R> ms;
116
117 try {
118 ms = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null MaybeSource");
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 (checkTerminate(d, q)) {
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 innerComplete(InnerObserver inner) {
236 set.delete(inner);
237
238 if (get() == 0 && compareAndSet(0, 1)) {
239 boolean d = active.decrementAndGet() == 0;
240 SpscLinkedArrayQueue<R> q = queue.get();
241
242 if (checkTerminate(d, q)) {
243 errors.tryTerminateConsumer(downstream);
244 return;
245 }
246
247 if (maxConcurrency != Integer.MAX_VALUE) {
248 upstream.request(1);
249 }
250 if (decrementAndGet() == 0) {
251 return;
252 }
253 drainLoop();
254 } else {
255 active.decrementAndGet();
256 if (maxConcurrency != Integer.MAX_VALUE) {
257 upstream.request(1);
258 }
259 drain();
260 }
261 }
262
263 static boolean checkTerminate(boolean d, SpscLinkedArrayQueue<?> q) {
264 return d && (q == null || q.isEmpty());
265 }
266
267 void drain() {
268 if (getAndIncrement() == 0) {
269 drainLoop();
270 }
271 }
272
273 void clear() {
274 SpscLinkedArrayQueue<R> q = queue.get();
275 if (q != null) {
276 q.clear();
277 }
278 }
279
280 void drainLoop() {
281 int missed = 1;
282 Subscriber<? super R> a = downstream;
283 AtomicInteger n = active;
284 AtomicReference<SpscLinkedArrayQueue<R>> qr = queue;
285
286 for (;;) {
287 long r = requested.get();
288 long e = 0L;
289
290 while (e != r) {
291 if (cancelled) {
292 clear();
293 return;
294 }
295
296 if (!delayErrors) {
297 Throwable ex = errors.get();
298 if (ex != null) {
299 clear();
300 errors.tryTerminateConsumer(a);
301 return;
302 }
303 }
304
305 boolean d = n.get() == 0;
306 SpscLinkedArrayQueue<R> q = qr.get();
307 R v = q != null ? q.poll() : null;
308 boolean empty = v == null;
309
310 if (d && empty) {
311 errors.tryTerminateConsumer(a);
312 return;
313 }
314
315 if (empty) {
316 break;
317 }
318
319 a.onNext(v);
320
321 e++;
322 }
323
324 if (e == r) {
325 if (cancelled) {
326 clear();
327 return;
328 }
329
330 if (!delayErrors) {
331 Throwable ex = errors.get();
332 if (ex != null) {
333 clear();
334 errors.tryTerminateConsumer(a);
335 return;
336 }
337 }
338
339 boolean d = n.get() == 0;
340 SpscLinkedArrayQueue<R> q = qr.get();
341 boolean empty = q == null || q.isEmpty();
342
343 if (d && empty) {
344 errors.tryTerminateConsumer(a);
345 return;
346 }
347 }
348
349 if (e != 0L) {
350 BackpressureHelper.produced(requested, e);
351 if (maxConcurrency != Integer.MAX_VALUE) {
352 upstream.request(e);
353 }
354 }
355
356 missed = addAndGet(-missed);
357 if (missed == 0) {
358 break;
359 }
360 }
361 }
362
363 final class InnerObserver extends AtomicReference<Disposable>
364 implements MaybeObserver<R>, Disposable {
365 private static final long serialVersionUID = -502562646270949838L;
366
367 @Override
368 public void onSubscribe(Disposable d) {
369 DisposableHelper.setOnce(this, d);
370 }
371
372 @Override
373 public void onSuccess(R value) {
374 innerSuccess(this, value);
375 }
376
377 @Override
378 public void onError(Throwable e) {
379 innerError(this, e);
380 }
381
382 @Override
383 public void onComplete() {
384 innerComplete(this);
385 }
386
387 @Override
388 public boolean isDisposed() {
389 return DisposableHelper.isDisposed(get());
390 }
391
392 @Override
393 public void dispose() {
394 DisposableHelper.dispose(this);
395 }
396 }
397 }
398 }