| 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.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.internal.util.*; |
| 26 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 27 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 28 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
|
| 31 |
public final class FlowableSwitchMap<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 32 |
final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 33 |
final int bufferSize; |
| 34 |
final boolean delayErrors; |
| 35 |
|
| 36 |
public FlowableSwitchMap(Flowable<T> source, |
| 37 |
Function<? super T, ? extends Publisher<? extends R>> mapper, int bufferSize, |
| 38 |
boolean delayErrors) { |
| 39 |
super(source); |
| 40 |
this.mapper = mapper; |
| 41 |
this.bufferSize = bufferSize; |
| 42 |
this.delayErrors = delayErrors; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 47 |
if (FlowableScalarXMap.tryScalarXMapSubscribe(source, s, mapper)) { |
| 48 |
return; |
| 49 |
} |
| 50 |
source.subscribe(new SwitchMapSubscriber<>(s, mapper, bufferSize, delayErrors)); |
| 51 |
} |
| 52 |
|
| 53 |
static final class SwitchMapSubscriber<T, R> extends AtomicInteger implements FlowableSubscriber<T>, Subscription { |
| 54 |
|
| 55 |
private static final long serialVersionUID = -3491074160481096299L; |
| 56 |
final Subscriber<? super R> downstream; |
| 57 |
final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 58 |
final int bufferSize; |
| 59 |
final boolean delayErrors; |
| 60 |
|
| 61 |
volatile boolean done; |
| 62 |
final AtomicThrowable errors; |
| 63 |
|
| 64 |
volatile boolean cancelled; |
| 65 |
|
| 66 |
Subscription upstream; |
| 67 |
|
| 68 |
final AtomicReference<SwitchMapInnerSubscriber<T, R>> active = new AtomicReference<>(); |
| 69 |
|
| 70 |
final AtomicLong requested = new AtomicLong(); |
| 71 |
|
| 72 |
static final SwitchMapInnerSubscriber<Object, Object> CANCELLED; |
| 73 |
static { |
| 74 |
CANCELLED = new SwitchMapInnerSubscriber<>(null, -1L, 1); |
| 75 |
CANCELLED.cancel(); |
| 76 |
} |
| 77 |
|
| 78 |
volatile long unique; |
| 79 |
|
| 80 |
SwitchMapSubscriber(Subscriber<? super R> actual, |
| 81 |
Function<? super T, ? extends Publisher<? extends R>> mapper, int bufferSize, |
| 82 |
boolean delayErrors) { |
| 83 |
this.downstream = actual; |
| 84 |
this.mapper = mapper; |
| 85 |
this.bufferSize = bufferSize; |
| 86 |
this.delayErrors = delayErrors; |
| 87 |
this.errors = new AtomicThrowable(); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void onSubscribe(Subscription s) { |
| 92 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 93 |
this.upstream = s; |
| 94 |
downstream.onSubscribe(this); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public void onNext(T t) { |
| 100 |
if (done) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
long c = unique + 1; |
| 105 |
unique = c; |
| 106 |
|
| 107 |
SwitchMapInnerSubscriber<T, R> inner = active.get(); |
| 108 |
if (inner != null) { |
| 109 |
inner.cancel(); |
| 110 |
} |
| 111 |
|
| 112 |
Publisher<? extends R> p; |
| 113 |
try { |
| 114 |
p = Objects.requireNonNull(mapper.apply(t), "The publisher returned is null"); |
| 115 |
} catch (Throwable e) { |
| 116 |
Exceptions.throwIfFatal(e); |
| 117 |
upstream.cancel(); |
| 118 |
onError(e); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
SwitchMapInnerSubscriber<T, R> nextInner = new SwitchMapInnerSubscriber<>(this, c, bufferSize); |
| 123 |
|
| 124 |
for (;;) { |
| 125 |
inner = active.get(); |
| 126 |
if (inner == CANCELLED) { |
| 127 |
break; |
| 128 |
} |
| 129 |
if (active.compareAndSet(inner, nextInner)) { |
| 130 |
p.subscribe(nextInner); |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onError(Throwable t) { |
| 138 |
if (!done && errors.tryAddThrowable(t)) { |
| 139 |
if (!delayErrors) { |
| 140 |
disposeInner(); |
| 141 |
} |
| 142 |
done = true; |
| 143 |
drain(); |
| 144 |
} else { |
| 145 |
RxJavaPlugins.onError(t); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onComplete() { |
| 151 |
if (done) { |
| 152 |
return; |
| 153 |
} |
| 154 |
done = true; |
| 155 |
drain(); |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public void request(long n) { |
| 160 |
if (SubscriptionHelper.validate(n)) { |
| 161 |
BackpressureHelper.add(requested, n); |
| 162 |
if (unique == 0L) { |
| 163 |
upstream.request(Long.MAX_VALUE); |
| 164 |
} else { |
| 165 |
drain(); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public void cancel() { |
| 172 |
if (!cancelled) { |
| 173 |
cancelled = true; |
| 174 |
upstream.cancel(); |
| 175 |
|
| 176 |
disposeInner(); |
| 177 |
|
| 178 |
errors.tryTerminateAndReport(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
@SuppressWarnings("unchecked") |
| 183 |
void disposeInner() { |
| 184 |
SwitchMapInnerSubscriber<T, R> a = active.getAndSet((SwitchMapInnerSubscriber<T, R>)CANCELLED); |
| 185 |
if (a != CANCELLED && a != null) { |
| 186 |
a.cancel(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void drain() { |
| 191 |
if (getAndIncrement() != 0) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
final Subscriber<? super R> a = downstream; |
| 196 |
|
| 197 |
int missing = 1; |
| 198 |
|
| 199 |
for (;;) { |
| 200 |
|
| 201 |
if (cancelled) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if (done) { |
| 206 |
if (delayErrors) { |
| 207 |
if (active.get() == null) { |
| 208 |
errors.tryTerminateConsumer(a); |
| 209 |
return; |
| 210 |
} |
| 211 |
} else { |
| 212 |
Throwable err = errors.get(); |
| 213 |
if (err != null) { |
| 214 |
disposeInner(); |
| 215 |
errors.tryTerminateConsumer(a); |
| 216 |
return; |
| 217 |
} else |
| 218 |
if (active.get() == null) { |
| 219 |
a.onComplete(); |
| 220 |
return; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
SwitchMapInnerSubscriber<T, R> inner = active.get(); |
| 226 |
SimpleQueue<R> q = inner != null ? inner.queue : null; |
| 227 |
if (q != null) { |
| 228 |
long r = requested.get(); |
| 229 |
long e = 0L; |
| 230 |
boolean retry = false; |
| 231 |
|
| 232 |
while (e != r) { |
| 233 |
if (cancelled) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
boolean d = inner.done; |
| 238 |
R v; |
| 239 |
|
| 240 |
try { |
| 241 |
v = q.poll(); |
| 242 |
} catch (Throwable ex) { |
| 243 |
Exceptions.throwIfFatal(ex); |
| 244 |
inner.cancel(); |
| 245 |
errors.tryAddThrowableOrReport(ex); |
| 246 |
d = true; |
| 247 |
v = null; |
| 248 |
} |
| 249 |
boolean empty = v == null; |
| 250 |
|
| 251 |
if (inner != active.get()) { |
| 252 |
retry = true; |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
if (d) { |
| 257 |
if (!delayErrors) { |
| 258 |
Throwable err = errors.get(); |
| 259 |
if (err != null) { |
| 260 |
errors.tryTerminateConsumer(a); |
| 261 |
return; |
| 262 |
} else |
| 263 |
if (empty) { |
| 264 |
active.compareAndSet(inner, null); |
| 265 |
retry = true; |
| 266 |
break; |
| 267 |
} |
| 268 |
} else { |
| 269 |
if (empty) { |
| 270 |
active.compareAndSet(inner, null); |
| 271 |
retry = true; |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
if (empty) { |
| 278 |
break; |
| 279 |
} |
| 280 |
|
| 281 |
a.onNext(v); |
| 282 |
|
| 283 |
e++; |
| 284 |
} |
| 285 |
|
| 286 |
if (e == r) { |
| 287 |
if (inner.done) { |
| 288 |
if (!delayErrors) { |
| 289 |
Throwable err = errors.get(); |
| 290 |
if (err != null) { |
| 291 |
disposeInner(); |
| 292 |
errors.tryTerminateConsumer(a); |
| 293 |
return; |
| 294 |
} else |
| 295 |
if (q.isEmpty()) { |
| 296 |
active.compareAndSet(inner, null); |
| 297 |
continue; |
| 298 |
} |
| 299 |
} else { |
| 300 |
if (q.isEmpty()) { |
| 301 |
active.compareAndSet(inner, null); |
| 302 |
continue; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (e != 0L) { |
| 309 |
if (!cancelled) { |
| 310 |
if (r != Long.MAX_VALUE) { |
| 311 |
requested.addAndGet(-e); |
| 312 |
} |
| 313 |
inner.request(e); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if (retry) { |
| 318 |
continue; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
missing = addAndGet(-missing); |
| 323 |
if (missing == 0) { |
| 324 |
break; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
static final class SwitchMapInnerSubscriber<T, R> |
| 331 |
extends AtomicReference<Subscription> implements FlowableSubscriber<R> { |
| 332 |
|
| 333 |
private static final long serialVersionUID = 3837284832786408377L; |
| 334 |
final SwitchMapSubscriber<T, R> parent; |
| 335 |
final long index; |
| 336 |
final int bufferSize; |
| 337 |
|
| 338 |
volatile SimpleQueue<R> queue; |
| 339 |
|
| 340 |
volatile boolean done; |
| 341 |
|
| 342 |
int fusionMode; |
| 343 |
|
| 344 |
SwitchMapInnerSubscriber(SwitchMapSubscriber<T, R> parent, long index, int bufferSize) { |
| 345 |
this.parent = parent; |
| 346 |
this.index = index; |
| 347 |
this.bufferSize = bufferSize; |
| 348 |
} |
| 349 |
|
| 350 |
@Override |
| 351 |
public void onSubscribe(Subscription s) { |
| 352 |
if (SubscriptionHelper.setOnce(this, s)) { |
| 353 |
if (s instanceof QueueSubscription) { |
| 354 |
@SuppressWarnings("unchecked") |
| 355 |
QueueSubscription<R> qs = (QueueSubscription<R>) s; |
| 356 |
|
| 357 |
int m = qs.requestFusion(QueueSubscription.ANY | QueueSubscription.BOUNDARY); |
| 358 |
if (m == QueueSubscription.SYNC) { |
| 359 |
fusionMode = m; |
| 360 |
queue = qs; |
| 361 |
done = true; |
| 362 |
parent.drain(); |
| 363 |
return; |
| 364 |
} |
| 365 |
if (m == QueueSubscription.ASYNC) { |
| 366 |
fusionMode = m; |
| 367 |
queue = qs; |
| 368 |
s.request(bufferSize); |
| 369 |
return; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
queue = new SpscArrayQueue<>(bufferSize); |
| 374 |
|
| 375 |
s.request(bufferSize); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public void onNext(R t) { |
| 381 |
SwitchMapSubscriber<T, R> p = parent; |
| 382 |
if (index == p.unique) { |
| 383 |
if (fusionMode == QueueSubscription.NONE && !queue.offer(t)) { |
| 384 |
onError(new MissingBackpressureException("Queue full?!")); |
| 385 |
return; |
| 386 |
} |
| 387 |
p.drain(); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
@Override |
| 392 |
public void onError(Throwable t) { |
| 393 |
SwitchMapSubscriber<T, R> p = parent; |
| 394 |
if (index == p.unique && p.errors.tryAddThrowable(t)) { |
| 395 |
if (!p.delayErrors) { |
| 396 |
p.upstream.cancel(); |
| 397 |
p.done = true; |
| 398 |
} |
| 399 |
done = true; |
| 400 |
p.drain(); |
| 401 |
} else { |
| 402 |
RxJavaPlugins.onError(t); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
@Override |
| 407 |
public void onComplete() { |
| 408 |
SwitchMapSubscriber<T, R> p = parent; |
| 409 |
if (index == p.unique) { |
| 410 |
done = true; |
| 411 |
p.drain(); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
public void cancel() { |
| 416 |
SubscriptionHelper.cancel(this); |
| 417 |
} |
| 418 |
|
| 419 |
public void request(long n) { |
| 420 |
if (fusionMode != QueueSubscription.SYNC) { |
| 421 |
get().request(n); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
} |