Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatMapEager.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.*;
23 import io.reactivex.rxjava3.functions.Function;
24 import io.reactivex.rxjava3.internal.subscribers.*;
25 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
26 import io.reactivex.rxjava3.internal.util.*;
27 import io.reactivex.rxjava3.operators.SimpleQueue;
28 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
29
30 public final class FlowableConcatMapEager<T, R> extends AbstractFlowableWithUpstream<T, R> {
31
32 final Function<? super T, ? extends Publisher<? extends R>> mapper;
33
34 final int maxConcurrency;
35
36 final int prefetch;
37
38 final ErrorMode errorMode;
39
40 public FlowableConcatMapEager(Flowable<T> source,
41 Function<? super T, ? extends Publisher<? extends R>> mapper,
42 int maxConcurrency,
43 int prefetch,
44 ErrorMode errorMode) {
45 super(source);
46 this.mapper = mapper;
47 this.maxConcurrency = maxConcurrency;
48 this.prefetch = prefetch;
49 this.errorMode = errorMode;
50 }
51
52 @Override
53 protected void subscribeActual(Subscriber<? super R> s) {
54 source.subscribe(new ConcatMapEagerDelayErrorSubscriber<>(
55 s, mapper, maxConcurrency, prefetch, errorMode));
56 }
57
58 static final class ConcatMapEagerDelayErrorSubscriber<T, R>
59 extends AtomicInteger
60 implements FlowableSubscriber<T>, Subscription, InnerQueuedSubscriberSupport<R> {
61
62 private static final long serialVersionUID = -4255299542215038287L;
63
64 final Subscriber<? super R> downstream;
65
66 final Function<? super T, ? extends Publisher<? extends R>> mapper;
67
68 final int maxConcurrency;
69
70 final int prefetch;
71
72 final ErrorMode errorMode;
73
74 final AtomicThrowable errors;
75
76 final AtomicLong requested;
77
78 final SpscLinkedArrayQueue<InnerQueuedSubscriber<R>> subscribers;
79
80 Subscription upstream;
81
82 volatile boolean cancelled;
83
84 volatile boolean done;
85
86 volatile InnerQueuedSubscriber<R> current;
87
88 ConcatMapEagerDelayErrorSubscriber(Subscriber<? super R> actual,
89 Function<? super T, ? extends Publisher<? extends R>> mapper, int maxConcurrency, int prefetch,
90 ErrorMode errorMode) {
91 this.downstream = actual;
92 this.mapper = mapper;
93 this.maxConcurrency = maxConcurrency;
94 this.prefetch = prefetch;
95 this.errorMode = errorMode;
96 this.subscribers = new SpscLinkedArrayQueue<>(Math.min(prefetch, maxConcurrency));
97 this.errors = new AtomicThrowable();
98 this.requested = new AtomicLong();
99 }
100
101 @Override
102 public void onSubscribe(Subscription s) {
103 if (SubscriptionHelper.validate(this.upstream, s)) {
104 this.upstream = s;
105
106 downstream.onSubscribe(this);
107
108 s.request(maxConcurrency == Integer.MAX_VALUE ? Long.MAX_VALUE : maxConcurrency);
109 }
110
111 }
112
113 @Override
114 public void onNext(T t) {
115 Publisher<? extends R> p;
116
117 try {
118 p = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher");
119 } catch (Throwable ex) {
120 Exceptions.throwIfFatal(ex);
121 upstream.cancel();
122 onError(ex);
123 return;
124 }
125
126 InnerQueuedSubscriber<R> inner = new InnerQueuedSubscriber<>(this, prefetch);
127
128 if (cancelled) {
129 return;
130 }
131
132 subscribers.offer(inner);
133
134 p.subscribe(inner);
135
136 if (cancelled) {
137 inner.cancel();
138 drainAndCancel();
139 }
140 }
141
142 @Override
143 public void onError(Throwable t) {
144 if (errors.tryAddThrowableOrReport(t)) {
145 done = true;
146 drain();
147 }
148 }
149
150 @Override
151 public void onComplete() {
152 done = true;
153 drain();
154 }
155
156 @Override
157 public void cancel() {
158 if (cancelled) {
159 return;
160 }
161 cancelled = true;
162 upstream.cancel();
163 errors.tryTerminateAndReport();
164
165 drainAndCancel();
166 }
167
168 void drainAndCancel() {
169 if (getAndIncrement() == 0) {
170 do {
171 cancelAll();
172 } while (decrementAndGet() != 0);
173 }
174 }
175
176 void cancelAll() {
177 InnerQueuedSubscriber<R> inner = current;
178 current = null;
179
180 if (inner != null) {
181 inner.cancel();
182 }
183
184 while ((inner = subscribers.poll()) != null) {
185 inner.cancel();
186 }
187 }
188
189 @Override
190 public void request(long n) {
191 if (SubscriptionHelper.validate(n)) {
192 BackpressureHelper.add(requested, n);
193 drain();
194 }
195 }
196
197 @Override
198 public void innerNext(InnerQueuedSubscriber<R> inner, R value) {
199 if (inner.queue().offer(value)) {
200 drain();
201 } else {
202 inner.cancel();
203 innerError(inner, new MissingBackpressureException());
204 }
205 }
206
207 @Override
208 public void innerError(InnerQueuedSubscriber<R> inner, Throwable e) {
209 if (errors.tryAddThrowableOrReport(e)) {
210 inner.setDone();
211 if (errorMode != ErrorMode.END) {
212 upstream.cancel();
213 }
214 drain();
215 }
216 }
217
218 @Override
219 public void innerComplete(InnerQueuedSubscriber<R> inner) {
220 inner.setDone();
221 drain();
222 }
223
224 @Override
225 public void drain() {
226 if (getAndIncrement() != 0) {
227 return;
228 }
229
230 int missed = 1;
231 InnerQueuedSubscriber<R> inner = current;
232 Subscriber<? super R> a = downstream;
233 ErrorMode em = errorMode;
234
235 for (;;) {
236 long r = requested.get();
237 long e = 0L;
238
239 if (inner == null) {
240
241 if (em != ErrorMode.END) {
242 Throwable ex = errors.get();
243 if (ex != null) {
244 cancelAll();
245
246 errors.tryTerminateConsumer(downstream);
247 return;
248 }
249 }
250
251 boolean outerDone = done;
252
253 inner = subscribers.poll();
254
255 if (outerDone && inner == null) {
256 errors.tryTerminateConsumer(downstream);
257 return;
258 }
259
260 if (inner != null) {
261 current = inner;
262 }
263 }
264
265 boolean continueNextSource = false;
266
267 if (inner != null) {
268 SimpleQueue<R> q = inner.queue();
269 if (q != null) {
270 while (e != r) {
271 if (cancelled) {
272 cancelAll();
273 return;
274 }
275
276 if (em == ErrorMode.IMMEDIATE) {
277 Throwable ex = errors.get();
278 if (ex != null) {
279 current = null;
280 inner.cancel();
281 cancelAll();
282
283 errors.tryTerminateConsumer(downstream);
284 return;
285 }
286 }
287
288 boolean d = inner.isDone();
289
290 R v;
291
292 try {
293 v = q.poll();
294 } catch (Throwable ex) {
295 Exceptions.throwIfFatal(ex);
296 current = null;
297 inner.cancel();
298 cancelAll();
299 a.onError(ex);
300 return;
301 }
302
303 boolean empty = v == null;
304
305 if (d && empty) {
306 inner = null;
307 current = null;
308 upstream.request(1);
309 continueNextSource = true;
310 break;
311 }
312
313 if (empty) {
314 break;
315 }
316
317 a.onNext(v);
318
319 e++;
320
321 inner.request(1L);
322 }
323
324 if (e == r) {
325 if (cancelled) {
326 cancelAll();
327 return;
328 }
329
330 if (em == ErrorMode.IMMEDIATE) {
331 Throwable ex = errors.get();
332 if (ex != null) {
333 current = null;
334 inner.cancel();
335 cancelAll();
336
337 errors.tryTerminateConsumer(downstream);
338 return;
339 }
340 }
341
342 boolean d = inner.isDone();
343
344 boolean empty = q.isEmpty();
345
346 if (d && empty) {
347 inner = null;
348 current = null;
349 upstream.request(1);
350 continueNextSource = true;
351 }
352 }
353 }
354 }
355
356 if (e != 0L && r != Long.MAX_VALUE) {
357 requested.addAndGet(-e);
358 }
359
360 if (continueNextSource) {
361 continue;
362 }
363
364 missed = addAndGet(-missed);
365 if (missed == 0) {
366 break;
367 }
368 }
369 }
370 }
371 }