| 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.annotations.Nullable; |
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.*; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 26 |
import io.reactivex.rxjava3.internal.util.*; |
| 27 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 28 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 29 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
|
| 32 |
public final class FlowableFlattenIterable<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 33 |
|
| 34 |
final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 35 |
|
| 36 |
final int prefetch; |
| 37 |
|
| 38 |
public FlowableFlattenIterable(Flowable<T> source, |
| 39 |
Function<? super T, ? extends Iterable<? extends R>> mapper, int prefetch) { |
| 40 |
super(source); |
| 41 |
this.mapper = mapper; |
| 42 |
this.prefetch = prefetch; |
| 43 |
} |
| 44 |
|
| 45 |
@SuppressWarnings("unchecked") |
| 46 |
@Override |
| 47 |
public void subscribeActual(Subscriber<? super R> s) { |
| 48 |
if (source instanceof Supplier) { |
| 49 |
T v; |
| 50 |
|
| 51 |
try { |
| 52 |
v = ((Supplier<T>)source).get(); |
| 53 |
} catch (Throwable ex) { |
| 54 |
Exceptions.throwIfFatal(ex); |
| 55 |
EmptySubscription.error(ex, s); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if (v == null) { |
| 60 |
EmptySubscription.complete(s); |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
Iterator<? extends R> it; |
| 65 |
|
| 66 |
try { |
| 67 |
Iterable<? extends R> iterable = mapper.apply(v); |
| 68 |
|
| 69 |
it = iterable.iterator(); |
| 70 |
} catch (Throwable ex) { |
| 71 |
Exceptions.throwIfFatal(ex); |
| 72 |
EmptySubscription.error(ex, s); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
FlowableFromIterable.subscribe(s, it); |
| 77 |
|
| 78 |
return; |
| 79 |
} |
| 80 |
source.subscribe(new FlattenIterableSubscriber<>(s, mapper, prefetch)); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Create a {@link Subscriber} with the given parameters. |
| 85 |
* @param <T> the upstream value type |
| 86 |
* @param <R> the {@link Iterable} and output value type |
| 87 |
* @param downstream the downstream {@code Subscriber} to wrap |
| 88 |
* @param mapper the mapper function |
| 89 |
* @param prefetch the number of items to prefetch |
| 90 |
* @return the new {@code Subscriber} |
| 91 |
*/ |
| 92 |
public static <T, R> Subscriber<T> subscribe(Subscriber<? super R> downstream, Function<? super T, ? extends Iterable<? extends R>> mapper, int prefetch) { |
| 93 |
return new FlattenIterableSubscriber<>(downstream, mapper, prefetch); |
| 94 |
} |
| 95 |
|
| 96 |
static final class FlattenIterableSubscriber<T, R> |
| 97 |
extends BasicIntQueueSubscription<R> |
| 98 |
implements FlowableSubscriber<T> { |
| 99 |
|
| 100 |
private static final long serialVersionUID = -3096000382929934955L; |
| 101 |
|
| 102 |
final Subscriber<? super R> downstream; |
| 103 |
|
| 104 |
final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 105 |
|
| 106 |
final int prefetch; |
| 107 |
|
| 108 |
final int limit; |
| 109 |
|
| 110 |
final AtomicLong requested; |
| 111 |
|
| 112 |
Subscription upstream; |
| 113 |
|
| 114 |
SimpleQueue<T> queue; |
| 115 |
|
| 116 |
volatile boolean done; |
| 117 |
|
| 118 |
volatile boolean cancelled; |
| 119 |
|
| 120 |
final AtomicReference<Throwable> error; |
| 121 |
|
| 122 |
Iterator<? extends R> current; |
| 123 |
|
| 124 |
int consumed; |
| 125 |
|
| 126 |
int fusionMode; |
| 127 |
|
| 128 |
FlattenIterableSubscriber(Subscriber<? super R> actual, |
| 129 |
Function<? super T, ? extends Iterable<? extends R>> mapper, int prefetch) { |
| 130 |
this.downstream = actual; |
| 131 |
this.mapper = mapper; |
| 132 |
this.prefetch = prefetch; |
| 133 |
this.limit = prefetch - (prefetch >> 2); |
| 134 |
this.error = new AtomicReference<>(); |
| 135 |
this.requested = new AtomicLong(); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void onSubscribe(Subscription s) { |
| 140 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 141 |
this.upstream = s; |
| 142 |
|
| 143 |
if (s instanceof QueueSubscription) { |
| 144 |
@SuppressWarnings("unchecked") |
| 145 |
QueueSubscription<T> qs = (QueueSubscription<T>) s; |
| 146 |
|
| 147 |
int m = qs.requestFusion(ANY); |
| 148 |
|
| 149 |
if (m == SYNC) { |
| 150 |
fusionMode = m; |
| 151 |
this.queue = qs; |
| 152 |
done = true; |
| 153 |
|
| 154 |
downstream.onSubscribe(this); |
| 155 |
|
| 156 |
return; |
| 157 |
} |
| 158 |
if (m == ASYNC) { |
| 159 |
fusionMode = m; |
| 160 |
this.queue = qs; |
| 161 |
|
| 162 |
downstream.onSubscribe(this); |
| 163 |
|
| 164 |
s.request(prefetch); |
| 165 |
return; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
queue = new SpscArrayQueue<>(prefetch); |
| 170 |
|
| 171 |
downstream.onSubscribe(this); |
| 172 |
|
| 173 |
s.request(prefetch); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public void onNext(T t) { |
| 179 |
if (done) { |
| 180 |
return; |
| 181 |
} |
| 182 |
if (fusionMode == NONE && !queue.offer(t)) { |
| 183 |
onError(new MissingBackpressureException("Queue is full?!")); |
| 184 |
return; |
| 185 |
} |
| 186 |
drain(); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public void onError(Throwable t) { |
| 191 |
if (!done && ExceptionHelper.addThrowable(error, t)) { |
| 192 |
done = true; |
| 193 |
drain(); |
| 194 |
} else { |
| 195 |
RxJavaPlugins.onError(t); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public void onComplete() { |
| 201 |
if (done) { |
| 202 |
return; |
| 203 |
} |
| 204 |
done = true; |
| 205 |
drain(); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public void request(long n) { |
| 210 |
if (SubscriptionHelper.validate(n)) { |
| 211 |
BackpressureHelper.add(requested, n); |
| 212 |
drain(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public void cancel() { |
| 218 |
if (!cancelled) { |
| 219 |
cancelled = true; |
| 220 |
|
| 221 |
upstream.cancel(); |
| 222 |
|
| 223 |
if (getAndIncrement() == 0) { |
| 224 |
queue.clear(); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
void drain() { |
| 230 |
if (getAndIncrement() != 0) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
final Subscriber<? super R> a = downstream; |
| 235 |
final SimpleQueue<T> q = queue; |
| 236 |
final boolean replenish = fusionMode != SYNC; |
| 237 |
|
| 238 |
int missed = 1; |
| 239 |
|
| 240 |
Iterator<? extends R> it = current; |
| 241 |
|
| 242 |
for (;;) { |
| 243 |
|
| 244 |
if (it == null) { |
| 245 |
|
| 246 |
boolean d = done; |
| 247 |
|
| 248 |
T t; |
| 249 |
|
| 250 |
try { |
| 251 |
t = q.poll(); |
| 252 |
} catch (Throwable ex) { |
| 253 |
Exceptions.throwIfFatal(ex); |
| 254 |
upstream.cancel(); |
| 255 |
ExceptionHelper.addThrowable(error, ex); |
| 256 |
ex = ExceptionHelper.terminate(error); |
| 257 |
|
| 258 |
current = null; |
| 259 |
q.clear(); |
| 260 |
|
| 261 |
a.onError(ex); |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
boolean empty = t == null; |
| 266 |
|
| 267 |
if (checkTerminated(d, empty, a, q)) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
if (t != null) { |
| 272 |
Iterable<? extends R> iterable; |
| 273 |
|
| 274 |
boolean b; |
| 275 |
|
| 276 |
try { |
| 277 |
iterable = mapper.apply(t); |
| 278 |
|
| 279 |
it = iterable.iterator(); |
| 280 |
|
| 281 |
b = it.hasNext(); |
| 282 |
} catch (Throwable ex) { |
| 283 |
Exceptions.throwIfFatal(ex); |
| 284 |
upstream.cancel(); |
| 285 |
ExceptionHelper.addThrowable(error, ex); |
| 286 |
ex = ExceptionHelper.terminate(error); |
| 287 |
a.onError(ex); |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
if (!b) { |
| 292 |
it = null; |
| 293 |
consumedOne(replenish); |
| 294 |
continue; |
| 295 |
} |
| 296 |
|
| 297 |
current = it; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if (it != null) { |
| 302 |
long r = requested.get(); |
| 303 |
long e = 0L; |
| 304 |
|
| 305 |
while (e != r) { |
| 306 |
if (checkTerminated(done, false, a, q)) { |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
R v; |
| 311 |
|
| 312 |
try { |
| 313 |
v = Objects.requireNonNull(it.next(), "The iterator returned a null value"); |
| 314 |
} catch (Throwable ex) { |
| 315 |
Exceptions.throwIfFatal(ex); |
| 316 |
current = null; |
| 317 |
upstream.cancel(); |
| 318 |
ExceptionHelper.addThrowable(error, ex); |
| 319 |
ex = ExceptionHelper.terminate(error); |
| 320 |
a.onError(ex); |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
a.onNext(v); |
| 325 |
|
| 326 |
if (checkTerminated(done, false, a, q)) { |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
e++; |
| 331 |
|
| 332 |
boolean b; |
| 333 |
|
| 334 |
try { |
| 335 |
b = it.hasNext(); |
| 336 |
} catch (Throwable ex) { |
| 337 |
Exceptions.throwIfFatal(ex); |
| 338 |
current = null; |
| 339 |
upstream.cancel(); |
| 340 |
ExceptionHelper.addThrowable(error, ex); |
| 341 |
ex = ExceptionHelper.terminate(error); |
| 342 |
a.onError(ex); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
if (!b) { |
| 347 |
consumedOne(replenish); |
| 348 |
it = null; |
| 349 |
current = null; |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if (e == r) { |
| 355 |
boolean d = done; |
| 356 |
boolean empty = q.isEmpty() && it == null; |
| 357 |
|
| 358 |
if (checkTerminated(d, empty, a, q)) { |
| 359 |
return; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if (e != 0L) { |
| 364 |
if (r != Long.MAX_VALUE) { |
| 365 |
requested.addAndGet(-e); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
if (it == null) { |
| 370 |
continue; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
missed = addAndGet(-missed); |
| 375 |
if (missed == 0) { |
| 376 |
break; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
void consumedOne(boolean enabled) { |
| 382 |
if (enabled) { |
| 383 |
int c = consumed + 1; |
| 384 |
if (c == limit) { |
| 385 |
consumed = 0; |
| 386 |
upstream.request(c); |
| 387 |
} else { |
| 388 |
consumed = c; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
boolean checkTerminated(boolean d, boolean empty, Subscriber<?> a, SimpleQueue<?> q) { |
| 394 |
if (cancelled) { |
| 395 |
current = null; |
| 396 |
q.clear(); |
| 397 |
return true; |
| 398 |
} |
| 399 |
if (d) { |
| 400 |
Throwable ex = error.get(); |
| 401 |
if (ex != null) { |
| 402 |
ex = ExceptionHelper.terminate(error); |
| 403 |
|
| 404 |
current = null; |
| 405 |
q.clear(); |
| 406 |
|
| 407 |
a.onError(ex); |
| 408 |
return true; |
| 409 |
} else if (empty) { |
| 410 |
a.onComplete(); |
| 411 |
return true; |
| 412 |
} |
| 413 |
} |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
@Override |
| 418 |
public void clear() { |
| 419 |
current = null; |
| 420 |
queue.clear(); |
| 421 |
} |
| 422 |
|
| 423 |
@Override |
| 424 |
public boolean isEmpty() { |
| 425 |
return current == null && queue.isEmpty(); |
| 426 |
} |
| 427 |
|
| 428 |
@Nullable |
| 429 |
@Override |
| 430 |
public R poll() throws Throwable { |
| 431 |
Iterator<? extends R> it = current; |
| 432 |
for (;;) { |
| 433 |
if (it == null) { |
| 434 |
T v = queue.poll(); |
| 435 |
if (v == null) { |
| 436 |
return null; |
| 437 |
} |
| 438 |
|
| 439 |
it = mapper.apply(v).iterator(); |
| 440 |
|
| 441 |
if (!it.hasNext()) { |
| 442 |
it = null; |
| 443 |
continue; |
| 444 |
} |
| 445 |
current = it; |
| 446 |
} |
| 447 |
|
| 448 |
R r = Objects.requireNonNull(it.next(), "The iterator returned a null value"); |
| 449 |
|
| 450 |
if (!it.hasNext()) { |
| 451 |
current = null; |
| 452 |
} |
| 453 |
|
| 454 |
return r; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
@Override |
| 459 |
public int requestFusion(int requestedMode) { |
| 460 |
if ((requestedMode & SYNC) != 0 && fusionMode == SYNC) { |
| 461 |
return SYNC; |
| 462 |
} |
| 463 |
return NONE; |
| 464 |
} |
| 465 |
} |
| 466 |
} |