Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableGroupJoin.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.*;
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.*;
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.SimpleQueue;
28 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.UnicastProcessor;
31
32 public final class FlowableGroupJoin<TLeft, TRight, TLeftEnd, TRightEnd, R> extends AbstractFlowableWithUpstream<TLeft, R> {
33
34 final Publisher<? extends TRight> other;
35
36 final Function<? super TLeft, ? extends Publisher<TLeftEnd>> leftEnd;
37
38 final Function<? super TRight, ? extends Publisher<TRightEnd>> rightEnd;
39
40 final BiFunction<? super TLeft, ? super Flowable<TRight>, ? extends R> resultSelector;
41
42 public FlowableGroupJoin(
43 Flowable<TLeft> source,
44 Publisher<? extends TRight> other,
45 Function<? super TLeft, ? extends Publisher<TLeftEnd>> leftEnd,
46 Function<? super TRight, ? extends Publisher<TRightEnd>> rightEnd,
47 BiFunction<? super TLeft, ? super Flowable<TRight>, ? extends R> resultSelector) {
48 super(source);
49 this.other = other;
50 this.leftEnd = leftEnd;
51 this.rightEnd = rightEnd;
52 this.resultSelector = resultSelector;
53 }
54
55 @Override
56 protected void subscribeActual(Subscriber<? super R> s) {
57
58 GroupJoinSubscription<TLeft, TRight, TLeftEnd, TRightEnd, R> parent =
59 new GroupJoinSubscription<>(s, leftEnd, rightEnd, resultSelector);
60
61 s.onSubscribe(parent);
62
63 LeftRightSubscriber left = new LeftRightSubscriber(parent, true);
64 parent.disposables.add(left);
65 LeftRightSubscriber right = new LeftRightSubscriber(parent, false);
66 parent.disposables.add(right);
67
68 source.subscribe(left);
69 other.subscribe(right);
70 }
71
72 interface JoinSupport {
73
74 void innerError(Throwable ex);
75
76 void innerComplete(LeftRightSubscriber sender);
77
78 void innerValue(boolean isLeft, Object o);
79
80 void innerClose(boolean isLeft, LeftRightEndSubscriber index);
81
82 void innerCloseError(Throwable ex);
83 }
84
85 static final class GroupJoinSubscription<TLeft, TRight, TLeftEnd, TRightEnd, R>
86 extends AtomicInteger implements Subscription, JoinSupport {
87
88 private static final long serialVersionUID = -6071216598687999801L;
89
90 final Subscriber<? super R> downstream;
91
92 final AtomicLong requested;
93
94 final SpscLinkedArrayQueue<Object> queue;
95
96 final CompositeDisposable disposables;
97
98 final Map<Integer, UnicastProcessor<TRight>> lefts;
99
100 final Map<Integer, TRight> rights;
101
102 final AtomicReference<Throwable> error;
103
104 final Function<? super TLeft, ? extends Publisher<TLeftEnd>> leftEnd;
105
106 final Function<? super TRight, ? extends Publisher<TRightEnd>> rightEnd;
107
108 final BiFunction<? super TLeft, ? super Flowable<TRight>, ? extends R> resultSelector;
109
110 final AtomicInteger active;
111
112 int leftIndex;
113
114 int rightIndex;
115
116 volatile boolean cancelled;
117
118 static final Integer LEFT_VALUE = 1;
119
120 static final Integer RIGHT_VALUE = 2;
121
122 static final Integer LEFT_CLOSE = 3;
123
124 static final Integer RIGHT_CLOSE = 4;
125
126 GroupJoinSubscription(Subscriber<? super R> actual, Function<? super TLeft, ? extends Publisher<TLeftEnd>> leftEnd,
127 Function<? super TRight, ? extends Publisher<TRightEnd>> rightEnd,
128 BiFunction<? super TLeft, ? super Flowable<TRight>, ? extends R> resultSelector) {
129 this.downstream = actual;
130 this.requested = new AtomicLong();
131 this.disposables = new CompositeDisposable();
132 this.queue = new SpscLinkedArrayQueue<>(bufferSize());
133 this.lefts = new LinkedHashMap<>();
134 this.rights = new LinkedHashMap<>();
135 this.error = new AtomicReference<>();
136 this.leftEnd = leftEnd;
137 this.rightEnd = rightEnd;
138 this.resultSelector = resultSelector;
139 this.active = new AtomicInteger(2);
140 }
141
142 @Override
143 public void request(long n) {
144 if (SubscriptionHelper.validate(n)) {
145 BackpressureHelper.add(requested, n);
146 }
147 }
148
149 @Override
150 public void cancel() {
151 if (cancelled) {
152 return;
153 }
154 cancelled = true;
155 cancelAll();
156 if (getAndIncrement() == 0) {
157 queue.clear();
158 }
159 }
160
161 void cancelAll() {
162 disposables.dispose();
163 }
164
165 void errorAll(Subscriber<?> a) {
166 Throwable ex = ExceptionHelper.terminate(error);
167
168 for (UnicastProcessor<TRight> up : lefts.values()) {
169 up.onError(ex);
170 }
171
172 lefts.clear();
173 rights.clear();
174
175 a.onError(ex);
176 }
177
178 void fail(Throwable exc, Subscriber<?> a, SimpleQueue<?> q) {
179 Exceptions.throwIfFatal(exc);
180 ExceptionHelper.addThrowable(error, exc);
181 q.clear();
182 cancelAll();
183 errorAll(a);
184 }
185
186 void drain() {
187 if (getAndIncrement() != 0) {
188 return;
189 }
190
191 int missed = 1;
192 SpscLinkedArrayQueue<Object> q = queue;
193 Subscriber<? super R> a = downstream;
194
195 for (;;) {
196 for (;;) {
197 if (cancelled) {
198 q.clear();
199 return;
200 }
201
202 Throwable ex = error.get();
203 if (ex != null) {
204 q.clear();
205 cancelAll();
206 errorAll(a);
207 return;
208 }
209
210 boolean d = active.get() == 0;
211
212 Integer mode = (Integer)q.poll();
213
214 boolean empty = mode == null;
215
216 if (d && empty) {
217 for (UnicastProcessor<?> up : lefts.values()) {
218 up.onComplete();
219 }
220
221 lefts.clear();
222 rights.clear();
223 disposables.dispose();
224
225 a.onComplete();
226 return;
227 }
228
229 if (empty) {
230 break;
231 }
232
233 Object val = q.poll();
234
235 if (mode == LEFT_VALUE) {
236 @SuppressWarnings("unchecked")
237 TLeft left = (TLeft)val;
238
239 UnicastProcessor<TRight> up = UnicastProcessor.create();
240 int idx = leftIndex++;
241 lefts.put(idx, up);
242
243 Publisher<TLeftEnd> p;
244
245 try {
246 p = Objects.requireNonNull(leftEnd.apply(left), "The leftEnd returned a null Publisher");
247 } catch (Throwable exc) {
248 fail(exc, a, q);
249 return;
250 }
251
252 LeftRightEndSubscriber end = new LeftRightEndSubscriber(this, true, idx);
253 disposables.add(end);
254
255 p.subscribe(end);
256
257 ex = error.get();
258 if (ex != null) {
259 q.clear();
260 cancelAll();
261 errorAll(a);
262 return;
263 }
264
265 R w;
266
267 try {
268 w = Objects.requireNonNull(resultSelector.apply(left, up), "The resultSelector returned a null value");
269 } catch (Throwable exc) {
270 fail(exc, a, q);
271 return;
272 }
273
274 // TODO since only left emission calls the actual, it is possible to link downstream backpressure with left's source and not error out
275 if (requested.get() != 0L) {
276 a.onNext(w);
277 BackpressureHelper.produced(requested, 1);
278 } else {
279 fail(new MissingBackpressureException("Could not emit value due to lack of requests"), a, q);
280 return;
281 }
282
283 for (TRight right : rights.values()) {
284 up.onNext(right);
285 }
286 }
287 else if (mode == RIGHT_VALUE) {
288 @SuppressWarnings("unchecked")
289 TRight right = (TRight)val;
290
291 int idx = rightIndex++;
292
293 rights.put(idx, right);
294
295 Publisher<TRightEnd> p;
296
297 try {
298 p = Objects.requireNonNull(rightEnd.apply(right), "The rightEnd returned a null Publisher");
299 } catch (Throwable exc) {
300 fail(exc, a, q);
301 return;
302 }
303
304 LeftRightEndSubscriber end = new LeftRightEndSubscriber(this, false, idx);
305 disposables.add(end);
306
307 p.subscribe(end);
308
309 ex = error.get();
310 if (ex != null) {
311 q.clear();
312 cancelAll();
313 errorAll(a);
314 return;
315 }
316
317 for (UnicastProcessor<TRight> up : lefts.values()) {
318 up.onNext(right);
319 }
320 }
321 else if (mode == LEFT_CLOSE) {
322 LeftRightEndSubscriber end = (LeftRightEndSubscriber)val;
323
324 UnicastProcessor<TRight> up = lefts.remove(end.index);
325 disposables.remove(end);
326 if (up != null) {
327 up.onComplete();
328 }
329 }
330 else {
331 LeftRightEndSubscriber end = (LeftRightEndSubscriber)val;
332
333 rights.remove(end.index);
334 disposables.remove(end);
335 }
336 }
337
338 missed = addAndGet(-missed);
339 if (missed == 0) {
340 break;
341 }
342 }
343 }
344
345 @Override
346 public void innerError(Throwable ex) {
347 if (ExceptionHelper.addThrowable(error, ex)) {
348 active.decrementAndGet();
349 drain();
350 } else {
351 RxJavaPlugins.onError(ex);
352 }
353 }
354
355 @Override
356 public void innerComplete(LeftRightSubscriber sender) {
357 disposables.delete(sender);
358 active.decrementAndGet();
359 drain();
360 }
361
362 @Override
363 public void innerValue(boolean isLeft, Object o) {
364 synchronized (this) {
365 queue.offer(isLeft ? LEFT_VALUE : RIGHT_VALUE, o);
366 }
367 drain();
368 }
369
370 @Override
371 public void innerClose(boolean isLeft, LeftRightEndSubscriber index) {
372 synchronized (this) {
373 queue.offer(isLeft ? LEFT_CLOSE : RIGHT_CLOSE, index);
374 }
375 drain();
376 }
377
378 @Override
379 public void innerCloseError(Throwable ex) {
380 if (ExceptionHelper.addThrowable(error, ex)) {
381 drain();
382 } else {
383 RxJavaPlugins.onError(ex);
384 }
385 }
386 }
387
388 static final class LeftRightSubscriber
389 extends AtomicReference<Subscription>
390 implements FlowableSubscriber<Object>, Disposable {
391
392 private static final long serialVersionUID = 1883890389173668373L;
393
394 final JoinSupport parent;
395
396 final boolean isLeft;
397
398 LeftRightSubscriber(JoinSupport parent, boolean isLeft) {
399 this.parent = parent;
400 this.isLeft = isLeft;
401 }
402
403 @Override
404 public void dispose() {
405 SubscriptionHelper.cancel(this);
406 }
407
408 @Override
409 public boolean isDisposed() {
410 return get() == SubscriptionHelper.CANCELLED;
411 }
412
413 @Override
414 public void onSubscribe(Subscription s) {
415 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
416 }
417
418 @Override
419 public void onNext(Object t) {
420 parent.innerValue(isLeft, t);
421 }
422
423 @Override
424 public void onError(Throwable t) {
425 parent.innerError(t);
426 }
427
428 @Override
429 public void onComplete() {
430 parent.innerComplete(this);
431 }
432
433 }
434
435 static final class LeftRightEndSubscriber
436 extends AtomicReference<Subscription>
437 implements FlowableSubscriber<Object>, Disposable {
438
439 private static final long serialVersionUID = 1883890389173668373L;
440
441 final JoinSupport parent;
442
443 final boolean isLeft;
444
445 final int index;
446
447 LeftRightEndSubscriber(JoinSupport parent,
448 boolean isLeft, int index) {
449 this.parent = parent;
450 this.isLeft = isLeft;
451 this.index = index;
452 }
453
454 @Override
455 public void dispose() {
456 SubscriptionHelper.cancel(this);
457 }
458
459 @Override
460 public boolean isDisposed() {
461 return get() == SubscriptionHelper.CANCELLED;
462 }
463
464 @Override
465 public void onSubscribe(Subscription s) {
466 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
467 }
468
469 @Override
470 public void onNext(Object t) {
471 if (SubscriptionHelper.cancel(this)) {
472 parent.innerClose(isLeft, this);
473 }
474 }
475
476 @Override
477 public void onError(Throwable t) {
478 parent.innerCloseError(t);
479 }
480
481 @Override
482 public void onComplete() {
483 parent.innerClose(isLeft, this);
484 }
485
486 }
487
488 }