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