Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableWindow.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.ArrayDeque;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
24 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
25 import io.reactivex.rxjava3.processors.UnicastProcessor;
26
27 public final class FlowableWindow<T> extends AbstractFlowableWithUpstream<T, Flowable<T>> {
28 final long size;
29
30 final long skip;
31
32 final int bufferSize;
33
34 public FlowableWindow(Flowable<T> source, long size, long skip, int bufferSize) {
35 super(source);
36 this.size = size;
37 this.skip = skip;
38 this.bufferSize = bufferSize;
39 }
40
41 @Override
42 public void subscribeActual(Subscriber<? super Flowable<T>> s) {
43 if (skip == size) {
44 source.subscribe(new WindowExactSubscriber<>(s, size, bufferSize));
45 } else
46 if (skip > size) {
47 source.subscribe(new WindowSkipSubscriber<>(s, size, skip, bufferSize));
48 } else {
49 source.subscribe(new WindowOverlapSubscriber<>(s, size, skip, bufferSize));
50 }
51 }
52
53 static final class WindowExactSubscriber<T>
54 extends AtomicInteger
55 implements FlowableSubscriber<T>, Subscription, Runnable {
56
57 private static final long serialVersionUID = -2365647875069161133L;
58
59 final Subscriber<? super Flowable<T>> downstream;
60
61 final long size;
62
63 final AtomicBoolean once;
64
65 final int bufferSize;
66
67 long index;
68
69 Subscription upstream;
70
71 UnicastProcessor<T> window;
72
73 WindowExactSubscriber(Subscriber<? super Flowable<T>> actual, long size, int bufferSize) {
74 super(1);
75 this.downstream = actual;
76 this.size = size;
77 this.once = new AtomicBoolean();
78 this.bufferSize = bufferSize;
79 }
80
81 @Override
82 public void onSubscribe(Subscription s) {
83 if (SubscriptionHelper.validate(this.upstream, s)) {
84 this.upstream = s;
85 downstream.onSubscribe(this);
86 }
87 }
88
89 @Override
90 public void onNext(T t) {
91 long i = index;
92
93 UnicastProcessor<T> w = window;
94 FlowableWindowSubscribeIntercept<T> intercept = null;
95 if (i == 0) {
96 getAndIncrement();
97
98 w = UnicastProcessor.create(bufferSize, this);
99 window = w;
100
101 intercept = new FlowableWindowSubscribeIntercept<>(w);
102 downstream.onNext(intercept);
103 }
104
105 i++;
106
107 w.onNext(t);
108
109 if (i == size) {
110 index = 0;
111 window = null;
112 w.onComplete();
113 } else {
114 index = i;
115 }
116
117 if (intercept != null && intercept.tryAbandon()) {
118 intercept.window.onComplete();
119 }
120 }
121
122 @Override
123 public void onError(Throwable t) {
124 Processor<T, T> w = window;
125 if (w != null) {
126 window = null;
127 w.onError(t);
128 }
129
130 downstream.onError(t);
131 }
132
133 @Override
134 public void onComplete() {
135 Processor<T, T> w = window;
136 if (w != null) {
137 window = null;
138 w.onComplete();
139 }
140
141 downstream.onComplete();
142 }
143
144 @Override
145 public void request(long n) {
146 if (SubscriptionHelper.validate(n)) {
147 long u = BackpressureHelper.multiplyCap(size, n);
148 upstream.request(u);
149 }
150 }
151
152 @Override
153 public void cancel() {
154 if (once.compareAndSet(false, true)) {
155 run();
156 }
157 }
158
159 @Override
160 public void run() {
161 if (decrementAndGet() == 0) {
162 upstream.cancel();
163 }
164 }
165 }
166
167 static final class WindowSkipSubscriber<T>
168 extends AtomicInteger
169 implements FlowableSubscriber<T>, Subscription, Runnable {
170
171 private static final long serialVersionUID = -8792836352386833856L;
172
173 final Subscriber<? super Flowable<T>> downstream;
174
175 final long size;
176
177 final long skip;
178
179 final AtomicBoolean once;
180
181 final AtomicBoolean firstRequest;
182
183 final int bufferSize;
184
185 long index;
186
187 Subscription upstream;
188
189 UnicastProcessor<T> window;
190
191 WindowSkipSubscriber(Subscriber<? super Flowable<T>> actual, long size, long skip, int bufferSize) {
192 super(1);
193 this.downstream = actual;
194 this.size = size;
195 this.skip = skip;
196 this.once = new AtomicBoolean();
197 this.firstRequest = new AtomicBoolean();
198 this.bufferSize = bufferSize;
199 }
200
201 @Override
202 public void onSubscribe(Subscription s) {
203 if (SubscriptionHelper.validate(this.upstream, s)) {
204 this.upstream = s;
205 downstream.onSubscribe(this);
206 }
207 }
208
209 @Override
210 public void onNext(T t) {
211 long i = index;
212
213 FlowableWindowSubscribeIntercept<T> intercept = null;
214 UnicastProcessor<T> w = window;
215 if (i == 0) {
216 getAndIncrement();
217
218 w = UnicastProcessor.create(bufferSize, this);
219 window = w;
220
221 intercept = new FlowableWindowSubscribeIntercept<>(w);
222 downstream.onNext(intercept);
223 }
224
225 i++;
226
227 if (w != null) {
228 w.onNext(t);
229 }
230
231 if (i == size) {
232 window = null;
233 w.onComplete();
234 }
235
236 if (i == skip) {
237 index = 0;
238 } else {
239 index = i;
240 }
241
242 if (intercept != null && intercept.tryAbandon()) {
243 intercept.window.onComplete();
244 }
245 }
246
247 @Override
248 public void onError(Throwable t) {
249 Processor<T, T> w = window;
250 if (w != null) {
251 window = null;
252 w.onError(t);
253 }
254
255 downstream.onError(t);
256 }
257
258 @Override
259 public void onComplete() {
260 Processor<T, T> w = window;
261 if (w != null) {
262 window = null;
263 w.onComplete();
264 }
265
266 downstream.onComplete();
267 }
268
269 @Override
270 public void request(long n) {
271 if (SubscriptionHelper.validate(n)) {
272 if (!firstRequest.get() && firstRequest.compareAndSet(false, true)) {
273 long u = BackpressureHelper.multiplyCap(size, n);
274 long v = BackpressureHelper.multiplyCap(skip - size, n - 1);
275 long w = BackpressureHelper.addCap(u, v);
276 upstream.request(w);
277 } else {
278 long u = BackpressureHelper.multiplyCap(skip, n);
279 upstream.request(u);
280 }
281 }
282 }
283
284 @Override
285 public void cancel() {
286 if (once.compareAndSet(false, true)) {
287 run();
288 }
289 }
290
291 @Override
292 public void run() {
293 if (decrementAndGet() == 0) {
294 upstream.cancel();
295 }
296 }
297 }
298
299 static final class WindowOverlapSubscriber<T>
300 extends AtomicInteger
301 implements FlowableSubscriber<T>, Subscription, Runnable {
302
303 private static final long serialVersionUID = 2428527070996323976L;
304
305 final Subscriber<? super Flowable<T>> downstream;
306
307 final SpscLinkedArrayQueue<UnicastProcessor<T>> queue;
308
309 final long size;
310
311 final long skip;
312
313 final ArrayDeque<UnicastProcessor<T>> windows;
314
315 final AtomicBoolean once;
316
317 final AtomicBoolean firstRequest;
318
319 final AtomicLong requested;
320
321 final AtomicInteger wip;
322
323 final int bufferSize;
324
325 long index;
326
327 long produced;
328
329 Subscription upstream;
330
331 volatile boolean done;
332 Throwable error;
333
334 volatile boolean cancelled;
335
336 WindowOverlapSubscriber(Subscriber<? super Flowable<T>> actual, long size, long skip, int bufferSize) {
337 super(1);
338 this.downstream = actual;
339 this.size = size;
340 this.skip = skip;
341 this.queue = new SpscLinkedArrayQueue<>(bufferSize);
342 this.windows = new ArrayDeque<>();
343 this.once = new AtomicBoolean();
344 this.firstRequest = new AtomicBoolean();
345 this.requested = new AtomicLong();
346 this.wip = new AtomicInteger();
347 this.bufferSize = bufferSize;
348 }
349
350 @Override
351 public void onSubscribe(Subscription s) {
352 if (SubscriptionHelper.validate(this.upstream, s)) {
353 this.upstream = s;
354 downstream.onSubscribe(this);
355 }
356 }
357
358 @Override
359 public void onNext(T t) {
360 long i = index;
361
362 UnicastProcessor<T> newWindow = null;
363 if (i == 0) {
364 if (!cancelled) {
365 getAndIncrement();
366
367 newWindow = UnicastProcessor.create(bufferSize, this);
368
369 windows.offer(newWindow);
370 }
371 }
372
373 i++;
374
375 for (Processor<T, T> w : windows) {
376 w.onNext(t);
377 }
378
379 if (newWindow != null) {
380 queue.offer(newWindow);
381 drain();
382 }
383
384 long p = produced + 1;
385 if (p == size) {
386 produced = p - skip;
387
388 Processor<T, T> w = windows.poll();
389 if (w != null) {
390 w.onComplete();
391 }
392 } else {
393 produced = p;
394 }
395
396 if (i == skip) {
397 index = 0;
398 } else {
399 index = i;
400 }
401 }
402
403 @Override
404 public void onError(Throwable t) {
405 for (Processor<T, T> w : windows) {
406 w.onError(t);
407 }
408 windows.clear();
409
410 error = t;
411 done = true;
412 drain();
413 }
414
415 @Override
416 public void onComplete() {
417 for (Processor<T, T> w : windows) {
418 w.onComplete();
419 }
420 windows.clear();
421
422 done = true;
423 drain();
424 }
425
426 void drain() {
427 if (wip.getAndIncrement() != 0) {
428 return;
429 }
430
431 final Subscriber<? super Flowable<T>> a = downstream;
432 final SpscLinkedArrayQueue<UnicastProcessor<T>> q = queue;
433 int missed = 1;
434
435 outer:
436 for (;;) {
437
438 if (cancelled) {
439 UnicastProcessor<T> up = null;
440 while ((up = q.poll()) != null) {
441 up.onComplete();
442 }
443 } else {
444 long r = requested.get();
445 long e = 0;
446
447 while (e != r) {
448 boolean d = done;
449
450 UnicastProcessor<T> t = q.poll();
451
452 boolean empty = t == null;
453
454 if (cancelled) {
455 continue outer;
456 }
457
458 if (checkTerminated(d, empty, a, q)) {
459 return;
460 }
461
462 if (empty) {
463 break;
464 }
465
466 FlowableWindowSubscribeIntercept<T> intercept = new FlowableWindowSubscribeIntercept<>(t);
467 a.onNext(intercept);
468
469 if (intercept.tryAbandon()) {
470 t.onComplete();
471 }
472 e++;
473 }
474
475 if (e == r) {
476 if (cancelled) {
477 continue;
478 }
479
480 if (checkTerminated(done, q.isEmpty(), a, q)) {
481 return;
482 }
483 }
484
485 if (e != 0L && r != Long.MAX_VALUE) {
486 requested.addAndGet(-e);
487 }
488 }
489
490 missed = wip.addAndGet(-missed);
491 if (missed == 0) {
492 break;
493 }
494 }
495 }
496
497 boolean checkTerminated(boolean d, boolean empty, Subscriber<?> a, SpscLinkedArrayQueue<?> q) {
498 if (d) {
499 Throwable e = error;
500
501 if (e != null) {
502 q.clear();
503 a.onError(e);
504 return true;
505 } else
506 if (empty) {
507 a.onComplete();
508 return true;
509 }
510 }
511
512 return false;
513 }
514
515 @Override
516 public void request(long n) {
517 if (SubscriptionHelper.validate(n)) {
518 BackpressureHelper.add(requested, n);
519
520 if (!firstRequest.get() && firstRequest.compareAndSet(false, true)) {
521 long u = BackpressureHelper.multiplyCap(skip, n - 1);
522 long v = BackpressureHelper.addCap(size, u);
523 upstream.request(v);
524 } else {
525 long u = BackpressureHelper.multiplyCap(skip, n);
526 upstream.request(u);
527 }
528
529 drain();
530 }
531 }
532
533 @Override
534 public void cancel() {
535 cancelled = true;
536 if (once.compareAndSet(false, true)) {
537 run();
538 }
539 drain();
540 }
541
542 @Override
543 public void run() {
544 if (decrementAndGet() == 0) {
545 upstream.cancel();
546 }
547 }
548 }
549 }