Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableWindowBoundarySelector.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.Function;
25 import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue;
26 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
27 import io.reactivex.rxjava3.internal.util.*;
28 import io.reactivex.rxjava3.operators.SimplePlainQueue;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.UnicastProcessor;
31
32 public final class FlowableWindowBoundarySelector<T, B, V> extends AbstractFlowableWithUpstream<T, Flowable<T>> {
33 final Publisher<B> open;
34 final Function<? super B, ? extends Publisher<V>> closingIndicator;
35 final int bufferSize;
36
37 public FlowableWindowBoundarySelector(
38 Flowable<T> source,
39 Publisher<B> open, Function<? super B, ? extends Publisher<V>> closingIndicator,
40 int bufferSize) {
41 super(source);
42 this.open = open;
43 this.closingIndicator = closingIndicator;
44 this.bufferSize = bufferSize;
45 }
46
47 @Override
48 protected void subscribeActual(Subscriber<? super Flowable<T>> s) {
49 source.subscribe(new WindowBoundaryMainSubscriber<>(
50 s, open, closingIndicator, bufferSize));
51 }
52
53 static final class WindowBoundaryMainSubscriber<T, B, V>
54 extends AtomicInteger
55 implements FlowableSubscriber<T>, Subscription, Runnable {
56 private static final long serialVersionUID = 8646217640096099753L;
57
58 final Subscriber<? super Flowable<T>> downstream;
59 final Publisher<B> open;
60 final Function<? super B, ? extends Publisher<V>> closingIndicator;
61 final int bufferSize;
62 final CompositeDisposable resources;
63
64 final WindowStartSubscriber<B> startSubscriber;
65
66 final List<UnicastProcessor<T>> windows;
67
68 final SimplePlainQueue<Object> queue;
69
70 final AtomicLong windowCount;
71
72 final AtomicBoolean downstreamCancelled;
73
74 final AtomicLong requested;
75 long emitted;
76
77 volatile boolean upstreamCanceled;
78
79 volatile boolean upstreamDone;
80 volatile boolean openDone;
81 final AtomicThrowable error;
82
83 Subscription upstream;
84
85 WindowBoundaryMainSubscriber(Subscriber<? super Flowable<T>> actual,
86 Publisher<B> open, Function<? super B, ? extends Publisher<V>> closingIndicator, int bufferSize) {
87 this.downstream = actual;
88 this.queue = new MpscLinkedQueue<>();
89 this.open = open;
90 this.closingIndicator = closingIndicator;
91 this.bufferSize = bufferSize;
92 this.resources = new CompositeDisposable();
93 this.windows = new ArrayList<>();
94 this.windowCount = new AtomicLong(1L);
95 this.downstreamCancelled = new AtomicBoolean();
96 this.error = new AtomicThrowable();
97 this.startSubscriber = new WindowStartSubscriber<>(this);
98 this.requested = new AtomicLong();
99 }
100
101 @Override
102 public void onSubscribe(Subscription s) {
103 if (SubscriptionHelper.validate(this.upstream, s)) {
104 this.upstream = s;
105
106 downstream.onSubscribe(this);
107
108 open.subscribe(startSubscriber);
109
110 s.request(Long.MAX_VALUE);
111 }
112 }
113
114 @Override
115 public void onNext(T t) {
116 queue.offer(t);
117 drain();
118 }
119
120 @Override
121 public void onError(Throwable t) {
122 startSubscriber.cancel();
123 resources.dispose();
124 if (error.tryAddThrowableOrReport(t)) {
125 upstreamDone = true;
126 drain();
127 }
128 }
129
130 @Override
131 public void onComplete() {
132 startSubscriber.cancel();
133 resources.dispose();
134 upstreamDone = true;
135 drain();
136 }
137
138 @Override
139 public void request(long n) {
140 if (SubscriptionHelper.validate(n)) {
141 BackpressureHelper.add(requested, n);
142 }
143 }
144
145 @Override
146 public void cancel() {
147 if (downstreamCancelled.compareAndSet(false, true)) {
148 if (windowCount.decrementAndGet() == 0) {
149 upstream.cancel();
150 startSubscriber.cancel();
151 resources.dispose();
152 error.tryTerminateAndReport();
153 upstreamCanceled = true;
154 drain();
155 } else {
156 startSubscriber.cancel();
157 }
158 }
159 }
160
161 @Override
162 public void run() {
163 if (windowCount.decrementAndGet() == 0) {
164 upstream.cancel();
165 startSubscriber.cancel();
166 resources.dispose();
167 error.tryTerminateAndReport();
168 upstreamCanceled = true;
169 drain();
170 }
171 }
172
173 void open(B startValue) {
174 queue.offer(new WindowStartItem<>(startValue));
175 drain();
176 }
177
178 void openError(Throwable t) {
179 upstream.cancel();
180 resources.dispose();
181 if (error.tryAddThrowableOrReport(t)) {
182 upstreamDone = true;
183 drain();
184 }
185 }
186
187 void openComplete() {
188 openDone = true;
189 drain();
190 }
191
192 void close(WindowEndSubscriberIntercept<T, V> what) {
193 queue.offer(what);
194 drain();
195 }
196
197 void closeError(Throwable t) {
198 upstream.cancel();
199 startSubscriber.cancel();
200 resources.dispose();
201 if (error.tryAddThrowableOrReport(t)) {
202 upstreamDone = true;
203 drain();
204 }
205 }
206
207 void drain() {
208 if (getAndIncrement() != 0) {
209 return;
210 }
211
212 int missed = 1;
213 final Subscriber<? super Flowable<T>> downstream = this.downstream;
214 final SimplePlainQueue<Object> queue = this.queue;
215 final List<UnicastProcessor<T>> windows = this.windows;
216
217 for (;;) {
218 if (upstreamCanceled) {
219 queue.clear();
220 windows.clear();
221 } else {
222 boolean isDone = upstreamDone;
223 Object o = queue.poll();
224 boolean isEmpty = o == null;
225
226 if (isDone) {
227 if (isEmpty || error.get() != null) {
228 terminateDownstream(downstream);
229 upstreamCanceled = true;
230 continue;
231 }
232 }
233
234 if (!isEmpty) {
235 if (o instanceof WindowStartItem) {
236 if (!downstreamCancelled.get()) {
237 long emitted = this.emitted;
238 if (requested.get() != emitted) {
239 this.emitted = ++emitted;
240
241 @SuppressWarnings("unchecked")
242 B startItem = ((WindowStartItem<B>)o).item;
243
244 Publisher<V> endSource;
245 try {
246 endSource = Objects.requireNonNull(closingIndicator.apply(startItem), "The closingIndicator returned a null Publisher");
247 } catch (Throwable ex) {
248 Exceptions.throwIfFatal(ex);
249 upstream.cancel();
250 startSubscriber.cancel();
251 resources.dispose();
252 Exceptions.throwIfFatal(ex);
253 error.tryAddThrowableOrReport(ex);
254 upstreamDone = true;
255 continue;
256 }
257
258 windowCount.getAndIncrement();
259 UnicastProcessor<T> newWindow = UnicastProcessor.create(bufferSize, this);
260 WindowEndSubscriberIntercept<T, V> endSubscriber = new WindowEndSubscriberIntercept<>(this, newWindow);
261
262 downstream.onNext(endSubscriber);
263
264 if (endSubscriber.tryAbandon()) {
265 newWindow.onComplete();
266 } else {
267 windows.add(newWindow);
268 resources.add(endSubscriber);
269 endSource.subscribe(endSubscriber);
270 }
271 } else {
272 upstream.cancel();
273 startSubscriber.cancel();
274 resources.dispose();
275 error.tryAddThrowableOrReport(new MissingBackpressureException(FlowableWindowTimed.missingBackpressureMessage(emitted)));
276 upstreamDone = true;
277 }
278 }
279 }
280 else if (o instanceof WindowEndSubscriberIntercept) {
281 @SuppressWarnings("unchecked")
282 UnicastProcessor<T> w = ((WindowEndSubscriberIntercept<T, V>)o).window;
283
284 windows.remove(w);
285 resources.delete((Disposable)o);
286 w.onComplete();
287 } else {
288 @SuppressWarnings("unchecked")
289 T item = (T)o;
290
291 for (UnicastProcessor<T> w : windows) {
292 w.onNext(item);
293 }
294 }
295
296 continue;
297 }
298 else if (openDone && windows.size() == 0) {
299 upstream.cancel();
300 startSubscriber.cancel();
301 resources.dispose();
302 terminateDownstream(downstream);
303 upstreamCanceled = true;
304 continue;
305 }
306 }
307
308 missed = addAndGet(-missed);
309 if (missed == 0) {
310 break;
311 }
312 }
313 }
314
315 void terminateDownstream(Subscriber<?> downstream) {
316 Throwable ex = error.terminate();
317 if (ex == null) {
318 for (UnicastProcessor<T> w : windows) {
319 w.onComplete();
320 }
321 downstream.onComplete();
322 } else if (ex != ExceptionHelper.TERMINATED) {
323 for (UnicastProcessor<T> w : windows) {
324 w.onError(ex);
325 }
326 downstream.onError(ex);
327 }
328 }
329
330 static final class WindowStartItem<B> {
331
332 final B item;
333
334 WindowStartItem(B item) {
335 this.item = item;
336 }
337 }
338
339 static final class WindowStartSubscriber<B> extends AtomicReference<Subscription>
340 implements FlowableSubscriber<B> {
341
342 private static final long serialVersionUID = -3326496781427702834L;
343
344 final WindowBoundaryMainSubscriber<?, B, ?> parent;
345
346 WindowStartSubscriber(WindowBoundaryMainSubscriber<?, B, ?> parent) {
347 this.parent = parent;
348 }
349
350 @Override
351 public void onSubscribe(Subscription s) {
352 if (SubscriptionHelper.setOnce(this, s)) {
353 s.request(Long.MAX_VALUE);
354 }
355 }
356
357 @Override
358 public void onNext(B t) {
359 parent.open(t);
360 }
361
362 @Override
363 public void onError(Throwable t) {
364 parent.openError(t);
365 }
366
367 @Override
368 public void onComplete() {
369 parent.openComplete();
370 }
371
372 void cancel() {
373 SubscriptionHelper.cancel(this);
374 }
375 }
376
377 static final class WindowEndSubscriberIntercept<T, V> extends Flowable<T>
378 implements FlowableSubscriber<V>, Disposable {
379
380 final WindowBoundaryMainSubscriber<T, ?, V> parent;
381
382 final UnicastProcessor<T> window;
383
384 final AtomicReference<Subscription> upstream;
385
386 final AtomicBoolean once;
387
388 WindowEndSubscriberIntercept(WindowBoundaryMainSubscriber<T, ?, V> parent, UnicastProcessor<T> window) {
389 this.parent = parent;
390 this.window = window;
391 this.upstream = new AtomicReference<>();
392 this.once = new AtomicBoolean();
393 }
394
395 @Override
396 public void onSubscribe(Subscription s) {
397 if (SubscriptionHelper.setOnce(upstream, s)) {
398 s.request(Long.MAX_VALUE);
399 }
400 }
401
402 @Override
403 public void onNext(V t) {
404 if (SubscriptionHelper.cancel(upstream)) {
405 parent.close(this);
406 }
407 }
408
409 @Override
410 public void onError(Throwable t) {
411 if (isDisposed()) {
412 RxJavaPlugins.onError(t);
413 } else {
414 parent.closeError(t);
415 }
416 }
417
418 @Override
419 public void onComplete() {
420 parent.close(this);
421 }
422
423 @Override
424 public void dispose() {
425 SubscriptionHelper.cancel(upstream);
426 }
427
428 @Override
429 public boolean isDisposed() {
430 return upstream.get() == SubscriptionHelper.CANCELLED;
431 }
432
433 @Override
434 protected void subscribeActual(Subscriber<? super T> s) {
435 window.subscribe(s);
436 once.set(true);
437 }
438
439 boolean tryAbandon() {
440 return !once.get() && once.compareAndSet(false, true);
441 }
442 }
443 }
444
445 }