| 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.Flowable; |
| 22 |
import io.reactivex.rxjava3.disposables.CompositeDisposable; |
| 23 |
import io.reactivex.rxjava3.exceptions.*; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableGroupJoin.*; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 27 |
import io.reactivex.rxjava3.internal.util.*; |
| 28 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 29 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
|
| 32 |
public final class FlowableJoin<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 TRight, ? extends R> resultSelector; |
| 41 |
|
| 42 |
public FlowableJoin( |
| 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 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 |
JoinSubscription<TLeft, TRight, TLeftEnd, TRightEnd, R> parent = |
| 59 |
new JoinSubscription<>(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 |
static final class JoinSubscription<TLeft, TRight, TLeftEnd, TRightEnd, R> |
| 73 |
extends AtomicInteger implements Subscription, JoinSupport { |
| 74 |
|
| 75 |
private static final long serialVersionUID = -6071216598687999801L; |
| 76 |
|
| 77 |
final Subscriber<? super R> downstream; |
| 78 |
|
| 79 |
final AtomicLong requested; |
| 80 |
|
| 81 |
final SpscLinkedArrayQueue<Object> queue; |
| 82 |
|
| 83 |
final CompositeDisposable disposables; |
| 84 |
|
| 85 |
final Map<Integer, TLeft> lefts; |
| 86 |
|
| 87 |
final Map<Integer, TRight> rights; |
| 88 |
|
| 89 |
final AtomicReference<Throwable> error; |
| 90 |
|
| 91 |
final Function<? super TLeft, ? extends Publisher<TLeftEnd>> leftEnd; |
| 92 |
|
| 93 |
final Function<? super TRight, ? extends Publisher<TRightEnd>> rightEnd; |
| 94 |
|
| 95 |
final BiFunction<? super TLeft, ? super TRight, ? extends R> resultSelector; |
| 96 |
|
| 97 |
final AtomicInteger active; |
| 98 |
|
| 99 |
int leftIndex; |
| 100 |
|
| 101 |
int rightIndex; |
| 102 |
|
| 103 |
volatile boolean cancelled; |
| 104 |
|
| 105 |
static final Integer LEFT_VALUE = 1; |
| 106 |
|
| 107 |
static final Integer RIGHT_VALUE = 2; |
| 108 |
|
| 109 |
static final Integer LEFT_CLOSE = 3; |
| 110 |
|
| 111 |
static final Integer RIGHT_CLOSE = 4; |
| 112 |
|
| 113 |
JoinSubscription(Subscriber<? super R> actual, Function<? super TLeft, ? extends Publisher<TLeftEnd>> leftEnd, |
| 114 |
Function<? super TRight, ? extends Publisher<TRightEnd>> rightEnd, |
| 115 |
BiFunction<? super TLeft, ? super TRight, ? extends R> resultSelector) { |
| 116 |
this.downstream = actual; |
| 117 |
this.requested = new AtomicLong(); |
| 118 |
this.disposables = new CompositeDisposable(); |
| 119 |
this.queue = new SpscLinkedArrayQueue<>(bufferSize()); |
| 120 |
this.lefts = new LinkedHashMap<>(); |
| 121 |
this.rights = new LinkedHashMap<>(); |
| 122 |
this.error = new AtomicReference<>(); |
| 123 |
this.leftEnd = leftEnd; |
| 124 |
this.rightEnd = rightEnd; |
| 125 |
this.resultSelector = resultSelector; |
| 126 |
this.active = new AtomicInteger(2); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void request(long n) { |
| 131 |
if (SubscriptionHelper.validate(n)) { |
| 132 |
BackpressureHelper.add(requested, n); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void cancel() { |
| 138 |
if (cancelled) { |
| 139 |
return; |
| 140 |
} |
| 141 |
cancelled = true; |
| 142 |
cancelAll(); |
| 143 |
if (getAndIncrement() == 0) { |
| 144 |
queue.clear(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void cancelAll() { |
| 149 |
disposables.dispose(); |
| 150 |
} |
| 151 |
|
| 152 |
void errorAll(Subscriber<?> a) { |
| 153 |
Throwable ex = ExceptionHelper.terminate(error); |
| 154 |
|
| 155 |
lefts.clear(); |
| 156 |
rights.clear(); |
| 157 |
|
| 158 |
a.onError(ex); |
| 159 |
} |
| 160 |
|
| 161 |
void fail(Throwable exc, Subscriber<?> a, SimpleQueue<?> q) { |
| 162 |
Exceptions.throwIfFatal(exc); |
| 163 |
ExceptionHelper.addThrowable(error, exc); |
| 164 |
q.clear(); |
| 165 |
cancelAll(); |
| 166 |
errorAll(a); |
| 167 |
} |
| 168 |
|
| 169 |
void drain() { |
| 170 |
if (getAndIncrement() != 0) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
int missed = 1; |
| 175 |
SpscLinkedArrayQueue<Object> q = queue; |
| 176 |
Subscriber<? super R> a = downstream; |
| 177 |
|
| 178 |
for (;;) { |
| 179 |
for (;;) { |
| 180 |
if (cancelled) { |
| 181 |
q.clear(); |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
Throwable ex = error.get(); |
| 186 |
if (ex != null) { |
| 187 |
q.clear(); |
| 188 |
cancelAll(); |
| 189 |
errorAll(a); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
boolean d = active.get() == 0; |
| 194 |
|
| 195 |
Integer mode = (Integer)q.poll(); |
| 196 |
|
| 197 |
boolean empty = mode == null; |
| 198 |
|
| 199 |
if (d && empty) { |
| 200 |
|
| 201 |
lefts.clear(); |
| 202 |
rights.clear(); |
| 203 |
disposables.dispose(); |
| 204 |
|
| 205 |
a.onComplete(); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
if (empty) { |
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
Object val = q.poll(); |
| 214 |
|
| 215 |
if (mode == LEFT_VALUE) { |
| 216 |
@SuppressWarnings("unchecked") |
| 217 |
TLeft left = (TLeft)val; |
| 218 |
|
| 219 |
int idx = leftIndex++; |
| 220 |
lefts.put(idx, left); |
| 221 |
|
| 222 |
Publisher<TLeftEnd> p; |
| 223 |
|
| 224 |
try { |
| 225 |
p = Objects.requireNonNull(leftEnd.apply(left), "The leftEnd returned a null Publisher"); |
| 226 |
} catch (Throwable exc) { |
| 227 |
fail(exc, a, q); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
LeftRightEndSubscriber end = new LeftRightEndSubscriber(this, true, idx); |
| 232 |
disposables.add(end); |
| 233 |
|
| 234 |
p.subscribe(end); |
| 235 |
|
| 236 |
ex = error.get(); |
| 237 |
if (ex != null) { |
| 238 |
q.clear(); |
| 239 |
cancelAll(); |
| 240 |
errorAll(a); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
long r = requested.get(); |
| 245 |
long e = 0L; |
| 246 |
|
| 247 |
for (TRight right : rights.values()) { |
| 248 |
|
| 249 |
R w; |
| 250 |
|
| 251 |
try { |
| 252 |
w = Objects.requireNonNull(resultSelector.apply(left, right), "The resultSelector returned a null value"); |
| 253 |
} catch (Throwable exc) { |
| 254 |
fail(exc, a, q); |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
if (e != r) { |
| 259 |
a.onNext(w); |
| 260 |
|
| 261 |
e++; |
| 262 |
} else { |
| 263 |
ExceptionHelper.addThrowable(error, new MissingBackpressureException("Could not emit value due to lack of requests")); |
| 264 |
q.clear(); |
| 265 |
cancelAll(); |
| 266 |
errorAll(a); |
| 267 |
return; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if (e != 0L) { |
| 272 |
BackpressureHelper.produced(requested, e); |
| 273 |
} |
| 274 |
} |
| 275 |
else if (mode == RIGHT_VALUE) { |
| 276 |
@SuppressWarnings("unchecked") |
| 277 |
TRight right = (TRight)val; |
| 278 |
|
| 279 |
int idx = rightIndex++; |
| 280 |
|
| 281 |
rights.put(idx, right); |
| 282 |
|
| 283 |
Publisher<TRightEnd> p; |
| 284 |
|
| 285 |
try { |
| 286 |
p = Objects.requireNonNull(rightEnd.apply(right), "The rightEnd returned a null Publisher"); |
| 287 |
} catch (Throwable exc) { |
| 288 |
fail(exc, a, q); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
LeftRightEndSubscriber end = new LeftRightEndSubscriber(this, false, idx); |
| 293 |
disposables.add(end); |
| 294 |
|
| 295 |
p.subscribe(end); |
| 296 |
|
| 297 |
ex = error.get(); |
| 298 |
if (ex != null) { |
| 299 |
q.clear(); |
| 300 |
cancelAll(); |
| 301 |
errorAll(a); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
long r = requested.get(); |
| 306 |
long e = 0L; |
| 307 |
|
| 308 |
for (TLeft left : lefts.values()) { |
| 309 |
|
| 310 |
R w; |
| 311 |
|
| 312 |
try { |
| 313 |
w = Objects.requireNonNull(resultSelector.apply(left, right), "The resultSelector returned a null value"); |
| 314 |
} catch (Throwable exc) { |
| 315 |
fail(exc, a, q); |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
if (e != r) { |
| 320 |
a.onNext(w); |
| 321 |
|
| 322 |
e++; |
| 323 |
} else { |
| 324 |
ExceptionHelper.addThrowable(error, new MissingBackpressureException("Could not emit value due to lack of requests")); |
| 325 |
q.clear(); |
| 326 |
cancelAll(); |
| 327 |
errorAll(a); |
| 328 |
return; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if (e != 0L) { |
| 333 |
BackpressureHelper.produced(requested, e); |
| 334 |
} |
| 335 |
} |
| 336 |
else if (mode == LEFT_CLOSE) { |
| 337 |
LeftRightEndSubscriber end = (LeftRightEndSubscriber)val; |
| 338 |
|
| 339 |
lefts.remove(end.index); |
| 340 |
disposables.remove(end); |
| 341 |
} |
| 342 |
else { |
| 343 |
LeftRightEndSubscriber end = (LeftRightEndSubscriber)val; |
| 344 |
|
| 345 |
rights.remove(end.index); |
| 346 |
disposables.remove(end); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
missed = addAndGet(-missed); |
| 351 |
if (missed == 0) { |
| 352 |
break; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
@Override |
| 358 |
public void innerError(Throwable ex) { |
| 359 |
if (ExceptionHelper.addThrowable(error, ex)) { |
| 360 |
active.decrementAndGet(); |
| 361 |
drain(); |
| 362 |
} else { |
| 363 |
RxJavaPlugins.onError(ex); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public void innerComplete(LeftRightSubscriber sender) { |
| 369 |
disposables.delete(sender); |
| 370 |
active.decrementAndGet(); |
| 371 |
drain(); |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public void innerValue(boolean isLeft, Object o) { |
| 376 |
synchronized (this) { |
| 377 |
queue.offer(isLeft ? LEFT_VALUE : RIGHT_VALUE, o); |
| 378 |
} |
| 379 |
drain(); |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
public void innerClose(boolean isLeft, LeftRightEndSubscriber index) { |
| 384 |
synchronized (this) { |
| 385 |
queue.offer(isLeft ? LEFT_CLOSE : RIGHT_CLOSE, index); |
| 386 |
} |
| 387 |
drain(); |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public void innerCloseError(Throwable ex) { |
| 392 |
if (ExceptionHelper.addThrowable(error, ex)) { |
| 393 |
drain(); |
| 394 |
} else { |
| 395 |
RxJavaPlugins.onError(ex); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |